javascript - 你如何在fabricjs中进行边界检测?
问题描述
当光标悬停在路径或矩形或其他东西的边缘(仅在边缘而不是对象上)时,我的应用程序需要执行某些操作,但我找不到任何 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。 http://fabricjs.com/docs/fabric.Path.html#perPixelTargetFind
path.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;
}
}
});
推荐阅读
- matlab - 根据最后一列的值拆分行并将它们保存到不同的文件中
- syntax - 你能判断一门编程语言是仅仅通过它的语法来编译还是解释的吗?
- ember.js - 尝试使用已在使用的 id 注册视图:2 - EmberJs
- python-3.x - 转换二进制
十六进制字节到十进制值列表 - python - 具有多标签和长时间窗口的 LSTM 回归
- unity3d - 如何在统一 2d 中重新使用绑定动画进行左右移动?
- javascript - Javascript 闭包是保留整个父词法环境还是仅保留闭包引用的值的子集?
- python - 如何用 Pandas 对另外两列中的唯一值求和?
- audio - 与“真实”完全不同的合成和弦声音
- java - 如何在 MySQL 中仅更改一个字段值?