Question

I have been using fabricjs since version 1.1.9 and created quite a big application. Now i'm trying to use the newer version 1.4.0, and have found out there are many changes. It was possible to use getActiveObject() to select single object and also to select multiple group object, but now it is throwing error on group object while i am trying to get some property of selected group (like strokeWidth), the error is "Uncaught TypeError: Cannot read property 'strokeWidth' of null". But if i use getActiveGroup() for group object, there is no error. The fact is if this is the problem, i have to recreate my whole project. Is it possible to resolve my situation with some minor fix? Where can i find the change log for version 1.4.0?

Was it helpful?

Solution

maybe this is could be a solution for you. getActiveObject() returns null if a group is selected and if a object is selected getAtctiveGroup() returns null as well. Just create a function that returns whatever is selected on the canvas.

function getSelection(){
  return canvas.getActiveObject() == null ? canvas.getActiveGroup() : canvas.getActiveObject()
}

If you now replace all getActiveObject() with getSelection() calls in your code this should solve your problem. Hope this helps.

OTHER TIPS

You can use canvas.getActiveObjects() which will return all selected objects.

canvas.getActiveObjects().forEach((object) => {
  // do something with object
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top