【发布时间】:2015-12-20 14:51:43
【问题描述】:
我编写了一个代码,在画布中随机生成 60 个圆圈,我该如何处理按下的事件 onclick 圆圈消失,然后生成另一个?
这可能是最好的解决方案?
代码是:
var canvas, ctx;
var circles = [];
var selectedCircle;
var vel=200;
function Circle(x, y, radius){
this.x = x;
this.y = y;
this.radius = radius;
}
function clear() { // clear canvas
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function drawCircle(ctx, x, y, radius) {
var color = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
}
function generate(){
canvas = document.getElementById('my');
ctx = canvas.getContext('2d');
var circleRadius = 25;
var width = canvas.width;
var height = canvas.height;
var timer, j = 0;
var circlesCount = 60;
for (var i=0; i<circlesCount; i++) {
var x = Math.random()*width;
var y = Math.random()*height;
circles.push(new Circle(x,y,circleRadius));
}
timer = window.setInterval(function()
{
if(j==60)
{
clear();
return;
}
drawCircle(ctx, circles[j].x, circles[j].y, circles[j].radius);
// go to next circle
j++;
}, vel);
}
【问题讨论】:
-
您的圈子与其他圈子重叠。你打算如何清除它?
-
是的,我怎样才能不重叠圆圈并单击删除圆圈?
-
如何将生成的圆的所有位置保存到一个数组中,然后在绘制新圆之前检查(计算)它。
-
好的,然后点击事件删除圈子?请帮我写代码
-
什么都没有?请帮帮我
标签: javascript arrays canvas onclick html5-canvas