【发布时间】:2013-10-12 04:57:06
【问题描述】:
我正在寻找可以将我的形状动画到 x,y 位置的画布函数。 例如,我有一个包含所有要绘制的形状的数组,我只是“告诉”对象“动画到 x,y 坐标”,如下所示:
// Array that holds all the shapes to draw
var shapes = new Array();
// Setting up some shapes
for (var i = 0; i < 10; i++) {
var x = Math.random()*250;
var y = Math.random()*250;
var width = height = Math.random()*30;
shapes.push(new Shape(x, y, width, height));
};
function animate() {
// Clear
context.clearRect(0, 0, canvasWidth, canvasHeight);
// Loop through every object
var shapesLength = shapes.length;
for (var i = 0; i < shapesLength; i++) {
var tmpShape = shapes[i];
//here should go function that would animate my shape to x,y postition
var x = tmpShape.x????;
var y = tmpShape.y????;
// Draw
context.fillRect(x, y, tmpShape.width, tmpShape.height);
};
setTimeout(animate, 33);
};
我什么都没试过,因为我根本不知道该怎么做。
【问题讨论】:
-
“要求代码的问题必须表明对正在解决的问题有最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期的结果。”跨度>
-
欢迎使用 Stackoverflow 并感谢您添加代码。我们通过在这里使用代码而茁壮成长!
标签: javascript html canvas