@oliversalzburg/js-utils 0.7.0 → 0.7.1

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 (173) hide show
  1. package/lib/async/async.d.ts +44 -0
  2. package/lib/async/async.js +76 -0
  3. package/lib/async/async.js.map +1 -0
  4. package/lib/async/async.test.d.ts +1 -0
  5. package/lib/async/async.test.js +26 -0
  6. package/lib/async/async.test.js.map +1 -0
  7. package/lib/core.d.ts +43 -0
  8. package/lib/core.js +2 -0
  9. package/lib/core.js.map +1 -0
  10. package/lib/data/array.d.ts +55 -0
  11. package/lib/data/array.js +90 -0
  12. package/lib/data/array.js.map +1 -0
  13. package/lib/data/nil.d.ts +92 -0
  14. package/lib/data/nil.js +131 -0
  15. package/lib/data/nil.js.map +1 -0
  16. package/lib/data/random.d.ts +111 -0
  17. package/lib/data/random.js +381 -0
  18. package/lib/data/random.js.map +1 -0
  19. package/lib/data/string.d.ts +30 -0
  20. package/lib/data/string.js +53 -0
  21. package/lib/data/string.js.map +1 -0
  22. package/lib/data/tree.d.ts +28 -0
  23. package/lib/data/tree.js +45 -0
  24. package/lib/data/tree.js.map +1 -0
  25. package/lib/device/shake.d.ts +79 -0
  26. package/lib/device/shake.js +135 -0
  27. package/lib/device/shake.js.map +1 -0
  28. package/lib/dom/core.d.ts +21 -0
  29. package/lib/dom/core.js +41 -0
  30. package/lib/dom/core.js.map +1 -0
  31. package/lib/errors/AbstractError.d.ts +29 -0
  32. package/lib/errors/AbstractError.js +59 -0
  33. package/lib/errors/AbstractError.js.map +1 -0
  34. package/lib/errors/InternalError.d.ts +19 -0
  35. package/lib/errors/InternalError.js +38 -0
  36. package/lib/errors/InternalError.js.map +1 -0
  37. package/lib/errors/InvalidArgumentError.d.ts +13 -0
  38. package/lib/errors/InvalidArgumentError.js +22 -0
  39. package/lib/errors/InvalidArgumentError.js.map +1 -0
  40. package/lib/errors/InvalidOperationError.d.ts +13 -0
  41. package/lib/errors/InvalidOperationError.js +22 -0
  42. package/lib/errors/InvalidOperationError.js.map +1 -0
  43. package/lib/errors/NotImplementedError.d.ts +12 -0
  44. package/lib/errors/NotImplementedError.js +21 -0
  45. package/lib/errors/NotImplementedError.js.map +1 -0
  46. package/lib/errors/PermissionViolationError.d.ts +13 -0
  47. package/lib/errors/PermissionViolationError.js +22 -0
  48. package/lib/errors/PermissionViolationError.js.map +1 -0
  49. package/lib/errors/ResourceConflictError.d.ts +12 -0
  50. package/lib/errors/ResourceConflictError.js +21 -0
  51. package/lib/errors/ResourceConflictError.js.map +1 -0
  52. package/lib/errors/UnknownError.d.ts +16 -0
  53. package/lib/errors/UnknownError.js +25 -0
  54. package/lib/errors/UnknownError.js.map +1 -0
  55. package/lib/errors/console.d.ts +7 -0
  56. package/lib/errors/console.js +13 -0
  57. package/lib/errors/console.js.map +1 -0
  58. package/lib/errors/error-serializer.d.ts +45 -0
  59. package/lib/errors/error-serializer.js +74 -0
  60. package/lib/errors/error-serializer.js.map +1 -0
  61. package/lib/errors/error-serializer.test.d.ts +1 -0
  62. package/lib/errors/error-serializer.test.js +109 -0
  63. package/lib/errors/error-serializer.test.js.map +1 -0
  64. package/lib/errors/stream.d.ts +7 -0
  65. package/lib/errors/stream.js +15 -0
  66. package/lib/errors/stream.js.map +1 -0
  67. package/lib/errors/tools.d.ts +10 -0
  68. package/lib/errors/tools.js +13 -0
  69. package/lib/errors/tools.js.map +1 -0
  70. package/lib/events/event.d.ts +9 -0
  71. package/lib/events/event.js +10 -0
  72. package/lib/events/event.js.map +1 -0
  73. package/lib/format/bytes.d.ts +118 -0
  74. package/lib/format/bytes.js +138 -0
  75. package/lib/format/bytes.js.map +1 -0
  76. package/lib/format/bytes.test.d.ts +1 -0
  77. package/lib/format/bytes.test.js +146 -0
  78. package/lib/format/bytes.test.js.map +1 -0
  79. package/lib/format/count.d.ts +14 -0
  80. package/lib/format/count.js +51 -0
  81. package/lib/format/count.js.map +1 -0
  82. package/lib/format/count.test.d.ts +1 -0
  83. package/lib/format/count.test.js +42 -0
  84. package/lib/format/count.test.js.map +1 -0
  85. package/lib/format/milliseconds.d.ts +100 -0
  86. package/lib/format/milliseconds.js +199 -0
  87. package/lib/format/milliseconds.js.map +1 -0
  88. package/lib/format/milliseconds.test.d.ts +1 -0
  89. package/lib/format/milliseconds.test.js +393 -0
  90. package/lib/format/milliseconds.test.js.map +1 -0
  91. package/lib/format/string.d.ts +24 -0
  92. package/lib/format/string.js +32 -0
  93. package/lib/format/string.js.map +1 -0
  94. package/lib/format/string.test.d.ts +1 -0
  95. package/lib/format/string.test.js +17 -0
  96. package/lib/format/string.test.js.map +1 -0
  97. package/lib/graphics/canvas-sandbox-mp.d.ts +258 -0
  98. package/lib/graphics/canvas-sandbox-mp.js +280 -0
  99. package/lib/graphics/canvas-sandbox-mp.js.map +1 -0
  100. package/lib/graphics/canvas-sandbox.d.ts +255 -0
  101. package/lib/graphics/canvas-sandbox.js +358 -0
  102. package/lib/graphics/canvas-sandbox.js.map +1 -0
  103. package/lib/graphics/canvas.d.ts +57 -0
  104. package/lib/graphics/canvas.js +37 -0
  105. package/lib/graphics/canvas.js.map +1 -0
  106. package/lib/graphics/canvas2d-headless.d.ts +85 -0
  107. package/lib/graphics/canvas2d-headless.js +197 -0
  108. package/lib/graphics/canvas2d-headless.js.map +1 -0
  109. package/lib/graphics/canvas2d.d.ts +162 -0
  110. package/lib/graphics/canvas2d.js +332 -0
  111. package/lib/graphics/canvas2d.js.map +1 -0
  112. package/lib/graphics/canvas3d.d.ts +41 -0
  113. package/lib/graphics/canvas3d.js +67 -0
  114. package/lib/graphics/canvas3d.js.map +1 -0
  115. package/lib/graphics/color.d.ts +22 -0
  116. package/lib/graphics/color.js +38 -0
  117. package/lib/graphics/color.js.map +1 -0
  118. package/lib/graphics/core.d.ts +89 -0
  119. package/lib/graphics/core.js +146 -0
  120. package/lib/graphics/core.js.map +1 -0
  121. package/lib/graphics/palette-sampler.d.ts +7 -0
  122. package/lib/graphics/palette-sampler.js +21 -0
  123. package/lib/graphics/palette-sampler.js.map +1 -0
  124. package/lib/graphics/palette.d.ts +120 -0
  125. package/lib/graphics/palette.js +574 -0
  126. package/lib/graphics/palette.js.map +1 -0
  127. package/lib/graphics/palette.test.d.ts +1 -0
  128. package/lib/graphics/palette.test.js +43 -0
  129. package/lib/graphics/palette.test.js.map +1 -0
  130. package/lib/graphics/render-loop.d.ts +76 -0
  131. package/lib/graphics/render-loop.js +114 -0
  132. package/lib/graphics/render-loop.js.map +1 -0
  133. package/lib/graphics/shader.d.ts +56 -0
  134. package/lib/graphics/shader.js +80 -0
  135. package/lib/graphics/shader.js.map +1 -0
  136. package/lib/log/log.d.ts +11 -0
  137. package/lib/log/log.js +2 -0
  138. package/lib/log/log.js.map +1 -0
  139. package/lib/log/report.d.ts +57 -0
  140. package/lib/log/report.js +88 -0
  141. package/lib/log/report.js.map +1 -0
  142. package/lib/math/core.d.ts +85 -0
  143. package/lib/math/core.js +125 -0
  144. package/lib/math/core.js.map +1 -0
  145. package/lib/math/easing.d.ts +204 -0
  146. package/lib/math/easing.js +366 -0
  147. package/lib/math/easing.js.map +1 -0
  148. package/lib/math/euler.d.ts +18 -0
  149. package/lib/math/euler.js +22 -0
  150. package/lib/math/euler.js.map +1 -0
  151. package/lib/math/matrix3.d.ts +227 -0
  152. package/lib/math/matrix3.js +508 -0
  153. package/lib/math/matrix3.js.map +1 -0
  154. package/lib/math/quaternion.d.ts +119 -0
  155. package/lib/math/quaternion.js +270 -0
  156. package/lib/math/quaternion.js.map +1 -0
  157. package/lib/math/transformation.d.ts +26 -0
  158. package/lib/math/transformation.js +31 -0
  159. package/lib/math/transformation.js.map +1 -0
  160. package/lib/math/vector2.d.ts +202 -0
  161. package/lib/math/vector2.js +310 -0
  162. package/lib/math/vector2.js.map +1 -0
  163. package/lib/math/vector3.d.ts +293 -0
  164. package/lib/math/vector3.js +492 -0
  165. package/lib/math/vector3.js.map +1 -0
  166. package/lib/math/vector4.d.ts +182 -0
  167. package/lib/math/vector4.js +302 -0
  168. package/lib/math/vector4.js.map +1 -0
  169. package/lib/measurement/performance.d.ts +13 -0
  170. package/lib/measurement/performance.js +19 -0
  171. package/lib/measurement/performance.js.map +1 -0
  172. package/lib/tsconfig.tsbuildinfo +1 -0
  173. package/package.json +3 -5
@@ -0,0 +1,43 @@
1
+ import assert from "node:assert";
2
+ import { it } from "node:test";
3
+ import { hslPalette, hsvPalette } from "./palette.js";
4
+ const render = (_) => _.map((x) => x.toString(16).padStart(2, "0")).join("");
5
+ it("renders the correct HSL palettes - Size 1", () => {
6
+ assert.deepStrictEqual(hslPalette(1).map(render), ["ff0000"], "Default case");
7
+ // Under-saturated
8
+ assert.deepStrictEqual(hslPalette(1, 0, 0.25, 0.5).map(render), ["9f5f5f"], "Under-saturated 0.25");
9
+ assert.deepStrictEqual(hslPalette(1, 0, 0.5, 0.5).map(render), ["bf3f3f"], "Under-saturated 0.5");
10
+ assert.deepStrictEqual(hslPalette(1, 0, 0.75, 0.5).map(render), ["df1f1f"], "Under-saturated 0.75");
11
+ // Darker
12
+ assert.deepStrictEqual(hslPalette(1, 0, 0.25, 0.25).map(render), ["4f2f2f"], "Darker 0.25 0.25");
13
+ assert.deepStrictEqual(hslPalette(1, 0, 0.5, 0.25).map(render), ["5f1f1f"], "Darker 0.5 0.25");
14
+ assert.deepStrictEqual(hslPalette(1, 0, 0.75, 0.25).map(render), ["6f0f0f"], "Darker 0.75 0.25");
15
+ assert.deepStrictEqual(hslPalette(1, 0, 1, 0.25).map(render), ["7f0000"], "Darker 1 0.25");
16
+ // Lighter
17
+ assert.deepStrictEqual(hslPalette(1, 0, 0.25, 0.75).map(render), ["cfafaf"], "Lighter 0.25 0.75");
18
+ assert.deepStrictEqual(hslPalette(1, 0, 0.5, 0.75).map(render), ["df9f9f"], "Lighter 0.5 0.75");
19
+ assert.deepStrictEqual(hslPalette(1, 0, 0.75, 0.75).map(render), ["ef8f8f"], "Lighter 0.75 0.75");
20
+ assert.deepStrictEqual(hslPalette(1, 0, 1, 0.75).map(render), ["ff7f7f"], "Lighter 1 0.75");
21
+ });
22
+ it("renders the correct HSL palette - 2", () => {
23
+ assert.deepStrictEqual(hslPalette(2).map((_) => _.map((x) => x.toString(16).padStart(2, "0")).join("")), ["ff0000", "00ffff"]);
24
+ });
25
+ it("renders the correct HSL palette - 3", () => {
26
+ assert.deepStrictEqual(hslPalette(3).map((_) => _.map((x) => x.toString(16).padStart(2, "0")).join("")), ["ff0000", "00ff00", "0000ff"]);
27
+ });
28
+ it("renders the correct HSL palette - 4", () => {
29
+ assert.deepStrictEqual(hslPalette(4).map((_) => _.map((x) => x.toString(16).padStart(2, "0")).join("")), ["ff0000", "7fff00", "00ffff", "7f00ff"]);
30
+ });
31
+ it("renders the correct HSL palette - 5", () => {
32
+ assert.deepStrictEqual(hslPalette(5).map((_) => _.map((x) => x.toString(16).padStart(2, "0")).join("")), ["ff0000", "ccff00", "00ff66", "0066ff", "cb00ff"]);
33
+ });
34
+ it("renders the correct HSL palette - 6", () => {
35
+ assert.deepStrictEqual(hslPalette(6).map((_) => _.map((x) => x.toString(16).padStart(2, "0")).join("")), ["ff0000", "ffff00", "00ff00", "00ffff", "0000ff", "ff00ff"]);
36
+ });
37
+ it("renders the correct HSL palette - 7", () => {
38
+ assert.deepStrictEqual(hslPalette(7).map((_) => _.map((x) => x.toString(16).padStart(2, "0")).join("")), ["ff0000", "ffda00", "48ff00", "00ff91", "0091ff", "4800ff", "ff00da"]);
39
+ });
40
+ it("renders the correct HSV palette - 1", () => {
41
+ assert.deepStrictEqual(hsvPalette(1).map((_) => _.map((x) => x.toString(16).padStart(2, "0")).join("")), ["ff0000"]);
42
+ });
43
+ //# sourceMappingURL=palette.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"palette.test.js","sourceRoot":"","sources":["../../source/graphics/palette.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,EAAE,EAAE,MAAM,WAAW,CAA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,EAAE,QAAQ,CAAC,CACtE,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,EAAE,CAAC,qCAAqC,EAAE,GAAG,EAAE;IAC9C,MAAM,CAAC,eAAe,CACrB,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CACvB,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CACtD,EACD,CAAC,QAAQ,CAAC,CACV,CAAC;AACH,CAAC,CAAC,CAAC"}
@@ -0,0 +1,76 @@
1
+ import type { Canvas } from "./canvas.js";
2
+ /**
3
+ * The amount of milliseconds your can spend in a frame, if you want to
4
+ * reach a frame rate of 60 FPS.
5
+ */
6
+ export declare const MS_PER_FRAME_60FPS: number;
7
+ /**
8
+ * The signature of a function that is called to draw a frame.
9
+ */
10
+ export type RenderLoopCallback = (delta: number, timestamp: number) => unknown;
11
+ /**
12
+ * Configuration for a {@linkcode RenderLoop},
13
+ */
14
+ export interface RenderLoopOptions {
15
+ /**
16
+ * Should the current frames per second be rendered onto the canvas
17
+ * each frame?
18
+ */
19
+ drawFps: boolean;
20
+ /**
21
+ * Should the buffer be cleared or faded to black before each frame?
22
+ */
23
+ onRefresh: "clear";
24
+ }
25
+ /**
26
+ * Conveniently provides a way to have a render loop called at
27
+ * a constant frame rate.
28
+ */
29
+ export declare class RenderLoop {
30
+ #private;
31
+ /**
32
+ * The {@linkcode Canvas} we're rendering to.
33
+ */
34
+ readonly canvas: Canvas;
35
+ /**
36
+ * A function that we call when a new frame should be drawn.
37
+ */
38
+ readonly renderLoop: RenderLoopCallback;
39
+ /**
40
+ * The configuration that was used for this render loop.
41
+ */
42
+ readonly options: Readonly<Partial<RenderLoopOptions>>;
43
+ /**
44
+ * The time at which we drew the last frame to the offscreen buffer.
45
+ */
46
+ private previousTimestampDraw;
47
+ /**
48
+ * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame} request.
49
+ */
50
+ private frameRequestId;
51
+ /**
52
+ * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/setTimeout setTimeout}-based
53
+ * offscreen rendering loop.
54
+ */
55
+ private drawTimeout;
56
+ /**
57
+ * Retrieves the amount of frames drawn to the DOM throughout the loops lifetime.
58
+ * @returns The frame count.
59
+ */
60
+ get frameCounter(): number;
61
+ /**
62
+ * Constructs a new {@linkcode RenderLoop}.
63
+ * @param renderLoop - The function to call when a new frame should be drawn.
64
+ * @param canvas - The canvas we're rendering to.
65
+ * @param options - The configuration for thie {@linkcode RenderLoop}.
66
+ */
67
+ constructor(renderLoop: RenderLoopCallback, canvas: Canvas, options?: Partial<RenderLoopOptions>);
68
+ /**
69
+ * Stop the render loop.
70
+ */
71
+ block(): void;
72
+ /**
73
+ * Start the render loop.
74
+ */
75
+ unblock(): void;
76
+ }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The amount of milliseconds your can spend in a frame, if you want to
3
+ * reach a frame rate of 60 FPS.
4
+ */
5
+ export const MS_PER_FRAME_60FPS = /* __PURE __ */ 1000 / 60;
6
+ /**
7
+ * Conveniently provides a way to have a render loop called at
8
+ * a constant frame rate.
9
+ */
10
+ export class RenderLoop {
11
+ /**
12
+ * The {@linkcode Canvas} we're rendering to.
13
+ */
14
+ canvas;
15
+ /**
16
+ * A function that we call when a new frame should be drawn.
17
+ */
18
+ renderLoop;
19
+ /**
20
+ * The configuration that was used for this render loop.
21
+ */
22
+ options;
23
+ /**
24
+ * The time at which we drew the last frame to the offscreen buffer.
25
+ */
26
+ previousTimestampDraw;
27
+ /**
28
+ * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame} request.
29
+ */
30
+ frameRequestId = null;
31
+ /**
32
+ * The ID of our {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/setTimeout setTimeout}-based
33
+ * offscreen rendering loop.
34
+ */
35
+ drawTimeout = null;
36
+ /**
37
+ * How many frames we've drawn to the DOM throughout the loops lifetime.
38
+ */
39
+ #frameCounter = 0;
40
+ /**
41
+ * Retrieves the amount of frames drawn to the DOM throughout the loops lifetime.
42
+ * @returns The frame count.
43
+ */
44
+ get frameCounter() {
45
+ return this.#frameCounter;
46
+ }
47
+ /**
48
+ * Constructs a new {@linkcode RenderLoop}.
49
+ * @param renderLoop - The function to call when a new frame should be drawn.
50
+ * @param canvas - The canvas we're rendering to.
51
+ * @param options - The configuration for thie {@linkcode RenderLoop}.
52
+ */
53
+ constructor(renderLoop, canvas, options = {}) {
54
+ this.canvas = canvas;
55
+ this.options = options;
56
+ this.renderLoop = renderLoop;
57
+ this.previousTimestampDraw = Date.now();
58
+ }
59
+ /**
60
+ * Stop the render loop.
61
+ */
62
+ block() {
63
+ if (this.frameRequestId !== null) {
64
+ cancelAnimationFrame(this.frameRequestId);
65
+ this.frameRequestId = null;
66
+ }
67
+ if (this.drawTimeout !== null) {
68
+ clearTimeout(this.drawTimeout);
69
+ this.drawTimeout = null;
70
+ }
71
+ }
72
+ /**
73
+ * Start the render loop.
74
+ */
75
+ unblock() {
76
+ if (this.frameRequestId !== null) {
77
+ return;
78
+ }
79
+ this.frameRequestId = requestAnimationFrame(this.#drawFrame);
80
+ }
81
+ /**
82
+ * The duration it took to produce previous frames.
83
+ * This is used for the frame graph.
84
+ */
85
+ #frameTimes = [];
86
+ /**
87
+ * Draws a new frame to the offscreen buffer.
88
+ */
89
+ #drawFrame = () => {
90
+ this.frameRequestId = null;
91
+ const timestamp = Date.now();
92
+ const timeDelta = timestamp - this.previousTimestampDraw;
93
+ if (this.options.onRefresh === "clear") {
94
+ this.canvas.clearWith(0);
95
+ }
96
+ this.renderLoop(timeDelta, timestamp);
97
+ const frameTime = Date.now() - timestamp;
98
+ if (this.options.drawFps) {
99
+ const recordCount = this.#frameTimes.unshift(frameTime);
100
+ if (10000 < recordCount) {
101
+ this.#frameTimes.splice(5000, 5000);
102
+ }
103
+ }
104
+ this.previousTimestampDraw = timestamp;
105
+ this.canvas.update();
106
+ if (this.options.drawFps) {
107
+ this.canvas.renderFpsInfo(this.#frameTimes, frameTime, timeDelta);
108
+ }
109
+ this.canvas.render();
110
+ this.unblock();
111
+ ++this.#frameCounter;
112
+ };
113
+ }
114
+ //# sourceMappingURL=render-loop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render-loop.js","sourceRoot":"","sources":["../../source/graphics/render-loop.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,eAAe,CAAC,IAAI,GAAG,EAAE,CAAC;AAuB5D;;;GAGG;AACH,MAAM,OAAO,UAAU;IACtB;;OAEG;IACM,MAAM,CAAS;IAExB;;OAEG;IACM,UAAU,CAAqB;IAExC;;OAEG;IACM,OAAO,CAAuC;IAEvD;;OAEG;IACK,qBAAqB,CAAS;IAEtC;;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;QACf,OAAO,IAAI,CAAC,aAAa,CAAC;IAC3B,CAAC;IAED;;;;;OAKG;IACH,YACC,UAA8B,EAC9B,MAAc,EACd,OAAO,GAA+B,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,IAAI,CAAC,GAAG,EAAE,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,KAAK;QACJ,IAAI,IAAI,CAAC,cAAc,KAAK,IAAI,EAAE,CAAC;YAClC,oBAAoB,CAAC,IAAI,CA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AAC,qBAAqB,GAAG,SAAS,CAAC;QAEvC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAErB,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YAC1B,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,WAAW,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;QACnE,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAErB,IAAI,CAAC,OAAO,EAAE,CAAC;QAEf,EAAE,IAAI,CAAC,aAAa,CAAC;IACtB,CAAC,CAAC;CACF"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Provides the input as-is.
3
+ *
4
+ * This method exists only to make use of the template string tag in code,
5
+ * which allows us to use in-IDE support for GLSL, even if there is no build
6
+ * process to interpret the tag.
7
+ * @param input - Should only be the source of a fragment shader.
8
+ * @returns The fragment shader as-is.
9
+ */
10
+ export declare const frag: (input: TemplateStringsArray) => string;
11
+ /**
12
+ * Provides the input as-is.
13
+ *
14
+ * This method exists only to make use of the template string tag in code,
15
+ * which allows us to use in-IDE support for GLSL, even if there is no build
16
+ * process to interpret the tag.
17
+ * @param input - Should only be the source of a vertex shader.
18
+ * @returns The vertex shader as-is.
19
+ */
20
+ export declare const vert: (input: TemplateStringsArray) => string;
21
+ /**
22
+ * Construction options for a {@linkcode !WebGLShader}.
23
+ */
24
+ export interface ShaderOptions {
25
+ /**
26
+ * The GLSL source code of the shader.
27
+ */
28
+ source: string;
29
+ /**
30
+ * The type of the shader.
31
+ */
32
+ type: WebGL2RenderingContext["FRAGMENT_SHADER"] | WebGL2RenderingContext["VERTEX_SHADER"];
33
+ }
34
+ /**
35
+ * Constructs a new shader.
36
+ * @param gl - The rendering context in which to create the shader.
37
+ * @param options - The construction options for the shader.
38
+ * @returns The shader.
39
+ */
40
+ export declare const createShader: (gl: WebGL2RenderingContext, options: ShaderOptions) => WebGLShader;
41
+ /**
42
+ * Creates a new WebGL program.
43
+ * @param gl - The rendering context in which to create the program.
44
+ * @param shaders - The shaders we want to create.
45
+ * @param transformFeedbackVaryings - Which values to record in transform
46
+ * feedback buffer? See {@linkcode !WebGLTransformFeedback}.
47
+ * @returns The program.
48
+ */
49
+ export declare const createGLProgram: (gl: WebGL2RenderingContext, shaders: Array<ShaderOptions>, transformFeedbackVaryings: Array<string> | null) => WebGLProgram;
50
+ /**
51
+ * Create a buffer with random data in 2 channels (red/green).
52
+ * @param width - Width of the buffer.
53
+ * @param height - Height of the buffer.
54
+ * @returns The buffer.
55
+ */
56
+ export declare const randomRGData: (width: number, height: number) => Uint8Array<ArrayBuffer>;
@@ -0,0 +1,80 @@
1
+ import { mustExist } from "../data/nil.js";
2
+ import { InvalidOperationError } from "../errors/InvalidOperationError.js";
3
+ /**
4
+ * Provides the input as-is.
5
+ *
6
+ * This method exists only to make use of the template string tag in code,
7
+ * which allows us to use in-IDE support for GLSL, even if there is no build
8
+ * process to interpret the tag.
9
+ * @param input - Should only be the source of a fragment shader.
10
+ * @returns The fragment shader as-is.
11
+ */
12
+ export const frag = (input) => input.join();
13
+ /**
14
+ * Provides the input as-is.
15
+ *
16
+ * This method exists only to make use of the template string tag in code,
17
+ * which allows us to use in-IDE support for GLSL, even if there is no build
18
+ * process to interpret the tag.
19
+ * @param input - Should only be the source of a vertex shader.
20
+ * @returns The vertex shader as-is.
21
+ */
22
+ export const vert = (input) => input.join();
23
+ /**
24
+ * Constructs a new shader.
25
+ * @param gl - The rendering context in which to create the shader.
26
+ * @param options - The construction options for the shader.
27
+ * @returns The shader.
28
+ */
29
+ export const createShader = (gl, options) => {
30
+ const shader = mustExist(gl.createShader(options.type));
31
+ const shader_source = options.source.replace(/^\s*/, "");
32
+ gl.shaderSource(shader, shader_source);
33
+ gl.compileShader(shader);
34
+ const compileStatus = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
35
+ if (!compileStatus) {
36
+ const errorMessage = gl.getShaderInfoLog(shader);
37
+ throw new InvalidOperationError(`Could not compile shader\n${errorMessage ?? "<no error message>"}`);
38
+ }
39
+ return shader;
40
+ };
41
+ /**
42
+ * Creates a new WebGL program.
43
+ * @param gl - The rendering context in which to create the program.
44
+ * @param shaders - The shaders we want to create.
45
+ * @param transformFeedbackVaryings - Which values to record in transform
46
+ * feedback buffer? See {@linkcode !WebGLTransformFeedback}.
47
+ * @returns The program.
48
+ */
49
+ export const createGLProgram = (gl, shaders, transformFeedbackVaryings) => {
50
+ const program = mustExist(gl.createProgram());
51
+ for (const shader_info of shaders) {
52
+ const shader = createShader(gl, shader_info);
53
+ gl.attachShader(program, shader);
54
+ }
55
+ if (transformFeedbackVaryings !== null) {
56
+ gl.transformFeedbackVaryings(program, transformFeedbackVaryings, gl.INTERLEAVED_ATTRIBS);
57
+ }
58
+ gl.linkProgram(program);
59
+ const link_status = gl.getProgramParameter(program, gl.LINK_STATUS);
60
+ if (!link_status) {
61
+ const error_message = gl.getProgramInfoLog(program);
62
+ throw new InvalidOperationError(`Could not link program.\n${error_message ?? "<no error message>"}`);
63
+ }
64
+ return program;
65
+ };
66
+ /**
67
+ * Create a buffer with random data in 2 channels (red/green).
68
+ * @param width - Width of the buffer.
69
+ * @param height - Height of the buffer.
70
+ * @returns The buffer.
71
+ */
72
+ export const randomRGData = (width, height) => {
73
+ const data = new Array();
74
+ for (let i = 0; i < width * height; ++i) {
75
+ data.push(Math.random() * 255.0);
76
+ data.push(Math.random() * 255.0);
77
+ }
78
+ return new Uint8Array(data);
79
+ };
80
+ //# sourceMappingURL=shader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shader.js","sourceRoot":"","sources":["../../source/graphics/shader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAE3E;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,KAA2B,EAAU,EAAE,CAAC,KAAK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AED,IAAI,yBAAyB,KAAK,IAAI,EAAE,CAAC;QACxC,EAAE,CAAC,yBAAyB,CAC3B,OAAO,EACP,yBAAyB,EACzB,EAAE,CAAC,mBAAmB,CACtB,CAAC;IACH,CAAC;IAED,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACxB,MAAM,WAAW,GAAY,EAAE,CAAC,mBAAmB,CAAC,OAAO,EAAE,EAAE,CAAC,WAAW,CAAC,CAAC;IAC7E,IAAI,CAAC,WAAW,EAAE,CAAC;QAClB,MAAM,aAAa,GAAG,EAAE,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QACpD,MAAM,IAAI,qBAAqB,CAC9B,4BAA4B,aAAa,IAAI,oBAAoB,EAAE,CACnE,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAa,EAAE,MAAc,EAAE,EAAE;IAC7D,MAAM,IAAI,GAAG,IAAI,KAAK,EAAU,CAAC;IACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,GAAG,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC;QACzC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Describes the most simple logger.
3
+ */
4
+ export interface BareLogger {
5
+ /**
6
+ * Logs a message.
7
+ * @param message - The message to log.
8
+ * @param args - Additional arguments to log.
9
+ */
10
+ log(message: string, ...args: Array<unknown>): void;
11
+ }
package/lib/log/log.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=log.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.js","sourceRoot":"","sources":["../../source/log/log.ts"],"names":[],"mappings":""}
@@ -0,0 +1,57 @@
1
+ import { TreeNode } from "../data/tree.js";
2
+ import type { BareLogger } from "./log.js";
3
+ /**
4
+ * An entry of a {@linkcode Report}.
5
+ */
6
+ export interface ReportEntry {
7
+ /**
8
+ * The message in the entry.
9
+ */
10
+ message: string;
11
+ /**
12
+ * An optional key-value hash, that might have been
13
+ * provided with the entry.
14
+ */
15
+ context: Record<string, unknown> | undefined;
16
+ }
17
+ /**
18
+ * Ensures an origin stays within reasonable display size.
19
+ * @param origin - The original log origin.
20
+ * @returns A shortened origin for display.
21
+ */
22
+ export declare const makeLogOrigin: (origin: string) => string;
23
+ /**
24
+ * A hierarchical background report/log.
25
+ */
26
+ export declare class Report extends TreeNode<Report> {
27
+ #private;
28
+ /**
29
+ * The ID of this report.
30
+ */
31
+ readonly origin: string;
32
+ /**
33
+ * Retrieve the message store.
34
+ * @returns The message store.
35
+ */
36
+ get store(): Map<string, Array<ReportEntry>>;
37
+ /**
38
+ * Constructs a new report.
39
+ * @param origin - The ID of this report.
40
+ * @param parent - The paren report for this report.
41
+ * @throws {@linkcode InvalidOperationError} When the origin was already
42
+ * used by another report in this hierarchy.
43
+ */
44
+ constructor(origin: string, parent?: Report);
45
+ /**
46
+ * Put an entry into the report.
47
+ * @param message - The message to add.
48
+ * @param context - An arbitrary key-value object to store with the message.
49
+ */
50
+ log(message: string, context?: Record<string, unknown>): void;
51
+ /**
52
+ * Prints the contents of the report.
53
+ * @param logger - A logger to use to print the report.
54
+ * @param depth - How deep to indent this part of the report.
55
+ */
56
+ aggregate(logger: BareLogger, depth?: number): void;
57
+ }
@@ -0,0 +1,88 @@
1
+ import { hashCyrb53, indent } from "../data/string.js";
2
+ import { TreeNode } from "../data/tree.js";
3
+ import { InvalidOperationError } from "../errors/InvalidOperationError.js";
4
+ /**
5
+ * Ensures an origin stays within reasonable display size.
6
+ * @param origin - The original log origin.
7
+ * @returns A shortened origin for display.
8
+ */
9
+ export const makeLogOrigin = (origin) => origin.includes("\n") ? hashCyrb53(origin) : origin;
10
+ /**
11
+ * A hierarchical background report/log.
12
+ */
13
+ export class Report extends TreeNode {
14
+ /**
15
+ * The ID of this report.
16
+ */
17
+ origin;
18
+ /**
19
+ * Our storage for all logged messages.
20
+ */
21
+ #store = new Map();
22
+ /**
23
+ * Retrieve the message store.
24
+ * @returns The message store.
25
+ */
26
+ get store() {
27
+ return this.parent?.store ?? this.#store;
28
+ }
29
+ /**
30
+ * Constructs a new report.
31
+ * @param origin - The ID of this report.
32
+ * @param parent - The paren report for this report.
33
+ * @throws {@linkcode InvalidOperationError} When the origin was already
34
+ * used by another report in this hierarchy.
35
+ */
36
+ constructor(origin, parent) {
37
+ super(parent);
38
+ if (this.store.has(origin)) {
39
+ throw new InvalidOperationError(`The origin '${origin}' was already used in this report.`);
40
+ }
41
+ this.origin = origin;
42
+ }
43
+ /**
44
+ * Retrieves an entry of the store for the given origin.
45
+ * @param origin - The origin of the entry.
46
+ * @returns The store entry with that origin.
47
+ */
48
+ #getStoreEntry(origin) {
49
+ if (!this.#store.has(origin)) {
50
+ this.#store.set(origin, new Array());
51
+ }
52
+ return this.#store.get(origin);
53
+ }
54
+ /**
55
+ * Put an entry into the report.
56
+ * @param message - The message to add.
57
+ * @param context - An arbitrary key-value object to store with the message.
58
+ */
59
+ log(message, context) {
60
+ this.#getStoreEntry(makeLogOrigin(this.origin))?.push({ context, message });
61
+ }
62
+ /**
63
+ * Prints the contents of the report.
64
+ * @param logger - A logger to use to print the report.
65
+ * @param depth - How deep to indent this part of the report.
66
+ */
67
+ aggregate(logger, depth = 0) {
68
+ for (const [origin, records] of this.#store.entries()) {
69
+ if (origin !== this.origin) {
70
+ continue;
71
+ }
72
+ logger.log(indent(origin, depth));
73
+ for (const entry of records) {
74
+ logger.log(indent(` - ${entry.message}`, depth));
75
+ if (entry.context) {
76
+ logger.log(indent(JSON.stringify(entry.context, undefined, depth + 4), depth));
77
+ }
78
+ }
79
+ }
80
+ if (this.children.size === 0) {
81
+ return;
82
+ }
83
+ for (const child of this.children) {
84
+ child.aggregate(logger, depth + 1);
85
+ }
86
+ }
87
+ }
88
+ //# sourceMappingURL=report.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"report.js","sourceRoot":"","sources":["../../source/log/report.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAmB3E;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,MAAc,EAAU,EAAE,CACvD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;AAErD;;GAEG;AACH,MAAM,OAAO,MAAO,SAAQ,QAAgB;IAC3C;;OAEG;IACM,MAAM,CAAS;IACxB;;OAEG;IACH,MAAM,GAAG,IAAI,GAAG,EAA8B,CAAC;IAE/C;;;OAGG;IACH,IAAI,KAAK;QACR,OAAO,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACH,YAAY,MAAc,EAAE,MAAe;QAC1C,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,qBAAqB,CAC9B,eAAe,MAAM,oCAAoC,CACzD,CAAC;QACH,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACH,cAAc,CAAC,MAAc;QAC5B,IAAI,CAAC,IAAI,CA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AAM,KAAK,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnC,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACpC,CAAC;IACF,CAAC;CACD"}
@@ -0,0 +1,85 @@
1
+ /**
2
+ * `PI` times 2.
3
+ */
4
+ export declare const TWO_PI: number;
5
+ /**
6
+ * Converts the degrees to radians.
7
+ * @param degrees - The degrees to convert to radians.
8
+ * @returns The degrees in radians.
9
+ */
10
+ export declare const degreesToRadians: (degrees: number) => number;
11
+ /**
12
+ * Convertrs the radians to degrees.
13
+ * @param radians - The radians to convert to degrees.
14
+ * @returns The radians as degrees.
15
+ */
16
+ export declare const radiansToDegrees: (radians: number) => number;
17
+ /**
18
+ * Calculates the sine for a given degree value.
19
+ * @param value - The value in degrees to calculate the sine for.
20
+ * @returns The sine for the given value.
21
+ */
22
+ export declare const sinDegrees: (value: number) => number;
23
+ /**
24
+ * Calculates the cosine for a given degree value.
25
+ * @param value - The value in degrees to calculate the cosine for.
26
+ * @returns The cosine for the given value.
27
+ */
28
+ export declare const cosDegrees: (value: number) => number;
29
+ /**
30
+ * Calculates the distance between two vectors.
31
+ * @param x1 - The X component of the first vector.
32
+ * @param y1 - The Y comoinent of the first vector.
33
+ * @param x2 - The X component of the second vector.
34
+ * @param y2 - The Y component of the second vector.
35
+ * @returns The distance between the two vectors.
36
+ */
37
+ export declare const distance: (x1: number, y1: number, x2: number, y2: number) => number;
38
+ /**
39
+ * Finds the integer square root of a positive number.
40
+ * @param value - The value to calcuate the square root for.
41
+ * @returns The square root for the given value.
42
+ */
43
+ export declare const isqrt: (value: number) => number;
44
+ /**
45
+ * Checks whether a number is an integer.
46
+ * @param value - The value to check.
47
+ * @returns `true` when the value is an integer, `false` otherwise.
48
+ */
49
+ export declare const isInteger: (value: number) => boolean;
50
+ /**
51
+ * Ensures a given value is within a given boundary.
52
+ * @param input - The number to clamp.
53
+ * @param floor - The lower bound.
54
+ * @param ceil - The upper bound.
55
+ * @returns The number clamped to the desired range.
56
+ */
57
+ export declare const clamp: (input: number, floor: number, ceil: number) => number;
58
+ /**
59
+ * Rounds a given number to the next highest value.
60
+ * @param input - The number to round.
61
+ * @param fractionDigits - The amount of digits to retain in the fraction.
62
+ * @returns The rounded number.
63
+ */
64
+ export declare const ceilTo: (input: number, fractionDigits?: number) => number;
65
+ /**
66
+ * Rounds a given number to the next lowest value.
67
+ * @param input - The number to round.
68
+ * @param fractionDigits - The amount of digits to retain in the fraction.
69
+ * @returns The rounded number.
70
+ */
71
+ export declare const floorTo: (input: number, fractionDigits?: number) => number;
72
+ /**
73
+ * Rounds a given number to the next closest value.
74
+ * @param input - The number to round.
75
+ * @param fractionDigits - The amount of digits to retain in the fraction.
76
+ * @returns The rounded number.
77
+ */
78
+ export declare const roundTo: (input: number, fractionDigits?: number) => number;
79
+ /**
80
+ * Cuts off digits of a number.
81
+ * @param input - The number to round.
82
+ * @param fractionDigits - The amount of digits to retain in the fraction.
83
+ * @returns The rounded number.
84
+ */
85
+ export declare const truncTo: (input: number, fractionDigits?: number) => number;