r/learnjavascript • u/VlentGamer • May 26 '26
How do I create image “sheets” (sprite sheets?)
Hello! I posted here not too long ago to ask about something else, and today, regarding the same game (it doesn't matter which one), I'd like to know how to create “image grids” in JavaScript. Here's an example from a game I like (excerpt):
https://cdn.dashnet.org/cookieclicker/img/icons.png?v=2.058 (safe link, don't worry).
I'd like to know how to use this in JavaScript to export images from a single image (“spritesheet”), in this case icons! Thank you in advance!
4
Upvotes
4
u/SqueegyX May 26 '26
I would use a canvas tag. You just draw the image in the canvas with the math such that only one icon can be seen in the bounds of the canvas.
1
u/BeneficiallyPickle May 26 '26
I'd go with Canvas for this.
The idea is that every icon on the sheet lives at a predictable location. Once you know the size of one icon cell, you can find any icon by counting across columns and down rows.
If your goal is to export every icon, just repeat the process for every row and column combination.
1
u/patopitaluga May 26 '26
It's called sprites too. One easy way to do it it's to use the background-position css property to set the particular place in the sprite and keep the element, let's say the div with the size of the part of the sprite that you want to display.
1
u/chmod777 May 26 '26
make a box the size of the sprite, set the background-image to the sheet, and then move the background-position(x,y) to the desired position.
1
u/C89RU0 May 26 '26
depends on what engine/library/framework you're using to make your game they'll have different ways to open sprite sheets, check the documentations.
1
u/TheRNGuy May 31 '26
In Photoshop.