如何在fabric.js中的画布上触发自定义事件? [英] How can I fire custom events on canvas in fabric.js?

查看:103
本文介绍了如何在fabric.js中的画布上触发自定义事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在fabric.js中的画布上注册JavaScript事件.如何在画布上触发自定义事件?

I want to register JavaScript events on canvas in fabric.js. How can I fire custom events on canvas?

推荐答案

如果要使用自定义事件扩展fabricjs事件系统,那很简单:

If you want to extend fabricjs event system with your custom events, that is pretty simple:

var canvas = new fabric.Canvas('c');
// listen for any event like fabricjs do
canvas.on('custom:event', function(event) {
  // you will have your data here in `event` object 
});
// fire any event with any payload you want
canvas.fire('custom:event', { any: 'payload' });

我更喜欢这种方式,因为您不使用任何其他库,并且在所有情况下(本机事件和自定义事件)仍然具有相同的语法.

I prefer this way as you do not use any other libs and still have the same syntax in all the cases (native and custom events).

更新!不要忘记,您应该在触发事件之前定义侦听器!通过一个有效的示例检查提琴.

UPDATE! Do not forget that you should define listener BEFORE firing an event! Check this fiddle with a working example.

这篇关于如何在fabric.js中的画布上触发自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆