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

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

问题描述



我想在布料js中的canvas上注册javaScript事件。

解决方案

如果你想用你的自定义事件扩展fabricjs事件系统,这很简单:

  var canvas = new fabric.Canvas('c'); 
//使用任何有效负载消除任何事件
canvas.fire('custom:event',{any:'payload'});
//监听任何事件,如fabricjs do
canvas.on('custom:event',function(event){
//你的数据将在`event` object
});

我喜欢这种方式,因为你不使用任何其他libs,仍然有相同的语法在所有案例(本机和自定义事件)。


How can i fire custom events on canvas in fabric js?

I want to register javaScript events on canvas in fabric js.

解决方案

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

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

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).

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

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