简体   繁体   中英

How can I fire custom events on canvas in fabric.js?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM