【发布时间】:2017-03-08 17:53:48
【问题描述】:
我正在使用 drawCircle 函数在 html5 画布 (this.ctx) 上绘制圆圈。现在我想用移动圆圈功能将圆圈移动到不同的位置。有没有办法看到圆圈从一个地方移动到另一个地方?在这一点上,我什至不确定如何为用户删除前一个圈子。我可以将弧分配给一个对象吗?
customobject.prototype.drawCircle = function drawCircle(userID, x, y) {
var radius = 10;
this.ctx.beginPath();
this.ctx.arc(100, 00, 10, 0, Math.PI*2, true);
this.ctx.closePath();
this.ctx.fillStyle = 'blue';
this.ctx.fill();
this.ctx.lineWidth = 2;
this.ctx.strokeStyle = '#003300';
this.ctx.stroke();
}
customobject.prototype.moveCircle = function moveCircle(userID, x, y) {
}
我确实看到了一种可能删除圆圈的方法(不是动画 - 移动它):
remove circle(x, y, radius){
this.ctx.globalCompositeOperation = 'destination-out'
this.ctx.arc(x, y, radius, 0, Math.PI*2, true);
this.ctx.fill();
}
-> 所以在这种情况下,我会指定原始圆的坐标并将其切割?
我还看到this 发布了关于绕圈移动的帖子。但我不知道如何用多个圈子做到这一点。 (每个用户 ID 都会有一个圆圈)
【问题讨论】:
-
如果你有多个圈子,那么最好的方法是使用像easel.js这样的库。好处是您将能够给每个对象一个 ID,并可以单独对它们执行一些操作。当然,如果您使用自定义方法,那么@philipp 是正确的,您必须清除画布并使用 requestAnimationFrame() 重绘所有内容。
-
如果有多个圈子,他也可以使用像
[{start:, end:, p1:{}, p1:{}, …}, {…}, …]这样的简单数组。然后只是循环并渲染每个。但是如果你需要更多的形状、样式、缓动函数,那么库会更好。
标签: javascript html