RequestAnimationFrame无法正常工作

时间:2015-05-04 20:06:02

标签: javascript canvas

调用requestAnimationFrame后,方块不会移动。 预计这个广场会移动。

此代码有什么问题?

http://jsfiddle.net/o3qbesy6/1/

    // Check if canvas exists and get the context of the canvas
var canvas = document.getElementsByTagName('canvas')[0];
var ctx = canvas.getContext('2d');

// Add width and height of the canvas
var width = 640, height = 480;

// Make the canvas width and height as the variables
canvas.width = width, canvas.height = height;

rect = {
    x: 0,
    y: 0,
    width: 100,
    height: 100
}

ctx.fillStyle = 'red';
ctx.fillRect(rect.x,rect.y,rect.width,rect.height);

function animate(){

    if(rect.x <= 200){
        rect.x += 5;
    }
    requestID = requestAnimationFrame(animate);
}

requestAnimationFrame(animate);

0 个答案:

没有答案