2013-02-18 47 views
0

所以我想在Canvas中开发动画的理解,但由于某些原因,这段代码无法正常工作。黄色矩形显示出来,但位置不会重复。该脚本在我的html代码中被引用为<script src = "images/drawing.js"></script>。这里是drawing.js:为什么不是这个html5 canvas动画工作?

var x = 400; 
var y = 300; 

function init() { 

    var canvas = document.getElementById("canvas"); 
    var context = canvas.getContext("2d"); 
    canvas.width = 800; 
    canvas.height = 600; 

    context.fillStyle = "black"; 
    context.fillRect(x,y,150,150); 
    setInterval(animateBox(context), 30); 

} 



function animateBox(context) { 

    context.clearRect(0,0,context.canvas.width,context.canvas.height); 
    x += 5; 
    context.fillStyle = "yellow"; 
    context.fillRect(x,y,150,150); 
} 
+0

“不工作”不是一个很好的描述... – 2013-02-18 21:42:33

+0

你是对的我会解决它。 – kag359six 2013-02-18 21:53:52

回答

1

变化setInterval(animateBox(context), 30);

setInterval(function(){ 
    animateBox(context) 
}, 30); 

我认为这就是问题所在。这个问题有点模糊。确保你也打电话给init()功能(尽管你可能是)。