简体   繁体   中英

Disable group selection in Fabric.js

How to disable group selection in Fabric.js and leave single objects selectable one at a time? With group selection I mean selecting multiple objects using eg. SHIFT+Click.

you can easily achieve this with

  canvas.selection = false; // disable group selection

if you want it on individual object

  rect.set('selectable', false); // make object unselectable

Disable group selection via selection listener canvas method (in my opinion the best way)

canvas.on('selection:created', (e) => {
  if(e.target.type === 'activeSelection') {
    canvas.discardActiveObject();
  } else {
    //do nothing
  }
})

@CQ Smooth's answer was close to my requirements, thanks. But I needed the event to be fired when you select one object first, then add another via shift+click:

  canvas.on({
      'selection:updated': e => {
        const activeSelection = e.target
        if (activeSelection) {
           canvas.discardActiveObject();
        }
      }
  })

You can see the solution working at: https://app.collagemaker.uk/create

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