@@ -70,15 +70,15 @@ const imageUrls = {
7070 * ie which sprite are not transparent in the grid
7171 */
7272const countSprites = ( ( ) => {
73- const canvas = document . createElement ( "canvas" ) ;
74- canvas . width = 128 ;
75- canvas . height = 128 ;
76- canvas . style . position = "absolute" ;
77- canvas . style . top = "0" ;
78- canvas . style . width = "100%" ;
79- document . body . appendChild ( canvas ) ;
80-
81- // const canvas = new OffscreenCanvas(128, 128);
73+ // const canvas = document.createElement("canvas");
74+ // canvas.width = 128;
75+ // canvas.height = 128;
76+ // canvas.style.position = "absolute";
77+ // canvas.style.top = "0";
78+ // canvas.style.width = "100%";
79+ // document.body.appendChild(canvas);
80+
81+ const canvas = new OffscreenCanvas ( 128 , 128 ) ;
8282 const ctx = canvas . getContext ( "2d" ) ;
8383
8484 return ( img : CanvasImageSource & { width : number ; height : number } ) => {
@@ -169,55 +169,62 @@ export const createSpriteAtlas = async () => {
169169
170170 const destSize = SOURCE_SIZE ;
171171
172- const canvas = new OffscreenCanvas ( destSize * totalSpriteCount , destSize ) ;
172+ const destWidth = Math . floor ( 2048 / destSize ) * destSize ;
173+ const destHeight =
174+ Math . ceil ( totalSpriteCount / ( destWidth / destSize ) ) * destSize ;
175+ const canvas = new OffscreenCanvas ( destWidth , destHeight ) ;
173176
174177 // const canvas = document.createElement("canvas");
175- // canvas.width = destSize * totalSpriteCount ;
176- // canvas.height = destSize ;
178+ // canvas.width = destWidth ;
179+ // canvas.height = destHeight ;
177180 // canvas.style.position = "absolute";
178181 // canvas.style.top = "0";
179182 // canvas.style.width = "100%";
180183 // document.body.appendChild(canvas);
181184
182185 const ctx = canvas . getContext ( "2d" ) ;
183186
184- let i = 0 ;
185-
186187 const coords : Box [ ] [ ] = [ ] ;
187188
188189 const animationIndex = { } as AnimationIndex ;
189190
191+ let dx = 0 ;
192+ let dy = 0 ;
190193 for ( const { image, name, spriteCount } of images ) {
191194 animationIndex [ name ] = coords . length ;
192195
193196 const boxes = [ ] ;
194197 coords . push ( boxes ) ;
195198
196- let y = 0 ;
197- let x = 0 ;
199+ let sy = 0 ;
200+ let sx = 0 ;
198201 for ( let k = spriteCount ; k -- ; ) {
199202 boxes . push ( [
200- [ i / totalSpriteCount , 0 ] ,
201- [ ( i + 1 ) / totalSpriteCount , 1 ] ,
203+ [ dx / destWidth , dy / destHeight ] ,
204+ [ ( dx + destSize ) / destWidth , ( dy + destSize ) / destHeight ] ,
202205 ] ) ;
203206
204207 ctx . imageSmoothingEnabled = false ;
205208 ctx . drawImage (
206209 image ,
207- x * SOURCE_SIZE ,
208- y * SOURCE_SIZE ,
210+ sx ,
211+ sy ,
209212 SOURCE_SIZE ,
210213 SOURCE_SIZE ,
211- i * destSize ,
212- 0 ,
214+ dx ,
215+ dy ,
213216 destSize ,
214217 destSize ,
215218 ) ;
216- i ++ ;
217- x ++ ;
218- if ( x * SOURCE_SIZE >= image . width ) {
219- x = 0 ;
220- y ++ ;
219+ dx += destSize ;
220+ sx += SOURCE_SIZE ;
221+ if ( sx >= image . width ) {
222+ sx = 0 ;
223+ sy += SOURCE_SIZE ;
224+ }
225+ if ( dx >= destWidth ) {
226+ dx = 0 ;
227+ dy += destSize ;
221228 }
222229 }
223230 }
0 commit comments