简体   繁体   English

Fabric JS 处理鼠标移过行时出现的奇怪问题 object

[英]Fabric JS Strange problem when handling mouse move over line object

I'm developing an object drawing canvas that uses Fabric.js and i'm encountering some strange issues when trying to deal with lines.我正在开发使用 Fabric.js 的 object 绘图 canvas 并且在尝试处理线条时遇到了一些奇怪的问题。 To make things clear, i've created a very simple jsfiddle that show what i'm seeing.为了清楚起见,我创建了一个非常简单的 jsfiddle 来显示我所看到的。 It's an event inspector that checks if the mouse is over a line, over a square or nowhere.它是一个事件检查器,用于检查鼠标是否在一条线上、一个正方形上或任何地方。 The objects are movable if you want, it's the same with fixed objects.如果您愿意,对象是可移动的,与固定对象相同。 As you can see, if you try to position your mouse around the square, it only fires an event when you actually put the pointer on the square.如您所见,如果您尝试在正方形周围移动鼠标 position,它只会在您实际将指针放在正方形上时触发事件。 When you try to go near the line things change and we see a bigger area that fires this event.当您尝试在线附近的 go 时,情况发生了变化,我们看到一个更大的区域触发了此事件。 Am I making some trivial mistake, its a bug or something else that i didn't think of?我是否犯了一些微不足道的错误,它是一个错误还是我没有想到的其他东西? Thank you all for your interest谢谢大家的关注

var canvas = new fabric.Canvas('c');

var square = new fabric.Rect({
                    left: 200,
                    top: 100,
                    width: 20,
                    height: 20,
                });

var line = new fabric.Line([0,0,100,100], {
  stroke: 'red',
  strokeWidth: 1,
});
    
canvas.add(square);
canvas.add(line);
    
canvas.on('mouse:move', (e)=>{
    switch (e.target){
    case line:
    {
        console.log("Pointer over a line");
      canvas.backgroundColor="green";
        break;
    }
    case square:
    {
        console.log("Pointer over a square");
      canvas.backgroundColor="blue";
        break;
    }
    default:
    {
        canvas.backgroundColor="white";
        break;
    }
  }
  
  canvas.renderAll();
});

https://jsfiddle.net/arozgkct/3/ https://jsfiddle.net/arozgkct/3/

Mouse events in FabricJS are always triggered within the bounding box of the object, which means that the bounding dimensions of your line are actually determined by the width and height of your line object. FabricJS 中的鼠标事件总是在 object 的边界框内触发,这意味着你的线的边界尺寸实际上是由你的线 object 的宽度和高度决定的。 If you'd rather the events be confined to the line itself, draw the line on just one axis and then rotate it by setting an angle value of 45 degrees.如果您希望事件仅限于线条本身,请仅在一个轴上绘制线条,然后通过将角度值设置为 45 度来旋转它。

var line = new fabric.Line([0,100,100,100], {
  stroke: 'red',
  strokeWidth: 1,
  angle: 45,
  left: 50
});

https://jsfiddle.net/melchiar/puwgeb02/ https://jsfiddle.net/melchiar/puwgeb02/

In addition to what melchiar said, i learnt that there is another method to capture the event from the line only and not also from the bounding box around it.除了 melchiar 所说的之外,我了解到还有另一种方法可以仅从线路捕获事件,而不是从它周围的边界框捕获事件。 I document it here if anyone else need in the future.如果将来有其他人需要,我会在此处记录。 You just have to add these two lines after adding the line to the canvas您只需在将行添加到 canvas 后添加这两行

line.perPixelTargetFind = true;
line.targetFindTolerance = 4;

Complete code in the updated jsfiddle https://jsfiddle.net/gmto0h58/完整代码在更新的jsfiddle https://jsfiddle.net/gmto0h58/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM