javascript - Paper.js 中的事件处理程序

标签 javascript graphics paperjs

我是 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/

相关文章:

javascript - Vue.js 缓存方法结果?

java - Java swing 中图形的标记

graphics - 从 Ocaml Graphics 中的文件加载图像

javascript - 在 Paper.js 中对片段和位置进行动画处理

javascript - 过滤对象数组 - 几个条件

javascript - nodejs 将上传的文件直接传输到S3,无需双重等待时间

javascript - 如何让电子表格打开 Excel 而不是浏览器窗口?

c++ - 如何开始在 openGL 和 C++ 中构建 cooliris?

javascript - 尝试直接从 javascript 控制 Paper.js 时出现奇怪的行为

javascript - Paper.js 段未定义