Layer Ordering: API? Implementation?
See original GitHub issueTo support layer ordering, we’ll need to do one of two things:
- Change
Project.sprites
from an object (which has no explicit order) to an Array, and add agetSprite
orsprite
method to access a sprite by name. - Give each sprite a
layerOrder
property, and use some complex logic like Scratch does to keep them in order in the renderer. Some of this logic (e.g. sorting sprites by layer order) would have to be done each frame, as we don’t maintain a “draw list”, instead just drawing whateverProject.sprites
contains.
_Originally posted by @adroitwhiz in https://github.com/PullJosh/scratch-js/issues/23#issuecomment-569812205_
Issue Analytics
- State:
- Created 4 years ago
- Comments:17
Top Results From Across the Web
Control feature layer drawing order | Esri Leaflet
You can define a pane to control the order in which feature layers are drawn. This allows you more cartographic control over the...
Read more >Google Maps JavaScript API - Layer Ordering - Stack Overflow
I have a map that loads building footprints from GeoJSON. My map also uses Street View. Like all Google Maps, Street View is...
Read more >Implementing the microservice application layer using the ...
For instance, the application layer code of the ordering microservice is directly implemented as part of the Ordering.API project (an ASP.
Read more >Lab 2: Complete the Process API Layer for Order Fulfillment API
We'll post the order to the Order API. module10 lab2 api overview. The implementation will consist of a few steps. Extend the Order...
Read more >ArcGIS JavaScript API layer order? - GIS Stack Exchange
I'm sure there is a better way of doing this but here is how I did it: I stored the layers to be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
IMO any non-contiguous layering system is asking for trouble. Floating-point z-index in particular trades apparent simplicity for a bunch of complicated issues under the surface.
For instance, moving a sprite forward n layers means enumerating the first n sprites with a higher z-index, then swapping the nth sprite’s z-index with the current one. Moving a sprite backwards means a similar process.
If you want to insert a new layer between two other layers, you’d have to increment the z-indices of every layer above the new layer. Note that it’s not enough to make the new layer’s z-index the average of the layers above and below it–if you inserted enough layers in the same spot (see: any sprite which creates a couple hundred clones), the z-indices would be packed closer and closer and you’d quickly run out of floating-point precision.
Sprites’ z-indices probably wouldn’t be directly interactible anyways–that would allow too much room for annoying bugs, especially if the runtime wants to modify z-indices when, as I mentioned earlier, you perform any layering operations. If you allowed API consumers to modify z-indices, you’d open the door to them doing things like giving two layers the same z-index, or trying to move sprites forward one layer by incrementing the z-index (remember, this is a floating-point z-index so there can be an arbitrary number of sprites between successive integer z-indices!)
You’d have to sort sprites by z-index in the renderer anyway-- OpenGL’s z-buffer doesn’t work with transparent textures.
I think this leads into a wider question about
Project.sprites
. Currently, you can add sprites just by assigning a new property toProject.sprites
, and delete sprites by using thedelete
operator on one of its properties, but this doesn’t givescratch-js
any way to control what happens when you add or delete sprites.It feels more like it was designed around an
Object
of sprites being passed once whenProject
is initialized, with no sprites being added or removed at runtime.I think that using
Object
s for static sets andMap
s orArray
s for dynamic ones is more idiomatic in JS, so it comes down to whether the API should support the dynamic addition or removal of sprites in a runningProject
.If dynamic sprite addition/removal isn’t supported, the API becomes easy: just give each sprite in the object passed to the
Project
constructor alayerOrder
property, and inside theProject
constructor, create anArray
of the passed sprites sorted by that property. You could alsoObject.freeze
the project’ssprites
to prevent unexpected tomfoolery.If you do want to support adding/removing sprites, I think it makes much more sense to store them as an array (as is already done for another dynamic set: sprites’ clones). There would be
Project.addSprite
to add a sprite,Project.removeSprite
to remove one, andProject.sprite
orProject.getSprite
to get a sprite by its name. This would mean that sprites would be accessed by theirname
property as opposed to the object key they’re instantiated with, but for simplicity’s sake I think that’s a good thing. You could also keep the object-basedProject
constructor and automatically set a sprite’sname
based on the corresponding object key.