html检查鼠标是否按住,javascript – 检测鼠标是否在画布内的对象上

演示:

http://jsfiddle.net/m1erickson/Cw4ZN/

您需要这些概念来检查鼠标是否在一行内:

>定义起点&一条线的终点

>听取鼠标事件

>在鼠标移动时,检查鼠标是否在该行的指定距离内

这是一个带注释的示例代码供您学习.

body{ background-color: ivory; }

canvas{border:1px solid red;}

$(function(){

// canvas related variables

var canvas=document.getElementById("canvas");

var ctx=canvas.getContext("2d");

var $canvas=$("#canvas");

var canvasOffset=$canvas.offset();

var offsetX=canvasOffset.left;

var offsetY=canvasOffset.top;

// dom element to indicate if mouse is inside/outside line

var $hit=$("#hit");

// determine how close the mouse must be to the line

// for the mouse to be inside the line

var tolerance=5;

// define the starting & ending points of the line

var line={x0:50,y0:50,x1:100,y1:100};

// set the fillstyle of the canvas

ctx.fillStyle="red";

// draw the line for the first time

draw(line);

// function to draw the line

// and optionally draw a dot when the mouse is inside

function draw(line,mouseX,mouseY,lineX,lineY){

ctx.clearRect(0,0,canvas.width,canvas.height);

ctx.beginPath();

ctx.moveTo(line.x0,line.y0);

ctx.lineTo(line.x1,line.y1);

ctx.stroke();

if(mouseX && lineX){

ctx.beginPath();

ctx.arc(lineX,lineY,tolerance,0,Math.PI*2);

ctx.closePath();

ctx.fill();

}

}

// calculate the point on the line that's

// nearest to the mouse position

function linepointNearestMouse(line,x,y) {

//

lerp=function(a,b,x){ return(a+x*(b-a)); };

var dx=line.x1-line.x0;

var dy=line.y1-line.y0;

var t=((x-line.x0)*dx+(y-line.y0)*dy)/(dx*dx+dy*dy);

var lineX=lerp(line.x0, line.x1, t);

var lineY=lerp(line.y0, line.y1, t);

return({x:lineX,y:lineY});

};

// handle mousemove events

// calculate how close the mouse is to the line

// if that distance is less than tolerance then

// display a dot on the line

function handleMousemove(e){

e.preventDefault();

e.stopPropagation();

mouseX=parseInt(e.clientX-offsetX);

mouseY=parseInt(e.clientY-offsetY);

if(mouseXline.x1){

$hit.text("Outside");

draw(line);

return;

}

var linepoint=linepointNearestMouse(line,mouseX,mouseY);

var dx=mouseX-linepoint.x;

var dy=mouseY-linepoint.y;

var distance=Math.abs(Math.sqrt(dx*dx+dy*dy));

if(distance

$hit.text("Inside the line");

draw(line,mouseX,mouseY,linepoint.x,linepoint.y);

}else{

$hit.text("Outside");

draw(line);

}

}

// tell the browser to call handleMousedown

// whenever the mouse moves

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

}); // end $(function(){});

Move mouse near line

关于命中测试路径:

如果使用路径命令创建路径,则可以使用context.isPointInPath(mouseX,mouseY)检查鼠标是否在路径中. context.isPointInPath不适用于行但是因为行理论上具有零宽度来“击中”.

在QChart中,要识别用户是否点击了直线并支持按住鼠标平移直线,你可以使用QGraphicsItemGroup和QGraphicsLineItem配合QChartView的事件处理机制。以下是大致步骤: 1. **创建QGraphicsLineItem**: 首先,你需要在图形上下文中创建一个QGraphicsLineItem,并添加到QChartView的场景中。 ```python from PyQt5.QtCharts import QGraphicsLineItem, QGraphicsScene line = QGraphicsLineItem() line.setPen(QPen(Qt.SolidLine, 2)) # 设置线条样式 scene = QChartView.chartView.scene() # 获取场景 scene.addItem(line) ``` 2. **事件监听**: 使用`installEventFilter`方法给图表视图添加一个事件过滤器,用于捕捉鼠标按下、移动和释放事件。 ```python class ChartHandler: def eventFilter(self, watched, event): if isinstance(watched, QGraphicsScene) and event.type() in [QEvent.GraphicsSceneMousePress, QEvent.GraphicsSceneMouseMove]: # 检查鼠标点击位置是否落在线段上 pos = event.pos() linePos = line.mapToScene(pos) is_clicked = line.intersects(linePos) # 如果是点击事件,可以做相应的操作,比如更改颜色或显示提示信息 if event.type() == QEvent.GraphicsSceneMousePress and is_clicked: print("Clicked on line!") # 若是拖动事件,则更新线的位置 elif event.type() == QEvent.GraphicsSceneMouseMove and is_clicked: line.setLine(line.p1(), pos) watched.update() return True # 返回True表示继续传递事件 # 将事件处理器绑定到图表视图 handler = ChartHandler() chartView.installEventFilter(handler) ``` 3. **平移功能**: 用户按住鼠标左键时,可以开始拖动直线。在`eventFilter`中检查鼠标移动事件,并相应地更新直线的位置。 4. **清理事件过滤器**: 当不再需要监听时,记得从图表视图中移除事件过滤器。 ```python def remove_event_filter(): chartView.removeEventFilter(handler) # 在适当的时候清除事件过滤器 # remove_event_filter() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值