@oliversalzburg/js-utils 0.6.0 → 0.7.0

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 (172) hide show
  1. package/package.json +5 -5
  2. package/lib/async/async.d.ts +0 -44
  3. package/lib/async/async.js +0 -76
  4. package/lib/async/async.js.map +0 -1
  5. package/lib/async/async.test.d.ts +0 -1
  6. package/lib/async/async.test.js +0 -26
  7. package/lib/async/async.test.js.map +0 -1
  8. package/lib/core.d.ts +0 -43
  9. package/lib/core.js +0 -2
  10. package/lib/core.js.map +0 -1
  11. package/lib/data/array.d.ts +0 -45
  12. package/lib/data/array.js +0 -70
  13. package/lib/data/array.js.map +0 -1
  14. package/lib/data/nil.d.ts +0 -92
  15. package/lib/data/nil.js +0 -131
  16. package/lib/data/nil.js.map +0 -1
  17. package/lib/data/random.d.ts +0 -111
  18. package/lib/data/random.js +0 -377
  19. package/lib/data/random.js.map +0 -1
  20. package/lib/data/string.d.ts +0 -30
  21. package/lib/data/string.js +0 -52
  22. package/lib/data/string.js.map +0 -1
  23. package/lib/data/tree.d.ts +0 -28
  24. package/lib/data/tree.js +0 -45
  25. package/lib/data/tree.js.map +0 -1
  26. package/lib/device/shake.d.ts +0 -79
  27. package/lib/device/shake.js +0 -135
  28. package/lib/device/shake.js.map +0 -1
  29. package/lib/dom/core.d.ts +0 -21
  30. package/lib/dom/core.js +0 -41
  31. package/lib/dom/core.js.map +0 -1
  32. package/lib/errors/AbstractError.d.ts +0 -29
  33. package/lib/errors/AbstractError.js +0 -59
  34. package/lib/errors/AbstractError.js.map +0 -1
  35. package/lib/errors/InternalError.d.ts +0 -19
  36. package/lib/errors/InternalError.js +0 -38
  37. package/lib/errors/InternalError.js.map +0 -1
  38. package/lib/errors/InvalidArgumentError.d.ts +0 -13
  39. package/lib/errors/InvalidArgumentError.js +0 -22
  40. package/lib/errors/InvalidArgumentError.js.map +0 -1
  41. package/lib/errors/InvalidOperationError.d.ts +0 -13
  42. package/lib/errors/InvalidOperationError.js +0 -22
  43. package/lib/errors/InvalidOperationError.js.map +0 -1
  44. package/lib/errors/NotImplementedError.d.ts +0 -12
  45. package/lib/errors/NotImplementedError.js +0 -21
  46. package/lib/errors/NotImplementedError.js.map +0 -1
  47. package/lib/errors/PermissionViolationError.d.ts +0 -13
  48. package/lib/errors/PermissionViolationError.js +0 -22
  49. package/lib/errors/PermissionViolationError.js.map +0 -1
  50. package/lib/errors/ResourceConflictError.d.ts +0 -12
  51. package/lib/errors/ResourceConflictError.js +0 -21
  52. package/lib/errors/ResourceConflictError.js.map +0 -1
  53. package/lib/errors/UnknownError.d.ts +0 -16
  54. package/lib/errors/UnknownError.js +0 -25
  55. package/lib/errors/UnknownError.js.map +0 -1
  56. package/lib/errors/console.d.ts +0 -7
  57. package/lib/errors/console.js +0 -13
  58. package/lib/errors/console.js.map +0 -1
  59. package/lib/errors/error-serializer.d.ts +0 -45
  60. package/lib/errors/error-serializer.js +0 -71
  61. package/lib/errors/error-serializer.js.map +0 -1
  62. package/lib/errors/error-serializer.test.d.ts +0 -1
  63. package/lib/errors/error-serializer.test.js +0 -109
  64. package/lib/errors/error-serializer.test.js.map +0 -1
  65. package/lib/errors/stream.d.ts +0 -7
  66. package/lib/errors/stream.js +0 -15
  67. package/lib/errors/stream.js.map +0 -1
  68. package/lib/errors/tools.d.ts +0 -10
  69. package/lib/errors/tools.js +0 -13
  70. package/lib/errors/tools.js.map +0 -1
  71. package/lib/events/event.d.ts +0 -9
  72. package/lib/events/event.js +0 -10
  73. package/lib/events/event.js.map +0 -1
  74. package/lib/format/bytes.d.ts +0 -118
  75. package/lib/format/bytes.js +0 -99
  76. package/lib/format/bytes.js.map +0 -1
  77. package/lib/format/bytes.test.d.ts +0 -1
  78. package/lib/format/bytes.test.js +0 -138
  79. package/lib/format/bytes.test.js.map +0 -1
  80. package/lib/format/count.d.ts +0 -14
  81. package/lib/format/count.js +0 -50
  82. package/lib/format/count.js.map +0 -1
  83. package/lib/format/count.test.d.ts +0 -1
  84. package/lib/format/count.test.js +0 -42
  85. package/lib/format/count.test.js.map +0 -1
  86. package/lib/format/milliseconds.d.ts +0 -100
  87. package/lib/format/milliseconds.js +0 -192
  88. package/lib/format/milliseconds.js.map +0 -1
  89. package/lib/format/milliseconds.test.d.ts +0 -1
  90. package/lib/format/milliseconds.test.js +0 -343
  91. package/lib/format/milliseconds.test.js.map +0 -1
  92. package/lib/format/string.d.ts +0 -24
  93. package/lib/format/string.js +0 -30
  94. package/lib/format/string.js.map +0 -1
  95. package/lib/format/string.test.d.ts +0 -1
  96. package/lib/format/string.test.js +0 -17
  97. package/lib/format/string.test.js.map +0 -1
  98. package/lib/graphics/canvas-sandbox-mp.d.ts +0 -258
  99. package/lib/graphics/canvas-sandbox-mp.js +0 -279
  100. package/lib/graphics/canvas-sandbox-mp.js.map +0 -1
  101. package/lib/graphics/canvas-sandbox.d.ts +0 -255
  102. package/lib/graphics/canvas-sandbox.js +0 -361
  103. package/lib/graphics/canvas-sandbox.js.map +0 -1
  104. package/lib/graphics/canvas.d.ts +0 -57
  105. package/lib/graphics/canvas.js +0 -37
  106. package/lib/graphics/canvas.js.map +0 -1
  107. package/lib/graphics/canvas2d-headless.d.ts +0 -85
  108. package/lib/graphics/canvas2d-headless.js +0 -193
  109. package/lib/graphics/canvas2d-headless.js.map +0 -1
  110. package/lib/graphics/canvas2d.d.ts +0 -162
  111. package/lib/graphics/canvas2d.js +0 -328
  112. package/lib/graphics/canvas2d.js.map +0 -1
  113. package/lib/graphics/canvas3d.d.ts +0 -41
  114. package/lib/graphics/canvas3d.js +0 -67
  115. package/lib/graphics/canvas3d.js.map +0 -1
  116. package/lib/graphics/color.d.ts +0 -22
  117. package/lib/graphics/color.js +0 -42
  118. package/lib/graphics/color.js.map +0 -1
  119. package/lib/graphics/core.d.ts +0 -89
  120. package/lib/graphics/core.js +0 -146
  121. package/lib/graphics/core.js.map +0 -1
  122. package/lib/graphics/palette-sampler.d.ts +0 -7
  123. package/lib/graphics/palette-sampler.js +0 -21
  124. package/lib/graphics/palette-sampler.js.map +0 -1
  125. package/lib/graphics/palette.d.ts +0 -120
  126. package/lib/graphics/palette.js +0 -494
  127. package/lib/graphics/palette.js.map +0 -1
  128. package/lib/graphics/palette.test.d.ts +0 -1
  129. package/lib/graphics/palette.test.js +0 -43
  130. package/lib/graphics/palette.test.js.map +0 -1
  131. package/lib/graphics/render-loop.d.ts +0 -80
  132. package/lib/graphics/render-loop.js +0 -120
  133. package/lib/graphics/render-loop.js.map +0 -1
  134. package/lib/graphics/shader.d.ts +0 -56
  135. package/lib/graphics/shader.js +0 -80
  136. package/lib/graphics/shader.js.map +0 -1
  137. package/lib/log/log.d.ts +0 -11
  138. package/lib/log/log.js +0 -2
  139. package/lib/log/log.js.map +0 -1
  140. package/lib/log/report.d.ts +0 -57
  141. package/lib/log/report.js +0 -88
  142. package/lib/log/report.js.map +0 -1
  143. package/lib/math/core.d.ts +0 -85
  144. package/lib/math/core.js +0 -125
  145. package/lib/math/core.js.map +0 -1
  146. package/lib/math/easing.d.ts +0 -204
  147. package/lib/math/easing.js +0 -332
  148. package/lib/math/easing.js.map +0 -1
  149. package/lib/math/euler.d.ts +0 -18
  150. package/lib/math/euler.js +0 -22
  151. package/lib/math/euler.js.map +0 -1
  152. package/lib/math/matrix3.d.ts +0 -217
  153. package/lib/math/matrix3.js +0 -508
  154. package/lib/math/matrix3.js.map +0 -1
  155. package/lib/math/quaternion.d.ts +0 -119
  156. package/lib/math/quaternion.js +0 -270
  157. package/lib/math/quaternion.js.map +0 -1
  158. package/lib/math/transformation.d.ts +0 -26
  159. package/lib/math/transformation.js +0 -31
  160. package/lib/math/transformation.js.map +0 -1
  161. package/lib/math/vector2.d.ts +0 -202
  162. package/lib/math/vector2.js +0 -310
  163. package/lib/math/vector2.js.map +0 -1
  164. package/lib/math/vector3.d.ts +0 -293
  165. package/lib/math/vector3.js +0 -492
  166. package/lib/math/vector3.js.map +0 -1
  167. package/lib/math/vector4.d.ts +0 -182
  168. package/lib/math/vector4.js +0 -302
  169. package/lib/math/vector4.js.map +0 -1
  170. package/lib/measurement/performance.d.ts +0 -13
  171. package/lib/measurement/performance.js +0 -19
  172. package/lib/measurement/performance.js.map +0 -1
@@ -1,255 +0,0 @@
1
- import { Random } from "../data/random.js";
2
- import { Shake } from "../device/shake.js";
3
- import { Canvas } from "./canvas.js";
4
- import { RenderLoop } from "./render-loop.js";
5
- /**
6
- * Provides the input as-is.
7
- *
8
- * This method exists only to make use of the template string tag in code,
9
- * which allows us to use in-IDE support for CSS, even if there is no build
10
- * process to interpret the tag.
11
- * {@link https://lit.dev/docs/components/styles/ | Lit styles}.
12
- * @param input - The CSS code.
13
- * @returns The CSS code as-is.
14
- */
15
- export declare const css: (input: TemplateStringsArray) => string;
16
- /**
17
- * The CSS we inject into the document, if requested.
18
- */
19
- export declare const CANVAS_SANDBOX_DEFAULT_CSS: string;
20
- /**
21
- * The options your application has to accept so it can interact with the {@linkcode CanvasSandbox}.
22
- */
23
- export interface CanvasSandboxExpectedOptions {
24
- /**
25
- * Should your application run in dark mode?
26
- * You can ignore this setting and produce any colored output you want,
27
- * but the setting will be toggeled by the sandbox according to the user's
28
- * browser/OS preference.
29
- */
30
- darkMode: boolean;
31
- /**
32
- * The seed for the PRNG.
33
- */
34
- seed: number;
35
- /**
36
- * The part of the canvas that the targetted render kernel will handle.
37
- * This is ignored, unless you use multi-process rendering.
38
- */
39
- viewport: {
40
- /**
41
- * Start of the viewport in fractional world-space.
42
- */
43
- x: number;
44
- /**
45
- * Start of the viewport in fractional world-space.
46
- */
47
- y: number;
48
- /**
49
- * Width of the viewport in fractional world-space.
50
- */
51
- w: number;
52
- /**
53
- * Height of the viewport in fractional world-space.
54
- */
55
- h: number;
56
- };
57
- }
58
- /**
59
- * Describes an application running inside the {@linkcode CanvasSandbox}.
60
- * @typeParam TApplicationOptions - The type of the options object
61
- * with which the application is constructed.
62
- */
63
- export interface CanvasSandboxApplication<TCanvas extends Canvas, TApplicationOptions extends CanvasSandboxExpectedOptions> {
64
- /**
65
- * The options with which the application was constructed.
66
- */
67
- options: TApplicationOptions;
68
- /**
69
- * The PRNG used in this application.
70
- */
71
- random: Random;
72
- /**
73
- * Is the application currently paused?
74
- * Should be get/set, but can also be ignored, if the feature isn't used.
75
- */
76
- paused: boolean;
77
- /**
78
- * Request the application to draw a new frame.
79
- * @param delta - How many milliseconds passed since the last frame draw.
80
- * @param timestamp - The current timestamp.
81
- */
82
- onDraw(delta: number, timestamp: number): void;
83
- /**
84
- * Reconfigure the application with the given options and restart it.
85
- * @param canvas - The canvas to use.
86
- * @param options - The new options to use.
87
- */
88
- reconfigure(canvas: TCanvas, options?: Partial<TApplicationOptions>): void;
89
- /**
90
- * Start the application.
91
- */
92
- start(options?: Partial<TApplicationOptions>): void;
93
- /**
94
- * Pause rendering.
95
- * @param paused - Should rendering be paused (`true`) or unpaused (`false`)?
96
- */
97
- pause(paused: boolean): void;
98
- }
99
- /**
100
- * Options for a {@linkcode CanvasSandbox}.
101
- */
102
- export interface CanvasSandboxOptions {
103
- /**
104
- * Should the sandbox render developer information?
105
- * @defaultValue false
106
- */
107
- devMode: boolean;
108
- /**
109
- * Should the sandbox default CSS be injected into the body?
110
- * @defaultValue true
111
- */
112
- injectDefaultCss: boolean;
113
- }
114
- /**
115
- * The {@linkcode CanvasSandbox} provides a scaffold to quickly perform experiments
116
- * with a {@linkcode !HTMLCanvasElement} in a document. Common functionality for such
117
- * experiments is implemented in the sandbox already.
118
- * @example
119
- * ### `main.ts`
120
- * ```ts
121
- * import { Random, seedFromString } from "@oliversalzburg/js-utils/lib/random.js";
122
- * import { Canvas } from "@oliversalzburg/js-utils/lib/graphics/canvas.js";
123
- * import { getDocumentElementTypeByIdStrict } from "@oliversalzburg/js-utils/lib/dom/core.js";
124
- * import { CanvasSandbox } from "@oliversalzburg/js-utils/lib/graphics/canvas-sandbox.js";
125
- *
126
- * interface ApplicationOptions {
127
- * seed: string;
128
- * somethingElse: number;
129
- * }
130
- *
131
- * class Application {
132
- * canvas: Canvas;
133
- * options: ApplicationOptions;
134
- * random: Random;
135
- *
136
- * constructor(canvas:Canvas, options: ApplicationOptions) {
137
- * this.canvas = canvas;
138
- * this.options = options;
139
- * this.random = new Random(seedFromString(options.seed));
140
- * }
141
- *
142
- * reconfigure(canvas:Canvas, options: Partial<ApplicationOptions> = {}) {
143
- * this.canvas = canvas;
144
- * this.options = { ...this.options, ...options };
145
- * this.random = new Random(seedFromString(options.seed));
146
- * }
147
- *
148
- * onDraw(delta:number, timestamp:number) {
149
- * // draw something on `this.canvas`
150
- * }
151
- *
152
- * start() {}
153
- * }
154
- *
155
- * const canvasNode = getDocumentElementTypeByIdStrict(document, "main", HTMLCanvasElement);
156
- * const canvasSandbox = new CanvasSandbox(
157
- * window,
158
- * canvasNode,
159
- * Application,
160
- * {seed: "foo", somethingElse: 12345}
161
- * );
162
- *
163
- * // By running the sandbox, your `start()` and `onDraw()` methods will automatically be invoked
164
- * // by the sandbox. You shouldn't invoke those methods directly from your own code.
165
- * canvasSandbox.run();
166
- * ```
167
- * ### `index.html`
168
- * ```html
169
- * <!doctype html>
170
- * <html>
171
- * <head>
172
- * <meta charset="UTF-8" />
173
- * </head>
174
- *
175
- * <body>
176
- * <canvas id="main" class="darkMode" width="512" height="512"></canvas>
177
- * </body>
178
- * <script src="main.ts" type="module"></script>
179
- * </html>
180
- * ```
181
- * @typeParam TApplicationOptions - The type of the options
182
- * the application in the sandbox will be constructed with.
183
- */
184
- export declare class CanvasSandbox<TCanvas extends Canvas, TApplicationOptions extends CanvasSandboxExpectedOptions> {
185
- #private;
186
- /**
187
- * The window the sandbox is running in.
188
- */
189
- readonly window: Window;
190
- /**
191
- * The document the sandbox is interacting with.
192
- */
193
- readonly document: Document;
194
- /**
195
- * The application running inside the sandbox.
196
- */
197
- readonly application: CanvasSandboxApplication<TCanvas, TApplicationOptions>;
198
- /**
199
- * The canvas node that we're drawing into.
200
- */
201
- readonly canvasNode: HTMLCanvasElement;
202
- /**
203
- * Our convenience canvas wrapper, which we provide to the application
204
- * to render to.
205
- */
206
- readonly canvas: TCanvas;
207
- /**
208
- * The render loop we use to control frame rendering.
209
- */
210
- readonly renderLoop: RenderLoop;
211
- /**
212
- * Our handler for device shaking.
213
- */
214
- readonly shakeHandler: Shake;
215
- /**
216
- * The options for this sandbox.
217
- */
218
- readonly sandboxOptions: Partial<CanvasSandboxOptions>;
219
- /**
220
- * Construct a new {@linkcode CanvasSandbox}.
221
- * @param window - The window we're running inside.
222
- * @param canvasNode - The canvas node we're drawing to.
223
- * @param CanvasImpl - The canvas implementation you want to use in this sandbox.
224
- * see {@linkcode Canvas2D} and {@linkcode Canvas3D}.
225
- * @param Application - The application that is going to run in the sandbox.
226
- * @param options - The options that the application should be constructed with.
227
- * @param sandboxOptions - The options for the sandbox itself.
228
- */
229
- constructor(window: Window, canvasNode: HTMLCanvasElement, CanvasImpl: new (canvasNode: HTMLCanvasElement) => TCanvas, Application: new (
230
- /**
231
- * The canvas that the application will receive.
232
- */
233
- canvas: TCanvas,
234
- /**
235
- * The options that will be passed to the application.
236
- */
237
- options: TApplicationOptions) => CanvasSandboxApplication<TCanvas, TApplicationOptions>, options: TApplicationOptions, sandboxOptions?: Partial<CanvasSandboxOptions>);
238
- /**
239
- * Injects our default CSS into the document, if it hasn't already been injected.
240
- * @see {@linkcode CANVAS_SANDBOX_DEFAULT_CSS}
241
- */
242
- injectDefaultCss(): void;
243
- /**
244
- * Run the sandbox.
245
- */
246
- run(): void;
247
- /**
248
- * Switch the sandbox to dark mode.
249
- */
250
- toDarkMode(): void;
251
- /**
252
- * Switch the sandbox to light mode.
253
- */
254
- toLightMode(): void;
255
- }
@@ -1,361 +0,0 @@
1
- import { Shake } from "../device/shake.js";
2
- import { getDocumentElementTypeById } from "../dom/core.js";
3
- import { redirectErrorsToConsole } from "../errors/console.js";
4
- import { RenderLoop } from "./render-loop.js";
5
- /**
6
- * Provides the input as-is.
7
- *
8
- * This method exists only to make use of the template string tag in code,
9
- * which allows us to use in-IDE support for CSS, even if there is no build
10
- * process to interpret the tag.
11
- * {@link https://lit.dev/docs/components/styles/ | Lit styles}.
12
- * @param input - The CSS code.
13
- * @returns The CSS code as-is.
14
- */
15
- export const css = (input) => input.join("");
16
- /**
17
- * The CSS we inject into the document, if requested.
18
- */
19
- export const CANVAS_SANDBOX_DEFAULT_CSS = /* PURE */ css `
20
- html,
21
- body {
22
- height: 100%;
23
- width: 100%;
24
- padding: 0;
25
- margin: 0;
26
- background-color: #808080;
27
- }
28
-
29
- body {
30
- display: block !important;
31
- transition: background-color 1s ease-in-out;
32
- }
33
- body.darkMode {
34
- background-color: #211f1f;
35
-
36
- .credits a {
37
- color: #a0a0a0;
38
- }
39
- }
40
- body.lightMode {
41
- background-color: #e6e6e6;
42
-
43
- .credits a {
44
- color: #606060;
45
- }
46
- }
47
-
48
- #main {
49
- display: flex;
50
- position: absolute;
51
- margin: auto;
52
- top: 0;
53
- left: 0;
54
- right: 0;
55
- bottom: 0;
56
- justify-content: center;
57
- align-items: center;
58
- z-index: 1;
59
-
60
- filter: drop-shadow(10px 10px 4px rgba(0, 0, 0, 0.5));
61
- transition: all 1s;
62
- }
63
-
64
- .credits {
65
- display: block;
66
- font-family: sans-serif;
67
- font-size: 10pt;
68
- color: #808080;
69
- position: absolute;
70
- top: 80%;
71
- text-align: center;
72
- user-select: none;
73
- width: 100%;
74
- }
75
- `;
76
- /**
77
- * The {@linkcode CanvasSandbox} provides a scaffold to quickly perform experiments
78
- * with a {@linkcode !HTMLCanvasElement} in a document. Common functionality for such
79
- * experiments is implemented in the sandbox already.
80
- * @example
81
- * ### `main.ts`
82
- * ```ts
83
- * import { Random, seedFromString } from "@oliversalzburg/js-utils/lib/random.js";
84
- * import { Canvas } from "@oliversalzburg/js-utils/lib/graphics/canvas.js";
85
- * import { getDocumentElementTypeByIdStrict } from "@oliversalzburg/js-utils/lib/dom/core.js";
86
- * import { CanvasSandbox } from "@oliversalzburg/js-utils/lib/graphics/canvas-sandbox.js";
87
- *
88
- * interface ApplicationOptions {
89
- * seed: string;
90
- * somethingElse: number;
91
- * }
92
- *
93
- * class Application {
94
- * canvas: Canvas;
95
- * options: ApplicationOptions;
96
- * random: Random;
97
- *
98
- * constructor(canvas:Canvas, options: ApplicationOptions) {
99
- * this.canvas = canvas;
100
- * this.options = options;
101
- * this.random = new Random(seedFromString(options.seed));
102
- * }
103
- *
104
- * reconfigure(canvas:Canvas, options: Partial<ApplicationOptions> = {}) {
105
- * this.canvas = canvas;
106
- * this.options = { ...this.options, ...options };
107
- * this.random = new Random(seedFromString(options.seed));
108
- * }
109
- *
110
- * onDraw(delta:number, timestamp:number) {
111
- * // draw something on `this.canvas`
112
- * }
113
- *
114
- * start() {}
115
- * }
116
- *
117
- * const canvasNode = getDocumentElementTypeByIdStrict(document, "main", HTMLCanvasElement);
118
- * const canvasSandbox = new CanvasSandbox(
119
- * window,
120
- * canvasNode,
121
- * Application,
122
- * {seed: "foo", somethingElse: 12345}
123
- * );
124
- *
125
- * // By running the sandbox, your `start()` and `onDraw()` methods will automatically be invoked
126
- * // by the sandbox. You shouldn't invoke those methods directly from your own code.
127
- * canvasSandbox.run();
128
- * ```
129
- * ### `index.html`
130
- * ```html
131
- * <!doctype html>
132
- * <html>
133
- * <head>
134
- * <meta charset="UTF-8" />
135
- * </head>
136
- *
137
- * <body>
138
- * <canvas id="main" class="darkMode" width="512" height="512"></canvas>
139
- * </body>
140
- * <script src="main.ts" type="module"></script>
141
- * </html>
142
- * ```
143
- * @typeParam TApplicationOptions - The type of the options
144
- * the application in the sandbox will be constructed with.
145
- */
146
- export class CanvasSandbox {
147
- /**
148
- * The window the sandbox is running in.
149
- */
150
- window;
151
- /**
152
- * The document the sandbox is interacting with.
153
- */
154
- document;
155
- /**
156
- * The application running inside the sandbox.
157
- */
158
- application;
159
- /**
160
- * The canvas node that we're drawing into.
161
- */
162
- canvasNode;
163
- /**
164
- * Our convenience canvas wrapper, which we provide to the application
165
- * to render to.
166
- */
167
- canvas;
168
- /**
169
- * The render loop we use to control frame rendering.
170
- */
171
- renderLoop;
172
- /**
173
- * Our handler for device shaking.
174
- */
175
- shakeHandler;
176
- /**
177
- * The options for this sandbox.
178
- */
179
- sandboxOptions;
180
- /**
181
- * The handle that keeps track of debouncing of `resize` events.
182
- */
183
- #resizeDebounce = null;
184
- /**
185
- * Construct a new {@linkcode CanvasSandbox}.
186
- * @param window - The window we're running inside.
187
- * @param canvasNode - The canvas node we're drawing to.
188
- * @param CanvasImpl - The canvas implementation you want to use in this sandbox.
189
- * see {@linkcode Canvas2D} and {@linkcode Canvas3D}.
190
- * @param Application - The application that is going to run in the sandbox.
191
- * @param options - The options that the application should be constructed with.
192
- * @param sandboxOptions - The options for the sandbox itself.
193
- */
194
- constructor(window, canvasNode, CanvasImpl, Application, options, sandboxOptions) {
195
- this.window = window;
196
- this.document = window.document;
197
- this.canvasNode = canvasNode;
198
- this.canvas = new CanvasImpl(canvasNode);
199
- this.sandboxOptions = sandboxOptions ?? {};
200
- if (sandboxOptions?.injectDefaultCss ?? true) {
201
- this.injectDefaultCss();
202
- }
203
- this.application = new Application(this.canvas, options);
204
- const applicationOnDraw = this.application.onDraw.bind(this.application);
205
- this.renderLoop = new RenderLoop(applicationOnDraw, this.canvas, {
206
- drawFps: sandboxOptions?.devMode,
207
- });
208
- this.shakeHandler = new Shake();
209
- this.#hookWindow();
210
- this.#hookBody();
211
- this.#hookCanvas();
212
- const systemPrefersDarkMode = this.window.matchMedia("(prefers-color-scheme: dark)").matches;
213
- if (systemPrefersDarkMode) {
214
- this.toDarkMode();
215
- }
216
- else {
217
- this.toLightMode();
218
- }
219
- }
220
- /**
221
- * Injects our default CSS into the document, if it hasn't already been injected.
222
- * @see {@linkcode CANVAS_SANDBOX_DEFAULT_CSS}
223
- */
224
- injectDefaultCss() {
225
- const existingStyle = getDocumentElementTypeById(this.document, "canvas-sandbox-styles", HTMLStyleElement);
226
- if (existingStyle) {
227
- return;
228
- }
229
- const styleNode = this.document.createElement("style");
230
- styleNode.textContent = CANVAS_SANDBOX_DEFAULT_CSS;
231
- this.document.head.appendChild(styleNode);
232
- }
233
- /**
234
- * Run the sandbox.
235
- */
236
- run() {
237
- if (this.sandboxOptions.devMode) {
238
- // biome-ignore lint/suspicious/noConsole: This is a development mode option.
239
- console.info("CanvasSandbox: Starting application with these options.", this.application.options);
240
- }
241
- this.application.start();
242
- // In case the application doesn't maintain this state itself.
243
- this.application.paused = false;
244
- this.renderLoop.unblock();
245
- }
246
- /**
247
- * Create event listeners on the document `body`.
248
- */
249
- #hookBody() {
250
- this.document.body.addEventListener("click", async (event) => {
251
- if (event.defaultPrevented) {
252
- return;
253
- }
254
- if (!this.document.fullscreenEnabled) {
255
- return;
256
- }
257
- // Make canvas fullscreen.
258
- if (!this.document.fullscreenElement && event.target === this.document.body) {
259
- if (this.sandboxOptions.devMode) {
260
- // biome-ignore lint/suspicious/noConsole: This is a development mode option.
261
- console.info("CanvasSandbox: Canvas is entering fullscreen mode...");
262
- }
263
- await this.canvasNode.requestFullscreen();
264
- }
265
- });
266
- this.document.body.addEventListener("keyup", event => {
267
- switch (event.key) {
268
- case "Enter":
269
- this.#reconfigureApplication();
270
- break;
271
- case " ":
272
- this.application.pause(!this.application.paused);
273
- break;
274
- }
275
- });
276
- }
277
- /**
278
- * Reconfigures, and then restarts, the application.
279
- * @param options - The new options for the application.
280
- */
281
- #reconfigureApplication(options = {}) {
282
- this.application.reconfigure(this.canvas, options);
283
- this.application.start();
284
- // In case the application doesn't maintain this state itself.
285
- this.application.paused = false;
286
- }
287
- /**
288
- * Create event listeners on the canvas node in the document.
289
- */
290
- #hookCanvas() {
291
- this.canvasNode.addEventListener("click", (event) => {
292
- this.application.pause(!this.application.paused);
293
- event.preventDefault();
294
- });
295
- }
296
- /**
297
- * Create event listeners on the window we're running in.
298
- */
299
- #hookWindow() {
300
- this.shakeHandler
301
- .start()
302
- .then(() => {
303
- this.shakeHandler.addEventListener("shake", () => {
304
- if (this.sandboxOptions.devMode) {
305
- // biome-ignore lint/suspicious/noConsole: This is a development mode option.
306
- console.info("CanvasSandbox: Shake detected. Reconfiguring application...");
307
- }
308
- this.#reconfigureApplication();
309
- });
310
- })
311
- .catch(redirectErrorsToConsole(console));
312
- this.window
313
- .matchMedia("(prefers-color-scheme: dark)")
314
- .addEventListener("change", ({ matches }) => {
315
- if (matches) {
316
- this.toDarkMode();
317
- }
318
- else {
319
- this.toLightMode();
320
- }
321
- });
322
- this.window.addEventListener("resize", _event => {
323
- if (this.#resizeDebounce !== null) {
324
- return;
325
- }
326
- this.#resizeDebounce = this.window.setTimeout(() => {
327
- this.#resizeDebounce = null;
328
- }, 1000);
329
- if (this.sandboxOptions.devMode) {
330
- // biome-ignore lint/suspicious/noConsole: This is a development mode option.
331
- console.info(`CanvasSandbox: Window resized (${this.window.innerWidth.toFixed(0)}x${this.window.innerHeight.toFixed(0)}x${this.window.devicePixelRatio.toFixed(3)}). Reconfiguring application...`);
332
- }
333
- this.application.reconfigure(this.canvas);
334
- this.canvas.refreshCanvasNode();
335
- this.application.start();
336
- // In case the application doesn't maintain this state itself.
337
- this.application.paused = false;
338
- });
339
- }
340
- /**
341
- * Switch the sandbox to dark mode.
342
- */
343
- toDarkMode() {
344
- this.#reconfigureApplication({
345
- darkMode: true,
346
- });
347
- document.body.classList.add("darkMode");
348
- document.body.classList.remove("lightMode");
349
- }
350
- /**
351
- * Switch the sandbox to light mode.
352
- */
353
- toLightMode() {
354
- this.#reconfigureApplication({
355
- darkMode: false,
356
- });
357
- document.body.classList.add("lightMode");
358
- document.body.classList.remove("darkMode");
359
- }
360
- }
361
- //# sourceMappingURL=canvas-sandbox.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"canvas-sandbox.js","sourceRoot":"","sources":["../../source/graphics/canvas-sandbox.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC;A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xC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9C,CAAC;IAED;;OAEG;IACH,WAAW;QACT,IAAI,CAAC,uBAAuB,CAAC;YAC3B,QAAQ,EAAE,KAAK;SACgB,CAAC,CAAC;QACnC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QACzC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAC7C,CAAC;CACF"}
@@ -1,57 +0,0 @@
1
- /**
2
- * The base class for canvas helpers, which also defines a common interface.
3
- *
4
- * Derivates of the class might chose to leave implementations of individual methods
5
- * empty, if it fits their pattern better. For example, one canvas might wrap its
6
- * context and applications using that canvas will only interact with its interface.
7
- * Another canvas implementation might expose the context and all give the application
8
- * full control over the rendering cycle.
9
- */
10
- export declare abstract class Canvas {
11
- /**
12
- * The canvas element we're interacting with.
13
- */
14
- readonly canvasElement: HTMLCanvasElement | OffscreenCanvas;
15
- /**
16
- * Retrieves the width of the canvas.
17
- * @returns The width of the canvas.
18
- */
19
- get width(): number;
20
- /**
21
- * Retrieves the height of the canvas.
22
- * @returns The height of the canvas.
23
- */
24
- get height(): number;
25
- /**
26
- * Constructs a new canvas helper.
27
- * @param canvas - The canvas node to wrap.
28
- */
29
- constructor(canvas: HTMLCanvasElement | OffscreenCanvas);
30
- /**
31
- * Rebuild rendering pipeline in reaction to a change in the canvas.
32
- * Usually, when the canvas was resized.
33
- */
34
- abstract refreshCanvasNode(): void;
35
- /**
36
- * Handle any internal update calls that would be required to present a new frame.
37
- * Not all implementations will need this.
38
- */
39
- abstract update(): void;
40
- /**
41
- * Present a new frame.
42
- * Not all implementations will need this.
43
- */
44
- abstract render(): void;
45
- /**
46
- * Clear the canvas with the given color.
47
- * @param color - The color to clear the canvas with.
48
- */
49
- abstract clearWith(color: number): void;
50
- /**
51
- * Draw FPS information onto the canvas.
52
- * @param frameTimes - Durations of previous frame draws.
53
- * @param frameTime - The duration of the previous frame draw.
54
- * @param timeDelta - The time elapsed since the last frame.
55
- */
56
- abstract renderFpsInfo(frameTimes: Array<number>, frameTime: number, timeDelta: number): void;
57
- }