No.
seperate the tileset image into a bunch of different Image objects
It wouldn't be faster to create a slew of separate Image
s: each of these would be a copy existing on a different part of the heap (memory); you'd induce more cache misses by scattering multiple image copies across the heap. Better to use the 9-parameter formatdrawImage()
to reference intorefer to one single part of the Image
's heap allocation, to recall each sub-image, then modify it, then copy back.
however, I may decide to change the map at runtime
Image
is based on an ImageData
reference which itself is based on its data
property, which is a Uint8ClampedArray
. This buffer can be fast-set using set:
image.data.set(subImage, startOffset);
If you want to fast-modify, you can copy out part of this Uint8ClampedArray
, modify it, and write it back again using subarray:
const subimage = image.data.subarray(startOffset, endOffset); //note: creates a new buffer
//...modify copied subimage here...
image.data.set(subImage, startOffset); //set back into the atlas
This is the fastest method Javascript alone (without WASM) offers for image manipulation, to date. For modifying the subimage pixel by pixel, see this question for tips on fastest pixelwise writes.
better ... faster
These are two different things, nearly always, when it comes to code. You need to decide which you want. If you mean "easier for the programmer" then by all means split into multiple Image
s. If you mean faster, definitely do not do that. The Engineering Triangle is a salient reminder of the trade-offs we make. Always beA good engineer is always very specific in yourtheir communication with yourselfthemselves -- for clarity of thought -- and with others. Choose your words wisely.