Javascript 如何在fabricjs中进行边界检测?
我的应用程序需要在光标悬停在路径或rect或其他对象的边缘时执行某些操作(仅在边缘而不是对象),但我找不到任何api来执行此操作。如何实现?谢谢 例如下面的伪代码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
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;
}
}
});