@oliversalzburg/js-utils 0.0.10 → 0.0.12

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.
Files changed (90) hide show
  1. package/_site/assets/navigation.js +1 -1
  2. package/_site/assets/search.js +1 -1
  3. package/_site/classes/AbstractError.html +5 -5
  4. package/_site/classes/Canvas.html +36 -25
  5. package/_site/classes/InternalError.html +6 -6
  6. package/_site/classes/InvalidArgumentError.html +5 -5
  7. package/_site/classes/InvalidOperationError.html +5 -5
  8. package/_site/classes/NotImplementedError.html +5 -5
  9. package/_site/classes/PermissionViolationError.html +5 -5
  10. package/_site/classes/Random.html +12 -12
  11. package/_site/classes/RenderLoop.html +31 -18
  12. package/_site/classes/ResourceConflictError.html +5 -5
  13. package/_site/classes/UnexpectedNilError.html +3 -3
  14. package/_site/classes/UnknownError.html +5 -5
  15. package/_site/classes/Vector2.html +32 -29
  16. package/_site/classes/Vector3.html +8 -8
  17. package/_site/functions/addVector2.html +1 -1
  18. package/_site/functions/blend.html +1 -1
  19. package/_site/functions/blendAdditive.html +1 -1
  20. package/_site/functions/blendSubtractive.html +1 -1
  21. package/_site/functions/clamp.html +6 -0
  22. package/_site/functions/cosDeg.html +1 -1
  23. package/_site/functions/deg2rad.html +1 -1
  24. package/_site/functions/difference.html +1 -1
  25. package/_site/functions/distance.html +1 -1
  26. package/_site/functions/errorToJSON.html +1 -1
  27. package/_site/functions/errorToRecord.html +1 -1
  28. package/_site/functions/errorToSimpleSerializable.html +1 -1
  29. package/_site/functions/filterType.html +1 -1
  30. package/_site/functions/formatString.html +1 -1
  31. package/_site/functions/formatStringTemplate.html +1 -1
  32. package/_site/functions/fromRGB.html +1 -1
  33. package/_site/functions/fromRGBA.html +1 -1
  34. package/_site/functions/getA.html +1 -1
  35. package/_site/functions/getB.html +1 -1
  36. package/_site/functions/getDocumentElementTypeById.html +1 -1
  37. package/_site/functions/getG.html +1 -1
  38. package/_site/functions/getR.html +1 -1
  39. package/_site/functions/intersect.html +1 -1
  40. package/_site/functions/is.html +1 -1
  41. package/_site/functions/isError.html +1 -1
  42. package/_site/functions/isInteger.html +1 -1
  43. package/_site/functions/isNil.html +1 -1
  44. package/_site/functions/isqrt.html +1 -1
  45. package/_site/functions/multiplyVector2.html +1 -1
  46. package/_site/functions/mustExist.html +1 -1
  47. package/_site/functions/nextPalette.html +1 -1
  48. package/_site/functions/putPixel32.html +8 -1
  49. package/_site/functions/putPixel32Add.html +8 -1
  50. package/_site/functions/putPixel32Sub.html +8 -1
  51. package/_site/functions/putSubPixel32.html +1 -1
  52. package/_site/functions/rad2deg.html +1 -1
  53. package/_site/functions/randomRange.html +1 -1
  54. package/_site/functions/safeRGBComponent.html +1 -1
  55. package/_site/functions/seedFromString.html +1 -1
  56. package/_site/functions/shuffleArray.html +1 -1
  57. package/_site/functions/sinDeg.html +1 -1
  58. package/_site/functions/sleep.html +1 -1
  59. package/_site/functions/somecolor.html +1 -1
  60. package/_site/functions/subtractVector2.html +1 -1
  61. package/_site/functions/toGrayScale.html +1 -1
  62. package/_site/functions/unknownToError.html +1 -1
  63. package/_site/index.html +5 -1
  64. package/_site/interfaces/CanvasOptions.html +5 -0
  65. package/_site/interfaces/RenderLoopOptions.html +7 -0
  66. package/_site/modules.html +5 -2
  67. package/_site/types/AnyConstructor.html +1 -1
  68. package/_site/types/AnyFunction.html +1 -1
  69. package/_site/types/ConstructorOf.html +1 -1
  70. package/_site/types/FunctionReturning.html +1 -1
  71. package/_site/types/Maybe.html +1 -1
  72. package/_site/types/Mixin.html +1 -1
  73. package/_site/types/Nil.html +1 -1
  74. package/_site/types/RenderLoopCallback.html +1 -1
  75. package/_site/types/SerializedError.html +1 -1
  76. package/_site/variables/PALETTES.html +1 -1
  77. package/_site/variables/TWO_PI.html +1 -1
  78. package/_site/variables/random-1.html +1 -1
  79. package/lib/array.js +1 -1
  80. package/lib/core.d.ts +8 -0
  81. package/lib/core.js +10 -1
  82. package/lib/core.js.map +1 -1
  83. package/lib/dom/core.d.ts +1 -0
  84. package/lib/graphics/canvas.d.ts +39 -6
  85. package/lib/graphics/canvas.js +63 -31
  86. package/lib/graphics/canvas.js.map +1 -1
  87. package/lib/graphics/render-loop.d.ts +17 -3
  88. package/lib/graphics/render-loop.js +57 -17
  89. package/lib/graphics/render-loop.js.map +1 -1
  90. package/package.json +3 -3
@@ -32,10 +32,10 @@ export class Canvas {
32
32
  */
33
33
  context;
34
34
  /**
35
- * The {@linkcode !ImageData} object
36
- * that represents our frontbuffer, which is the canvas itself.
35
+ * The {@linkcode !ImageData} object in our offscreen canvas. We usually interact with its
36
+ * {@linkcode Canvas.buffer buffer} directly.
37
37
  */
38
- pixMap;
38
+ #imageData;
39
39
  /**
40
40
  * Our back buffer.
41
41
  */
@@ -56,22 +56,24 @@ export class Canvas {
56
56
  this.height = height;
57
57
  this.options = options;
58
58
  this.canvasDom = canvas;
59
- this.#canvasDomContext = mustExist(this.canvasDom.getContext("bitmaprenderer"), "Unable to create rendering context for DOM canvas.");
59
+ this.#canvasDomContext = mustExist(this.canvasDom.getContext("bitmaprenderer", {
60
+ alpha: false,
61
+ }), "Unable to create rendering context for DOM canvas.");
60
62
  this.#canvasOffscreen = new OffscreenCanvas(this.width, this.height);
61
63
  this.context = mustExist(this.#canvasOffscreen.getContext("2d", {
62
64
  alpha: false,
63
65
  desynchronized: true,
64
66
  willReadFrequently: this.options.supportReadBack,
65
67
  }), "Unable to create rendering context for offscreen canvas.");
66
- this.pixMap = this.context.createImageData(width, height);
67
- this.buffer = this.pixMap.data;
68
+ this.#imageData = this.context.createImageData(width, height);
69
+ this.buffer = this.#imageData.data;
68
70
  this.context.font = "12px sans-serif";
69
71
  }
70
72
  /**
71
73
  * Draws the back buffer onto the canvas.
72
74
  */
73
75
  update() {
74
- this.context.putImageData(this.pixMap, 0, 0);
76
+ this.context.putImageData(this.#imageData, 0, 0);
75
77
  }
76
78
  /**
77
79
  * Draws the offscreen canvas into the DOM canvas.
@@ -83,12 +85,12 @@ export class Canvas {
83
85
  /**
84
86
  * Draws a slightly transparent, black rectangle over the canvas,
85
87
  * and then reads the result back into the buffer.
88
+ * @param color The color to fade the buffer with. The alpha component of
89
+ * the color defines how strong the fade is. `0` is not at all, `255` makes
90
+ * it instantly black.
86
91
  */
87
- fade() {
88
- this.context.fillStyle = "rgba(0,0,0,0.1)";
89
- this.context.rect(0, 0, this.width, this.height);
90
- this.context.fill();
91
- this.bufferReadBack();
92
+ fade(color = fromRGBA(0, 0, 0, 1)) {
93
+ this.blendWith(color);
92
94
  }
93
95
  /**
94
96
  * Reads the front buffer back into the back buffer.
@@ -97,8 +99,8 @@ export class Canvas {
97
99
  if (!this.options.supportReadBack) {
98
100
  throw new InvalidOperationError("To use `bufferReadBack()`, you must construct the `Canvas` with `options.supportReadBack` set to `true`.");
99
101
  }
100
- this.pixMap = this.context.getImageData(0, 0, this.width, this.height);
101
- this.buffer = this.pixMap.data;
102
+ this.#imageData = this.context.getImageData(0, 0, this.width, this.height);
103
+ this.buffer = this.#imageData.data;
102
104
  }
103
105
  /**
104
106
  * Returns the color of a pixel in the back buffer.
@@ -107,7 +109,7 @@ export class Canvas {
107
109
  * @returns The color of the pixel at the given local.
108
110
  */
109
111
  getPixel32(x, y) {
110
- const bufferOffset = Math.trunc((x + y * this.width) * 4);
112
+ const bufferOffset = (x + y * this.width) * 4;
111
113
  return fromRGBA(this.buffer[bufferOffset + 0], this.buffer[bufferOffset + 1], this.buffer[bufferOffset + 2], this.buffer[bufferOffset + 3]);
112
114
  }
113
115
  /**
@@ -117,7 +119,7 @@ export class Canvas {
117
119
  * @param color The color to place at the coordinate.
118
120
  */
119
121
  setPixel32(x, y, color) {
120
- const bufferOffset = Math.trunc((x + y * this.width) * 4);
122
+ const bufferOffset = (x + y * this.width) * 4;
121
123
  this.buffer[bufferOffset + 0] = getR(color);
122
124
  this.buffer[bufferOffset + 1] = getG(color);
123
125
  this.buffer[bufferOffset + 2] = getB(color);
@@ -139,9 +141,34 @@ export class Canvas {
139
141
  this.buffer[bufferOffset + 3] = a;
140
142
  }
141
143
  }
144
+ /**
145
+ * Fills the entire back buffer with the given color.
146
+ * @param color The color to fill the back buffer with.
147
+ */
148
+ blendWith(color) {
149
+ const r = getR(color);
150
+ const g = getG(color);
151
+ const b = getB(color);
152
+ const a = getA(color);
153
+ for (let bufferOffset = 0; bufferOffset < this.width * this.height * 4; bufferOffset += 4) {
154
+ this.buffer[bufferOffset + 0] = (a * r + (255 - a) * this.buffer[bufferOffset + 0]) >> 8;
155
+ this.buffer[bufferOffset + 1] = (a * g + (255 - a) * this.buffer[bufferOffset + 1]) >> 8;
156
+ this.buffer[bufferOffset + 2] = (a * b + (255 - a) * this.buffer[bufferOffset + 2]) >> 8;
157
+ }
158
+ }
142
159
  }
143
160
  /**
144
161
  * Linearly blends a new pixel with an existing color value in a {@linkcode Canvas}.
162
+ *
163
+ * As this method is often called many times in render loops, only bounds
164
+ * checking is applied to the given coordinates.
165
+ *
166
+ * The coordinates are **expected to already be integers**. Supplying numbers with fractions
167
+ * has performance implications. Ensure you're working with integers, or truncate the
168
+ * coordinates before passing them in.
169
+ *
170
+ * The `alpha` value is expected to be an integer in the range from `0` to `255`.
171
+ * `0` meaning fully transparent, `255` meaning fully opaque.
145
172
  * @param canvas The {@linkcode Canvas} to interact with.
146
173
  * @param x The X coordinate at which to place the pixel.
147
174
  * @param y The Y coordinate at which to place the pixel.
@@ -153,17 +180,22 @@ export const putPixel32 = (canvas, x, y, color, alpha) => {
153
180
  if (canvas.width < x || x < 0 || canvas.height < y || y < 0) {
154
181
  return;
155
182
  }
156
- x = Math.trunc(x);
157
- y = Math.trunc(y);
158
- if (alpha > 255) {
159
- alpha = 255;
160
- }
161
183
  const srcColor = canvas.getPixel32(x, y);
162
184
  const newColor = blend(srcColor, color, alpha);
163
185
  canvas.setPixel32(x, y, newColor);
164
186
  };
165
187
  /**
166
188
  * Additively blends a new pixel with an existing color value in a {@linkcode Canvas}.
189
+ *
190
+ * As this method is often called many times in render loops, only bounds
191
+ * checking is applied to the given coordinates.
192
+ *
193
+ * The coordinates are **expected to already be integers**. Supplying numbers with fractions
194
+ * has performance implications. Ensure you're working with integers, or truncate the
195
+ * coordinates before passing them in.
196
+ *
197
+ * The `alpha` value is expected to be an integer in the range from `0` to `255`.
198
+ * `0` meaning fully transparent, `255` meaning fully opaque.
167
199
  * @param canvas The {@linkcode Canvas} to interact with.
168
200
  * @param x The X coordinate at which to place the pixel.
169
201
  * @param y The Y coordinate at which to place the pixel.
@@ -175,17 +207,22 @@ export const putPixel32Add = (canvas, x, y, color, alpha) => {
175
207
  if (canvas.width < x || x < 0 || canvas.height < y || y < 0) {
176
208
  return;
177
209
  }
178
- x = Math.round(x);
179
- y = Math.round(y);
180
- if (alpha > 255) {
181
- alpha = 255;
182
- }
183
210
  const srcColor = canvas.getPixel32(x, y);
184
211
  const newColor = blendAdditive(srcColor, color, alpha);
185
212
  canvas.setPixel32(x, y, newColor);
186
213
  };
187
214
  /**
188
215
  * Subtractively blends a new pixel with an existing color value in a {@linkcode Canvas}.
216
+ *
217
+ * As this method is often called many times in render loops, only bounds
218
+ * checking is applied to the given coordinates.
219
+ *
220
+ * The coordinates are **expected to already be integers**. Supplying numbers with fractions
221
+ * has performance implications. Ensure you're working with integers, or truncate the
222
+ * coordinates before passing them in.
223
+ *
224
+ * The `alpha` value is expected to be an integer in the range from `0` to `255`.
225
+ * `0` meaning fully transparent, `255` meaning fully opaque.
189
226
  * @param canvas The {@linkcode Canvas} to interact with.
190
227
  * @param x The X coordinate at which to place the pixel.
191
228
  * @param y The Y coordinate at which to place the pixel.
@@ -197,11 +234,6 @@ export const putPixel32Sub = (canvas, x, y, color, alpha) => {
197
234
  if (canvas.width < x || x < 0 || canvas.height < y || y < 0) {
198
235
  return;
199
236
  }
200
- x = Math.round(x);
201
- y = Math.round(y);
202
- if (alpha > 255) {
203
- alpha = 255;
204
- }
205
237
  const srcColor = canvas.getPixel32(x, y);
206
238
  const newColor = blendSubtractive(srcColor, color, alpha);
207
239
  canvas.setPixel32(x, y, newColor);
@@ -1 +1 @@
1
- {"version":3,"file":"canvas.js","sourceRoot":"","sources":["../../source/graphics/canvas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,KAAK,EACL,aAAa,EACb,gBAAgB,EAChB,QAAQ,EACR,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,GACL,MAAM,WAAW,CAAC;AAcnB;;;;GAIG;AACH,MAAM,OAAO,MAAM;IACjB;;OAEG;IACM,SAAS,CAAoB;IAEtC;;OAEG;IACH,iBAAiB,CAA8B;IAE/C;;OAEG;IACH,gBAAgB,CAAkB;IAElC;;OAEG;IACM,KAAK,CAAS;IAEvB;;OAEG;IACM,MAAM,CAAS;IAExB;;OAEG;IACM,OAAO,CAAoC;IAEpD;;;OAGG;IACH,MAAM,CAAY;IAElB;;OAEG;IACH,MAAM,CAAoB;IAE1B;;OAEG;IACM,OAAO,CAAmC;IAEnD;;;;;;OAMG;IACH,YACE,MAAyB,EACzB,KAAa,EACb,MAAc,EACd,UAAkC,EAAE;QAEpC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC;QACxB,IAAI,CAAC,iBAAiB,GAAG,SAAS,CAChC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,gBAAgB,CAAC,EAC3C,oDAAoD,CACrD,CAAC;QAEF,IAAI,CAAC,gBAAgB,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QACrE,IAAI,CAAC,OAAO,GAAG,SAAS,CACtB,IAAI,CAAC,gBAAgB,CAAC,UAAU,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,KAAK,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,QAAQ,GAAG,CAAC,GAAG,OAAO,CAAC;IAC7B,MAAM,QAAQ,GAAG,CAAC,GAAG,OAAO,CAAC;IAE7B,MAAM,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,KAAK,CAAC;IAC3C,MAAM,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,KAAK,CAAC;IAC1C,MAAM,MAAM,GAAG,QAAQ,GAAG,OAAO,GAAG,KAAK,CAAC;IAC1C,MAAM,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,KAAK,CAAC;IAEzC,UAAU,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IAChD,UAAU,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IAChD,UAAU,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IAChD,UAAU,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AAClD,CAAC,CAAC"}
@@ -17,7 +17,7 @@ export interface RenderLoopOptions {
17
17
  /**
18
18
  * Should the buffer be cleared or faded to black before each frame?
19
19
  */
20
- onRefresh: "clear" | "fade";
20
+ onRefresh: "clear";
21
21
  }
22
22
  /**
23
23
  * Conveniently provides a way to have a render loop called at
@@ -39,9 +39,13 @@ export declare class RenderLoop {
39
39
  */
40
40
  readonly options: Readonly<Partial<RenderLoopOptions>>;
41
41
  /**
42
- * The timestamp we got for the previous frame.
42
+ * The time at which we drew the last frame to the offscreen buffer.
43
43
  */
44
- private previousTimestamp;
44
+ private previousTimestampDraw;
45
+ /**
46
+ * The time at which we rendered the last frame to the DOM.
47
+ */
48
+ private previousTimestampRender;
45
49
  /**
46
50
  * Our `#main` function bound to this classes
47
51
  * for easier invokation through external callers.
@@ -51,6 +55,16 @@ export declare class RenderLoop {
51
55
  * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame} request.
52
56
  */
53
57
  private frameRequestId;
58
+ /**
59
+ * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/setTimeout setTimeout}-based
60
+ * offscreen rendering loop.
61
+ */
62
+ private drawTimeout;
63
+ /**
64
+ * Retrieves the amount of frames drawn to the DOM throughout the loops lifetime.
65
+ * @returns The frame count.
66
+ */
67
+ get frameCounter(): number;
54
68
  /**
55
69
  * Constructs a new {@linkcode RenderLoop}.
56
70
  * @param renderLoop The function to call when a new frame should be drawn.
@@ -17,9 +17,13 @@ export class RenderLoop {
17
17
  */
18
18
  options;
19
19
  /**
20
- * The timestamp we got for the previous frame.
20
+ * The time at which we drew the last frame to the offscreen buffer.
21
21
  */
22
- previousTimestamp;
22
+ previousTimestampDraw;
23
+ /**
24
+ * The time at which we rendered the last frame to the DOM.
25
+ */
26
+ previousTimestampRender;
23
27
  /**
24
28
  * Our `#main` function bound to this classes
25
29
  * for easier invokation through external callers.
@@ -29,6 +33,22 @@ export class RenderLoop {
29
33
  * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame} request.
30
34
  */
31
35
  frameRequestId = null;
36
+ /**
37
+ * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/setTimeout setTimeout}-based
38
+ * offscreen rendering loop.
39
+ */
40
+ drawTimeout = null;
41
+ /**
42
+ * How many frames we've drawn to the DOM throughout the loops lifetime.
43
+ */
44
+ #frameCounter = 0;
45
+ /**
46
+ * Retrieves the amount of frames drawn to the DOM throughout the loops lifetime.
47
+ * @returns The frame count.
48
+ */
49
+ get frameCounter() {
50
+ return this.#frameCounter;
51
+ }
32
52
  /**
33
53
  * Constructs a new {@linkcode RenderLoop}.
34
54
  * @param renderLoop The function to call when a new frame should be drawn.
@@ -39,46 +59,66 @@ export class RenderLoop {
39
59
  this.canvas = canvas;
40
60
  this.options = options;
41
61
  this.renderLoop = renderLoop;
42
- this.previousTimestamp = 0;
62
+ this.previousTimestampDraw = 0;
63
+ this.previousTimestampRender = 0;
43
64
  this.boundMain = this.#main.bind(this);
44
65
  }
45
66
  /**
46
67
  * Stop the render loop.
47
68
  */
48
69
  block() {
49
- if (this.frameRequestId === null) {
50
- return;
70
+ if (this.frameRequestId !== null) {
71
+ window.cancelAnimationFrame(this.frameRequestId);
72
+ this.frameRequestId = null;
73
+ }
74
+ if (this.drawTimeout !== null) {
75
+ window.clearTimeout(this.drawTimeout);
76
+ this.drawTimeout = null;
51
77
  }
52
- window.cancelAnimationFrame(this.frameRequestId);
53
- this.frameRequestId = null;
54
78
  }
55
79
  /**
56
80
  * Start the render loop.
57
81
  */
58
82
  unblock() {
59
83
  this.frameRequestId = window.requestAnimationFrame(this.boundMain);
84
+ if (!this.drawTimeout) {
85
+ this.drawTimeout = setTimeout(() => {
86
+ this.#drawFrame();
87
+ });
88
+ }
60
89
  }
61
90
  /**
62
- * Our main loop.
91
+ * Our main loop, as invoked through the frame request.
63
92
  * @param timestamp The current timestamp.
64
93
  */
65
94
  #main(timestamp) {
66
- const timeDelta = timestamp - this.previousTimestamp;
67
- this.#drawFrame(timestamp, timeDelta);
95
+ const timeDelta = timestamp - this.previousTimestampRender;
96
+ this.#renderFrame(timeDelta);
68
97
  this.unblock();
69
- this.previousTimestamp = timestamp;
98
+ this.previousTimestampRender = timestamp;
99
+ ++this.#frameCounter;
100
+ }
101
+ /**
102
+ * Draws a new frame to the offscreen buffer.
103
+ */
104
+ #drawFrame() {
105
+ if (this.frameRequestId === null) {
106
+ return;
107
+ }
108
+ const timestamp = new Date().getTime();
109
+ const timeDelta = timestamp - this.previousTimestampDraw;
110
+ this.renderLoop(timeDelta, timestamp);
111
+ this.previousTimestampDraw = timestamp;
112
+ this.drawTimeout = setTimeout(() => {
113
+ this.#drawFrame();
114
+ });
70
115
  }
71
116
  /**
72
117
  * Draw a new frame.
73
- * @param timestamp The current timestamp.
74
118
  * @param delta The delta to the timestamp of the previous frame.
75
119
  */
76
- #drawFrame(timestamp, delta) {
77
- this.renderLoop(delta, timestamp);
120
+ #renderFrame(delta) {
78
121
  this.canvas.update();
79
- if (this.options.onRefresh === "fade") {
80
- this.canvas.fade();
81
- }
82
122
  if (this.options.onRefresh === "clear") {
83
123
  this.canvas.clearWith(0);
84
124
  }
@@ -1 +1 @@
1
- {"version":3,"file":"render-loop.js","sourceRoot":"","sources":["../../source/graphics/render-loop.ts"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH,MAAM,OAAO,UAAU;IACrB;;OAEG;IACM,MAAM,CAAS;IAExB;;OAEG;IACM,UAAU,CAAqB;IAExC;;OAEG;IACM,OAAO,CAAuC;IAEvD;;OAEG;IACK,iBAAiB,CAAS;IAElC;;;OAGG;IACc,SAAS,CAA8B;IAExD;;OAEG;IACK,cAAc,GAAkB,IAAI,CAAC;IAE7C;;;;;OAKG;IACH,YACE,UAA8B,EAC9B,MAAc,EACd,UAAsC,EAAE;QAExC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,iBAAiB,GAAG,CAAC,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,IAAI,CAAC,cAAc,KAAK,IAAI,EAAE;YAChC,OAAO;SACR;QAED,MAAM,CAAC,oBAAoB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QACjD,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;IAC7B,CAAC;IAED;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AAC,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;SAC1C;QAED,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF"}
1
+ {"version":3,"file":"render-loop.js","sourceRoot":"","sources":["../../source/graphics/render-loop.ts"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH,MAAM,OAAO,UAAU;IACrB;;OAEG;IACM,MAAM,CAAS;IAExB;;OAEG;IACM,UAAU,CAAqB;IAExC;;OAEG;IACM,OAAO,CAAuC;IAEvD;;OAEG;IACK,qBAAqB,CAAS;IAEtC;;OAEG;IACK,uBAAuB,CAAS;IAExC;;;OAGG;IACc,SAAS,CAA8B;IAExD;;OAEG;IACK,cAAc,GAAkB,IAAI,CAAC;IAE7C;;;OAGG;IACK,WAAW,GAAyC,IAAI,CAAC;IAEjE;;OAEG;IACH,aAAa,GAAG,CAAC,CAAC;IAElB;;;OAGG;IACH,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;IAED;;;;;OAKG;IACH,YACE,UAA8B,EAC9B,MAAc,EACd,UAAsC,EAAE;QAExC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,qBAAqB,GAAG,CAAC,CAAC;QAC/B,IAAI,CAAC,uBAAuB,GAAG,CAAC,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,IAAI,CAAC,cAAc,KAAK,IAAI,EAAE;YAChC,MAAM,CAAC,oBAAoB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;YACjD,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;SAC5B;QAED,IAA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C,MAAM,EAAE,CAAC;QAErB,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,KAAK,OAAO,EAAE;YACtC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;SAC1B;QAED,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;YACxB,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC;YAC7C,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,GAAG,6BAA6B,CAAC;YAChE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,GAAG,CAAC,CAAC;YAClC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;YAC3C,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;YAC1C,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;SAC1C;QAED,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@oliversalzburg/js-utils",
4
- "version": "0.0.10",
4
+ "version": "0.0.12",
5
5
  "license": "MIT",
6
6
  "author": "Oliver Salzburg <oliver.salzburg@gmail.com>",
7
7
  "type": "module",
@@ -45,12 +45,12 @@
45
45
  "chai-as-promised": "7.1.1",
46
46
  "esbuild": "0.19.5",
47
47
  "eslint": "8.53.0",
48
- "eslint-plugin-jsdoc": "46.8.2",
48
+ "eslint-plugin-jsdoc": "46.9.0",
49
49
  "lint-staged": "15.0.2",
50
50
  "mocha": "10.2.0",
51
51
  "prettier": "3.0.3",
52
52
  "prettier-package-json": "2.8.0",
53
- "prettier-plugin-organize-imports": "3.2.3",
53
+ "prettier-plugin-organize-imports": "3.2.4",
54
54
  "typedoc": "0.25.3",
55
55
  "typedoc-plugin-mdn-links": "3.1.0",
56
56
  "typescript": "5.2.2"