Javascript 如何从PaperJS中的CompoundPath中识别鼠标事件的目标子路径?
我将“mousedown”事件侦听器添加到CompoundPath,而不是它的子级。但我需要知道事件发生在哪个孩子身上。事件对象的“target”属性正在返回CompoundPath。那么,解决方案是什么呢?解决方案主要取决于您的特定用例。 但解决这个问题的一般方法是在每个CompoundPath子级的事件点上进行命中测试,并推断是哪一个触发了事件。 下面是一个演示解决方案的示例Javascript 如何从PaperJS中的CompoundPath中识别鼠标事件的目标子路径?,javascript,paperjs,Javascript,Paperjs,我将“mousedown”事件侦听器添加到CompoundPath,而不是它的子级。但我需要知道事件发生在哪个孩子身上。事件对象的“target”属性正在返回CompoundPath。那么,解决方案是什么呢?解决方案主要取决于您的特定用例。 但解决这个问题的一般方法是在每个CompoundPath子级的事件点上进行命中测试,并推断是哪一个触发了事件。 下面是一个演示解决方案的示例 const circle1 = new Path.Circle({ center: view.center
const circle1 = new Path.Circle({
center: view.center - 50,
radius: 50
});
const circle2 = new Path.Circle({
center: view.center + 50,
radius: 50
});
const compoundPath = new CompoundPath({
children: [circle1, circle2],
fillColor: 'orange',
onMouseDown: event => {
if (circle1.hitTest(event.point)) {
alert('circle 1 clicked');
} else if (circle2.hitTest(event.point)) {
alert('circle 2 clicked');
}
}
});