html5 canvas - 同时旋转和移动图像
我已经找到了如何使用画布旋转图像,但我在创建同时旋转和移动图像的动画时遇到问题。这就是我已经拥有的:
var img;
var context;
var processID;
var i = 0;
var rectWidth = 64;
var rectHeight = 64;
window.onload = function(){
var canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
context.translate(canvas.width / 2, canvas.height / 2);
processID = setInterval(draw,1000/100);
}
function draw() {
if(i <= 360) {
context.clearRect(-canvas.width / 2, -canvas.width / 2, canvas.width, canvas.height);
context.strokeRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
context.rotate(1 * Math.PI / 180);
i++;
} else {
clearInterval(processID);
}
}
所以现在我希望我的方块在旋转时移动到顶部。我真的在这里迷路了,希望得到一些帮助。
i allready found out how to rotate an image with canvas, but im having problems creating an animation, which rotates and moves an image at the same time. This is what i already have:
var img;
var context;
var processID;
var i = 0;
var rectWidth = 64;
var rectHeight = 64;
window.onload = function(){
var canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
context.translate(canvas.width / 2, canvas.height / 2);
processID = setInterval(draw,1000/100);
}
function draw() {
if(i <= 360) {
context.clearRect(-canvas.width / 2, -canvas.width / 2, canvas.width, canvas.height);
context.strokeRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
context.rotate(1 * Math.PI / 180);
i++;
} else {
clearInterval(processID);
}
}
So now i want my square to move to the top while rotating. I'm really lost here and would appreciate some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
translate
方法,或更一般地使用transform
方法。请参阅文档有关您可以执行的操作的参考,以及 wiki 页面变换矩阵You could use the
translate
method, or more generically thetransform
method. See the docs for a reference of what you can do, and the wiki page for Transformation Matrices幸运的是我自己找到了答案:
So luckily i found the answer myself: