如何将CSS rotatex()和rotatey()添加到画布中的图像
我正在尝试在jQuery中制作一个简单的帆布游戏,
基本上,我有一个无限的循环,它不断绘制播放器和背景:
loop = () => {
$('#player').css("transform", "rotateX("+xr+"deg) rotateY("+yr+"deg)");
ctx.fillStyle = 'gray';
ctx.fillRect(0,0,400,400);
ctx.drawImage(player, x, y, 50, 50);
window.setTimeout(loop, 1);
}
然后我有一个开关语句,可以在播放器按下A和D按钮时改变播放器的XR和yr:
$(document).ready(()=>{
loop();
$(document).bind('keydown',(e)=>{
switch(e.keyCode){
case 65:
//moving left (altering the xr and yr values)
break;
case 87:
//moving up (altering the y and x values based on SIN and COS and player's XR and YR)
break;
etc...
}
});
});
我可以说它正在检测到键推定,因为当我在Switch语句中运行Console.log()时,它会将某些内容打印到控制台上,
因此很明显,这是图像上绘制的图像的问题。
如果有人可以弄清楚如何将图像的CSS绘制到画布上,或者改变在画布的CSS中绘制的图像,那就太好了。
不,我在其他任何地方都没有找到任何答案。这个问题似乎还没有问过。
编辑:XR和YR是XROTATION和yROTATION变量。
i am trying to make a simple canvas game in jquery,
basically, i have an infinite loop that constantly draws the player and background:
loop = () => {
$('#player').css("transform", "rotateX("+xr+"deg) rotateY("+yr+"deg)");
ctx.fillStyle = 'gray';
ctx.fillRect(0,0,400,400);
ctx.drawImage(player, x, y, 50, 50);
window.setTimeout(loop, 1);
}
And then i have a switch statement that alters the player's XR and YR when they push the A and D buttons:
$(document).ready(()=>{
loop();
$(document).bind('keydown',(e)=>{
switch(e.keyCode){
case 65:
//moving left (altering the xr and yr values)
break;
case 87:
//moving up (altering the y and x values based on SIN and COS and player's XR and YR)
break;
etc...
}
});
});
i can tell it's detecting the key pushes, because when i run CONSOLE.LOG() in the switch statement, it prints something to the console,
so clearly this is a problem with images drawn to a canvas not keeping their css styling.
if anybody can figure out how to keep an image's css when drawing it to a canvas, or alter an image draw in a canvas's css, that would be great.
and no, i didn't find any answers anywhere else. this question doesn't seem to have been asked yet.
EDIT: xr and yr are XROTATION and YROTATION variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,图像不会将其CSS保留在画布中,也无法添加它。没关系,猜猜我会尝试其他事情。
apparently, images don't keep their css in canvases, and there is no way to add this. never mind, guess i'll try something else.
您不需要CSS,可以使用画布来完成所有操作,翻译和
You don't need CSS, you can do everything with the Canvas, you have translate and rotate methods. Here a very basic example: