@n3rdw1z4rd/core 0.18.5 → 0.19.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 (109) hide show
  1. package/README.md +72 -10
  2. package/dist/astar.d.ts +44 -0
  3. package/dist/astar.js +118 -0
  4. package/dist/canvas-renderer.d.ts +157 -0
  5. package/dist/canvas-renderer.js +207 -0
  6. package/dist/clock.d.ts +69 -0
  7. package/dist/clock.js +108 -0
  8. package/dist/color-palette.d.ts +25 -0
  9. package/dist/color-palette.js +112 -0
  10. package/dist/color.d.ts +90 -0
  11. package/dist/color.js +224 -0
  12. package/dist/css/main.css +1 -3
  13. package/dist/disposable.d.ts +27 -0
  14. package/dist/disposable.js +32 -0
  15. package/dist/dungeon-generator.d.ts +142 -0
  16. package/dist/dungeon-generator.js +288 -0
  17. package/dist/ecs.d.ts +87 -0
  18. package/dist/ecs.js +101 -0
  19. package/dist/emitter.d.ts +40 -0
  20. package/dist/emitter.js +54 -0
  21. package/dist/event-queue.d.ts +29 -0
  22. package/dist/event-queue.js +40 -0
  23. package/dist/index.cjs +3820 -0
  24. package/dist/index.d.cts +2657 -0
  25. package/dist/index.d.ts +35 -873
  26. package/dist/index.js +34 -3981
  27. package/dist/input/action-map.d.ts +79 -0
  28. package/dist/input/action-map.js +131 -0
  29. package/dist/input/bindings.d.ts +273 -0
  30. package/dist/input/bindings.js +250 -0
  31. package/dist/input/gestures.d.ts +108 -0
  32. package/dist/input/gestures.js +159 -0
  33. package/dist/input/index.d.ts +5 -0
  34. package/dist/input/index.js +5 -0
  35. package/dist/input/input.d.ts +297 -0
  36. package/dist/input/input.js +695 -0
  37. package/dist/log.d.ts +8 -0
  38. package/dist/log.js +9 -0
  39. package/dist/map2d.d.ts +83 -0
  40. package/dist/map2d.js +120 -0
  41. package/dist/map3d-CHDJiq5T.d.cts +236 -0
  42. package/dist/map3d.d.ts +94 -0
  43. package/dist/map3d.js +148 -0
  44. package/dist/math.d.ts +176 -0
  45. package/dist/math.js +206 -0
  46. package/dist/misc.d.ts +46 -0
  47. package/dist/misc.js +71 -0
  48. package/dist/observable.d.ts +43 -0
  49. package/dist/observable.js +53 -0
  50. package/dist/particle-renderer.d.ts +47 -0
  51. package/dist/particle-renderer.js +82 -0
  52. package/dist/particle-system-2d.d.ts +60 -0
  53. package/dist/particle-system-2d.js +147 -0
  54. package/dist/random.d.ts +46 -0
  55. package/dist/random.js +74 -0
  56. package/dist/rectangle.d.ts +44 -0
  57. package/dist/rectangle.js +69 -0
  58. package/dist/rng.d.ts +72 -0
  59. package/dist/rng.js +152 -0
  60. package/dist/signal.d.ts +34 -0
  61. package/dist/signal.js +41 -0
  62. package/dist/spatial-partition-2d.d.ts +42 -0
  63. package/dist/spatial-partition-2d.js +52 -0
  64. package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
  65. package/dist/spatial-partition-3d.d.ts +44 -0
  66. package/dist/spatial-partition-3d.js +57 -0
  67. package/dist/three/asteroid-mesh.d.ts +41 -0
  68. package/dist/three/asteroid-mesh.js +44 -0
  69. package/dist/three/camera-rig.d.ts +140 -0
  70. package/dist/three/camera-rig.js +197 -0
  71. package/dist/three/cube-sphere-geometry.d.ts +55 -0
  72. package/dist/three/cube-sphere-geometry.js +239 -0
  73. package/dist/three/ecs.d.ts +75 -0
  74. package/dist/three/ecs.js +64 -0
  75. package/dist/three/index.d.ts +12 -0
  76. package/dist/three/index.js +12 -0
  77. package/dist/three/particle-system-3d.d.ts +53 -0
  78. package/dist/three/particle-system-3d.js +224 -0
  79. package/dist/three/renderer.d.ts +38 -0
  80. package/dist/three/renderer.js +57 -0
  81. package/dist/three/starfield.d.ts +23 -0
  82. package/dist/three/starfield.js +64 -0
  83. package/dist/three/texture-atlas.d.ts +76 -0
  84. package/dist/three/texture-atlas.js +84 -0
  85. package/dist/three/texture-utils.d.ts +31 -0
  86. package/dist/three/texture-utils.js +44 -0
  87. package/dist/three/voxel-geometry.d.ts +72 -0
  88. package/dist/three/voxel-geometry.js +231 -0
  89. package/dist/three/voxel-mesh.d.ts +67 -0
  90. package/dist/three/voxel-mesh.js +71 -0
  91. package/dist/three.cjs +1307 -0
  92. package/dist/three.d.cts +655 -0
  93. package/dist/tilemap.d.ts +17 -0
  94. package/dist/tilemap.js +26 -0
  95. package/dist/types.d.ts +34 -0
  96. package/dist/types.js +0 -0
  97. package/dist/vector.d.ts +78 -0
  98. package/dist/vector.js +169 -0
  99. package/dist/webgl-renderer.d.ts +89 -0
  100. package/dist/webgl-renderer.js +140 -0
  101. package/dist/webgl.d.ts +357 -0
  102. package/dist/webgl.js +628 -0
  103. package/dist/worker-interface.d.ts +30 -0
  104. package/dist/worker-interface.js +39 -0
  105. package/package.json +55 -13
  106. package/dist/css/index.ts +0 -1
  107. package/dist/index.css +0 -104
  108. package/dist/index.d.mts +0 -873
  109. package/dist/index.mjs +0 -3977
package/dist/math.d.ts ADDED
@@ -0,0 +1,176 @@
1
+ //#region src/math.d.ts
2
+ /**
3
+ * π, half a turn in radians.
4
+ * @category Math & Random
5
+ */
6
+ export declare const PI: number;
7
+ /**
8
+ * 2π, a full turn in radians.
9
+ * @category Math & Random
10
+ */
11
+ export declare const TAU: number;
12
+ /**
13
+ * π / 2, a quarter turn in radians.
14
+ * @category Math & Random
15
+ */
16
+ export declare const HALF_PI: number;
17
+ /**
18
+ * π / 4, an eighth of a turn in radians.
19
+ * @category Math & Random
20
+ */
21
+ export declare const QUARTER_PI: number;
22
+ /**
23
+ * Euler's number, the base of natural logarithms.
24
+ * @category Math & Random
25
+ */
26
+ export declare const E: number;
27
+ /**
28
+ * Multiply degrees by this to get radians.
29
+ * @category Math & Random
30
+ */
31
+ export declare const DEG2RAD: number;
32
+ /**
33
+ * Multiply radians by this to get degrees.
34
+ * @category Math & Random
35
+ */
36
+ export declare const RAD2DEG: number;
37
+ /**
38
+ * Natural logarithm of 2.
39
+ * @category Math & Random
40
+ */
41
+ export declare const LN2: number;
42
+ /**
43
+ * Natural logarithm of 10.
44
+ * @category Math & Random
45
+ */
46
+ export declare const LN10: number;
47
+ /**
48
+ * Base-2 logarithm of e.
49
+ * @category Math & Random
50
+ */
51
+ export declare const LOG2E: number;
52
+ /**
53
+ * Base-10 logarithm of e.
54
+ * @category Math & Random
55
+ */
56
+ export declare const LOG10E: number;
57
+ /**
58
+ * Square root of 2.
59
+ * @category Math & Random
60
+ */
61
+ export declare const SQRT2: number;
62
+ /**
63
+ * Square root of 1/2.
64
+ * @category Math & Random
65
+ */
66
+ export declare const SQRT1_2: number;
67
+ /**
68
+ * `Number.EPSILON` (about 2.2e-16), the default tolerance of {@link nearlyEqual}.
69
+ * @category Math & Random
70
+ */
71
+ export declare const EPSILON: number;
72
+ /**
73
+ * Limits `value` to the inclusive range [`minValue`, `maxValue`].
74
+ * @category Math & Random
75
+ */
76
+ export declare const clamp: (value: number, minValue: number, maxValue: number) => number;
77
+ /**
78
+ * Clamps `value` to [0, 1].
79
+ * @category Math & Random
80
+ */
81
+ export declare const saturate: (value: number) => number;
82
+ /**
83
+ * Linearly interpolates from `start` (t = 0) to `end` (t = 1). `t` is not clamped, so values
84
+ * outside [0, 1] extrapolate.
85
+ * @category Math & Random
86
+ */
87
+ export declare const lerp: (start: number, end: number, t: number) => number;
88
+ /**
89
+ * The inverse of {@link lerp}: returns the `t` at which `value` lies between `start` and `end`
90
+ * (0 at `start`, 1 at `end`, unclamped). Returns 0 when `start === end`.
91
+ * @category Math & Random
92
+ */
93
+ export declare const inverseLerp: (start: number, end: number, value: number) => number;
94
+ /**
95
+ * Hermite smoothstep, as in GLSL: 0 at or below `edge0`, 1 at or above `edge1`, with a smooth
96
+ * S-curve (3t² - 2t³) in between. With `edge0 === edge1` it is a step: 0 below the edge, 1 at or above.
97
+ * @category Math & Random
98
+ */
99
+ export declare const smoothstep: (edge0: number, edge1: number, value: number) => number;
100
+ /**
101
+ * Fractional part of `value`, always in [0, 1) (e.g. `fract(-0.25)` is 0.75).
102
+ * @category Math & Random
103
+ */
104
+ export declare const fract: (value: number) => number;
105
+ /**
106
+ * Modulo whose result takes the sign of `divisor` (so `mod(-1, 4)` is 3), unlike JavaScript's `%`.
107
+ * @category Math & Random
108
+ */
109
+ export declare const mod: (value: number, divisor: number) => number;
110
+ /**
111
+ * Wraps `value` into the half-open range [`minValue`, `maxValue`), e.g. `wrap(370, 0, 360)` is 10.
112
+ * An empty range (`minValue === maxValue`) returns `minValue`.
113
+ * @category Math & Random
114
+ */
115
+ export declare const wrap: (value: number, minValue: number, maxValue: number) => number;
116
+ /**
117
+ * Converts degrees to radians.
118
+ * @category Math & Random
119
+ */
120
+ export declare const radians: (degrees: number) => number;
121
+ /**
122
+ * Converts radians to degrees.
123
+ * @category Math & Random
124
+ */
125
+ export declare const degrees: (radians: number) => number;
126
+ /**
127
+ * Wraps an angle in radians into [-π, π).
128
+ * @category Math & Random
129
+ */
130
+ export declare const normalizeRadians: (radians: number) => number;
131
+ /**
132
+ * Euclidean distance between (`x1`, `y1`) and (`x2`, `y2`).
133
+ * @category Math & Random
134
+ */
135
+ export declare const distance2d: (x1: number, y1: number, x2: number, y2: number) => number;
136
+ /**
137
+ * Euclidean distance between (`x1`, `y1`, `z1`) and (`x2`, `y2`, `z2`).
138
+ * @category Math & Random
139
+ */
140
+ export declare const distance3d: (x1: number, y1: number, z1: number, x2: number, y2: number, z2: number) => number;
141
+ /**
142
+ * Squared 2D Euclidean distance; cheaper than {@link distance2d} when only comparing distances.
143
+ * @category Math & Random
144
+ */
145
+ export declare const squaredDistance: (x1: number, y1: number, x2: number, y2: number) => number;
146
+ /**
147
+ * 2D Manhattan (taxicab) distance: `|dx| + |dy|`.
148
+ * @category Math & Random
149
+ */
150
+ export declare const manhattanDistance: (x1: number, y1: number, x2: number, y2: number) => number;
151
+ /**
152
+ * True when `a` and `b` differ by at most `epsilon` (default {@link EPSILON}), scaled by the larger
153
+ * magnitude once that exceeds 1: absolute near zero, relative for large values.
154
+ * @category Math & Random
155
+ */
156
+ export declare const nearlyEqual: (a: number, b: number, epsilon?: number) => boolean;
157
+ /**
158
+ * Rounds `value` to `digits` decimal places (default 0); negative `digits` round to tens,
159
+ * hundreds, etc.
160
+ * @category Math & Random
161
+ */
162
+ export declare const roundTo: (value: number, digits?: number) => number;
163
+ /**
164
+ * Deterministic 32-bit integer hash (the MurmurHash3 finalizer). `n` is truncated to a 32-bit
165
+ * integer first.
166
+ * @returns an unsigned 32-bit integer in [0, 2³²)
167
+ * @category Math & Random
168
+ */
169
+ export declare const hash32: (n: number) => number;
170
+ /**
171
+ * Deterministically hashes any number of values (each truncated to a 32-bit integer) into one
172
+ * unsigned 32-bit integer by folding them through {@link hash32}. Order matters; no values gives 0.
173
+ * @category Math & Random
174
+ */
175
+ export declare const hash32mix: (...values: number[]) => number;
176
+ //#endregion
package/dist/math.js ADDED
@@ -0,0 +1,206 @@
1
+ //#region src/math.ts
2
+ /**
3
+ * π, half a turn in radians.
4
+ * @category Math & Random
5
+ */
6
+ const PI = Math.PI;
7
+ /**
8
+ * 2π, a full turn in radians.
9
+ * @category Math & Random
10
+ */
11
+ const TAU = Math.PI * 2;
12
+ /**
13
+ * π / 2, a quarter turn in radians.
14
+ * @category Math & Random
15
+ */
16
+ const HALF_PI = Math.PI / 2;
17
+ /**
18
+ * π / 4, an eighth of a turn in radians.
19
+ * @category Math & Random
20
+ */
21
+ const QUARTER_PI = Math.PI / 4;
22
+ /**
23
+ * Euler's number, the base of natural logarithms.
24
+ * @category Math & Random
25
+ */
26
+ const E = Math.E;
27
+ /**
28
+ * Multiply degrees by this to get radians.
29
+ * @category Math & Random
30
+ */
31
+ const DEG2RAD = PI / 180;
32
+ /**
33
+ * Multiply radians by this to get degrees.
34
+ * @category Math & Random
35
+ */
36
+ const RAD2DEG = 180 / PI;
37
+ /**
38
+ * Natural logarithm of 2.
39
+ * @category Math & Random
40
+ */
41
+ const LN2 = Math.LN2;
42
+ /**
43
+ * Natural logarithm of 10.
44
+ * @category Math & Random
45
+ */
46
+ const LN10 = Math.LN10;
47
+ /**
48
+ * Base-2 logarithm of e.
49
+ * @category Math & Random
50
+ */
51
+ const LOG2E = Math.LOG2E;
52
+ /**
53
+ * Base-10 logarithm of e.
54
+ * @category Math & Random
55
+ */
56
+ const LOG10E = Math.LOG10E;
57
+ /**
58
+ * Square root of 2.
59
+ * @category Math & Random
60
+ */
61
+ const SQRT2 = Math.SQRT2;
62
+ /**
63
+ * Square root of 1/2.
64
+ * @category Math & Random
65
+ */
66
+ const SQRT1_2 = Math.SQRT1_2;
67
+ /**
68
+ * `Number.EPSILON` (about 2.2e-16), the default tolerance of {@link nearlyEqual}.
69
+ * @category Math & Random
70
+ */
71
+ const EPSILON = Number.EPSILON;
72
+ /**
73
+ * Limits `value` to the inclusive range [`minValue`, `maxValue`].
74
+ * @category Math & Random
75
+ */
76
+ const clamp = (value, minValue, maxValue) => Math.min(Math.max(value, minValue), maxValue);
77
+ /**
78
+ * Clamps `value` to [0, 1].
79
+ * @category Math & Random
80
+ */
81
+ const saturate = (value) => clamp(value, 0, 1);
82
+ /**
83
+ * Linearly interpolates from `start` (t = 0) to `end` (t = 1). `t` is not clamped, so values
84
+ * outside [0, 1] extrapolate.
85
+ * @category Math & Random
86
+ */
87
+ const lerp = (start, end, t) => start + (end - start) * t;
88
+ /**
89
+ * The inverse of {@link lerp}: returns the `t` at which `value` lies between `start` and `end`
90
+ * (0 at `start`, 1 at `end`, unclamped). Returns 0 when `start === end`.
91
+ * @category Math & Random
92
+ */
93
+ const inverseLerp = (start, end, value) => end === start ? 0 : (value - start) / (end - start);
94
+ /**
95
+ * Hermite smoothstep, as in GLSL: 0 at or below `edge0`, 1 at or above `edge1`, with a smooth
96
+ * S-curve (3t² - 2t³) in between. With `edge0 === edge1` it is a step: 0 below the edge, 1 at or above.
97
+ * @category Math & Random
98
+ */
99
+ const smoothstep = (edge0, edge1, value) => {
100
+ if (edge1 === edge0) return value < edge0 ? 0 : 1;
101
+ const t = saturate((value - edge0) / (edge1 - edge0));
102
+ return t * t * (3 - 2 * t);
103
+ };
104
+ /**
105
+ * Fractional part of `value`, always in [0, 1) (e.g. `fract(-0.25)` is 0.75).
106
+ * @category Math & Random
107
+ */
108
+ const fract = (value) => value - Math.floor(value);
109
+ /**
110
+ * Modulo whose result takes the sign of `divisor` (so `mod(-1, 4)` is 3), unlike JavaScript's `%`.
111
+ * @category Math & Random
112
+ */
113
+ const mod = (value, divisor) => (value % divisor + divisor) % divisor;
114
+ /**
115
+ * Wraps `value` into the half-open range [`minValue`, `maxValue`), e.g. `wrap(370, 0, 360)` is 10.
116
+ * An empty range (`minValue === maxValue`) returns `minValue`.
117
+ * @category Math & Random
118
+ */
119
+ const wrap = (value, minValue, maxValue) => {
120
+ const range = maxValue - minValue;
121
+ return range === 0 ? minValue : minValue + mod(value - minValue, range);
122
+ };
123
+ /**
124
+ * Converts degrees to radians.
125
+ * @category Math & Random
126
+ */
127
+ const radians = (degrees) => degrees * DEG2RAD;
128
+ /**
129
+ * Converts radians to degrees.
130
+ * @category Math & Random
131
+ */
132
+ const degrees = (radians) => radians * RAD2DEG;
133
+ /**
134
+ * Wraps an angle in radians into [-π, π).
135
+ * @category Math & Random
136
+ */
137
+ const normalizeRadians = (radians) => wrap(radians, -PI, PI);
138
+ /**
139
+ * Euclidean distance between (`x1`, `y1`) and (`x2`, `y2`).
140
+ * @category Math & Random
141
+ */
142
+ const distance2d = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1);
143
+ /**
144
+ * Euclidean distance between (`x1`, `y1`, `z1`) and (`x2`, `y2`, `z2`).
145
+ * @category Math & Random
146
+ */
147
+ const distance3d = (x1, y1, z1, x2, y2, z2) => Math.hypot(x2 - x1, y2 - y1, z2 - z1);
148
+ /**
149
+ * Squared 2D Euclidean distance; cheaper than {@link distance2d} when only comparing distances.
150
+ * @category Math & Random
151
+ */
152
+ const squaredDistance = (x1, y1, x2, y2) => {
153
+ const dx = x2 - x1;
154
+ const dy = y2 - y1;
155
+ return dx * dx + dy * dy;
156
+ };
157
+ /**
158
+ * 2D Manhattan (taxicab) distance: `|dx| + |dy|`.
159
+ * @category Math & Random
160
+ */
161
+ const manhattanDistance = (x1, y1, x2, y2) => Math.abs(x2 - x1) + Math.abs(y2 - y1);
162
+ /**
163
+ * True when `a` and `b` differ by at most `epsilon` (default {@link EPSILON}), scaled by the larger
164
+ * magnitude once that exceeds 1: absolute near zero, relative for large values.
165
+ * @category Math & Random
166
+ */
167
+ const nearlyEqual = (a, b, epsilon = EPSILON) => Math.abs(a - b) <= epsilon * Math.max(1, Math.abs(a), Math.abs(b));
168
+ /**
169
+ * Rounds `value` to `digits` decimal places (default 0); negative `digits` round to tens,
170
+ * hundreds, etc.
171
+ * @category Math & Random
172
+ */
173
+ const roundTo = (value, digits = 0) => {
174
+ const factor = 10 ** digits;
175
+ return Math.round(value * factor) / factor;
176
+ };
177
+ /**
178
+ * Deterministic 32-bit integer hash (the MurmurHash3 finalizer). `n` is truncated to a 32-bit
179
+ * integer first.
180
+ * @returns an unsigned 32-bit integer in [0, 2³²)
181
+ * @category Math & Random
182
+ */
183
+ const hash32 = (n) => {
184
+ n |= 0;
185
+ n ^= n >>> 16;
186
+ n = Math.imul(n, 2246822507);
187
+ n ^= n >>> 13;
188
+ n = Math.imul(n, 3266489909);
189
+ n ^= n >>> 16;
190
+ return n >>> 0;
191
+ };
192
+ /**
193
+ * Deterministically hashes any number of values (each truncated to a 32-bit integer) into one
194
+ * unsigned 32-bit integer by folding them through {@link hash32}. Order matters; no values gives 0.
195
+ * @category Math & Random
196
+ */
197
+ const hash32mix = (...values) => {
198
+ let n = 0;
199
+ for (const value of values) {
200
+ n ^= value | 0;
201
+ n = hash32(n);
202
+ }
203
+ return n;
204
+ };
205
+ //#endregion
206
+ export { DEG2RAD, E, EPSILON, HALF_PI, LN10, LN2, LOG10E, LOG2E, PI, QUARTER_PI, RAD2DEG, SQRT1_2, SQRT2, TAU, clamp, degrees, distance2d, distance3d, fract, hash32, hash32mix, inverseLerp, lerp, manhattanDistance, mod, nearlyEqual, normalizeRadians, radians, roundTo, saturate, smoothstep, squaredDistance, wrap };
package/dist/misc.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { KeyValue } from "./types.js";
2
+ //#region src/misc.d.ts
3
+ /**
4
+ * True if `target` is `null` or `undefined`.
5
+ * @category Utilities
6
+ */
7
+ export declare function isNullOrUndefined(target: any): boolean;
8
+ /**
9
+ * True if `target` is a non-empty string.
10
+ * @category Utilities
11
+ */
12
+ export declare function isPositiveString(target: string): boolean;
13
+ /**
14
+ * Maps `value / max` onto an 11-step red -> yellow -> green gradient and returns its `'#rrggbb'`
15
+ * color; ratios at or below 0 (or a `max` at or below 0) are red, ratios at or above 1 are green.
16
+ * `max` defaults to 1, so a single argument is rated as a 0-1 fraction.
17
+ * @category Utilities
18
+ */
19
+ export declare const getRedYellowGreenGradientHex: (value: number, max?: number) => string;
20
+ /**
21
+ * The current page's query-string parameters as a plain object of strings (the last value wins
22
+ * for repeated keys). Needs `location`, so browser only.
23
+ * @category Utilities
24
+ */
25
+ export declare function GetUrlParams(): KeyValue;
26
+ /**
27
+ * Joins `x` and `y` into a string key such as `'3,4'`, e.g. for use in a `Map` or `Set`.
28
+ * @category Utilities
29
+ */
30
+ export declare const xy2k: (x: number, y: number, delimiter?: string) => string;
31
+ /**
32
+ * Splits a key made by {@link xy2k} back into `[x, y]` (parsed with `parseFloat`).
33
+ * @category Utilities
34
+ */
35
+ export declare const k2xy: (key: string, delimiter?: string) => [number, number];
36
+ /**
37
+ * Joins `x`, `y` and `z` into a string key such as `'1,2,3'`.
38
+ * @category Utilities
39
+ */
40
+ export declare const xyz2k: (x: number, y: number, z: number, delimiter?: string) => string;
41
+ /**
42
+ * Splits a key made by {@link xyz2k} back into `[x, y, z]` (parsed with `parseFloat`).
43
+ * @category Utilities
44
+ */
45
+ export declare const k2xyz: (key: string, delimiter?: string) => [number, number, number];
46
+ //#endregion
package/dist/misc.js ADDED
@@ -0,0 +1,71 @@
1
+ import { clamp } from "./math.js";
2
+ //#region src/misc.ts
3
+ /**
4
+ * True if `target` is `null` or `undefined`.
5
+ * @category Utilities
6
+ */
7
+ function isNullOrUndefined(target) {
8
+ return target === null || target === void 0;
9
+ }
10
+ /**
11
+ * True if `target` is a non-empty string.
12
+ * @category Utilities
13
+ */
14
+ function isPositiveString(target) {
15
+ return target.length > 0;
16
+ }
17
+ /**
18
+ * Maps `value / max` onto an 11-step red -> yellow -> green gradient and returns its `'#rrggbb'`
19
+ * color; ratios at or below 0 (or a `max` at or below 0) are red, ratios at or above 1 are green.
20
+ * `max` defaults to 1, so a single argument is rated as a 0-1 fraction.
21
+ * @category Utilities
22
+ */
23
+ const getRedYellowGreenGradientHex = (value, max = 1) => {
24
+ const percent = max > 0 ? value / max : 0;
25
+ return [
26
+ "#ff0000",
27
+ "#ff3300",
28
+ "#ff6600",
29
+ "#ff9900",
30
+ "#ffcc00",
31
+ "#ffff00",
32
+ "#ccff00",
33
+ "#99ff00",
34
+ "#66ff00",
35
+ "#33ff00",
36
+ "#00ff00"
37
+ ][clamp(Math.floor(percent * 10), 0, 10)];
38
+ };
39
+ /**
40
+ * The current page's query-string parameters as a plain object of strings (the last value wins
41
+ * for repeated keys). Needs `location`, so browser only.
42
+ * @category Utilities
43
+ */
44
+ function GetUrlParams() {
45
+ const params = {};
46
+ const url = new URLSearchParams(location.search);
47
+ for (const [key, value] of url.entries()) params[key] = value;
48
+ return params;
49
+ }
50
+ /**
51
+ * Joins `x` and `y` into a string key such as `'3,4'`, e.g. for use in a `Map` or `Set`.
52
+ * @category Utilities
53
+ */
54
+ const xy2k = (x, y, delimiter = ",") => `${x}${delimiter}${y}`;
55
+ /**
56
+ * Splits a key made by {@link xy2k} back into `[x, y]` (parsed with `parseFloat`).
57
+ * @category Utilities
58
+ */
59
+ const k2xy = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
60
+ /**
61
+ * Joins `x`, `y` and `z` into a string key such as `'1,2,3'`.
62
+ * @category Utilities
63
+ */
64
+ const xyz2k = (x, y, z, delimiter = ",") => `${x}${delimiter}${y}${delimiter}${z}`;
65
+ /**
66
+ * Splits a key made by {@link xyz2k} back into `[x, y, z]` (parsed with `parseFloat`).
67
+ * @category Utilities
68
+ */
69
+ const k2xyz = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
70
+ //#endregion
71
+ export { GetUrlParams, getRedYellowGreenGradientHex, isNullOrUndefined, isPositiveString, k2xy, k2xyz, xy2k, xyz2k };
@@ -0,0 +1,43 @@
1
+ //#region src/observable.d.ts
2
+ /**
3
+ * An {@link Observable} observer: called with the notified value (and any extra arguments).
4
+ *
5
+ * @category Events & Timing
6
+ */
7
+ export type Observer<T> = (value: T, ...args: any[]) => void;
8
+ /**
9
+ * A set of observers notified with a value (plus optional extra arguments). Observers are
10
+ * called in subscription order; subscribing the same function twice has no effect.
11
+ *
12
+ * ```ts
13
+ * const onLoaded = new Observable<string>();
14
+ * const unsubscribe = onLoaded.subscribe(url => console.log('loaded', url));
15
+ *
16
+ * onLoaded.notify('a.png');
17
+ * unsubscribe();
18
+ * ```
19
+ *
20
+ * @category Events & Timing
21
+ */
22
+ export declare class Observable<T> {
23
+ private readonly observers;
24
+ /** Adds an observer; returns a function that removes it. */
25
+ subscribe(observer: Observer<T>): () => void;
26
+ /**
27
+ * Subscribes for the next notification only; the observer gets the same arguments as with
28
+ * {@link Observable.subscribe}.
29
+ *
30
+ * @returns a function that removes the observer before it fires
31
+ */
32
+ once(observer: Observer<T>): () => void;
33
+ /**
34
+ * Calls every observer with `value` and `args`. Iterates over a snapshot, so observers may
35
+ * subscribe or unsubscribe during the call.
36
+ */
37
+ notify(value: T, ...args: any[]): void;
38
+ /** Removes all observers. */
39
+ clear(): void;
40
+ /** number of observers */
41
+ get size(): number;
42
+ }
43
+ //#endregion
@@ -0,0 +1,53 @@
1
+ //#region src/observable.ts
2
+ /**
3
+ * A set of observers notified with a value (plus optional extra arguments). Observers are
4
+ * called in subscription order; subscribing the same function twice has no effect.
5
+ *
6
+ * ```ts
7
+ * const onLoaded = new Observable<string>();
8
+ * const unsubscribe = onLoaded.subscribe(url => console.log('loaded', url));
9
+ *
10
+ * onLoaded.notify('a.png');
11
+ * unsubscribe();
12
+ * ```
13
+ *
14
+ * @category Events & Timing
15
+ */
16
+ var Observable = class {
17
+ observers = /* @__PURE__ */ new Set();
18
+ /** Adds an observer; returns a function that removes it. */
19
+ subscribe(observer) {
20
+ this.observers.add(observer);
21
+ return () => this.observers.delete(observer);
22
+ }
23
+ /**
24
+ * Subscribes for the next notification only; the observer gets the same arguments as with
25
+ * {@link Observable.subscribe}.
26
+ *
27
+ * @returns a function that removes the observer before it fires
28
+ */
29
+ once(observer) {
30
+ const unsubscribe = this.subscribe((value, ...args) => {
31
+ unsubscribe();
32
+ observer(value, ...args);
33
+ });
34
+ return unsubscribe;
35
+ }
36
+ /**
37
+ * Calls every observer with `value` and `args`. Iterates over a snapshot, so observers may
38
+ * subscribe or unsubscribe during the call.
39
+ */
40
+ notify(value, ...args) {
41
+ for (const observer of [...this.observers]) observer(value, ...args);
42
+ }
43
+ /** Removes all observers. */
44
+ clear() {
45
+ this.observers.clear();
46
+ }
47
+ /** number of observers */
48
+ get size() {
49
+ return this.observers.size;
50
+ }
51
+ };
52
+ //#endregion
53
+ export { Observable };
@@ -0,0 +1,47 @@
1
+ import { Color } from "./color.js";
2
+ //#region src/particle-renderer.d.ts
3
+ /**
4
+ * Double-buffered square-pixel renderer for particle effects: draw with `setPixel()` into an
5
+ * offscreen buffer, then `render()` copies the buffer to the visible canvas and clears it.
6
+ * Requires `OffscreenCanvas`. Used by {@link ParticleSystem2d}.
7
+ *
8
+ * ```ts
9
+ * const renderer = new ParticleRenderer();
10
+ * renderer.appendTo(document.body);
11
+ *
12
+ * renderer.setPixel(10, 10, Color.RED);
13
+ * renderer.render();
14
+ * ```
15
+ *
16
+ * @category Canvas 2D
17
+ */
18
+ export declare class ParticleRenderer {
19
+ private _screenCanvas;
20
+ private _bufferCanvas;
21
+ /** context of the visible canvas */
22
+ screenContext: CanvasRenderingContext2D;
23
+ /** context of the offscreen buffer that `setPixel()` draws into */
24
+ bufferContext: OffscreenCanvasRenderingContext2D;
25
+ /** canvas width in pixels */
26
+ get width(): number;
27
+ /** canvas height in pixels */
28
+ get height(): number;
29
+ /**
30
+ * @param canvas the visible canvas; defaults to a new canvas element, not attached to the page
31
+ * @throws an `Error` if a 2D context can't be created
32
+ */
33
+ constructor(canvas?: HTMLCanvasElement);
34
+ /** Moves the canvas into `target` and, with `autoResize` (default true), resizes it to fit. */
35
+ appendTo(target: HTMLElement, autoResize?: boolean): void;
36
+ /**
37
+ * Sets the canvas and buffer size in pixels (truncated to integers). A missing dimension
38
+ * defaults to the parent element's size (or the canvas's own size on the page). Resizing
39
+ * clears the buffer. Returns whether the size changed.
40
+ */
41
+ resize(displayWidth?: number, displayHeight?: number): boolean;
42
+ /** Draws a filled `size` x `size` square (pixels, default 2) centered on (`x`, `y`) into the buffer. */
43
+ setPixel(x: number, y: number, color: Color, size?: number): void;
44
+ /** Replaces the visible canvas with the buffer's contents, then clears the buffer for the next frame. */
45
+ render(): void;
46
+ }
47
+ //#endregion