0

如何检测在 HTML 画布元素中绘制的圆弧内的鼠标点击?

我正在创建这样的弧:

ctx.beginPath();
ctx.arc(250, 250, outsideRadius, angle, angle + arc, false);
ctx.arc(250, 250, 0, 0, 0, true);
ctx.fill();

我确实尝试为每个绘制的弧关联上下文对象,然后使用 myArc.ctx.isPointInPath(mouseX, mouseY) - 但这不起作用 - 所以我想使用基本三角函数来确定鼠标是否单击是否在界限内。

提前致谢!

4

1 回答 1

2

你应该使用

ctx.isPointInPath(x, y);

我所做的是将beginPath,路径“绘图”移动到一个函数中,我也将其称为绘图,以及确定鼠标是否击中形状。

function shape() {
    ctx.beginPath();
    ctx.arc(250, 250, outsideRadius, angle, angle + arc, false);
    ctx.arc(250, 250, 0, 0, 0, true);
}

function draw() {
    shape();
    ctx.fill();
}

function checkHit(x, y) {
    shape();
    return ctx.isPointInPath(x, y);
}

如果你像这样构建你的应用程序,添加其他形状也很容易

于 2012-05-01T20:08:54.260 回答