简体   繁体   English

将事件侦听器附加到 Paper.js 中图层上的所有项目

[英]Attach event listeners to all items on a layer in Paper.js

In jQuery, I can attach an event to all elements like a paragraph, using $("p") .在 jQuery 中,我可以使用$("p")将事件附加到段落等所有元素。 Does something similar exist for Paper.js. Paper.js 是否存在类似的东西。 I tried using project.activeLayer.children.onMouseEnter but that did not work.我尝试使用project.activeLayer.children.onMouseEnter但没有奏效。

I was thinking about looping over all the children inside the onFrame function but that seems wrong.我正在考虑遍历onFrame function 内的所有孩子,但这似乎是错误的。 It will probably attach multiple event listeners to the same item.它可能会将多个事件侦听器附加到同一个项目。 So, all items will have many event listeners attached to them but I am not sure.因此,所有项目都会附加许多事件侦听器,但我不确定。

What is the best way to attach event listeners to all current and future items in a project layer in Paper.js?在 Paper.js 的项目层中将事件侦听器附加到所有当前和未来项目的最佳方法是什么?

Link: http://paperjs.org/reference/item/#onmouseenter链接: http://paperjs.org/reference/item/#onmouseenter

You just have to attack an event listener to the Tool .您只需要攻击Tool的事件侦听器。
The event argument that will be passed to the listener will then have an item property which is the event target item.然后,将传递给侦听器的event参数将具有一个item属性,该属性是事件目标项。
Here is a simple sketch demonstrating this.这是一个简单的草图来证明这一点。

tool.onMouseDown = (event) => console.log(event.item && event.item.name);

let id = 0;

drawRandomCircle();
setInterval(drawRandomCircle, 2000);

new PointText({
    content: 'Click on a circle to log its name',
    point: view.center + [0, -80],
    justification: 'center'
});

function drawRandomCircle() {
    new Path.Circle({
        center: Point.random() * view.size,
        radius: 50,
        fillColor: Color.random(),
        name: `item ${++id}`
    });
}

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

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