【发布时间】:2015-12-15 00:50:43
【问题描述】:
我有一个基本的 JSFiddle,我想在一个圆圈内绘制随机点。
但我不知道如何将点限制在圆圈内。
这是我目前拥有的:
var ctx = canvas.getContext('2d'),
count = 1000, // number of random points
cx = 150,
cy = 150,
radius = 148;
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.arc(canvas.width/2, canvas.height/2, radius, 0, 2*Math.PI);
ctx.closePath();
ctx.fillStyle = '#00000';
ctx.fill();
// create random points
ctx.fillStyle = '#ffffff';
while(count) {
// randomise x:y
ctx.fillRect(x + canvas.width/2, y + canvas.height/2, 2, 2);
count--;
}
我将如何生成随机 (x,y) 坐标以在圆内绘制随机点?
我现在的小提琴:http://jsfiddle.net/e8jqdxp3/
【问题讨论】:
标签: javascript canvas random geometry