@lightningjs/renderer 2.9.0-beta2 → 2.9.0-beta3

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 (189) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +147 -147
  5. package/dist/src/core/CoreNode.d.ts +2 -1
  6. package/dist/src/core/CoreNode.js +54 -43
  7. package/dist/src/core/CoreNode.js.map +1 -1
  8. package/dist/src/core/CoreTextureManager.d.ts +61 -15
  9. package/dist/src/core/CoreTextureManager.js +179 -104
  10. package/dist/src/core/CoreTextureManager.js.map +1 -1
  11. package/dist/src/core/Stage.d.ts +7 -0
  12. package/dist/src/core/Stage.js +33 -1
  13. package/dist/src/core/Stage.js.map +1 -1
  14. package/dist/src/core/TextureMemoryManager.js +2 -5
  15. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  16. package/dist/src/core/lib/ImageWorker.js +31 -28
  17. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  18. package/dist/src/core/lib/WebGlContextWrapper.d.ts +1 -0
  19. package/dist/src/core/lib/WebGlContextWrapper.js +2 -0
  20. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  21. package/dist/src/core/renderers/CoreContextTexture.d.ts +1 -0
  22. package/dist/src/core/renderers/CoreContextTexture.js +1 -0
  23. package/dist/src/core/renderers/CoreContextTexture.js.map +1 -1
  24. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +1 -1
  25. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +13 -5
  26. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -1
  27. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +6 -8
  28. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -1
  29. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +3 -1
  30. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -1
  31. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +1 -2
  32. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +26 -24
  33. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  34. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +0 -1
  35. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +7 -17
  36. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
  37. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  38. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  39. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  40. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  41. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  42. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  43. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  44. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  45. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  46. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  47. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  48. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  49. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  50. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  51. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  52. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  53. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  54. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  55. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +3 -5
  56. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  57. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +2 -1
  58. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  59. package/dist/src/core/textures/ColorTexture.d.ts +1 -1
  60. package/dist/src/core/textures/ColorTexture.js +2 -1
  61. package/dist/src/core/textures/ColorTexture.js.map +1 -1
  62. package/dist/src/core/textures/ImageTexture.d.ts +8 -1
  63. package/dist/src/core/textures/ImageTexture.js +39 -1
  64. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  65. package/dist/src/core/textures/NoiseTexture.d.ts +1 -1
  66. package/dist/src/core/textures/NoiseTexture.js +2 -1
  67. package/dist/src/core/textures/NoiseTexture.js.map +1 -1
  68. package/dist/src/core/textures/RenderTexture.d.ts +1 -1
  69. package/dist/src/core/textures/RenderTexture.js +2 -1
  70. package/dist/src/core/textures/RenderTexture.js.map +1 -1
  71. package/dist/src/core/textures/SubTexture.d.ts +1 -1
  72. package/dist/src/core/textures/SubTexture.js +21 -4
  73. package/dist/src/core/textures/SubTexture.js.map +1 -1
  74. package/dist/src/core/textures/Texture.d.ts +43 -21
  75. package/dist/src/core/textures/Texture.js +105 -33
  76. package/dist/src/core/textures/Texture.js.map +1 -1
  77. package/dist/src/main-api/Renderer.d.ts +18 -0
  78. package/dist/src/main-api/Renderer.js +6 -4
  79. package/dist/src/main-api/Renderer.js.map +1 -1
  80. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  81. package/exports/canvas.ts +39 -39
  82. package/exports/index.ts +89 -89
  83. package/exports/inspector.ts +24 -24
  84. package/exports/utils.ts +44 -44
  85. package/exports/webgl.ts +38 -38
  86. package/package.json +1 -1
  87. package/scripts/please-use-pnpm.js +13 -13
  88. package/src/common/CommonTypes.ts +139 -139
  89. package/src/common/EventEmitter.ts +77 -77
  90. package/src/common/IAnimationController.ts +92 -92
  91. package/src/common/IEventEmitter.ts +28 -28
  92. package/src/core/CoreNode.test.ts +95 -95
  93. package/src/core/CoreNode.ts +2282 -2258
  94. package/src/core/CoreShaderManager.ts +292 -292
  95. package/src/core/CoreTextNode.ts +450 -450
  96. package/src/core/CoreTextureManager.ts +522 -432
  97. package/src/core/Stage.ts +699 -652
  98. package/src/core/TextureMemoryManager.ts +277 -279
  99. package/src/core/animations/AnimationManager.ts +38 -38
  100. package/src/core/animations/CoreAnimation.ts +340 -340
  101. package/src/core/animations/CoreAnimationController.ts +157 -157
  102. package/src/core/lib/ContextSpy.ts +41 -41
  103. package/src/core/lib/ImageWorker.ts +270 -267
  104. package/src/core/lib/Matrix3d.ts +244 -244
  105. package/src/core/lib/RenderCoords.ts +86 -86
  106. package/src/core/lib/WebGlContextWrapper.ts +1322 -1320
  107. package/src/core/lib/textureCompression.ts +152 -152
  108. package/src/core/lib/textureSvg.ts +78 -78
  109. package/src/core/lib/utils.ts +306 -306
  110. package/src/core/platform.ts +61 -61
  111. package/src/core/renderers/CoreContextTexture.ts +43 -42
  112. package/src/core/renderers/CoreRenderOp.ts +22 -22
  113. package/src/core/renderers/CoreRenderer.ts +114 -114
  114. package/src/core/renderers/CoreShader.ts +41 -41
  115. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +364 -353
  116. package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -149
  117. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  118. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  119. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  120. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
  121. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -48
  122. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +298 -290
  123. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  124. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -817
  125. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  126. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  127. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  128. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  129. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  130. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  131. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  132. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  133. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  134. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  135. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  136. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  137. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  138. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  139. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  140. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  141. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  142. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  143. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  144. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  145. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  146. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  147. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  148. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  149. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  150. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  151. package/src/core/text-rendering/TrFontManager.ts +183 -183
  152. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +169 -171
  153. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  154. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  155. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  156. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  157. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +89 -89
  158. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -507
  159. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +798 -798
  160. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  161. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  162. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  163. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  164. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  165. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  166. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  167. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  168. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +403 -403
  169. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  170. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  171. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  172. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  173. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  174. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  175. package/src/core/textures/ColorTexture.ts +102 -100
  176. package/src/core/textures/ImageTexture.ts +378 -337
  177. package/src/core/textures/NoiseTexture.ts +104 -101
  178. package/src/core/textures/RenderTexture.ts +85 -83
  179. package/src/core/textures/SubTexture.ts +171 -146
  180. package/src/core/textures/Texture.ts +407 -318
  181. package/src/core/utils.ts +227 -227
  182. package/src/env.d.ts +7 -7
  183. package/src/main-api/DynamicShaderController.ts +104 -104
  184. package/src/main-api/INode.ts +101 -101
  185. package/src/main-api/Inspector.ts +505 -505
  186. package/src/main-api/Renderer.ts +693 -670
  187. package/src/main-api/ShaderController.ts +80 -80
  188. package/src/main-api/utils.ts +45 -45
  189. package/src/utils.ts +248 -248
@@ -1,505 +1,505 @@
1
- import {
2
- CoreNode,
3
- type CoreNodeAnimateProps,
4
- type CoreNodeProps,
5
- } from '../core/CoreNode.js';
6
- import { type RendererMainSettings } from './Renderer.js';
7
- import type { AnimationSettings } from '../core/animations/CoreAnimation.js';
8
- import type { IAnimationController } from '../common/IAnimationController.js';
9
- import { isProductionEnvironment } from '../utils.js';
10
- import type { CoreTextNode, CoreTextNodeProps } from '../core/CoreTextNode.js';
11
-
12
- /**
13
- * Inspector
14
- *
15
- * The inspector is a tool that allows you to inspect the state of the renderer
16
- * and the nodes that are being rendered. It is a tool that is used for debugging
17
- * and development purposes.
18
- *
19
- * The inspector will generate a DOM tree that mirrors the state of the renderer
20
- */
21
-
22
- /**
23
- * stylePropertyMap is a map of renderer properties that are mapped to CSS properties
24
- *
25
- * It can either return a string or an object with a prop and value property. Once a
26
- * property is found in the map, the value is set on the style of the div element.
27
- * Erik H made me do it.
28
- */
29
- interface StyleResponse {
30
- prop: string;
31
- value: string;
32
- }
33
- const stylePropertyMap: {
34
- [key: string]: (
35
- value: string | number | boolean,
36
- ) => string | StyleResponse | null;
37
- } = {
38
- alpha: (v) => {
39
- if (v === 1) {
40
- return null;
41
- }
42
-
43
- return { prop: 'opacity', value: `${v}` };
44
- },
45
- x: (x) => {
46
- return { prop: 'left', value: `${x}px` };
47
- },
48
- y: (y) => {
49
- return { prop: 'top', value: `${y}px` };
50
- },
51
- width: (w) => {
52
- if (w === 0) {
53
- return null;
54
- }
55
-
56
- return { prop: 'width', value: `${w}px` };
57
- },
58
- height: (h) => {
59
- if (h === 0) {
60
- return null;
61
- }
62
-
63
- return { prop: 'height', value: `${h}px` };
64
- },
65
- zIndex: () => 'z-index',
66
- fontFamily: () => 'font-family',
67
- fontSize: () => 'font-size',
68
- fontStyle: () => 'font-style',
69
- fontWeight: () => 'font-weight',
70
- fontStretch: () => 'font-stretch',
71
- lineHeight: () => 'line-height',
72
- letterSpacing: () => 'letter-spacing',
73
- textAlign: () => 'text-align',
74
- overflowSuffix: () => 'overflow-suffix',
75
- maxLines: () => 'max-lines',
76
- contain: () => 'contain',
77
- verticalAlign: () => 'vertical-align',
78
- clipping: (v) => {
79
- if (v === false) {
80
- return null;
81
- }
82
-
83
- return { prop: 'overflow', value: v ? 'hidden' : 'visible' };
84
- },
85
- rotation: (v) => {
86
- if (v === 0) {
87
- return null;
88
- }
89
-
90
- return { prop: 'transform', value: `rotate(${v}rad)` };
91
- },
92
- scale: (v) => {
93
- if (v === 1) {
94
- return null;
95
- }
96
-
97
- return { prop: 'transform', value: `scale(${v})` };
98
- },
99
- scaleX: (v) => {
100
- if (v === 1) {
101
- return null;
102
- }
103
-
104
- return { prop: 'transform', value: `scaleX(${v})` };
105
- },
106
- scaleY: (v) => {
107
- if (v === 1) {
108
- return null;
109
- }
110
-
111
- return { prop: 'transform', value: `scaleY(${v})` };
112
- },
113
- color: (v) => {
114
- if (v === 0) {
115
- return null;
116
- }
117
-
118
- return { prop: 'color', value: convertColorToRgba(v as number) };
119
- },
120
- };
121
-
122
- const convertColorToRgba = (color: number) => {
123
- const a = (color & 0xff) / 255;
124
- const b = (color >> 8) & 0xff;
125
- const g = (color >> 16) & 0xff;
126
- const r = (color >> 24) & 0xff;
127
- return `rgba(${r},${g},${b},${a})`;
128
- };
129
-
130
- const domPropertyMap: { [key: string]: string } = {
131
- id: 'test-id',
132
- };
133
-
134
- const gradientColorPropertyMap = [
135
- 'colorTop',
136
- 'colorBottom',
137
- 'colorLeft',
138
- 'colorRight',
139
- 'colorTl',
140
- 'colorTr',
141
- 'colorBl',
142
- 'colorBr',
143
- ];
144
-
145
- const knownProperties = new Set<string>([
146
- ...Object.keys(stylePropertyMap),
147
- ...Object.keys(domPropertyMap),
148
- // ...gradientColorPropertyMap,
149
- 'src',
150
- 'parent',
151
- 'data',
152
- ]);
153
-
154
- export class Inspector {
155
- private root: HTMLElement | null = null;
156
- private canvas: HTMLCanvasElement | null = null;
157
- private height = 1080;
158
- private width = 1920;
159
- private scaleX = 1;
160
- private scaleY = 1;
161
-
162
- constructor(canvas: HTMLCanvasElement, settings: RendererMainSettings) {
163
- if (isProductionEnvironment()) return;
164
-
165
- if (!settings) {
166
- throw new Error('settings is required');
167
- }
168
-
169
- // calc dimensions based on the devicePixelRatio
170
- this.height = Math.ceil(
171
- settings.appHeight ?? 1080 / (settings.deviceLogicalPixelRatio ?? 1),
172
- );
173
-
174
- this.width = Math.ceil(
175
- settings.appWidth ?? 1920 / (settings.deviceLogicalPixelRatio ?? 1),
176
- );
177
-
178
- this.scaleX = settings.deviceLogicalPixelRatio ?? 1;
179
- this.scaleY = settings.deviceLogicalPixelRatio ?? 1;
180
-
181
- this.canvas = canvas;
182
- this.root = document.createElement('div');
183
- this.setRootPosition();
184
- document.body.appendChild(this.root);
185
-
186
- //listen for changes on canvas
187
- const mutationObserver = new MutationObserver(
188
- this.setRootPosition.bind(this),
189
- );
190
- mutationObserver.observe(canvas, {
191
- attributes: true,
192
- childList: false,
193
- subtree: false,
194
- });
195
-
196
- // Create a ResizeObserver to watch for changes in the element's size
197
- const resizeObserver = new ResizeObserver(this.setRootPosition.bind(this));
198
- resizeObserver.observe(canvas);
199
-
200
- //listen for changes on window
201
- window.addEventListener('resize', this.setRootPosition.bind(this));
202
-
203
- console.warn('Inspector is enabled, this will impact performance');
204
- }
205
-
206
- setRootPosition() {
207
- if (this.root === null || this.canvas === null) {
208
- return;
209
- }
210
-
211
- // get the world position of the canvas object, so we can match the inspector to it
212
- const rect = this.canvas.getBoundingClientRect();
213
- const top = document.documentElement.scrollTop + rect.top;
214
- const left = document.documentElement.scrollLeft + rect.left;
215
-
216
- this.root.id = 'root';
217
- this.root.style.left = `${left}px`;
218
- this.root.style.top = `${top}px`;
219
- this.root.style.width = `${this.width}px`;
220
- this.root.style.height = `${this.height}px`;
221
- this.root.style.position = 'absolute';
222
- this.root.style.transformOrigin = '0 0 0';
223
- this.root.style.transform = `scale(${this.scaleX}, ${this.scaleY})`;
224
- this.root.style.overflow = 'hidden';
225
- this.root.style.zIndex = '65534';
226
- }
227
-
228
- createDiv(
229
- id: number,
230
- properties: CoreNodeProps | CoreTextNodeProps,
231
- ): HTMLElement {
232
- const div = document.createElement('div');
233
- div.style.position = 'absolute';
234
- div.id = id.toString();
235
-
236
- // set initial properties
237
- for (const key in properties) {
238
- this.updateNodeProperty(
239
- div,
240
- // really typescript? really?
241
- key as keyof CoreNodeProps,
242
- properties[key as keyof CoreNodeProps],
243
- properties,
244
- );
245
- }
246
-
247
- return div;
248
- }
249
-
250
- createNode(node: CoreNode): CoreNode {
251
- const div = this.createDiv(node.id, node.props);
252
- (div as HTMLElement & { node: CoreNode }).node = node;
253
- (node as CoreNode & { div: HTMLElement }).div = div;
254
-
255
- node.on('inViewport', () => div.setAttribute('state', 'inViewport'));
256
- node.on('inBounds', () => div.setAttribute('state', 'inBounds'));
257
- node.on('outOfBounds', () => div.setAttribute('state', 'outOfBounds'));
258
-
259
- // Monitor only relevant properties by trapping with selective assignment
260
- return this.createProxy(node, div);
261
- }
262
-
263
- createTextNode(node: CoreNode): CoreTextNode {
264
- const div = this.createDiv(node.id, node.props);
265
- (div as HTMLElement & { node: CoreNode }).node = node;
266
- (node as CoreNode & { div: HTMLElement }).div = div;
267
-
268
- return this.createProxy(node, div) as CoreTextNode;
269
- }
270
-
271
- createProxy(
272
- node: CoreNode | CoreTextNode,
273
- div: HTMLElement,
274
- ): CoreNode | CoreTextNode {
275
- // Define traps for each property in knownProperties
276
- knownProperties.forEach((property) => {
277
- let originalProp = Object.getOwnPropertyDescriptor(node, property);
278
-
279
- if (originalProp === undefined) {
280
- // Search the prototype chain for the property descriptor
281
- const proto = Object.getPrototypeOf(node) as CoreNode | CoreTextNode;
282
- originalProp = Object.getOwnPropertyDescriptor(proto, property);
283
- }
284
-
285
- if (originalProp === undefined) {
286
- return;
287
- }
288
-
289
- Object.defineProperty(node, property, {
290
- get() {
291
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
292
- return originalProp?.get?.call(node);
293
- },
294
- set: (value) => {
295
- originalProp?.set?.call(node, value);
296
- this.updateNodeProperty(
297
- div,
298
- property as keyof CoreNodeProps | keyof CoreTextNodeProps,
299
- value,
300
- node.props,
301
- );
302
- },
303
- configurable: true,
304
- enumerable: true,
305
- });
306
- });
307
-
308
- const originalDestroy = node.destroy;
309
- Object.defineProperty(node, 'destroy', {
310
- value: () => {
311
- this.destroyNode(node.id);
312
- originalDestroy.call(node);
313
- },
314
- });
315
-
316
- const originalAnimate = node.animate;
317
- Object.defineProperty(node, 'animate', {
318
- value: (
319
- props: CoreNodeAnimateProps,
320
- settings: AnimationSettings,
321
- ): IAnimationController => {
322
- const animationController = originalAnimate.call(node, props, settings);
323
-
324
- const originalStart =
325
- animationController.start.bind(animationController);
326
- animationController.start = () => {
327
- this.animateNode(div, props, settings);
328
-
329
- return originalStart();
330
- };
331
-
332
- return animationController;
333
- },
334
- });
335
-
336
- return node;
337
- }
338
-
339
- destroyNode(id: number) {
340
- const div = document.getElementById(id.toString());
341
- div?.remove();
342
- }
343
-
344
- updateNodeProperty(
345
- div: HTMLElement,
346
- property: keyof CoreNodeProps | keyof CoreTextNodeProps,
347
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
348
- value: any,
349
- props: CoreNodeProps | CoreTextNodeProps,
350
- ) {
351
- if (this.root === null || value === undefined || value === null) {
352
- return;
353
- }
354
-
355
- /**
356
- * Special case for parent property
357
- */
358
- if (property === 'parent') {
359
- const parentId: number = value.id;
360
-
361
- // only way to detect if the parent is the root node
362
- // if you are reading this and have a better way, please let me know
363
- if (parentId === 1) {
364
- this.root.appendChild(div);
365
- return;
366
- }
367
-
368
- const parent = document.getElementById(parentId.toString());
369
- parent?.appendChild(div);
370
- return;
371
- }
372
-
373
- // special case for text
374
- if (property === 'text') {
375
- div.innerHTML = String(value);
376
-
377
- // hide text because we can't render SDF fonts
378
- // it would look weird and obstruct the WebGL rendering
379
- div.style.visibility = 'hidden';
380
- return;
381
- }
382
-
383
- // special case for images
384
- // we're not setting any CSS properties to avoid images getting loaded twice
385
- // as the renderer will handle the loading of the image. Setting it to `data-src`
386
- if (property === 'src' && value) {
387
- div.setAttribute(`data-src`, String(value));
388
- return;
389
- }
390
-
391
- // special case for color gradients (normal colors are handled by the stylePropertyMap)
392
- // FIXME the renderer seems to return the same number for all colors
393
- // if (gradientColorPropertyMap.includes(property as string)) {
394
- // const color = convertColorToRgba(value as number);
395
- // div.setAttribute(`data-${property}`, color);
396
- // return;
397
- // }
398
-
399
- // CSS mappable attribute
400
- if (stylePropertyMap[property]) {
401
- const mappedStyleResponse = stylePropertyMap[property]?.(value);
402
-
403
- if (mappedStyleResponse === null) {
404
- return;
405
- }
406
-
407
- if (typeof mappedStyleResponse === 'string') {
408
- div.style.setProperty(mappedStyleResponse, String(value));
409
- return;
410
- }
411
-
412
- if (typeof mappedStyleResponse === 'object') {
413
- let value = mappedStyleResponse.value;
414
- if (property === 'x') {
415
- const mount = props.mountX;
416
- const width = props.width;
417
-
418
- if (mount) {
419
- value = `${parseInt(value) - width * mount}px`;
420
- }
421
- } else if (property === 'y') {
422
- const mount = props.mountY;
423
- const height = props.height;
424
-
425
- if (mount) {
426
- value = `${parseInt(value) - height * mount}px`;
427
- }
428
- }
429
- div.style.setProperty(mappedStyleResponse.prop, value);
430
- }
431
-
432
- return;
433
- }
434
-
435
- // DOM properties
436
- if (domPropertyMap[property]) {
437
- const domProperty = domPropertyMap[property];
438
- if (!domProperty) {
439
- return;
440
- }
441
-
442
- div.setAttribute(String(domProperty), String(value));
443
- return;
444
- }
445
-
446
- // custom data properties
447
- if (property === 'data') {
448
- for (const key in value) {
449
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
450
- const keyValue: unknown = value[key];
451
- if (keyValue === undefined) {
452
- div.removeAttribute(`data-${key}`);
453
- } else {
454
- div.setAttribute(`data-${key}`, String(keyValue));
455
- }
456
- }
457
- return;
458
- }
459
- }
460
-
461
- // simple animation handler
462
- animateNode(
463
- div: HTMLElement,
464
- props: CoreNodeAnimateProps,
465
- settings: AnimationSettings,
466
- ) {
467
- const {
468
- duration = 1000,
469
- delay = 0,
470
- // easing = 'linear',
471
- // repeat = 0,
472
- // loop = false,
473
- // stopMethod = false,
474
- } = settings;
475
-
476
- const {
477
- x,
478
- y,
479
- width,
480
- height,
481
- alpha = 1,
482
- rotation = 0,
483
- scale = 1,
484
- color,
485
- mountX,
486
- mountY,
487
- } = props;
488
-
489
- // ignoring loops and repeats for now, as that might be a bit too much for the inspector
490
- function animate() {
491
- setTimeout(() => {
492
- div.style.top = `${y - height * mountY}px`;
493
- div.style.left = `${x - width * mountX}px`;
494
- div.style.width = `${width}px`;
495
- div.style.height = `${height}px`;
496
- div.style.opacity = `${alpha}`;
497
- div.style.rotate = `${rotation}rad`;
498
- div.style.scale = `${scale}`;
499
- div.style.color = convertColorToRgba(color);
500
- }, duration);
501
- }
502
-
503
- setTimeout(animate, delay);
504
- }
505
- }
1
+ import {
2
+ CoreNode,
3
+ type CoreNodeAnimateProps,
4
+ type CoreNodeProps,
5
+ } from '../core/CoreNode.js';
6
+ import { type RendererMainSettings } from './Renderer.js';
7
+ import type { AnimationSettings } from '../core/animations/CoreAnimation.js';
8
+ import type { IAnimationController } from '../common/IAnimationController.js';
9
+ import { isProductionEnvironment } from '../utils.js';
10
+ import type { CoreTextNode, CoreTextNodeProps } from '../core/CoreTextNode.js';
11
+
12
+ /**
13
+ * Inspector
14
+ *
15
+ * The inspector is a tool that allows you to inspect the state of the renderer
16
+ * and the nodes that are being rendered. It is a tool that is used for debugging
17
+ * and development purposes.
18
+ *
19
+ * The inspector will generate a DOM tree that mirrors the state of the renderer
20
+ */
21
+
22
+ /**
23
+ * stylePropertyMap is a map of renderer properties that are mapped to CSS properties
24
+ *
25
+ * It can either return a string or an object with a prop and value property. Once a
26
+ * property is found in the map, the value is set on the style of the div element.
27
+ * Erik H made me do it.
28
+ */
29
+ interface StyleResponse {
30
+ prop: string;
31
+ value: string;
32
+ }
33
+ const stylePropertyMap: {
34
+ [key: string]: (
35
+ value: string | number | boolean,
36
+ ) => string | StyleResponse | null;
37
+ } = {
38
+ alpha: (v) => {
39
+ if (v === 1) {
40
+ return null;
41
+ }
42
+
43
+ return { prop: 'opacity', value: `${v}` };
44
+ },
45
+ x: (x) => {
46
+ return { prop: 'left', value: `${x}px` };
47
+ },
48
+ y: (y) => {
49
+ return { prop: 'top', value: `${y}px` };
50
+ },
51
+ width: (w) => {
52
+ if (w === 0) {
53
+ return null;
54
+ }
55
+
56
+ return { prop: 'width', value: `${w}px` };
57
+ },
58
+ height: (h) => {
59
+ if (h === 0) {
60
+ return null;
61
+ }
62
+
63
+ return { prop: 'height', value: `${h}px` };
64
+ },
65
+ zIndex: () => 'z-index',
66
+ fontFamily: () => 'font-family',
67
+ fontSize: () => 'font-size',
68
+ fontStyle: () => 'font-style',
69
+ fontWeight: () => 'font-weight',
70
+ fontStretch: () => 'font-stretch',
71
+ lineHeight: () => 'line-height',
72
+ letterSpacing: () => 'letter-spacing',
73
+ textAlign: () => 'text-align',
74
+ overflowSuffix: () => 'overflow-suffix',
75
+ maxLines: () => 'max-lines',
76
+ contain: () => 'contain',
77
+ verticalAlign: () => 'vertical-align',
78
+ clipping: (v) => {
79
+ if (v === false) {
80
+ return null;
81
+ }
82
+
83
+ return { prop: 'overflow', value: v ? 'hidden' : 'visible' };
84
+ },
85
+ rotation: (v) => {
86
+ if (v === 0) {
87
+ return null;
88
+ }
89
+
90
+ return { prop: 'transform', value: `rotate(${v}rad)` };
91
+ },
92
+ scale: (v) => {
93
+ if (v === 1) {
94
+ return null;
95
+ }
96
+
97
+ return { prop: 'transform', value: `scale(${v})` };
98
+ },
99
+ scaleX: (v) => {
100
+ if (v === 1) {
101
+ return null;
102
+ }
103
+
104
+ return { prop: 'transform', value: `scaleX(${v})` };
105
+ },
106
+ scaleY: (v) => {
107
+ if (v === 1) {
108
+ return null;
109
+ }
110
+
111
+ return { prop: 'transform', value: `scaleY(${v})` };
112
+ },
113
+ color: (v) => {
114
+ if (v === 0) {
115
+ return null;
116
+ }
117
+
118
+ return { prop: 'color', value: convertColorToRgba(v as number) };
119
+ },
120
+ };
121
+
122
+ const convertColorToRgba = (color: number) => {
123
+ const a = (color & 0xff) / 255;
124
+ const b = (color >> 8) & 0xff;
125
+ const g = (color >> 16) & 0xff;
126
+ const r = (color >> 24) & 0xff;
127
+ return `rgba(${r},${g},${b},${a})`;
128
+ };
129
+
130
+ const domPropertyMap: { [key: string]: string } = {
131
+ id: 'test-id',
132
+ };
133
+
134
+ const gradientColorPropertyMap = [
135
+ 'colorTop',
136
+ 'colorBottom',
137
+ 'colorLeft',
138
+ 'colorRight',
139
+ 'colorTl',
140
+ 'colorTr',
141
+ 'colorBl',
142
+ 'colorBr',
143
+ ];
144
+
145
+ const knownProperties = new Set<string>([
146
+ ...Object.keys(stylePropertyMap),
147
+ ...Object.keys(domPropertyMap),
148
+ // ...gradientColorPropertyMap,
149
+ 'src',
150
+ 'parent',
151
+ 'data',
152
+ ]);
153
+
154
+ export class Inspector {
155
+ private root: HTMLElement | null = null;
156
+ private canvas: HTMLCanvasElement | null = null;
157
+ private height = 1080;
158
+ private width = 1920;
159
+ private scaleX = 1;
160
+ private scaleY = 1;
161
+
162
+ constructor(canvas: HTMLCanvasElement, settings: RendererMainSettings) {
163
+ if (isProductionEnvironment()) return;
164
+
165
+ if (!settings) {
166
+ throw new Error('settings is required');
167
+ }
168
+
169
+ // calc dimensions based on the devicePixelRatio
170
+ this.height = Math.ceil(
171
+ settings.appHeight ?? 1080 / (settings.deviceLogicalPixelRatio ?? 1),
172
+ );
173
+
174
+ this.width = Math.ceil(
175
+ settings.appWidth ?? 1920 / (settings.deviceLogicalPixelRatio ?? 1),
176
+ );
177
+
178
+ this.scaleX = settings.deviceLogicalPixelRatio ?? 1;
179
+ this.scaleY = settings.deviceLogicalPixelRatio ?? 1;
180
+
181
+ this.canvas = canvas;
182
+ this.root = document.createElement('div');
183
+ this.setRootPosition();
184
+ document.body.appendChild(this.root);
185
+
186
+ //listen for changes on canvas
187
+ const mutationObserver = new MutationObserver(
188
+ this.setRootPosition.bind(this),
189
+ );
190
+ mutationObserver.observe(canvas, {
191
+ attributes: true,
192
+ childList: false,
193
+ subtree: false,
194
+ });
195
+
196
+ // Create a ResizeObserver to watch for changes in the element's size
197
+ const resizeObserver = new ResizeObserver(this.setRootPosition.bind(this));
198
+ resizeObserver.observe(canvas);
199
+
200
+ //listen for changes on window
201
+ window.addEventListener('resize', this.setRootPosition.bind(this));
202
+
203
+ console.warn('Inspector is enabled, this will impact performance');
204
+ }
205
+
206
+ setRootPosition() {
207
+ if (this.root === null || this.canvas === null) {
208
+ return;
209
+ }
210
+
211
+ // get the world position of the canvas object, so we can match the inspector to it
212
+ const rect = this.canvas.getBoundingClientRect();
213
+ const top = document.documentElement.scrollTop + rect.top;
214
+ const left = document.documentElement.scrollLeft + rect.left;
215
+
216
+ this.root.id = 'root';
217
+ this.root.style.left = `${left}px`;
218
+ this.root.style.top = `${top}px`;
219
+ this.root.style.width = `${this.width}px`;
220
+ this.root.style.height = `${this.height}px`;
221
+ this.root.style.position = 'absolute';
222
+ this.root.style.transformOrigin = '0 0 0';
223
+ this.root.style.transform = `scale(${this.scaleX}, ${this.scaleY})`;
224
+ this.root.style.overflow = 'hidden';
225
+ this.root.style.zIndex = '65534';
226
+ }
227
+
228
+ createDiv(
229
+ id: number,
230
+ properties: CoreNodeProps | CoreTextNodeProps,
231
+ ): HTMLElement {
232
+ const div = document.createElement('div');
233
+ div.style.position = 'absolute';
234
+ div.id = id.toString();
235
+
236
+ // set initial properties
237
+ for (const key in properties) {
238
+ this.updateNodeProperty(
239
+ div,
240
+ // really typescript? really?
241
+ key as keyof CoreNodeProps,
242
+ properties[key as keyof CoreNodeProps],
243
+ properties,
244
+ );
245
+ }
246
+
247
+ return div;
248
+ }
249
+
250
+ createNode(node: CoreNode): CoreNode {
251
+ const div = this.createDiv(node.id, node.props);
252
+ (div as HTMLElement & { node: CoreNode }).node = node;
253
+ (node as CoreNode & { div: HTMLElement }).div = div;
254
+
255
+ node.on('inViewport', () => div.setAttribute('state', 'inViewport'));
256
+ node.on('inBounds', () => div.setAttribute('state', 'inBounds'));
257
+ node.on('outOfBounds', () => div.setAttribute('state', 'outOfBounds'));
258
+
259
+ // Monitor only relevant properties by trapping with selective assignment
260
+ return this.createProxy(node, div);
261
+ }
262
+
263
+ createTextNode(node: CoreNode): CoreTextNode {
264
+ const div = this.createDiv(node.id, node.props);
265
+ (div as HTMLElement & { node: CoreNode }).node = node;
266
+ (node as CoreNode & { div: HTMLElement }).div = div;
267
+
268
+ return this.createProxy(node, div) as CoreTextNode;
269
+ }
270
+
271
+ createProxy(
272
+ node: CoreNode | CoreTextNode,
273
+ div: HTMLElement,
274
+ ): CoreNode | CoreTextNode {
275
+ // Define traps for each property in knownProperties
276
+ knownProperties.forEach((property) => {
277
+ let originalProp = Object.getOwnPropertyDescriptor(node, property);
278
+
279
+ if (originalProp === undefined) {
280
+ // Search the prototype chain for the property descriptor
281
+ const proto = Object.getPrototypeOf(node) as CoreNode | CoreTextNode;
282
+ originalProp = Object.getOwnPropertyDescriptor(proto, property);
283
+ }
284
+
285
+ if (originalProp === undefined) {
286
+ return;
287
+ }
288
+
289
+ Object.defineProperty(node, property, {
290
+ get() {
291
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
292
+ return originalProp?.get?.call(node);
293
+ },
294
+ set: (value) => {
295
+ originalProp?.set?.call(node, value);
296
+ this.updateNodeProperty(
297
+ div,
298
+ property as keyof CoreNodeProps | keyof CoreTextNodeProps,
299
+ value,
300
+ node.props,
301
+ );
302
+ },
303
+ configurable: true,
304
+ enumerable: true,
305
+ });
306
+ });
307
+
308
+ const originalDestroy = node.destroy;
309
+ Object.defineProperty(node, 'destroy', {
310
+ value: () => {
311
+ this.destroyNode(node.id);
312
+ originalDestroy.call(node);
313
+ },
314
+ });
315
+
316
+ const originalAnimate = node.animate;
317
+ Object.defineProperty(node, 'animate', {
318
+ value: (
319
+ props: CoreNodeAnimateProps,
320
+ settings: AnimationSettings,
321
+ ): IAnimationController => {
322
+ const animationController = originalAnimate.call(node, props, settings);
323
+
324
+ const originalStart =
325
+ animationController.start.bind(animationController);
326
+ animationController.start = () => {
327
+ this.animateNode(div, props, settings);
328
+
329
+ return originalStart();
330
+ };
331
+
332
+ return animationController;
333
+ },
334
+ });
335
+
336
+ return node;
337
+ }
338
+
339
+ destroyNode(id: number) {
340
+ const div = document.getElementById(id.toString());
341
+ div?.remove();
342
+ }
343
+
344
+ updateNodeProperty(
345
+ div: HTMLElement,
346
+ property: keyof CoreNodeProps | keyof CoreTextNodeProps,
347
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
348
+ value: any,
349
+ props: CoreNodeProps | CoreTextNodeProps,
350
+ ) {
351
+ if (this.root === null || value === undefined || value === null) {
352
+ return;
353
+ }
354
+
355
+ /**
356
+ * Special case for parent property
357
+ */
358
+ if (property === 'parent') {
359
+ const parentId: number = value.id;
360
+
361
+ // only way to detect if the parent is the root node
362
+ // if you are reading this and have a better way, please let me know
363
+ if (parentId === 1) {
364
+ this.root.appendChild(div);
365
+ return;
366
+ }
367
+
368
+ const parent = document.getElementById(parentId.toString());
369
+ parent?.appendChild(div);
370
+ return;
371
+ }
372
+
373
+ // special case for text
374
+ if (property === 'text') {
375
+ div.innerHTML = String(value);
376
+
377
+ // hide text because we can't render SDF fonts
378
+ // it would look weird and obstruct the WebGL rendering
379
+ div.style.visibility = 'hidden';
380
+ return;
381
+ }
382
+
383
+ // special case for images
384
+ // we're not setting any CSS properties to avoid images getting loaded twice
385
+ // as the renderer will handle the loading of the image. Setting it to `data-src`
386
+ if (property === 'src' && value) {
387
+ div.setAttribute(`data-src`, String(value));
388
+ return;
389
+ }
390
+
391
+ // special case for color gradients (normal colors are handled by the stylePropertyMap)
392
+ // FIXME the renderer seems to return the same number for all colors
393
+ // if (gradientColorPropertyMap.includes(property as string)) {
394
+ // const color = convertColorToRgba(value as number);
395
+ // div.setAttribute(`data-${property}`, color);
396
+ // return;
397
+ // }
398
+
399
+ // CSS mappable attribute
400
+ if (stylePropertyMap[property]) {
401
+ const mappedStyleResponse = stylePropertyMap[property]?.(value);
402
+
403
+ if (mappedStyleResponse === null) {
404
+ return;
405
+ }
406
+
407
+ if (typeof mappedStyleResponse === 'string') {
408
+ div.style.setProperty(mappedStyleResponse, String(value));
409
+ return;
410
+ }
411
+
412
+ if (typeof mappedStyleResponse === 'object') {
413
+ let value = mappedStyleResponse.value;
414
+ if (property === 'x') {
415
+ const mount = props.mountX;
416
+ const width = props.width;
417
+
418
+ if (mount) {
419
+ value = `${parseInt(value) - width * mount}px`;
420
+ }
421
+ } else if (property === 'y') {
422
+ const mount = props.mountY;
423
+ const height = props.height;
424
+
425
+ if (mount) {
426
+ value = `${parseInt(value) - height * mount}px`;
427
+ }
428
+ }
429
+ div.style.setProperty(mappedStyleResponse.prop, value);
430
+ }
431
+
432
+ return;
433
+ }
434
+
435
+ // DOM properties
436
+ if (domPropertyMap[property]) {
437
+ const domProperty = domPropertyMap[property];
438
+ if (!domProperty) {
439
+ return;
440
+ }
441
+
442
+ div.setAttribute(String(domProperty), String(value));
443
+ return;
444
+ }
445
+
446
+ // custom data properties
447
+ if (property === 'data') {
448
+ for (const key in value) {
449
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
450
+ const keyValue: unknown = value[key];
451
+ if (keyValue === undefined) {
452
+ div.removeAttribute(`data-${key}`);
453
+ } else {
454
+ div.setAttribute(`data-${key}`, String(keyValue));
455
+ }
456
+ }
457
+ return;
458
+ }
459
+ }
460
+
461
+ // simple animation handler
462
+ animateNode(
463
+ div: HTMLElement,
464
+ props: CoreNodeAnimateProps,
465
+ settings: AnimationSettings,
466
+ ) {
467
+ const {
468
+ duration = 1000,
469
+ delay = 0,
470
+ // easing = 'linear',
471
+ // repeat = 0,
472
+ // loop = false,
473
+ // stopMethod = false,
474
+ } = settings;
475
+
476
+ const {
477
+ x,
478
+ y,
479
+ width,
480
+ height,
481
+ alpha = 1,
482
+ rotation = 0,
483
+ scale = 1,
484
+ color,
485
+ mountX,
486
+ mountY,
487
+ } = props;
488
+
489
+ // ignoring loops and repeats for now, as that might be a bit too much for the inspector
490
+ function animate() {
491
+ setTimeout(() => {
492
+ div.style.top = `${y - height * mountY}px`;
493
+ div.style.left = `${x - width * mountX}px`;
494
+ div.style.width = `${width}px`;
495
+ div.style.height = `${height}px`;
496
+ div.style.opacity = `${alpha}`;
497
+ div.style.rotate = `${rotation}rad`;
498
+ div.style.scale = `${scale}`;
499
+ div.style.color = convertColorToRgba(color);
500
+ }, duration);
501
+ }
502
+
503
+ setTimeout(animate, delay);
504
+ }
505
+ }