gpt4 book ai didi

javascript - Item 上的 Canvas 鼠标移动事件

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

我在 Canvas 区域有 3 个圆弧起点,所以当用户拖动任何一个圆弧时,我想画一个圆,直到他顺时针方向拖动并在逆时针方向移除。

我的问题是如何从他正在拖动的 3 个中获得准确的弧线,以及他拖动到什么位置和方向。

我已经从谷歌尝试如下但不是运气,请查看 jsfiddle 并提出建议。

http://jsfiddle.net/ineffablep/azh8ma89/38/

var arcStep = 0.02;
var numberOfarcs = 3;
for (i = 1; i <= numberOfarcs; i++) {
var arcRadiusFactor = (1 + (1 / numberOfarcs) * i) - 0.1;
var arcRadius = arcRadiusFactor * radius;
context.beginPath();
context.arc(centerX, centerY, arcRadius, 1.5 * Math.PI, 1.51 * Math.PI, false);
context.lineWidth = 15;
context.setLineDash([0]);

context.strokeStyle = 'black';
context.stroke();

}

//add events
canvas.addEventListener("mousedown", onMouseDown, false);
canvas.addEventListener("mouseup", onMouseUp, false);
canvas.addEventListener("mousemove", onMouseMove, false);

var mouseDown = false;
function onMouseDown(e) {
mouseDown = true;
e.stopPropagation();
}
function onMouseUp(e) {
mouseDown = false;
e.stopPropagation();
}
function onMouseMove(e) {
e.stopPropagation();
if (!mouseDown) return;
var cursor = {
x: e.offsetX || e.originalEvent.layerX,
y: e.offsetY || e.originalEvent.layerY
};

console.log(cursor.x, cursor.y);

}

最佳答案

您可以使用三 Angular arcTangent 计算鼠标与中心点的 Angular 。

enter image description here

这是示例代码和演示:

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(); }

var isDown=false;
var startX,startY;

var cx=cw/2;
var cy=ch/2;
var PI=Math.PI;
var PI2=PI*2;
var radius=50;

var radius1=100;
var drag1W=16;
var drag1H=20;
var drag1X=cx-drag1W/2;
var drag1Y=cy-drag1H/2-radius1;
var drag1IsDragging=false;
var drag1Sweep=0;

ctx.lineWidth=5;

draw();

function draw(){

ctx.clearRect(0,0,cw,ch);

ctx.beginPath();
ctx.arc(cx,cy,radius,0,PI2);
ctx.closePath();
ctx.fillStyle='skyblue';
ctx.strokeStyle='lightgray';
ctx.fill();
ctx.stroke();

ctx.fillStyle='blue';
ctx.fillRect(drag1X,drag1Y,drag1W,drag1H);

if(drag1Sweep>0){
ctx.beginPath();
ctx.arc(cx,cy,radius1,-PI/2,-PI/2+drag1Sweep);
ctx.strokeStyle='blue';
ctx.stroke();
}

}

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

mx=parseInt(e.clientX-offsetX);
my=parseInt(e.clientY-offsetY);

// Put your mousedown stuff here
if(mx>=drag1X && mx<=drag1X+drag1W && my>=drag1Y && my<=drag1Y+drag1H){
isDown=true;
drag1IsDragging=true;
}
}

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

// Put your mouseup stuff here
isDown=false;
drag1IsDragging=false;
}

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

mx=parseInt(e.clientX-offsetX);
my=parseInt(e.clientY-offsetY);

// Put your mousemove stuff here
var dx=mx-cx;
var dy=my-cy;
drag1Sweep=(Math.atan2(dy,dx)+PI/2+PI2)%PI2;

draw();

}

$("#canvas").mousedown(handleMouseDown);
$("#canvas").mousemove(handleMouseMove);
$("#canvas").mouseup(handleMouseUp);
$("#canvas").mouseout(handleMouseUp);
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>Drag from blue rectangle around the circle</h4>
<canvas id="canvas" width=300 height=300></canvas>

关于javascript - Item 上的 Canvas 鼠标移动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29955955/

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