【问题标题】:Event handlers in Paper.jsPaper.js 中的事件处理程序
【发布时间】:2013-11-08 18:20:51
【问题描述】:

我是 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);
}

所以,它只是全局命名空间中的函数...
最终我对此有一些疑问,但我没有在互联网上找到任何关于此的内容:
- 如何将事件处理程序绑定到特定的画布?
- 如何将事件处理程序绑定到特定的“对象”(光栅图像、矩形等)?
- 如何绑定多个事件处理程序?

【问题讨论】:

    标签: javascript graphics paperjs


    【解决方案1】:

    您可以使用 attach() 方法(或其 jQuery 样式别名 on())绑定多个事件处理程序。您可以使用 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 创建一个工厂函数。

    【讨论】:

    • 亚历克斯,你答案的最后一部分将如何翻译成代码?我想在我的 Paper.js /JS 项目中为除光栅之外的所有对象附加 mouseEnter 和 mouseleave 的事件处理程序。我怎样才能做到这一点?这些项目是从 SVG 导入的,因此手动向每个路径添加处理程序有点麻烦。
    • 当我们直接通过 paper.js 使用 javascript 时,如何向形状添加事件? (我们必须手动添加工具...)
    【解决方案2】:

    我是 Paperjs 的新手,但这是我的想法:

    • 如何将事件处理程序绑定到特定的画布?

    当您将画布指定给脚本时,范围会自动关联到画布:

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

    此脚本的每条指令都将与此画布相关联。

    • 如何将事件处理程序绑定到特定的“对象”(光栅图像、矩形等)?

    根据类型,每个“对象”都有可用的事件处理程序集。 reference page 将为您提供每种类型对象的所有事件处理程序。

    • 如何将多个事件处理程序绑定到某个东西?

    例如 PathItem 有一个click event 和一个double click event

    var path = new Path.Circle();
    
    path.onClick = function(event) {
        this.fillColor = 'red';
    }
    
    path.onDoubleClick = function(event) {
        this.fillColor = 'green';
    }
    

    【讨论】:

    • 我的意思是一个事件的两个处理程序
    • 我不确定我是否理解“一个事件的两个处理程序”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多