@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.
- package/package.json +5 -5
- package/lib/async/async.d.ts +0 -44
- package/lib/async/async.js +0 -76
- package/lib/async/async.js.map +0 -1
- package/lib/async/async.test.d.ts +0 -1
- package/lib/async/async.test.js +0 -26
- package/lib/async/async.test.js.map +0 -1
- package/lib/core.d.ts +0 -43
- package/lib/core.js +0 -2
- package/lib/core.js.map +0 -1
- package/lib/data/array.d.ts +0 -45
- package/lib/data/array.js +0 -70
- package/lib/data/array.js.map +0 -1
- package/lib/data/nil.d.ts +0 -92
- package/lib/data/nil.js +0 -131
- package/lib/data/nil.js.map +0 -1
- package/lib/data/random.d.ts +0 -111
- package/lib/data/random.js +0 -377
- package/lib/data/random.js.map +0 -1
- package/lib/data/string.d.ts +0 -30
- package/lib/data/string.js +0 -52
- package/lib/data/string.js.map +0 -1
- package/lib/data/tree.d.ts +0 -28
- package/lib/data/tree.js +0 -45
- package/lib/data/tree.js.map +0 -1
- package/lib/device/shake.d.ts +0 -79
- package/lib/device/shake.js +0 -135
- package/lib/device/shake.js.map +0 -1
- package/lib/dom/core.d.ts +0 -21
- package/lib/dom/core.js +0 -41
- package/lib/dom/core.js.map +0 -1
- package/lib/errors/AbstractError.d.ts +0 -29
- package/lib/errors/AbstractError.js +0 -59
- package/lib/errors/AbstractError.js.map +0 -1
- package/lib/errors/InternalError.d.ts +0 -19
- package/lib/errors/InternalError.js +0 -38
- package/lib/errors/InternalError.js.map +0 -1
- package/lib/errors/InvalidArgumentError.d.ts +0 -13
- package/lib/errors/InvalidArgumentError.js +0 -22
- package/lib/errors/InvalidArgumentError.js.map +0 -1
- package/lib/errors/InvalidOperationError.d.ts +0 -13
- package/lib/errors/InvalidOperationError.js +0 -22
- package/lib/errors/InvalidOperationError.js.map +0 -1
- package/lib/errors/NotImplementedError.d.ts +0 -12
- package/lib/errors/NotImplementedError.js +0 -21
- package/lib/errors/NotImplementedError.js.map +0 -1
- package/lib/errors/PermissionViolationError.d.ts +0 -13
- package/lib/errors/PermissionViolationError.js +0 -22
- package/lib/errors/PermissionViolationError.js.map +0 -1
- package/lib/errors/ResourceConflictError.d.ts +0 -12
- package/lib/errors/ResourceConflictError.js +0 -21
- package/lib/errors/ResourceConflictError.js.map +0 -1
- package/lib/errors/UnknownError.d.ts +0 -16
- package/lib/errors/UnknownError.js +0 -25
- package/lib/errors/UnknownError.js.map +0 -1
- package/lib/errors/console.d.ts +0 -7
- package/lib/errors/console.js +0 -13
- package/lib/errors/console.js.map +0 -1
- package/lib/errors/error-serializer.d.ts +0 -45
- package/lib/errors/error-serializer.js +0 -71
- package/lib/errors/error-serializer.js.map +0 -1
- package/lib/errors/error-serializer.test.d.ts +0 -1
- package/lib/errors/error-serializer.test.js +0 -109
- package/lib/errors/error-serializer.test.js.map +0 -1
- package/lib/errors/stream.d.ts +0 -7
- package/lib/errors/stream.js +0 -15
- package/lib/errors/stream.js.map +0 -1
- package/lib/errors/tools.d.ts +0 -10
- package/lib/errors/tools.js +0 -13
- package/lib/errors/tools.js.map +0 -1
- package/lib/events/event.d.ts +0 -9
- package/lib/events/event.js +0 -10
- package/lib/events/event.js.map +0 -1
- package/lib/format/bytes.d.ts +0 -118
- package/lib/format/bytes.js +0 -99
- package/lib/format/bytes.js.map +0 -1
- package/lib/format/bytes.test.d.ts +0 -1
- package/lib/format/bytes.test.js +0 -138
- package/lib/format/bytes.test.js.map +0 -1
- package/lib/format/count.d.ts +0 -14
- package/lib/format/count.js +0 -50
- package/lib/format/count.js.map +0 -1
- package/lib/format/count.test.d.ts +0 -1
- package/lib/format/count.test.js +0 -42
- package/lib/format/count.test.js.map +0 -1
- package/lib/format/milliseconds.d.ts +0 -100
- package/lib/format/milliseconds.js +0 -192
- package/lib/format/milliseconds.js.map +0 -1
- package/lib/format/milliseconds.test.d.ts +0 -1
- package/lib/format/milliseconds.test.js +0 -343
- package/lib/format/milliseconds.test.js.map +0 -1
- package/lib/format/string.d.ts +0 -24
- package/lib/format/string.js +0 -30
- package/lib/format/string.js.map +0 -1
- package/lib/format/string.test.d.ts +0 -1
- package/lib/format/string.test.js +0 -17
- package/lib/format/string.test.js.map +0 -1
- package/lib/graphics/canvas-sandbox-mp.d.ts +0 -258
- package/lib/graphics/canvas-sandbox-mp.js +0 -279
- package/lib/graphics/canvas-sandbox-mp.js.map +0 -1
- package/lib/graphics/canvas-sandbox.d.ts +0 -255
- package/lib/graphics/canvas-sandbox.js +0 -361
- package/lib/graphics/canvas-sandbox.js.map +0 -1
- package/lib/graphics/canvas.d.ts +0 -57
- package/lib/graphics/canvas.js +0 -37
- package/lib/graphics/canvas.js.map +0 -1
- package/lib/graphics/canvas2d-headless.d.ts +0 -85
- package/lib/graphics/canvas2d-headless.js +0 -193
- package/lib/graphics/canvas2d-headless.js.map +0 -1
- package/lib/graphics/canvas2d.d.ts +0 -162
- package/lib/graphics/canvas2d.js +0 -328
- package/lib/graphics/canvas2d.js.map +0 -1
- package/lib/graphics/canvas3d.d.ts +0 -41
- package/lib/graphics/canvas3d.js +0 -67
- package/lib/graphics/canvas3d.js.map +0 -1
- package/lib/graphics/color.d.ts +0 -22
- package/lib/graphics/color.js +0 -42
- package/lib/graphics/color.js.map +0 -1
- package/lib/graphics/core.d.ts +0 -89
- package/lib/graphics/core.js +0 -146
- package/lib/graphics/core.js.map +0 -1
- package/lib/graphics/palette-sampler.d.ts +0 -7
- package/lib/graphics/palette-sampler.js +0 -21
- package/lib/graphics/palette-sampler.js.map +0 -1
- package/lib/graphics/palette.d.ts +0 -120
- package/lib/graphics/palette.js +0 -494
- package/lib/graphics/palette.js.map +0 -1
- package/lib/graphics/palette.test.d.ts +0 -1
- package/lib/graphics/palette.test.js +0 -43
- package/lib/graphics/palette.test.js.map +0 -1
- package/lib/graphics/render-loop.d.ts +0 -80
- package/lib/graphics/render-loop.js +0 -120
- package/lib/graphics/render-loop.js.map +0 -1
- package/lib/graphics/shader.d.ts +0 -56
- package/lib/graphics/shader.js +0 -80
- package/lib/graphics/shader.js.map +0 -1
- package/lib/log/log.d.ts +0 -11
- package/lib/log/log.js +0 -2
- package/lib/log/log.js.map +0 -1
- package/lib/log/report.d.ts +0 -57
- package/lib/log/report.js +0 -88
- package/lib/log/report.js.map +0 -1
- package/lib/math/core.d.ts +0 -85
- package/lib/math/core.js +0 -125
- package/lib/math/core.js.map +0 -1
- package/lib/math/easing.d.ts +0 -204
- package/lib/math/easing.js +0 -332
- package/lib/math/easing.js.map +0 -1
- package/lib/math/euler.d.ts +0 -18
- package/lib/math/euler.js +0 -22
- package/lib/math/euler.js.map +0 -1
- package/lib/math/matrix3.d.ts +0 -217
- package/lib/math/matrix3.js +0 -508
- package/lib/math/matrix3.js.map +0 -1
- package/lib/math/quaternion.d.ts +0 -119
- package/lib/math/quaternion.js +0 -270
- package/lib/math/quaternion.js.map +0 -1
- package/lib/math/transformation.d.ts +0 -26
- package/lib/math/transformation.js +0 -31
- package/lib/math/transformation.js.map +0 -1
- package/lib/math/vector2.d.ts +0 -202
- package/lib/math/vector2.js +0 -310
- package/lib/math/vector2.js.map +0 -1
- package/lib/math/vector3.d.ts +0 -293
- package/lib/math/vector3.js +0 -492
- package/lib/math/vector3.js.map +0 -1
- package/lib/math/vector4.d.ts +0 -182
- package/lib/math/vector4.js +0 -302
- package/lib/math/vector4.js.map +0 -1
- package/lib/measurement/performance.d.ts +0 -13
- package/lib/measurement/performance.js +0 -19
- package/lib/measurement/performance.js.map +0 -1
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
import { ConstructorOf } from "../core.js";
|
|
2
|
-
import { Random } from "../data/random.js";
|
|
3
|
-
import { Canvas } from "./canvas.js";
|
|
4
|
-
import { CanvasSandboxApplication, CanvasSandboxExpectedOptions } from "./canvas-sandbox.js";
|
|
5
|
-
import { Canvas2DHeadless } from "./canvas2d-headless.js";
|
|
6
|
-
import { RenderLoop } from "./render-loop.js";
|
|
7
|
-
/**
|
|
8
|
-
* Sent from the host when a worker should reconfigure itself.
|
|
9
|
-
*/
|
|
10
|
-
export interface CanvasWorkerMessageReconfigure<TApplicationOptions extends CanvasSandboxExpectedOptions> {
|
|
11
|
-
/**
|
|
12
|
-
* Type identifier of this message.
|
|
13
|
-
*/
|
|
14
|
-
type: "reconfigure";
|
|
15
|
-
/**
|
|
16
|
-
* New ID for the worker.
|
|
17
|
-
*/
|
|
18
|
-
id: string;
|
|
19
|
-
/**
|
|
20
|
-
* New canvas for the worker to use.
|
|
21
|
-
*/
|
|
22
|
-
canvas: OffscreenCanvas;
|
|
23
|
-
/**
|
|
24
|
-
* New application options to use.
|
|
25
|
-
*/
|
|
26
|
-
options: TApplicationOptions;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Sent from the host when a worker should start its rendering kernel.
|
|
30
|
-
*/
|
|
31
|
-
export interface CanvasWorkerMessageStart<TApplicationOptions extends CanvasSandboxExpectedOptions> {
|
|
32
|
-
/**
|
|
33
|
-
* Type identifier of this message.
|
|
34
|
-
*/
|
|
35
|
-
type: "start";
|
|
36
|
-
/**
|
|
37
|
-
* New application options to use before starting.
|
|
38
|
-
*/
|
|
39
|
-
options: TApplicationOptions;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Sent from the host when a worker should un/pause rendering.
|
|
43
|
-
*/
|
|
44
|
-
export interface CanvasWorkerMessagePause {
|
|
45
|
-
/**
|
|
46
|
-
* Type identifier of this message.
|
|
47
|
-
*/
|
|
48
|
-
type: "pause";
|
|
49
|
-
/**
|
|
50
|
-
* Should the worker pause (`true`) or unpause (`false`)?
|
|
51
|
-
*/
|
|
52
|
-
pause: boolean;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Sent from a worker when it finished rendering the current scene.
|
|
56
|
-
*/
|
|
57
|
-
export interface CanvasWorkerSceneFinishMessage {
|
|
58
|
-
/**
|
|
59
|
-
* Type identifier of this message.
|
|
60
|
-
*/
|
|
61
|
-
type: "sceneFinish";
|
|
62
|
-
/**
|
|
63
|
-
* Point in time when the scene finished rendering.
|
|
64
|
-
*/
|
|
65
|
-
timestamp: number;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* The messages that are passed between the canvas sandbox host and its workers.
|
|
69
|
-
*/
|
|
70
|
-
export type CanvasWorkerMessage<TApplicationOptions extends CanvasSandboxExpectedOptions> = CanvasWorkerMessageReconfigure<TApplicationOptions> | CanvasWorkerMessageStart<TApplicationOptions> | CanvasWorkerSceneFinishMessage | CanvasWorkerMessagePause | {
|
|
71
|
-
type: string;
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* The canvas worker handles a web worker instance in the canvas sandbox host.
|
|
75
|
-
*
|
|
76
|
-
* It provides a thin abstraction over the web worker IPC messaging.
|
|
77
|
-
*/
|
|
78
|
-
export declare class CanvasWorker<TApplicationOptions extends CanvasSandboxExpectedOptions> extends EventTarget {
|
|
79
|
-
/**
|
|
80
|
-
* The ID of this worker.
|
|
81
|
-
*/
|
|
82
|
-
id: string;
|
|
83
|
-
/**
|
|
84
|
-
* The canvas element that this worker should handle.
|
|
85
|
-
* This is purely for informational purposes on the host side.
|
|
86
|
-
*/
|
|
87
|
-
canvas: HTMLCanvasElement | undefined;
|
|
88
|
-
/**
|
|
89
|
-
* The offscreen canvas that we created for this worker.
|
|
90
|
-
*/
|
|
91
|
-
canvasOffscreen: OffscreenCanvas | undefined;
|
|
92
|
-
/**
|
|
93
|
-
* The application options for the worker.
|
|
94
|
-
*
|
|
95
|
-
* These are usually very similar, if not identical, to the host application options.
|
|
96
|
-
* Workers just usually get a different viewport assigned to them, correlating with the
|
|
97
|
-
* canvas element in the DOM that they draw to.
|
|
98
|
-
*/
|
|
99
|
-
readonly options: TApplicationOptions;
|
|
100
|
-
/**
|
|
101
|
-
* The web worker instance itself.
|
|
102
|
-
*/
|
|
103
|
-
readonly workerInstance: Worker;
|
|
104
|
-
/**
|
|
105
|
-
* Constructs a new canvas worker.
|
|
106
|
-
* @param id - The ID of the worker.
|
|
107
|
-
* @param source - The code that the worker should run. It's common to use a hybrid code module
|
|
108
|
-
* for as the sandbox application. This means the code of the host and the workers is identical,
|
|
109
|
-
* they just construct different code path on load/init. In such case, the source can simply be
|
|
110
|
-
* `new URL(import.meta.url)`.
|
|
111
|
-
* @param canvas - The canvas element to draw to.
|
|
112
|
-
* @param options - The application options.
|
|
113
|
-
*/
|
|
114
|
-
constructor(id: string, source: URL, canvas: HTMLCanvasElement, options: TApplicationOptions);
|
|
115
|
-
/**
|
|
116
|
-
* Posts a message to the worker.
|
|
117
|
-
*
|
|
118
|
-
* As messages to workers are _posted_ instead of _sent_, there is no delivery feedback.
|
|
119
|
-
* @param message - The message to post.
|
|
120
|
-
* @param transfer - The transferables to transfer to the worker with this message.
|
|
121
|
-
*/
|
|
122
|
-
postMessage(message: CanvasWorkerMessage<TApplicationOptions>, transfer?: Array<Transferable>): void;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* The canvas worker instance handles a web worker instance in worker instance itself, and
|
|
126
|
-
* handles a feedback channel to the host application.
|
|
127
|
-
*
|
|
128
|
-
* It provides a thin abstraction over the web worker IPC messaging.
|
|
129
|
-
*
|
|
130
|
-
* ```ts
|
|
131
|
-
* // Construct inside of worker from global scope and rendering kernel class.
|
|
132
|
-
* const worker = new CanvasWorkerInstance(self, RenderKernel);
|
|
133
|
-
* // Listen to custom events (messages) you send from the host.
|
|
134
|
-
* worker.addEventListener("fade", () => worker.renderKernel?.fadeOut());
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
export declare class CanvasWorkerInstance<TCanvas extends Canvas2DHeadless, TApplicationOptions extends CanvasSandboxExpectedOptions, TKernel extends CanvasSandboxApplication<TCanvas, TApplicationOptions>> extends EventTarget {
|
|
138
|
-
#private;
|
|
139
|
-
/**
|
|
140
|
-
* The global scope of the worker.
|
|
141
|
-
*/
|
|
142
|
-
readonly self: typeof globalThis;
|
|
143
|
-
/**
|
|
144
|
-
* The ID of the worker.
|
|
145
|
-
*/
|
|
146
|
-
id: string;
|
|
147
|
-
/**
|
|
148
|
-
* The rendering kernel, which produces frames, and renders them to our
|
|
149
|
-
* offscreen canvas.
|
|
150
|
-
*/
|
|
151
|
-
renderKernel: TKernel | undefined;
|
|
152
|
-
/**
|
|
153
|
-
* The canvas element we're presenting to.
|
|
154
|
-
*/
|
|
155
|
-
canvas: TCanvas | undefined;
|
|
156
|
-
/**
|
|
157
|
-
* The offscreen canvas we're rendering to.
|
|
158
|
-
*/
|
|
159
|
-
offscreenCanvas: OffscreenCanvas | undefined;
|
|
160
|
-
/**
|
|
161
|
-
* Our rendering context to draw to.
|
|
162
|
-
*/
|
|
163
|
-
offscreenContext: OffscreenCanvasRenderingContext2D | undefined;
|
|
164
|
-
/**
|
|
165
|
-
* Manages frame rendering timing.
|
|
166
|
-
*/
|
|
167
|
-
renderLoop: RenderLoop | undefined;
|
|
168
|
-
/**
|
|
169
|
-
* Constructs a new canvas worker instance.
|
|
170
|
-
*
|
|
171
|
-
* Note that many properties are only initialized after the host sent an
|
|
172
|
-
* initialization message.
|
|
173
|
-
* @param self - Our global worker scope.
|
|
174
|
-
* @param Kernel - The rendering kernel class to construct.
|
|
175
|
-
*/
|
|
176
|
-
constructor(self: typeof globalThis, Kernel: ConstructorOf<TKernel>);
|
|
177
|
-
/**
|
|
178
|
-
* Render a frame.
|
|
179
|
-
* @param delta - How many milliseconds have passed since the last invocation?
|
|
180
|
-
*/
|
|
181
|
-
render: (delta: number) => void;
|
|
182
|
-
/**
|
|
183
|
-
* Reconfigure the worker.
|
|
184
|
-
* @param id - New ID for this worker.
|
|
185
|
-
* @param offscreenCanvas - New canvas to render to.
|
|
186
|
-
* @param options - New application options.
|
|
187
|
-
*/
|
|
188
|
-
reconfigure(id: string, offscreenCanvas: OffscreenCanvas, options: TApplicationOptions): void;
|
|
189
|
-
/**
|
|
190
|
-
* Start rendering.
|
|
191
|
-
* @param options - New application options.
|
|
192
|
-
*/
|
|
193
|
-
start(options: TApplicationOptions): void;
|
|
194
|
-
/**
|
|
195
|
-
* Posts a message to the host.
|
|
196
|
-
* @param message - The message to post.
|
|
197
|
-
*/
|
|
198
|
-
postMessage(message: CanvasWorkerMessage<TApplicationOptions>): void;
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Base class for multi-process canvas sandbox applications.
|
|
202
|
-
*
|
|
203
|
-
* Helps in orchestrating the workers.
|
|
204
|
-
*/
|
|
205
|
-
export declare abstract class CanvasSandboxHostApplication<TCanvas extends Canvas, TApplicationOptions extends CanvasSandboxExpectedOptions> implements CanvasSandboxApplication<TCanvas, TApplicationOptions> {
|
|
206
|
-
/**
|
|
207
|
-
* Usually a hidden canvas that serves as a proxy for canvas sandbox events.
|
|
208
|
-
*/
|
|
209
|
-
canvas: TCanvas;
|
|
210
|
-
/**
|
|
211
|
-
* Application options.
|
|
212
|
-
*/
|
|
213
|
-
options: TApplicationOptions;
|
|
214
|
-
/**
|
|
215
|
-
* PRNG instance for the application.
|
|
216
|
-
*/
|
|
217
|
-
random: Random;
|
|
218
|
-
/**
|
|
219
|
-
* Is the application running right now?
|
|
220
|
-
*/
|
|
221
|
-
paused: boolean;
|
|
222
|
-
/**
|
|
223
|
-
* Web worker instances.
|
|
224
|
-
*/
|
|
225
|
-
readonly workers: CanvasWorker<TApplicationOptions>[];
|
|
226
|
-
/**
|
|
227
|
-
* Construct a new host application.
|
|
228
|
-
* @param canvas - Usually a hidden canvas in the DOM to serve as an event proxy.
|
|
229
|
-
* @param options - Application options.
|
|
230
|
-
*/
|
|
231
|
-
constructor(canvas: TCanvas, options: TApplicationOptions);
|
|
232
|
-
/**
|
|
233
|
-
* Reconfigure the application with the given options and restart it.
|
|
234
|
-
* @param canvas - The canvas to use.
|
|
235
|
-
* @param options - The new options to use.
|
|
236
|
-
*/
|
|
237
|
-
abstract reconfigure(canvas: TCanvas, options?: Partial<TApplicationOptions>): void;
|
|
238
|
-
/**
|
|
239
|
-
* Request the application to draw a new frame.
|
|
240
|
-
* This call has no effect in a host application, as all rendering happens in the workers.
|
|
241
|
-
* @param _delta - How many milliseconds passed since the last frame draw.
|
|
242
|
-
* @param _timestamp - The current timestamp.
|
|
243
|
-
*/
|
|
244
|
-
onDraw(_delta: number, _timestamp: number): void;
|
|
245
|
-
/**
|
|
246
|
-
* Pauses all workers.
|
|
247
|
-
*/
|
|
248
|
-
pause(): void;
|
|
249
|
-
/**
|
|
250
|
-
* Start the application.
|
|
251
|
-
*/
|
|
252
|
-
start(): void;
|
|
253
|
-
/**
|
|
254
|
-
* Posts a message to all workers.
|
|
255
|
-
* @param message - Message to post to all workers.
|
|
256
|
-
*/
|
|
257
|
-
postMessageAll(message: CanvasWorkerMessage<TApplicationOptions>): void;
|
|
258
|
-
}
|
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import { isNil, mustExist } from "../data/nil.js";
|
|
2
|
-
import { Random } from "../data/random.js";
|
|
3
|
-
import { Canvas2DHeadless } from "./canvas2d-headless.js";
|
|
4
|
-
import { RenderLoop } from "./render-loop.js";
|
|
5
|
-
/**
|
|
6
|
-
* The canvas worker handles a web worker instance in the canvas sandbox host.
|
|
7
|
-
*
|
|
8
|
-
* It provides a thin abstraction over the web worker IPC messaging.
|
|
9
|
-
*/
|
|
10
|
-
export class CanvasWorker extends EventTarget {
|
|
11
|
-
/**
|
|
12
|
-
* The ID of this worker.
|
|
13
|
-
*/
|
|
14
|
-
id;
|
|
15
|
-
/**
|
|
16
|
-
* The canvas element that this worker should handle.
|
|
17
|
-
* This is purely for informational purposes on the host side.
|
|
18
|
-
*/
|
|
19
|
-
canvas;
|
|
20
|
-
/**
|
|
21
|
-
* The offscreen canvas that we created for this worker.
|
|
22
|
-
*/
|
|
23
|
-
canvasOffscreen;
|
|
24
|
-
/**
|
|
25
|
-
* The application options for the worker.
|
|
26
|
-
*
|
|
27
|
-
* These are usually very similar, if not identical, to the host application options.
|
|
28
|
-
* Workers just usually get a different viewport assigned to them, correlating with the
|
|
29
|
-
* canvas element in the DOM that they draw to.
|
|
30
|
-
*/
|
|
31
|
-
options;
|
|
32
|
-
/**
|
|
33
|
-
* The web worker instance itself.
|
|
34
|
-
*/
|
|
35
|
-
workerInstance;
|
|
36
|
-
/**
|
|
37
|
-
* Constructs a new canvas worker.
|
|
38
|
-
* @param id - The ID of the worker.
|
|
39
|
-
* @param source - The code that the worker should run. It's common to use a hybrid code module
|
|
40
|
-
* for as the sandbox application. This means the code of the host and the workers is identical,
|
|
41
|
-
* they just construct different code path on load/init. In such case, the source can simply be
|
|
42
|
-
* `new URL(import.meta.url)`.
|
|
43
|
-
* @param canvas - The canvas element to draw to.
|
|
44
|
-
* @param options - The application options.
|
|
45
|
-
*/
|
|
46
|
-
constructor(id, source, canvas, options) {
|
|
47
|
-
super();
|
|
48
|
-
this.id = id;
|
|
49
|
-
this.canvas = canvas;
|
|
50
|
-
this.options = options;
|
|
51
|
-
this.workerInstance = new Worker(source, {
|
|
52
|
-
type: "module",
|
|
53
|
-
});
|
|
54
|
-
// Translate messages to events.
|
|
55
|
-
this.workerInstance.onmessage = (message) => this.dispatchEvent(new CustomEvent(message.data.type));
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Posts a message to the worker.
|
|
59
|
-
*
|
|
60
|
-
* As messages to workers are _posted_ instead of _sent_, there is no delivery feedback.
|
|
61
|
-
* @param message - The message to post.
|
|
62
|
-
* @param transfer - The transferables to transfer to the worker with this message.
|
|
63
|
-
*/
|
|
64
|
-
postMessage(message, transfer) {
|
|
65
|
-
this.workerInstance.postMessage(message, transfer ?? []);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* The canvas worker instance handles a web worker instance in worker instance itself, and
|
|
70
|
-
* handles a feedback channel to the host application.
|
|
71
|
-
*
|
|
72
|
-
* It provides a thin abstraction over the web worker IPC messaging.
|
|
73
|
-
*
|
|
74
|
-
* ```ts
|
|
75
|
-
* // Construct inside of worker from global scope and rendering kernel class.
|
|
76
|
-
* const worker = new CanvasWorkerInstance(self, RenderKernel);
|
|
77
|
-
* // Listen to custom events (messages) you send from the host.
|
|
78
|
-
* worker.addEventListener("fade", () => worker.renderKernel?.fadeOut());
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
export class CanvasWorkerInstance extends EventTarget {
|
|
82
|
-
/**
|
|
83
|
-
* The global scope of the worker.
|
|
84
|
-
*/
|
|
85
|
-
self;
|
|
86
|
-
/**
|
|
87
|
-
* The ID of the worker.
|
|
88
|
-
*/
|
|
89
|
-
id;
|
|
90
|
-
/**
|
|
91
|
-
* The rendering kernel, which produces frames, and renders them to our
|
|
92
|
-
* offscreen canvas.
|
|
93
|
-
*/
|
|
94
|
-
renderKernel;
|
|
95
|
-
/**
|
|
96
|
-
* The canvas element we're presenting to.
|
|
97
|
-
*/
|
|
98
|
-
canvas;
|
|
99
|
-
/**
|
|
100
|
-
* The offscreen canvas we're rendering to.
|
|
101
|
-
*/
|
|
102
|
-
offscreenCanvas;
|
|
103
|
-
/**
|
|
104
|
-
* Our rendering context to draw to.
|
|
105
|
-
*/
|
|
106
|
-
offscreenContext;
|
|
107
|
-
/**
|
|
108
|
-
* Manages frame rendering timing.
|
|
109
|
-
*/
|
|
110
|
-
renderLoop;
|
|
111
|
-
/**
|
|
112
|
-
* Constructor of our rendering kernel.
|
|
113
|
-
*/
|
|
114
|
-
#Kernel;
|
|
115
|
-
/**
|
|
116
|
-
* Constructs a new canvas worker instance.
|
|
117
|
-
*
|
|
118
|
-
* Note that many properties are only initialized after the host sent an
|
|
119
|
-
* initialization message.
|
|
120
|
-
* @param self - Our global worker scope.
|
|
121
|
-
* @param Kernel - The rendering kernel class to construct.
|
|
122
|
-
*/
|
|
123
|
-
constructor(self, Kernel) {
|
|
124
|
-
super();
|
|
125
|
-
this.self = self;
|
|
126
|
-
this.id = "<unassigned>";
|
|
127
|
-
this.#Kernel = Kernel;
|
|
128
|
-
self.onmessage = (message) => {
|
|
129
|
-
const event = new CustomEvent(message.data.type);
|
|
130
|
-
this.dispatchEvent(event);
|
|
131
|
-
if (event.defaultPrevented) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (message.data.type === "start") {
|
|
135
|
-
const startMessage = message.data;
|
|
136
|
-
this.start(startMessage.options);
|
|
137
|
-
}
|
|
138
|
-
else if (message.data.type === "pause") {
|
|
139
|
-
const pauseMessage = message.data;
|
|
140
|
-
this.renderKernel?.pause(pauseMessage.pause);
|
|
141
|
-
}
|
|
142
|
-
else if (message.data.type === "reconfigure") {
|
|
143
|
-
const reconfigureMessage = message.data;
|
|
144
|
-
this.reconfigure(reconfigureMessage.id, reconfigureMessage.canvas, reconfigureMessage.options);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Render a frame.
|
|
150
|
-
* @param delta - How many milliseconds have passed since the last invocation?
|
|
151
|
-
*/
|
|
152
|
-
render = (delta) => {
|
|
153
|
-
this.renderKernel?.onDraw(delta, new Date().valueOf());
|
|
154
|
-
};
|
|
155
|
-
/**
|
|
156
|
-
* Reconfigure the worker.
|
|
157
|
-
* @param id - New ID for this worker.
|
|
158
|
-
* @param offscreenCanvas - New canvas to render to.
|
|
159
|
-
* @param options - New application options.
|
|
160
|
-
*/
|
|
161
|
-
reconfigure(id, offscreenCanvas, options) {
|
|
162
|
-
this.renderLoop?.block();
|
|
163
|
-
this.id = id;
|
|
164
|
-
this.offscreenCanvas = offscreenCanvas;
|
|
165
|
-
this.offscreenContext = mustExist(this.offscreenCanvas.getContext("2d"));
|
|
166
|
-
this.canvas = new Canvas2DHeadless(this.offscreenCanvas, this.offscreenContext);
|
|
167
|
-
this.renderLoop = new RenderLoop(this.render, this.canvas);
|
|
168
|
-
if (isNil(this.renderKernel)) {
|
|
169
|
-
this.renderKernel = new this.#Kernel(this, this.canvas, options);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
this.renderKernel.reconfigure(this.canvas, options);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Start rendering.
|
|
177
|
-
* @param options - New application options.
|
|
178
|
-
*/
|
|
179
|
-
start(options) {
|
|
180
|
-
if (!this.renderKernel) {
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
this.renderKernel.random = new Random(options.seed);
|
|
184
|
-
this.renderKernel.start(options);
|
|
185
|
-
this.renderLoop?.unblock();
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Posts a message to the host.
|
|
189
|
-
* @param message - The message to post.
|
|
190
|
-
*/
|
|
191
|
-
postMessage(message) {
|
|
192
|
-
this.self.postMessage(message);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Base class for multi-process canvas sandbox applications.
|
|
197
|
-
*
|
|
198
|
-
* Helps in orchestrating the workers.
|
|
199
|
-
*/
|
|
200
|
-
export class CanvasSandboxHostApplication {
|
|
201
|
-
/**
|
|
202
|
-
* Usually a hidden canvas that serves as a proxy for canvas sandbox events.
|
|
203
|
-
*/
|
|
204
|
-
canvas;
|
|
205
|
-
/**
|
|
206
|
-
* Application options.
|
|
207
|
-
*/
|
|
208
|
-
options;
|
|
209
|
-
/**
|
|
210
|
-
* PRNG instance for the application.
|
|
211
|
-
*/
|
|
212
|
-
random;
|
|
213
|
-
/**
|
|
214
|
-
* Is the application running right now?
|
|
215
|
-
*/
|
|
216
|
-
paused = false;
|
|
217
|
-
/**
|
|
218
|
-
* Web worker instances.
|
|
219
|
-
*/
|
|
220
|
-
workers = new Array();
|
|
221
|
-
/**
|
|
222
|
-
* Construct a new host application.
|
|
223
|
-
* @param canvas - Usually a hidden canvas in the DOM to serve as an event proxy.
|
|
224
|
-
* @param options - Application options.
|
|
225
|
-
*/
|
|
226
|
-
constructor(canvas, options) {
|
|
227
|
-
this.options = options;
|
|
228
|
-
this.canvas = canvas;
|
|
229
|
-
this.random = new Random(options.seed);
|
|
230
|
-
this.reconfigure(canvas, options);
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Request the application to draw a new frame.
|
|
234
|
-
* This call has no effect in a host application, as all rendering happens in the workers.
|
|
235
|
-
* @param _delta - How many milliseconds passed since the last frame draw.
|
|
236
|
-
* @param _timestamp - The current timestamp.
|
|
237
|
-
*/
|
|
238
|
-
onDraw(_delta, _timestamp) {
|
|
239
|
-
// Draws happen in workers.
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Pauses all workers.
|
|
243
|
-
*/
|
|
244
|
-
pause() {
|
|
245
|
-
this.paused = !this.paused;
|
|
246
|
-
for (const worker of this.workers) {
|
|
247
|
-
worker.postMessage({
|
|
248
|
-
pause: this.paused,
|
|
249
|
-
type: "pause",
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Start the application.
|
|
255
|
-
*/
|
|
256
|
-
start() {
|
|
257
|
-
this.paused = false;
|
|
258
|
-
for (const worker of this.workers) {
|
|
259
|
-
worker.postMessage({
|
|
260
|
-
options: {
|
|
261
|
-
...this.options,
|
|
262
|
-
seed: this.random.seed,
|
|
263
|
-
viewport: worker.options.viewport,
|
|
264
|
-
},
|
|
265
|
-
type: "start",
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Posts a message to all workers.
|
|
271
|
-
* @param message - Message to post to all workers.
|
|
272
|
-
*/
|
|
273
|
-
postMessageAll(message) {
|
|
274
|
-
for (const worker of this.workers) {
|
|
275
|
-
worker.postMessage(message);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
//# sourceMappingURL=canvas-sandbox-mp.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"canvas-sandbox-mp.js","sourceRoot":"","sources":["../../source/graphics/canvas-sandbox-mp.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAA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