我是 Paper.js 的新手,在阅读教程时,我对事件系统感到疑惑。 这就是 tutorial 中描述的事件处理方式:
var path;
function onMouseDown(event) {
// Create a path:
path = new Path();
path.strokeColor = 'black';
// Add the mouse down position:
path.add(event.point);
}
function onMouseUp(event) {
// Add the mouse up position:
path.add(event.point);
}
所以,它只是在全局命名空间中起作用...
最后我有几个问题,我没有在互联网上找到任何关于这个的东西:
- 如何将事件处理程序绑定(bind)到特定 Canvas ?
- 如何将事件处理程序绑定(bind)到特定“对象”(光栅图像、矩形等)?
- 如何将多个事件处理程序绑定(bind)到某物?
最佳答案
您可以使用 attach() 方法(或其 jQuery 风格的别名 on())绑定(bind)多个事件处理程序。您可以使用 detach() 或 off() 删除它们。这是来自 the event handling documentation 的修改示例:
// 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);
});
如果你想为一种类型的对象的所有实例设置一个事件处理程序,最好创建一个工厂函数,根据 this answer .
关于javascript - Paper.js 中的事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19788760/