要回答你的第一个问题:是的,他们是可能的。要回答你的第二个问题:一个“最好”的方法是在画布上下文中使用任意的变换嵌套。
我创建了一个示例,显示如何在上下文中发出绘图命令,但不知道当前的转换是什么,然后将这些命令包装在为结果生成动画的转换中。
在这里看到的结果:http://phrogz.net/tmp/canvas_nested_animations.html
下面是该办法的一个基本概述:
// Draw a bug with animated legs
function drawBug(){
ctx.save();
// Issue drawing commands, assuming some 0,0 center and an unrotated bug
// Use the current time, or some frame counter, to change how things are drawn
ctx.restore();
}
// Move the (animated) bug around
function drawMovingBug(){
ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
ctx.save();
// Adjust the bug's location/rotation based on some animation path
// and the current time or frame counter.
var bugPosition = findCurrentBugPosition();
ctx.rotate(bugPosition.angle);
ctx.translate(bugPosition.x,bugPosition.y);
// Draw the bug using the new transformation
drawBug();
ctx.restore();
}
埃,使用内置的转换堆栈的路要走。 – DuckMaestro 2011-03-30 03:24:05
非常感谢,正是我想知道的!我会给它一个旋转! :D – iamdarrenhall 2011-03-30 08:13:00
@Progro,我可以先将translate命令放在drawMovingBug方法内,然后将命令放在下一个方向吗?通过这样做,会达到与以前相同的效果。 – rajkamal 2012-04-05 09:22:33