【发布时间】:2013-02-03 10:36:24
【问题描述】:
所以我正在尝试加深对 Canvas 中动画的理解,但由于某种原因,这段代码无法正常工作。黄色矩形出现,但位置没有迭代。这个脚本在我的 html 代码中被引用为<script src = "images/drawing.js"></script>。这是drawing.js:
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);
}
【问题讨论】:
-
“不工作”不是一个很好的描述...
-
你说得对,我会解决的。
标签: html animation canvas setinterval