Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在fabricjs中进行边界检测?_Javascript_Fabricjs - Fatal编程技术网

Javascript 如何在fabricjs中进行边界检测?

Javascript 如何在fabricjs中进行边界检测?,javascript,fabricjs,Javascript,Fabricjs,我的应用程序需要在光标悬停在路径或rect或其他对象的边缘时执行某些操作(仅在边缘而不是对象),但我找不到任何api来执行此操作。如何实现?谢谢 例如下面的伪代码 path.on('mouse:move', function () { if( mouse on the edge of the path ){ change the cursor to a star }else{ do nothing

我的应用程序需要在光标悬停在路径或rect或其他对象的边缘时执行某些操作(仅在边缘而不是对象),但我找不到任何api来执行此操作。如何实现?谢谢

例如下面的伪代码

path.on('mouse:move', function () {
          if(  mouse on the edge of the path ){
             change the cursor to a star
          }else{
             do nothing
          }
        });


考虑将对象的perPixelTargetFind属性设置为true。

这样你就可以:

canvas.on({
    "mouse:over": (event) => {
        if (event.target == path) {
            change cursor;
        }
        else {
            do nothing;
        }
    }
});
唯一的缺点是,当鼠标在对象填充上时也会触发,因此从技术上讲,如果只想进行边界检测,则可能必须删除对象中的任何填充

var pathFill = path.fill;
path.fill = "transparent";
然后,如果您想在鼠标不再悬停在边缘后重置所有内容,您可以:

canvas.on({
    "mouse:out": (event) => {
        if (event.target == path) {
            reset cursor;
            path.fill = pathFill;
        }
        else {
            do nothing;
        }
    }
});

感谢anwser,这将丢失填充部分的事件侦听。我在konvas上问了一个类似的问题,得到了一个很好的解决方法。
canvas.on({
    "mouse:out": (event) => {
        if (event.target == path) {
            reset cursor;
            path.fill = pathFill;
        }
        else {
            do nothing;
        }
    }
});