How can I fire custom events on canvas in Fabric JS?

我只是一个虾纸丫 提交于 2019-12-09 23:47:05

问题


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

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


回答1:


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.



来源:https://stackoverflow.com/questions/28235186/how-can-i-fire-custom-events-on-canvas-in-fabric-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!