【发布时间】:2016-01-11 21:56:30
【问题描述】:
我试图弄清楚如何判断鼠标是否位于 HTML5 Canvas 上的椭圆上。我需要比边界框检查更精确。
抱歉,这个问题太模糊了。我完全被难住了。
任何帮助将不胜感激。
到目前为止,我有这个:
// This is a pretend object. m.x and m.y will have the position of the mouse.
var m = {
x:245,
y:341,
onEnter: function(){}
};
m.onEnter = function () {
console.log('The mouse entered the ellipse!');
};
// The important parts of my ellipse object
var e = {
width:100,
height:50,
x:132,
y:143
};
// This is already working - I use a bounding-box first to increase performance.
if (m.x >= e.x - (e.width/2) && m.x <= e.x + (e.width/2) && m.y >= e.y - (e.height/2) && m.y <= e.y + (e.height/2)) {
if (/* I havn't got the slightest idea what to put here. */) {
m.onEnter();
}
}
【问题讨论】:
-
你如何在画布上描述你的椭圆?是用参数方程画线吗,例如
x = a cos t, y = b sin t或等价物,或者你是在用几个context.bezierCurveTo()函数来伪造一个椭圆? -
@BrianPeacock 我正在缩放一个圆圈:假设椭圆为 100 X 50 然后我
context.scale(1,0.5)然后context.arc
标签: javascript html math html5-canvas