我在JavaScript中的畫布上的範圍內的隨機位置上畫了一個圓。如何根據JavaScript中的head circle距離生成新的隨機圓圈?
我想在第一個圓的中心的特定距離(200)上的隨機位置上再生成2個圓。
有一種方法可以生成新的座標,直到距離等於200,但應該有更好的解決方案,對嗎?
我該怎麼做?
代碼:
var x = getRandomInt(200, 800)
var y = getRandomInt(200, 400)
var r = 60
drawCircle (x, y, r)
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function drawCircle(x, y, r) {
context.beginPath()
context.arc(x, y, r, 0, 2 * Math.PI, false)
context.fillStyle = "#fc3358"
context.fill()
context.closePath()
}
太棒了!這就是我一直在尋找的! – Engo