如何檢測用戶在紅色泡泡內點擊的時間?檢測用戶是否在圓圈內點擊
它不應該像一個方形領域。鼠標必須是真正的圈內:
下面的代碼:
<canvas id="canvas" width="1000" height="500"></canvas>
<script>
var canvas = document.getElementById("canvas")
var ctx = canvas.getContext("2d")
var w = canvas.width
var h = canvas.height
var bubble = {
x: w/2,
y: h/2,
r: 30,
}
window.onmousedown = function(e) {
x = e.pageX - canvas.getBoundingClientRect().left
y = e.pageY - canvas.getBoundingClientRect().top
if (MOUSE IS INSIDE BUBBLE) {
alert("HELLO!")
}
}
ctx.beginPath()
ctx.fillStyle = "red"
ctx.arc(bubble.x, bubble.y, bubble.r, 0, Math.PI*2, false)
ctx.fill()
ctx.closePath()
</script>
如果您知道光標和圓的位置和大小的位置(和它真的是一個圓,沒有省略號),它只是一個簡單的幾何計算來確定點(光標)是否在圓內。 http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle – qJake
聽說過竇和餘弦......? – CBroe
@CBroe Trig在這種情況下不需要,也不會有用。 –