-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation Selection Events #5695
Comments
this would be a pretty awesome doc. Can you old on some days i decided to update to fabric 3.0 and i update the website with what i have, then you can open a pr on fabricjs.com? |
Great doc! Just a thought, perhaps using the term "clear selection" rather than "delete selection" would help to prevent confusion about the objects themselves being deleted. Great work @awehwring! |
Thank you for the kind response. :-) @asturur: Are the sources of the Tutorial and the Demos are somewhere accesible? I think I could contribute to them or update them now and than. I'm not a very experienced JavaScript developer, so I'm afraid to open a PR for the fabric.js sourcecode. Might do more harm than good. @melchiar: English is a foreign language to me, so I'm happy about any improvements. I updated the initial posting with your suggestion. |
English is foreign to me too, and I use Grammarly to get better sentences. It integrates with browsers and teaches you lots of things. https://github.com/fabricjs/fabricjs.com/ is where the website is stored. Is all plain text so should be fine to add documents. I think it supports MD formatting, so maybe you can just add that or you can eventually make a simple html with it. If you have problems running the website locally let me know. |
the discussion about what to fix and improve is valid too, just i do not have time to examinate it and talk about it, but i m sure some points are valid. |
Ok, thanks, I'll try to contribute to the website. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
What about this beauty? |
ShaMan123, I'm still having this on my todo list. Would like to add it to the official documentation. But things slip ... 😏 As we are now already in Version 5 with fabricjs, I have to check if it still behaves the same. |
Version
2.7.0
Test Case
fiddle
Information about environment
Browser
Overview
While working with multi-selections I discovered that the selection functionality is much smarter than I thought. The various selection-events return different usefull properties, depending on the way you (de-)selected objects.
It would be worth to explain this better in the Documenation / Tutorial or as a Demo.
The following is a bit verbose, so that it can be used there.
At the end I propose some minor enhancements / bugs.
TL;DR ? Play the fiddle.
Selecting
You can select fabric.js objects by clicking at an object or by drawing a selection area with the mouse. Objects can be added or removed from the selection by shift-clicking them (hold down the shift-key while clicking).
Clicking at the canvas clears the selection.
Selection Events
There are four events that might be fired, depending on how you selected:
selection:created
After a new selection is created.selection:updated
After an existing selection is expanded or reduced.before:selection:cleared
After a selection is cleared, but before the selection-object is destroyed.selection:cleared
After the selection object is destroyed.Event Properties
Each event returns one or more properties:
evt.selected
is set when a new selection was created or expanded, withselection:created
andselection:updated
eventsevt.updated
is set when the selection changed, withselection:updated
eventevt.deselected
is set when the selection has changed, was reduced or cleared, withselection:created
andselection:cleared
eventsevt.target
holds the (still) selected object(s) after the selection took place, withselection:created
, orselection:updated
, orbefore:selection:cleared
eventsDatatypes
The datatypes of the event-properties are dissimilar.
evt.selected
andevt.deselected
always holds the object(s) in an array.selection:updated
holds the object directly.evt.target
holds the object directly or holds anActiveSelection
object when more than one object is affected.Get Current Selection
Besides the event-properties it is allways possible to obtain the currently selected object(s) with two methods:
Canvas.getActiveObject()
Returns the selected object or anActiveSelection
object, when more than one object is selected. Returnsundefined
ornull
when nohing is selected.Canvas.getActiveObjects()
Allways returns an array with the selected object(s). Returns an empty array[ ]
when nothing is selected.Generally the methods return the same objects as the
evt.target
property. Except for thebefore:selection:cleared
event, when more than one object was deselected (propably a bug, see below).Event-Properties Table
The table shows which events are triggered by different selection procedures and which properties and objects are returned with the event-object.
The details can be examined with the linked fiddle.
{new object,
old object}
old object}
new object]
{new object,
new object}
new object}
(first in collection)
old object}
old object]
Improvements / Bugs
a) The
evt.target
property after thebefore:selection:cleared
event returns only the first object, when more than one object was deselected. This differs from theCanvas.getActiveObject()
method.Fixed with Version 3.0, issue #5658.
b) The event-properties are either
undefined
, ornull
, or an empty array[ ]
when no object is returned in the specific case. A unification would be easier.c) That the event-properties contain the objects as different datatypes is somewhat confusing (object directly, array of objects,
ActiveSelection
object).d) Including this information in the Documentation / Tutorial / Demo would help newbies.
I hope this information helps someone to use the wonderfull fabric.js library more effective.
The text was updated successfully, but these errors were encountered: