Is there a way to layer objects on a Fabric.js canvas via the official API? Right now the only way I have found to do it is to manually iterate through the canvas._objects and reorder them so that they get drawn in a specific order. Is there a better way to do this that doesn't (potentially) break the object?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
- void before promise syntax
If you want to set a specific ordering for all objects, instead of moving one object forward/backward, iterative calls to bring/send to front/back are slow.
You can use the code of
bringToFront
as inspiration to speed this use case up: https://github.com/kangax/fabric.js/blob/586e61dd282b22c1d50e15c0361875707e526fd8/src/static_canvas.class.js#L1468And do this:
Where compare defines the sorting, like:
If you wish to bring smaller objects to the front.
[Edit] I've corrected my info below (my bad, I was originally thinking of the KineticJs api).
FabricJS has these API methods that change the z-index of objects:
Under the covers, FabricJs changes the z-index by removing the object from the getObjects() array and splicing it back in the desired position. It has a nice optimization that checks for intersecting objects.
step 1 : You can Use
preserveObjectStacking: true
step 2 : then use sendtoback,sendtofront....fabricjs options like below
Answered over here