littlejsengine 1.18.28 → 1.19.3

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.
@@ -230,10 +230,10 @@ class CanvasLayer extends EngineObject
230
230
  ASSERT(isVector2(canvasSize), 'canvasSize must be a Vector2');
231
231
  super(pos, size, undefined, angle, WHITE, renderOrder);
232
232
 
233
- /** @property {HTMLCanvasElement} - The canvas used by this layer */
234
- this.canvas = headlessMode ? undefined : new OffscreenCanvas(canvasSize.x, canvasSize.y);
235
233
  /** @property {OffscreenCanvasRenderingContext2D} - The 2D canvas context used by this layer */
236
- this.context = this.canvas?.getContext('2d');
234
+ this.context = headlessMode ? undefined : createCanvasContext(canvasSize.x, canvasSize.y);
235
+ /** @property {OffscreenCanvas} - The canvas used by this layer */
236
+ this.canvas = this.context?.canvas;
237
237
  /** @property {TextureInfo} - Texture info to use for this object rendering */
238
238
  this.textureInfo = new TextureInfo(this.canvas, useWebGL);
239
239
 
@@ -135,6 +135,19 @@ async function fetchJSON(url)
135
135
  function saveText(text, filename='text', type='text/plain')
136
136
  { saveDataURL(URL.createObjectURL(new Blob([text], {'type':type})), filename); }
137
137
 
138
+ /** Create an offscreen canvas to draw into, and return its 2D context
139
+ * - The canvas is context.canvas, which is what TextureInfo and the like take
140
+ * @param {number} width - In pixels
141
+ * @param {number} [height] - In pixels, defaults to the width for a square
142
+ * @param {boolean} [willReadFrequently] - Keep it in software, faster when getImageData is called on it often
143
+ * @return {OffscreenCanvasRenderingContext2D}
144
+ * @memberof Utilities */
145
+ function createCanvasContext(width, height=width, willReadFrequently=false)
146
+ {
147
+ ASSERT(isNumber(width) && isNumber(height), 'canvas width and height must be numbers', width, height);
148
+ return new OffscreenCanvas(width, height).getContext('2d', {willReadFrequently});
149
+ }
150
+
138
151
  /** Save a canvas to disk
139
152
  * @param {HTMLCanvasElement|OffscreenCanvas} canvas
140
153
  * @param {string} [filename]