请考虑这样的代码,draw()
函数在每个动画循环中被调用,其中阶段被清除并且梯度被应用于背景并且x,y被更改为形状:HTML5上的多个对象CANVAS,只绘制第一项
draw = function() {
for (i = 0; i < shapes.length; i++)
{
draw_sample_shape(i);
draw_coords(i);
}
}
draw_sample_shape = function(shape) {
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(shapes[shape].x, shapes[shape].y, 240, 0, Math.PI*2, false);
ctx.closePath();
ctx.fill();
}
draw_coords(shape) {
coords = shapes[shape].coords;
ctx.globalCompositeOperation = "destination-out";
ctx.beginPath();
ctx.moveTo(coords[0].x, coords[0].y);
for (i = 1; i < coords.length; i++)
{
if (coords[i].y == 'X' && coords[i].x == 'X')
{
ctx.closePath();
ctx.fill();
ctx.moveTo(0, 0);
ctx.beginPath();
if (typeof(coords[i+1]) != 'undefined')
{
ctx.moveTo((coords[i+1].x), (coords[i+1].y));
}
}
else
{
ctx.lineTo((coords[i].x), (coords[i].y));
}
}
ctx.closePath();
ctx.fill();
}
现在一切都运行完美,除非我在draw()
函数中调用draw_coords()
- 问题是它吸引和动画只有第一个形状,并跳过他们的休息。当我跳过这个呼叫时,它工作正常。
我使用[X,X]坐标来重置图形,并开始下一个形状。 有人能告诉我什么是错的吗?以及为什么当我绘制这些坐标时,它只会在第一个元素上绘制它们?
你能给我们一个“形状”的例子吗? – 2011-12-28 15:20:37