我在畫布上呈現了多個對象,每個對象都在傾聽方向移動的keydown,並使用requestAnimationFrame重繪。在多個對象在畫布上呈現之前清除畫布
問題是,如果我在每個對象的重繪上設置了清晰的畫布,那麼在btw不同對象的重繪中會運行多個清晰畫布函數,導致對象閃爍。什麼是在畫布上重繪(並清除)多個對象的最佳方式。
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();
};
如果通過_「重畫到全局setInterval」_你的意思是它通過活動對象數組循環並調用'myObject.draw()',那麼這就是我的建議。對不起,您沒有清楚地表達意見,但感謝您提供您低估的原因。 – Phrogz 2011-04-03 13:53:01
@Phrongz,btw,downvote不是我的! – bcm 2011-04-03 23:56:57