littlejsengine 1.18.23 → 1.18.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,7 +35,7 @@ const engineName = 'LittleJS';
35
35
  * @type {string}
36
36
  * @default
37
37
  * @memberof Engine */
38
- const engineVersion = '1.18.23';
38
+ const engineVersion = '1.18.24';
39
39
 
40
40
  /** Frames per second to update
41
41
  * @type {number}
@@ -14307,17 +14307,21 @@ class TextureSheet
14307
14307
  * @param {Vector2} imageSize - Size of the source image in pixels
14308
14308
  * @param {Vector2} [frameSize] - Size of each frame, or the whole image if not passed
14309
14309
  * @param {number} [padding] - How many pixels padding around each frame
14310
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
14310
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
14311
14311
  * @return {TileInfo} Tile for the packed image, or undefined if the sheet is full */
14312
14312
  tryAdd(imageSize, frameSize=imageSize, padding=textureSheetPadding, sourcePadding=0)
14313
14313
  {
14314
14314
  ASSERT(isVector2(imageSize) && isVector2(frameSize), 'sizes must be vec2');
14315
14315
  ASSERT(frameSize.x > 0 && frameSize.y > 0, 'frame size must be positive');
14316
- ASSERT(isNumber(sourcePadding) && sourcePadding >= 0, 'sourcePadding must be a number >= 0');
14316
+
14317
+ if (isNumber(sourcePadding))
14318
+ sourcePadding = vec2(sourcePadding);
14319
+ ASSERT(isVector2(sourcePadding) && sourcePadding.x >= 0 && sourcePadding.y >= 0,
14320
+ 'sourcePadding must be a number or vec2 >= 0');
14317
14321
 
14318
14322
  // the source may have its own padding baked in around each frame
14319
- const sourceCellWidth = frameSize.x + sourcePadding*2;
14320
- const sourceCellHeight = frameSize.y + sourcePadding*2;
14323
+ const sourceCellWidth = frameSize.x + sourcePadding.x*2;
14324
+ const sourceCellHeight = frameSize.y + sourcePadding.y*2;
14321
14325
  ASSERT(imageSize.x % sourceCellWidth === 0 && imageSize.y % sourceCellHeight === 0,
14322
14326
  'image size must be a multiple of the padded frame size');
14323
14327
 
@@ -14359,16 +14363,19 @@ class TextureSheet
14359
14363
  * @param {HTMLImageElement} image - Source image to copy from
14360
14364
  * @param {TileInfo} tileInfo - Where to put it, from tryAdd
14361
14365
  * @param {boolean} [update] - Upload to webgl now, pass false when batching
14362
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image */
14366
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image */
14363
14367
  drawImage(image, tileInfo, update=true, sourcePadding=0)
14364
14368
  {
14365
14369
  ASSERT(!!this.context, 'texture sheet has no canvas');
14366
14370
 
14371
+ if (isNumber(sourcePadding))
14372
+ sourcePadding = vec2(sourcePadding);
14373
+
14367
14374
  // copy frames in order, reading the source left to right, top to bottom
14368
14375
  // the destination wraps at tileInfo.columns which may be narrower than the source
14369
14376
  const frameSize = tileInfo.size;
14370
- const sourceCellWidth = frameSize.x + sourcePadding*2;
14371
- const sourceCellHeight = frameSize.y + sourcePadding*2;
14377
+ const sourceCellWidth = frameSize.x + sourcePadding.x*2;
14378
+ const sourceCellHeight = frameSize.y + sourcePadding.y*2;
14372
14379
  const sourceColumns = image.width / sourceCellWidth;
14373
14380
  const frameCount = sourceColumns * (image.height / sourceCellHeight);
14374
14381
  const columns = tileInfo.columns || frameCount;
@@ -14376,8 +14383,8 @@ class TextureSheet
14376
14383
  const cellHeight = frameSize.y + tileInfo.padding*2;
14377
14384
  for (let i = frameCount; i--;)
14378
14385
  {
14379
- const sourceX = (i % sourceColumns) * sourceCellWidth + sourcePadding;
14380
- const sourceY = (i / sourceColumns | 0) * sourceCellHeight + sourcePadding;
14386
+ const sourceX = (i % sourceColumns) * sourceCellWidth + sourcePadding.x;
14387
+ const sourceY = (i / sourceColumns | 0) * sourceCellHeight + sourcePadding.y;
14381
14388
  this.context.drawImage(image,
14382
14389
  sourceX, sourceY, frameSize.x, frameSize.y,
14383
14390
  tileInfo.pos.x + (i % columns) * cellWidth,
@@ -14411,7 +14418,7 @@ class TextureSheet
14411
14418
  * @param {string} src - Image source path
14412
14419
  * @param {Vector2|number} [frameSize] - Size of each animation frame in pixels
14413
14420
  * @param {number} [padding] - How many pixels padding around each frame
14414
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
14421
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
14415
14422
  * @return {TileInfo}
14416
14423
  * @example
14417
14424
  * const playerTile = loadSprite('player.png'); // a single sprite
@@ -14422,6 +14429,7 @@ function loadSprite(src, frameSize, padding=textureSheetPadding, sourcePadding=0
14422
14429
  ASSERT(isStringLike(src), 'image src must be a string');
14423
14430
  ASSERT(!frameSize || isVector2(frameSize) || isNumber(frameSize), 'frameSize must be a vec2 or number');
14424
14431
  ASSERT(isNumber(padding), 'padding must be a number');
14432
+ ASSERT(isNumber(sourcePadding) || isVector2(sourcePadding), 'sourcePadding must be a number or vec2');
14425
14433
 
14426
14434
  if (isNumber(frameSize))
14427
14435
  frameSize = vec2(frameSize);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "littlejsengine",
3
- "version": "1.18.23",
3
+ "version": "1.18.24",
4
4
  "description": "LittleJS - Tiny and Fast HTML5 Game Engine",
5
5
  "main": "dist/littlejs.esm.js",
6
6
  "types": "dist/littlejs.d.ts",
@@ -73,17 +73,21 @@ class TextureSheet
73
73
  * @param {Vector2} imageSize - Size of the source image in pixels
74
74
  * @param {Vector2} [frameSize] - Size of each frame, or the whole image if not passed
75
75
  * @param {number} [padding] - How many pixels padding around each frame
76
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
76
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
77
77
  * @return {TileInfo} Tile for the packed image, or undefined if the sheet is full */
78
78
  tryAdd(imageSize, frameSize=imageSize, padding=textureSheetPadding, sourcePadding=0)
79
79
  {
80
80
  ASSERT(isVector2(imageSize) && isVector2(frameSize), 'sizes must be vec2');
81
81
  ASSERT(frameSize.x > 0 && frameSize.y > 0, 'frame size must be positive');
82
- ASSERT(isNumber(sourcePadding) && sourcePadding >= 0, 'sourcePadding must be a number >= 0');
82
+
83
+ if (isNumber(sourcePadding))
84
+ sourcePadding = vec2(sourcePadding);
85
+ ASSERT(isVector2(sourcePadding) && sourcePadding.x >= 0 && sourcePadding.y >= 0,
86
+ 'sourcePadding must be a number or vec2 >= 0');
83
87
 
84
88
  // the source may have its own padding baked in around each frame
85
- const sourceCellWidth = frameSize.x + sourcePadding*2;
86
- const sourceCellHeight = frameSize.y + sourcePadding*2;
89
+ const sourceCellWidth = frameSize.x + sourcePadding.x*2;
90
+ const sourceCellHeight = frameSize.y + sourcePadding.y*2;
87
91
  ASSERT(imageSize.x % sourceCellWidth === 0 && imageSize.y % sourceCellHeight === 0,
88
92
  'image size must be a multiple of the padded frame size');
89
93
 
@@ -125,16 +129,19 @@ class TextureSheet
125
129
  * @param {HTMLImageElement} image - Source image to copy from
126
130
  * @param {TileInfo} tileInfo - Where to put it, from tryAdd
127
131
  * @param {boolean} [update] - Upload to webgl now, pass false when batching
128
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image */
132
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image */
129
133
  drawImage(image, tileInfo, update=true, sourcePadding=0)
130
134
  {
131
135
  ASSERT(!!this.context, 'texture sheet has no canvas');
132
136
 
137
+ if (isNumber(sourcePadding))
138
+ sourcePadding = vec2(sourcePadding);
139
+
133
140
  // copy frames in order, reading the source left to right, top to bottom
134
141
  // the destination wraps at tileInfo.columns which may be narrower than the source
135
142
  const frameSize = tileInfo.size;
136
- const sourceCellWidth = frameSize.x + sourcePadding*2;
137
- const sourceCellHeight = frameSize.y + sourcePadding*2;
143
+ const sourceCellWidth = frameSize.x + sourcePadding.x*2;
144
+ const sourceCellHeight = frameSize.y + sourcePadding.y*2;
138
145
  const sourceColumns = image.width / sourceCellWidth;
139
146
  const frameCount = sourceColumns * (image.height / sourceCellHeight);
140
147
  const columns = tileInfo.columns || frameCount;
@@ -142,8 +149,8 @@ class TextureSheet
142
149
  const cellHeight = frameSize.y + tileInfo.padding*2;
143
150
  for (let i = frameCount; i--;)
144
151
  {
145
- const sourceX = (i % sourceColumns) * sourceCellWidth + sourcePadding;
146
- const sourceY = (i / sourceColumns | 0) * sourceCellHeight + sourcePadding;
152
+ const sourceX = (i % sourceColumns) * sourceCellWidth + sourcePadding.x;
153
+ const sourceY = (i / sourceColumns | 0) * sourceCellHeight + sourcePadding.y;
147
154
  this.context.drawImage(image,
148
155
  sourceX, sourceY, frameSize.x, frameSize.y,
149
156
  tileInfo.pos.x + (i % columns) * cellWidth,
@@ -177,7 +184,7 @@ class TextureSheet
177
184
  * @param {string} src - Image source path
178
185
  * @param {Vector2|number} [frameSize] - Size of each animation frame in pixels
179
186
  * @param {number} [padding] - How many pixels padding around each frame
180
- * @param {number} [sourcePadding] - How many pixels padding around each frame in the source image
187
+ * @param {number|Vector2} [sourcePadding] - How many pixels padding around each frame in the source image
181
188
  * @return {TileInfo}
182
189
  * @example
183
190
  * const playerTile = loadSprite('player.png'); // a single sprite
@@ -188,6 +195,7 @@ function loadSprite(src, frameSize, padding=textureSheetPadding, sourcePadding=0
188
195
  ASSERT(isStringLike(src), 'image src must be a string');
189
196
  ASSERT(!frameSize || isVector2(frameSize) || isNumber(frameSize), 'frameSize must be a vec2 or number');
190
197
  ASSERT(isNumber(padding), 'padding must be a number');
198
+ ASSERT(isNumber(sourcePadding) || isVector2(sourcePadding), 'sourcePadding must be a number or vec2');
191
199
 
192
200
  if (isNumber(frameSize))
193
201
  frameSize = vec2(frameSize);
package/src/engine.js CHANGED
@@ -32,7 +32,7 @@ const engineName = 'LittleJS';
32
32
  * @type {string}
33
33
  * @default
34
34
  * @memberof Engine */
35
- const engineVersion = '1.18.23';
35
+ const engineVersion = '1.18.24';
36
36
 
37
37
  /** Frames per second to update
38
38
  * @type {number}