Javascript Paper.js中的事件处理程序

Javascript Paper.js中的事件处理程序,javascript,graphics,paperjs,Javascript,Graphics,Paperjs,我是Paper.js新手,在阅读教程时,我对事件系统感到惊讶。 汉林事件就是这样描述的: 所以,它只是在全局名称空间中运行… 最后,我有一些问题,但我在网上没有找到这方面的任何信息: -如何将事件处理程序绑定到特定画布? -如何将事件处理程序绑定到特定的“对象”(光栅图像、矩形等)? -如何将多个事件处理程序绑定到某个对象?我对Paperjs不熟悉,但我的想法如下: 如何将事件处理程序绑定到特定画布 将画布指定给脚本时,范围将自动关联到画布: <script type="text/pa

我是Paper.js新手,在阅读教程时,我对事件系统感到惊讶。 汉林事件就是这样描述的:

所以,它只是在全局名称空间中运行…
最后,我有一些问题,但我在网上没有找到这方面的任何信息:
-如何将事件处理程序绑定到特定画布?
-如何将事件处理程序绑定到特定的“对象”(光栅图像、矩形等)?

-如何将多个事件处理程序绑定到某个对象?

我对Paperjs不熟悉,但我的想法如下:

  • 如何将事件处理程序绑定到特定画布
将画布指定给脚本时,范围将自动关联到画布:

<script type="text/paperscript" src="js/myScript.js" canvas="myCanvas"></script>

可以使用attach()方法(或其jQuery样式别名on())绑定多个事件处理程序。您可以使用detach()或off()删除它们。以下是一个修改后的示例:


如果要为一类对象的所有实例设置事件处理程序,最好创建一个工厂函数,如。

我是说一个事件有两个处理程序。我不确定我是否理解“一个事件有两个处理程序”?Alex,你答案的最后一部分如何在代码中翻译?我想在我的Paper.js/js项目中为mouseEnter、mouseleave附加事件处理程序,用于除光栅外的所有对象。我该怎么做?这些项目是从SVG导入的,因此手动向每个路径添加处理程序有点混乱。当我们通过paper.js直接使用javascript时,我们如何向形状添加事件?(我们必须手动添加工具…)
<script type="text/paperscript" src="js/myScript.js" canvas="myCanvas"></script>
var path = new Path.Circle();

path.onClick = function(event) {
    this.fillColor = 'red';
}

path.onDoubleClick = function(event) {
    this.fillColor = 'green';
}
// Create a circle shaped path at the center of the view:
var path = new Path.Circle({
    center: view.center,
    radius: 25,
    fillColor: 'black'
});

var shiftPath = function() {
    this.position += new Point(15,15);
};

// When the mouse enters the item, set its fill color to red:
path.attach('mouseenter', function() {
    this.fillColor = 'red';
});

path.on('mouseenter', shiftPath);

// When the mouse leaves the item, set its fill color to black
// and remove the mover function:
path.on('mouseleave', function() {
    this.fillColor = 'black';
    path.detach('mouseenter', shiftPath);
});