简体   繁体   English

使用匿名函数将事件绑定到KineticJS对象

[英]Binding events to KineticJS objects using anonymous functions

I am working on a project using Canvas and KineticJS. 我正在使用Canvas和KineticJS进行项目。 I load/create a number of nodes and edges, and have desired functionality when I interact with these under various circumstances. 我加载/创建了许多节点和边缘,并在各种情况下与它们交互时具有所需的功能。

At the moment I'm binding event handlers to these objects as they're created using anonymous functions. 目前,我将事件处理程序绑定到这些使用匿名函数创建的对象上。 Something like this: 像这样:

Node.prototype.setupNode = function(x, y) {

    this.visual.on('tap', function(e) {
        console.log(this.id);
    });

    ...
}

If I have a couple of event handlers to bind for each item, some of which are a fair number of lines of code, and I'm often binding a few hundred node and edge objects, are there more optimal ways of binding these events to these objects so that I can increase performance and decrease load time and resource usage? 如果我为每个项目绑定了几个事件处理程序,其中一些是相当数量的代码行,并且我经常绑定数百个节点和边缘对象,那么有更好的方法将这些事件绑定到这些对象,以便提高性能并减少加载时间和资源使用量?

Thanks in advance for any help whatsoever. 预先感谢您的任何帮助。

You can create a named function and then pass it's reference to the event binding function, like this: 您可以创建一个命名函数,然后将其引用传递给事件绑定函数,如下所示:

function handleTap (e) {
   //your code
}

Node.prototype.setupNode = function(x, y) {
    this.visual.on('tap', handleTap);
    ...
}

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

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