简体   繁体   中英

Fabric.js: disable deselection

I need to disable deselection of a selected object in Fabric.js 1.7.19. I tried:

  object.on('deselected', function(e) {
    canvas.setActiveObject(object);
  });

This seems to do the job, but removal of a selected object doesn't work: remove() triggers 'deselected' as well, leading to some bad behaviour.

As for the onDeselect() callback mentioned in the docs, I can't seem to get it to work. Could someone help me with an example?

Fiddle here: https://jsfiddle.net/gl03/khwmk4yx/

You can check if the object is existing in canvas._objects array.

function find(objs, obj){
    for (var i = 0; i< objs.length; i++)
      if(objs[i] == obj) return 1;
  return 0;
} 
 image.on('deselected', function(e) {
    if(find(canvas._objects,image)) {
      console.log('deselected');
      canvas.setActiveObject(image);
     }
 });

see https://jsfiddle.net/4bvb8ofz/2/

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