Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 874
Timo Kähkönen Avatar asked Feb 07 '14 14:02

Timo Kähkönen


3 Answers

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
like image 86
Dakshika Avatar answered Nov 07 '22 19:11

Dakshika


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
  }
})
like image 21
CQ Smooth Avatar answered Nov 07 '22 19:11

CQ Smooth


@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

like image 1
londonfed Avatar answered Nov 07 '22 19:11

londonfed