gpt4 book ai didi

javascript - Ellipse - 鼠标碰撞检测

转载 作者:行者123 更新时间:2023-11-30 12:07:36 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何判断鼠标是否位于 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();
}

}

最佳答案

@pvg 的重复答案是可以的,但是重复的答案应该指出他们使用的变量是如何计算的:

// Given cx,cy,a,b defining an ellipse 
function isInEllipse(mouseX,mouseY){
var dx=mouseX-cx;
var dy=mouseY-cy;
return ((dx*dx)/(a*a)+(dy*dy)/(b*b)<=1);
}

这是示例代码和演示:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
function reOffset(){
var BB=canvas.getBoundingClientRect();
offsetX=BB.left;
offsetY=BB.top;
}
var offsetX,offsetY;
reOffset();
window.onscroll=function(e){ reOffset(); }
window.onresize=function(e){ reOffset(); }

var PI=Math.PI;
var PI2=PI*2;
var cx=cw/2;
var cy=ch/2;
var a=120;
var b=80;
var points=getPointsOnEllipse(cx,cy,a,b);

$("#canvas").mousemove(function(e){handleMouseMove(e);});

drawEllipse(points);


function dot(x,y,color){
ctx.beginPath();
ctx.arc(x,y,3,0,PI2);
ctx.closePath();
ctx.fillStyle=color;
ctx.fill();
}

function isInEllipse(x,y){
var dx=x-cx;
var dy=y-cy;
return ((dx*dx)/(a*a)+(dy*dy)/(b*b)<=1);
}

function drawEllipse(points){
ctx.beginPath();
ctx.moveTo(points[0].x,points[0].y);
for(var j=0;j<points.length;j++){
ctx.lineTo(points[j].x,points[j].y);
}
ctx.closePath();
ctx.lineWidth=1;
ctx.strokeStyle='forestgreen';
ctx.stroke();
}

function getPointsOnEllipse(cx,cy,a,b){
var startAngle=-PI/2;
var lastX=cx-(a*Math.cos(startAngle));
var lastY=cy+(b*Math.sin(startAngle));
var points=[];
for(var i=0;i<1000;i++){
var angle=startAngle+PI2/1000*i;
var x=cx-(a*Math.cos(angle));
var y=cy+(b*Math.sin(angle));
var dx=x-lastX;
var dy=y-lastY;
var length=parseInt(Math.sqrt(dx*dx+dy*dy));
var eAngle=(Math.atan2(dy,dx)+PI2)%PI2;
if(length>0){
points.push({x:x,y:y,angle:eAngle});
lastX=x;
lastY=y;
}
}
return(points);
}

function handleMouseMove(e){
// tell the browser we're handling this event
e.preventDefault();
e.stopPropagation();

mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);

var hit=isInEllipse(mouseX,mouseY);
var color=(hit)?'red':'green';
dot(mouseX,mouseY,color);

}
body{ background-color: ivory; }
#canvas{border:1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4>Move mouse in & out of ellipse.<br>Red mouse dots are inside ellipse, Green outside.</h4>
<canvas id="canvas" width=300 height=300></canvas>

关于javascript - Ellipse - 鼠标碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34731883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com