【发布时间】:2011-07-28 05:29:18
【问题描述】:
我在 Canvas 上渲染了多个对象,每个对象都监听 keydown 的方向移动,并使用 requestAnimationFrame 重新绘制。
问题是,如果我在每个对象的重绘上都设置了透明画布,那么在不同对象的重绘中会运行多个透明画布函数,从而导致对象闪烁。在画布上重绘(和清除)多个对象的最佳方法是什么。
function CreatePawn() {
var x = 25,
y = 25;
animate();
function draw() {
ctx.clearRect(0, 0, cwidth, cheight);
ctx.beginPath();
ctx.lineWidth="3";
ctx.arc(x, y, 2, 0, Math.PI * 2, true); // circle
ctx.stroke();
};
function animate() {
requestAnimationFrame( animate );
if(left) x -= speed;
if(up) y -= speed;
if(right) x += speed;
if(down) y += speed;
draw();
};
【问题讨论】:
标签: javascript html animation canvas