@lightningjs/renderer 2.9.0-beta2 → 2.9.0-beta4

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 (193) 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 +29 -6
  6. package/dist/src/core/CoreNode.js +118 -79
  7. package/dist/src/core/CoreNode.js.map +1 -1
  8. package/dist/src/core/CoreTextNode.d.ts +2 -2
  9. package/dist/src/core/CoreTextNode.js +7 -4
  10. package/dist/src/core/CoreTextNode.js.map +1 -1
  11. package/dist/src/core/CoreTextureManager.d.ts +61 -15
  12. package/dist/src/core/CoreTextureManager.js +179 -104
  13. package/dist/src/core/CoreTextureManager.js.map +1 -1
  14. package/dist/src/core/Stage.d.ts +7 -0
  15. package/dist/src/core/Stage.js +34 -1
  16. package/dist/src/core/Stage.js.map +1 -1
  17. package/dist/src/core/TextureMemoryManager.js +2 -5
  18. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  19. package/dist/src/core/lib/ImageWorker.js +31 -28
  20. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  21. package/dist/src/core/lib/WebGlContextWrapper.d.ts +1 -0
  22. package/dist/src/core/lib/WebGlContextWrapper.js +2 -0
  23. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  24. package/dist/src/core/renderers/CoreContextTexture.d.ts +1 -0
  25. package/dist/src/core/renderers/CoreContextTexture.js +1 -0
  26. package/dist/src/core/renderers/CoreContextTexture.js.map +1 -1
  27. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +1 -1
  28. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +13 -5
  29. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -1
  30. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +6 -8
  31. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -1
  32. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +3 -1
  33. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -1
  34. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +1 -2
  35. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +26 -24
  36. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  37. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +0 -1
  38. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +7 -17
  39. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
  40. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  41. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  42. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  43. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  44. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  45. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  46. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  47. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  48. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  49. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  50. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  51. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  52. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  53. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  54. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  55. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +12 -12
  56. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  57. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  58. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +9 -5
  59. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  60. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +2 -1
  61. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  62. package/dist/src/core/textures/ColorTexture.d.ts +1 -1
  63. package/dist/src/core/textures/ColorTexture.js +2 -1
  64. package/dist/src/core/textures/ColorTexture.js.map +1 -1
  65. package/dist/src/core/textures/ImageTexture.d.ts +8 -1
  66. package/dist/src/core/textures/ImageTexture.js +39 -1
  67. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  68. package/dist/src/core/textures/NoiseTexture.d.ts +1 -1
  69. package/dist/src/core/textures/NoiseTexture.js +2 -1
  70. package/dist/src/core/textures/NoiseTexture.js.map +1 -1
  71. package/dist/src/core/textures/RenderTexture.d.ts +1 -1
  72. package/dist/src/core/textures/RenderTexture.js +2 -1
  73. package/dist/src/core/textures/RenderTexture.js.map +1 -1
  74. package/dist/src/core/textures/SubTexture.d.ts +1 -1
  75. package/dist/src/core/textures/SubTexture.js +21 -4
  76. package/dist/src/core/textures/SubTexture.js.map +1 -1
  77. package/dist/src/core/textures/Texture.d.ts +44 -22
  78. package/dist/src/core/textures/Texture.js +116 -36
  79. package/dist/src/core/textures/Texture.js.map +1 -1
  80. package/dist/src/main-api/Renderer.d.ts +18 -0
  81. package/dist/src/main-api/Renderer.js +6 -4
  82. package/dist/src/main-api/Renderer.js.map +1 -1
  83. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  84. package/exports/canvas.ts +39 -39
  85. package/exports/index.ts +89 -89
  86. package/exports/inspector.ts +24 -24
  87. package/exports/utils.ts +44 -44
  88. package/exports/webgl.ts +38 -38
  89. package/package.json +1 -1
  90. package/scripts/please-use-pnpm.js +13 -13
  91. package/src/common/CommonTypes.ts +139 -139
  92. package/src/common/EventEmitter.ts +77 -77
  93. package/src/common/IAnimationController.ts +92 -92
  94. package/src/common/IEventEmitter.ts +28 -28
  95. package/src/core/CoreNode.test.ts +199 -95
  96. package/src/core/CoreNode.ts +2313 -2258
  97. package/src/core/CoreShaderManager.ts +292 -292
  98. package/src/core/CoreTextNode.ts +455 -450
  99. package/src/core/CoreTextureManager.ts +522 -432
  100. package/src/core/Stage.ts +700 -652
  101. package/src/core/TextureMemoryManager.ts +277 -279
  102. package/src/core/animations/AnimationManager.ts +38 -38
  103. package/src/core/animations/CoreAnimation.ts +340 -340
  104. package/src/core/animations/CoreAnimationController.ts +157 -157
  105. package/src/core/lib/ContextSpy.ts +41 -41
  106. package/src/core/lib/ImageWorker.ts +270 -267
  107. package/src/core/lib/Matrix3d.ts +244 -244
  108. package/src/core/lib/RenderCoords.ts +86 -86
  109. package/src/core/lib/WebGlContextWrapper.ts +1322 -1320
  110. package/src/core/lib/textureCompression.ts +152 -152
  111. package/src/core/lib/textureSvg.ts +78 -78
  112. package/src/core/lib/utils.ts +306 -306
  113. package/src/core/platform.ts +61 -61
  114. package/src/core/renderers/CoreContextTexture.ts +43 -42
  115. package/src/core/renderers/CoreRenderOp.ts +22 -22
  116. package/src/core/renderers/CoreRenderer.ts +114 -114
  117. package/src/core/renderers/CoreShader.ts +41 -41
  118. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +364 -353
  119. package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -149
  120. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  121. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  122. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  123. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
  124. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -48
  125. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +298 -290
  126. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  127. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -817
  128. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  129. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  130. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  131. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  132. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  133. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  134. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  135. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  136. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  137. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  138. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  139. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  140. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  141. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  142. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  143. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  144. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  145. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  146. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  147. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  148. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  149. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  150. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  151. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  152. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  153. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  154. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  155. package/src/core/text-rendering/TrFontManager.ts +183 -183
  156. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -171
  157. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  158. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  159. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  160. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  161. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +89 -89
  162. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -507
  163. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +798 -798
  164. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  165. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  166. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  167. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  168. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  169. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  170. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  171. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  172. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +403 -403
  173. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  174. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  175. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  176. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  177. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  178. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  179. package/src/core/textures/ColorTexture.ts +102 -100
  180. package/src/core/textures/ImageTexture.ts +378 -337
  181. package/src/core/textures/NoiseTexture.ts +104 -101
  182. package/src/core/textures/RenderTexture.ts +85 -83
  183. package/src/core/textures/SubTexture.ts +171 -146
  184. package/src/core/textures/Texture.ts +423 -318
  185. package/src/core/utils.ts +227 -227
  186. package/src/env.d.ts +7 -7
  187. package/src/main-api/DynamicShaderController.ts +104 -104
  188. package/src/main-api/INode.ts +101 -101
  189. package/src/main-api/Inspector.ts +505 -505
  190. package/src/main-api/Renderer.ts +693 -670
  191. package/src/main-api/ShaderController.ts +80 -80
  192. package/src/main-api/utils.ts +45 -45
  193. 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
+ }