问题描述
class Node{...}
a = new Node(...);
b = new Node(...);
var nodeSet = new Set(...);
let myEvent = new CustomEvent(...);
for (var node of nodeSet){
node.shape.dispatchEvent(myEvent);
}
由于我是 Javascript 初学者,请保持回复通俗易懂,谢谢。
解决方法
为了使您的自定义事件与 Konva Node
对象一起使用,Shape
子类有一个 on
方法,可以让您直接在形状。
这样做:
class Node{...}
a = new Node(...);
b = new Node(...);
a.shape.on('Yo!',handler);
b.share.on('Yo!',handler);
// all 4000 shapes
var nodeSet = new Set(...);
let myEvent = new CustomEvent('Yo!');
// If you only have one handler for every shape's treatment of CustomEvent
for (var node of nodeSet){
node.shape.on('Yo!',handler);
}
for (var node of nodeSet){
node.shape.dispatchEvent(myEvent);
}