@solidtv/solid 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +7 -0
  3. package/README.md +95 -0
  4. package/dist/jsx-runtime.d.ts +14 -0
  5. package/dist/src/activeElement.d.ts +2 -0
  6. package/dist/src/activeElement.js +3 -0
  7. package/dist/src/activeElement.js.map +1 -0
  8. package/dist/src/core/config.d.ts +51 -0
  9. package/dist/src/core/config.js +37 -0
  10. package/dist/src/core/config.js.map +1 -0
  11. package/dist/src/core/dom-renderer/domRenderer.d.ts +138 -0
  12. package/dist/src/core/dom-renderer/domRenderer.js +1844 -0
  13. package/dist/src/core/dom-renderer/domRenderer.js.map +1 -0
  14. package/dist/src/core/dom-renderer/domRendererTypes.d.ts +110 -0
  15. package/dist/src/core/dom-renderer/domRendererTypes.js +2 -0
  16. package/dist/src/core/dom-renderer/domRendererTypes.js.map +1 -0
  17. package/dist/src/core/dom-renderer/domRendererUtils.d.ts +23 -0
  18. package/dist/src/core/dom-renderer/domRendererUtils.js +232 -0
  19. package/dist/src/core/dom-renderer/domRendererUtils.js.map +1 -0
  20. package/dist/src/core/elementNode.d.ts +508 -0
  21. package/dist/src/core/elementNode.js +928 -0
  22. package/dist/src/core/elementNode.js.map +1 -0
  23. package/dist/src/core/flex.d.ts +2 -0
  24. package/dist/src/core/flex.js +284 -0
  25. package/dist/src/core/flex.js.map +1 -0
  26. package/dist/src/core/flexLayout.d.ts +2 -0
  27. package/dist/src/core/flexLayout.js +360 -0
  28. package/dist/src/core/flexLayout.js.map +1 -0
  29. package/dist/src/core/focusKeyTypes.d.ts +42 -0
  30. package/dist/src/core/focusKeyTypes.js +2 -0
  31. package/dist/src/core/focusKeyTypes.js.map +1 -0
  32. package/dist/src/core/focusManager.d.ts +13 -0
  33. package/dist/src/core/focusManager.js +270 -0
  34. package/dist/src/core/focusManager.js.map +1 -0
  35. package/dist/src/core/index.d.ts +14 -0
  36. package/dist/src/core/index.js +11 -0
  37. package/dist/src/core/index.js.map +1 -0
  38. package/dist/src/core/intrinsicTypes.d.ts +106 -0
  39. package/dist/src/core/intrinsicTypes.js +2 -0
  40. package/dist/src/core/intrinsicTypes.js.map +1 -0
  41. package/dist/src/core/lightningInit.d.ts +9 -0
  42. package/dist/src/core/lightningInit.js +34 -0
  43. package/dist/src/core/lightningInit.js.map +1 -0
  44. package/dist/src/core/nodeTypes.d.ts +6 -0
  45. package/dist/src/core/nodeTypes.js +6 -0
  46. package/dist/src/core/nodeTypes.js.map +1 -0
  47. package/dist/src/core/shaders.d.ts +52 -0
  48. package/dist/src/core/shaders.js +86 -0
  49. package/dist/src/core/shaders.js.map +1 -0
  50. package/dist/src/core/states.d.ts +12 -0
  51. package/dist/src/core/states.js +84 -0
  52. package/dist/src/core/states.js.map +1 -0
  53. package/dist/src/core/utils.d.ts +39 -0
  54. package/dist/src/core/utils.js +164 -0
  55. package/dist/src/core/utils.js.map +1 -0
  56. package/dist/src/devtools/index.d.ts +6 -0
  57. package/dist/src/devtools/index.js +65 -0
  58. package/dist/src/devtools/index.js.map +1 -0
  59. package/dist/src/index.d.ts +9 -0
  60. package/dist/src/index.js +7 -0
  61. package/dist/src/index.js.map +1 -0
  62. package/dist/src/primitives/Column.d.ts +3 -0
  63. package/dist/src/primitives/Column.jsx +25 -0
  64. package/dist/src/primitives/Column.jsx.map +1 -0
  65. package/dist/src/primitives/FPSCounter.d.ts +4 -0
  66. package/dist/src/primitives/FPSCounter.jsx +188 -0
  67. package/dist/src/primitives/FPSCounter.jsx.map +1 -0
  68. package/dist/src/primitives/FadeInOut.d.ts +18 -0
  69. package/dist/src/primitives/FadeInOut.jsx +39 -0
  70. package/dist/src/primitives/FadeInOut.jsx.map +1 -0
  71. package/dist/src/primitives/Grid.d.ts +23 -0
  72. package/dist/src/primitives/Grid.jsx +100 -0
  73. package/dist/src/primitives/Grid.jsx.map +1 -0
  74. package/dist/src/primitives/Image.d.ts +8 -0
  75. package/dist/src/primitives/Image.jsx +42 -0
  76. package/dist/src/primitives/Image.jsx.map +1 -0
  77. package/dist/src/primitives/KeepAlive.d.ts +42 -0
  78. package/dist/src/primitives/KeepAlive.jsx +160 -0
  79. package/dist/src/primitives/KeepAlive.jsx.map +1 -0
  80. package/dist/src/primitives/Lazy.d.ts +15 -0
  81. package/dist/src/primitives/Lazy.jsx +95 -0
  82. package/dist/src/primitives/Lazy.jsx.map +1 -0
  83. package/dist/src/primitives/LazyImport.d.ts +8 -0
  84. package/dist/src/primitives/LazyImport.js +40 -0
  85. package/dist/src/primitives/LazyImport.js.map +1 -0
  86. package/dist/src/primitives/Marquee.d.ts +64 -0
  87. package/dist/src/primitives/Marquee.jsx +86 -0
  88. package/dist/src/primitives/Marquee.jsx.map +1 -0
  89. package/dist/src/primitives/Preserve.d.ts +4 -0
  90. package/dist/src/primitives/Preserve.jsx +11 -0
  91. package/dist/src/primitives/Preserve.jsx.map +1 -0
  92. package/dist/src/primitives/Row.d.ts +3 -0
  93. package/dist/src/primitives/Row.jsx +24 -0
  94. package/dist/src/primitives/Row.jsx.map +1 -0
  95. package/dist/src/primitives/Suspense.d.ts +22 -0
  96. package/dist/src/primitives/Suspense.jsx +33 -0
  97. package/dist/src/primitives/Suspense.jsx.map +1 -0
  98. package/dist/src/primitives/Virtual.d.ts +18 -0
  99. package/dist/src/primitives/Virtual.jsx +495 -0
  100. package/dist/src/primitives/Virtual.jsx.map +1 -0
  101. package/dist/src/primitives/VirtualGrid.d.ts +13 -0
  102. package/dist/src/primitives/VirtualGrid.jsx +160 -0
  103. package/dist/src/primitives/VirtualGrid.jsx.map +1 -0
  104. package/dist/src/primitives/Visible.d.ts +6 -0
  105. package/dist/src/primitives/Visible.jsx +37 -0
  106. package/dist/src/primitives/Visible.jsx.map +1 -0
  107. package/dist/src/primitives/announcer/announcer.d.ts +40 -0
  108. package/dist/src/primitives/announcer/announcer.js +125 -0
  109. package/dist/src/primitives/announcer/announcer.js.map +1 -0
  110. package/dist/src/primitives/announcer/index.d.ts +6 -0
  111. package/dist/src/primitives/announcer/index.js +15 -0
  112. package/dist/src/primitives/announcer/index.js.map +1 -0
  113. package/dist/src/primitives/announcer/speech.d.ts +10 -0
  114. package/dist/src/primitives/announcer/speech.js +281 -0
  115. package/dist/src/primitives/announcer/speech.js.map +1 -0
  116. package/dist/src/primitives/borderBox.d.ts +8 -0
  117. package/dist/src/primitives/borderBox.jsx +47 -0
  118. package/dist/src/primitives/borderBox.jsx.map +1 -0
  119. package/dist/src/primitives/createFocusStack.d.ts +31 -0
  120. package/dist/src/primitives/createFocusStack.jsx +62 -0
  121. package/dist/src/primitives/createFocusStack.jsx.map +1 -0
  122. package/dist/src/primitives/createInfiniteItems.d.ts +27 -0
  123. package/dist/src/primitives/createInfiniteItems.js +46 -0
  124. package/dist/src/primitives/createInfiniteItems.js.map +1 -0
  125. package/dist/src/primitives/createTag.d.ts +8 -0
  126. package/dist/src/primitives/createTag.jsx +20 -0
  127. package/dist/src/primitives/createTag.jsx.map +1 -0
  128. package/dist/src/primitives/index.d.ts +32 -0
  129. package/dist/src/primitives/index.js +31 -0
  130. package/dist/src/primitives/index.js.map +1 -0
  131. package/dist/src/primitives/portal.d.ts +5 -0
  132. package/dist/src/primitives/portal.jsx +21 -0
  133. package/dist/src/primitives/portal.jsx.map +1 -0
  134. package/dist/src/primitives/router.d.ts +20 -0
  135. package/dist/src/primitives/router.js +90 -0
  136. package/dist/src/primitives/router.js.map +1 -0
  137. package/dist/src/primitives/types.d.ts +79 -0
  138. package/dist/src/primitives/types.js +2 -0
  139. package/dist/src/primitives/types.js.map +1 -0
  140. package/dist/src/primitives/useFocusManager.d.ts +5 -0
  141. package/dist/src/primitives/useFocusManager.js +21 -0
  142. package/dist/src/primitives/useFocusManager.js.map +1 -0
  143. package/dist/src/primitives/useHold.d.ts +27 -0
  144. package/dist/src/primitives/useHold.js +54 -0
  145. package/dist/src/primitives/useHold.js.map +1 -0
  146. package/dist/src/primitives/useMouse.d.ts +18 -0
  147. package/dist/src/primitives/useMouse.js +240 -0
  148. package/dist/src/primitives/useMouse.js.map +1 -0
  149. package/dist/src/primitives/utils/chainFunctions.d.ts +30 -0
  150. package/dist/src/primitives/utils/chainFunctions.js +32 -0
  151. package/dist/src/primitives/utils/chainFunctions.js.map +1 -0
  152. package/dist/src/primitives/utils/createBlurredImage.d.ts +56 -0
  153. package/dist/src/primitives/utils/createBlurredImage.js +223 -0
  154. package/dist/src/primitives/utils/createBlurredImage.js.map +1 -0
  155. package/dist/src/primitives/utils/createSpriteMap.d.ts +9 -0
  156. package/dist/src/primitives/utils/createSpriteMap.js +18 -0
  157. package/dist/src/primitives/utils/createSpriteMap.js.map +1 -0
  158. package/dist/src/primitives/utils/handleNavigation.d.ts +103 -0
  159. package/dist/src/primitives/utils/handleNavigation.js +300 -0
  160. package/dist/src/primitives/utils/handleNavigation.js.map +1 -0
  161. package/dist/src/primitives/utils/withScrolling.d.ts +22 -0
  162. package/dist/src/primitives/utils/withScrolling.js +186 -0
  163. package/dist/src/primitives/utils/withScrolling.js.map +1 -0
  164. package/dist/src/render.d.ts +28 -0
  165. package/dist/src/render.js +107 -0
  166. package/dist/src/render.js.map +1 -0
  167. package/dist/src/solidOpts.d.ts +15 -0
  168. package/dist/src/solidOpts.js +93 -0
  169. package/dist/src/solidOpts.js.map +1 -0
  170. package/dist/src/types.d.ts +11 -0
  171. package/dist/src/types.js +2 -0
  172. package/dist/src/types.js.map +1 -0
  173. package/dist/src/utils.d.ts +19 -0
  174. package/dist/src/utils.js +94 -0
  175. package/dist/src/utils.js.map +1 -0
  176. package/dist/tsconfig.tsbuildinfo +1 -0
  177. package/docs/AI_CODING_GUIDELINES.md +108 -0
  178. package/docs/README.md +35 -0
  179. package/jsx-runtime.d.ts +14 -0
  180. package/package.json +125 -0
  181. package/src/activeElement.ts +5 -0
  182. package/src/core/config.ts +101 -0
  183. package/src/core/dom-renderer/domRenderer.ts +2066 -0
  184. package/src/core/dom-renderer/domRendererTypes.ts +148 -0
  185. package/src/core/dom-renderer/domRendererUtils.ts +292 -0
  186. package/src/core/elementNode.ts +1623 -0
  187. package/src/core/flex.ts +331 -0
  188. package/src/core/flexLayout.ts +414 -0
  189. package/src/core/focusKeyTypes.ts +90 -0
  190. package/src/core/focusManager.ts +374 -0
  191. package/src/core/index.ts +15 -0
  192. package/src/core/intrinsicTypes.ts +216 -0
  193. package/src/core/lightningInit.ts +45 -0
  194. package/src/core/nodeTypes.ts +6 -0
  195. package/src/core/shaders.ts +187 -0
  196. package/src/core/states.ts +91 -0
  197. package/src/core/utils.ts +222 -0
  198. package/src/devtools/index.ts +77 -0
  199. package/src/index.ts +18 -0
  200. package/src/primitives/Column.tsx +59 -0
  201. package/src/primitives/FPSCounter.tsx +209 -0
  202. package/src/primitives/FadeInOut.tsx +58 -0
  203. package/src/primitives/Grid.tsx +151 -0
  204. package/src/primitives/Image.tsx +56 -0
  205. package/src/primitives/KeepAlive.tsx +268 -0
  206. package/src/primitives/Lazy.tsx +128 -0
  207. package/src/primitives/LazyImport.ts +53 -0
  208. package/src/primitives/Marquee.tsx +149 -0
  209. package/src/primitives/Preserve.tsx +18 -0
  210. package/src/primitives/Row.tsx +58 -0
  211. package/src/primitives/Suspense.tsx +39 -0
  212. package/src/primitives/Virtual.tsx +602 -0
  213. package/src/primitives/VirtualGrid.tsx +220 -0
  214. package/src/primitives/Visible.tsx +59 -0
  215. package/src/primitives/announcer/announcer.ts +198 -0
  216. package/src/primitives/announcer/index.ts +20 -0
  217. package/src/primitives/announcer/speech.ts +334 -0
  218. package/src/primitives/borderBox.tsx +69 -0
  219. package/src/primitives/createFocusStack.tsx +79 -0
  220. package/src/primitives/createInfiniteItems.ts +66 -0
  221. package/src/primitives/createTag.tsx +33 -0
  222. package/src/primitives/index.ts +37 -0
  223. package/src/primitives/portal.tsx +25 -0
  224. package/src/primitives/router.ts +147 -0
  225. package/src/primitives/types.ts +109 -0
  226. package/src/primitives/useFocusManager.ts +47 -0
  227. package/src/primitives/useHold.ts +69 -0
  228. package/src/primitives/useMouse.ts +417 -0
  229. package/src/primitives/utils/chainFunctions.ts +60 -0
  230. package/src/primitives/utils/createBlurredImage.ts +366 -0
  231. package/src/primitives/utils/createSpriteMap.ts +32 -0
  232. package/src/primitives/utils/handleNavigation.ts +373 -0
  233. package/src/primitives/utils/withScrolling.ts +238 -0
  234. package/src/render.ts +164 -0
  235. package/src/solidOpts.ts +107 -0
  236. package/src/types.ts +22 -0
  237. package/src/utils.ts +129 -0
@@ -0,0 +1,2066 @@
1
+ /*
2
+
3
+ Experimental DOM renderer
4
+
5
+ */
6
+
7
+ import * as lng from '@solidtv/renderer';
8
+
9
+ import { EventEmitter } from '@solidtv/renderer/utils';
10
+ import { Config } from '../config.js';
11
+ import { FontLoadOptions } from '../intrinsicTypes.js';
12
+ import type {
13
+ DomRendererMainSettings,
14
+ ExtractProps,
15
+ IRendererMain,
16
+ IRendererNode,
17
+ IRendererNodeProps,
18
+ IRendererShader,
19
+ IRendererStage,
20
+ IRendererTextNode,
21
+ IRendererTextNodeProps,
22
+ } from './domRendererTypes.js';
23
+ import {
24
+ applyEasing,
25
+ applySubTextureScaling,
26
+ buildGradientStops,
27
+ colorToRgba,
28
+ compactString,
29
+ computeLegacyObjectFit,
30
+ computeRenderStateForNode,
31
+ getNodeLineHeight,
32
+ interpolateProp,
33
+ isRenderStateInBounds,
34
+ nodeHasTextureSource,
35
+ } from './domRendererUtils.js';
36
+
37
+ // Feature detection for legacy brousers
38
+ const _styleRef: any =
39
+ typeof document !== 'undefined' ? document.documentElement?.style || {} : {};
40
+
41
+ const supportsObjectFit: boolean = 'objectFit' in _styleRef;
42
+ const supportsObjectPosition: boolean = 'objectPosition' in _styleRef;
43
+ const supportsMixBlendMode: boolean = 'mixBlendMode' in _styleRef;
44
+ const supportsStandardMask: boolean = 'maskImage' in _styleRef;
45
+ const supportsWebkitMask: boolean = 'webkitMaskImage' in _styleRef;
46
+ const supportsCssMask: boolean = supportsStandardMask || supportsWebkitMask;
47
+
48
+ /*
49
+ Animations
50
+ */
51
+
52
+ let animationTasks: AnimationController[] = [];
53
+ let animationFrameRequested = false;
54
+
55
+ function requestAnimationUpdate() {
56
+ if (!animationFrameRequested && animationTasks.length > 0) {
57
+ animationFrameRequested = true;
58
+ requestAnimationFrame(updateAnimations);
59
+ }
60
+ }
61
+
62
+ function updateAnimations(time: number) {
63
+ animationFrameRequested = false;
64
+
65
+ /*
66
+ tasks are iterated in insertion order
67
+ so that the later task will override the earlier ones
68
+ */
69
+ for (let i = 0; i < animationTasks.length; i++) {
70
+ let task = animationTasks[i]!;
71
+ if (task.pausedTime != null) continue;
72
+
73
+ let elapsed = time - task.timeStart;
74
+
75
+ // Still in delay period
76
+ if (elapsed < task.settings.delay) {
77
+ requestAnimationUpdate();
78
+ continue;
79
+ }
80
+
81
+ let activeTime = elapsed - task.settings.delay;
82
+
83
+ if (activeTime >= task.settings.duration) {
84
+ // Start next iteration
85
+ if (task.settings.loop || task.iteration < task.settings.repeat - 1) {
86
+ task.iteration++;
87
+ task.timeStart = time - task.settings.delay;
88
+ requestAnimationUpdate();
89
+ }
90
+ // Animation complete
91
+ else {
92
+ Object.assign(task.node.props, task.propsEnd);
93
+ task.node.boundsDirty = true;
94
+ task.node.markChildrenBoundsDirty();
95
+ updateNodeStyles(task.node);
96
+
97
+ task.stop();
98
+ i--;
99
+ }
100
+ continue;
101
+ }
102
+
103
+ /*
104
+ Update props and styles
105
+ */
106
+ let t = activeTime / task.settings.duration;
107
+ t = applyEasing(task.settings.easing, t);
108
+
109
+ for (let prop in task.propsEnd) {
110
+ let start = task.propsStart[prop]!;
111
+ let end = task.propsEnd[prop]!;
112
+ (task.node.props as any)[prop] = interpolateProp(prop, start, end, t);
113
+ }
114
+
115
+ updateNodeStyles(task.node);
116
+ }
117
+
118
+ requestAnimationUpdate();
119
+ }
120
+
121
+ class AnimationController implements lng.IAnimationController {
122
+ state: lng.AnimationControllerState = 'paused';
123
+
124
+ stopPromise: Promise<void> | null = null;
125
+ stopResolve: (() => void) | null = null;
126
+
127
+ propsStart: Record<string, number> = {};
128
+ propsEnd: Record<string, number> = {};
129
+ timeStart: number = performance.now();
130
+ timeEnd: number;
131
+ settings: Required<lng.AnimationSettings>;
132
+ iteration: number = 0;
133
+ pausedTime: number | null = null;
134
+
135
+ constructor(
136
+ public node: DOMNode,
137
+ props: Partial<lng.INodeAnimateProps<any>>,
138
+ rawSettings: Partial<lng.AnimationSettings>,
139
+ ) {
140
+ this.settings = {
141
+ duration: rawSettings.duration ?? 300,
142
+ delay: rawSettings.delay ?? 0,
143
+ easing: rawSettings.easing ?? 'linear',
144
+ loop: rawSettings.loop ?? false,
145
+ repeat: rawSettings.repeat ?? 1,
146
+ stopMethod: false,
147
+ };
148
+
149
+ this.timeEnd =
150
+ this.timeStart + this.settings.delay + this.settings.duration;
151
+
152
+ for (let [prop, value] of Object.entries(props)) {
153
+ if (value != null && typeof value === 'number') {
154
+ this.propsStart[prop] = (node.props as any)[prop];
155
+ this.propsEnd[prop] = value;
156
+ }
157
+ }
158
+
159
+ animationTasks.push(this);
160
+ }
161
+
162
+ start() {
163
+ if (this.pausedTime != null) {
164
+ this.timeStart += performance.now() - this.pausedTime;
165
+ this.pausedTime = null;
166
+ } else {
167
+ this.timeStart = performance.now();
168
+ }
169
+ this.state = 'running';
170
+ requestAnimationUpdate();
171
+ return this;
172
+ }
173
+ pause() {
174
+ this.pausedTime = performance.now();
175
+ this.state = 'paused';
176
+ return this;
177
+ }
178
+ stop() {
179
+ let index = animationTasks.indexOf(this);
180
+ if (index !== -1) {
181
+ animationTasks.splice(index, 1);
182
+ }
183
+ this.state = 'stopped';
184
+ if (this.stopResolve) {
185
+ this.stopResolve();
186
+ this.stopResolve = null;
187
+ this.stopPromise = null;
188
+ }
189
+ return this;
190
+ }
191
+ restore() {
192
+ return this;
193
+ }
194
+ waitUntilStopped() {
195
+ this.stopPromise ??= new Promise((resolve) => {
196
+ this.stopResolve = resolve;
197
+ });
198
+ return this.stopPromise;
199
+ }
200
+ on() {
201
+ return this;
202
+ }
203
+ once() {
204
+ return this;
205
+ }
206
+ off() {
207
+ return this;
208
+ }
209
+ emit() {
210
+ return this;
211
+ }
212
+ }
213
+
214
+ function animate(
215
+ this: DOMNode,
216
+ props: Partial<lng.INodeAnimateProps<any>>,
217
+ settings: Partial<lng.AnimationSettings>,
218
+ ): lng.IAnimationController {
219
+ return new AnimationController(this, props, settings);
220
+ }
221
+
222
+ /*
223
+ Node Properties
224
+ */
225
+
226
+ let elMap = new WeakMap<DOMNode, HTMLElement>();
227
+
228
+ function updateNodeParent(node: DOMNode | DOMText) {
229
+ const parent = node.props.parent;
230
+ if (parent instanceof DOMNode) {
231
+ elMap.get(parent)!.appendChild(node.div);
232
+ }
233
+ }
234
+
235
+ function updateNodeStyles(node: DOMNode | DOMText) {
236
+ let { props } = node;
237
+
238
+ let style = `position: absolute; z-index: ${props.zIndex}; opacity: ${props.alpha ?? 1};`;
239
+
240
+ if (props.clipping) {
241
+ style += `overflow: hidden;`;
242
+ }
243
+
244
+ // Transform
245
+ {
246
+ let transform = '';
247
+
248
+ let { x, y } = props;
249
+
250
+ const hasMountX = props.mountX != null && props.mountX !== 0;
251
+ const hasMountY = props.mountY != null && props.mountY !== 0;
252
+
253
+ if (x !== 0) transform += `translateX(${x}px)`;
254
+ if (hasMountX) transform += `translateX(${-props.mountX! * 100}%)`;
255
+
256
+ if (y !== 0) transform += `translateY(${y}px)`;
257
+ if (hasMountY) transform += `translateY(${-props.mountY! * 100}%)`;
258
+
259
+ if (props.rotation !== 0) transform += `rotate(${props.rotation}rad)`;
260
+
261
+ if (props.scale !== 1 && props.scale != null) {
262
+ transform += `scale(${props.scale})`;
263
+ } else {
264
+ if (props.scaleX !== 1) transform += `scaleX(${props.scaleX})`;
265
+ if (props.scaleY !== 1) transform += `scaleY(${props.scaleY})`;
266
+ }
267
+
268
+ if (transform.length > 0) {
269
+ style += `transform: ${transform};`;
270
+ }
271
+
272
+ let pivotX = props.pivotX ?? props.pivot ?? 0.5;
273
+ let pivotY = props.pivotY ?? props.pivot ?? 0.5;
274
+ if (pivotX !== 0.5 || pivotY !== 0.5) {
275
+ style += `transform-origin: ${pivotX * 100}% ${pivotY * 100}%;`;
276
+ }
277
+ }
278
+
279
+ // <Text>
280
+ if (node instanceof DOMText) {
281
+ let textProps = node.props;
282
+
283
+ if (textProps.color != null && textProps.color !== 0) {
284
+ style += `color: ${colorToRgba(textProps.color)};`;
285
+ }
286
+ if (textProps.fontFamily) {
287
+ style += `font-family: ${textProps.fontFamily};`;
288
+ }
289
+ if (textProps.fontSize) {
290
+ style += `font-size: ${textProps.fontSize}px;`;
291
+ }
292
+ if (textProps.fontStyle !== 'normal') {
293
+ style += `font-style: ${textProps.fontStyle};`;
294
+ }
295
+ if (textProps.fontWeight !== 'normal') {
296
+ style += `font-weight: ${textProps.fontWeight};`;
297
+ }
298
+ if (textProps.fontStretch && textProps.fontStretch !== 'normal') {
299
+ style += `font-stretch: ${textProps.fontStretch};`;
300
+ }
301
+ if (textProps.lineHeight) {
302
+ style += `line-height: ${textProps.lineHeight}px;`;
303
+ }
304
+ if (textProps.letterSpacing) {
305
+ style += `letter-spacing: ${textProps.letterSpacing}px;`;
306
+ }
307
+ if (textProps.textAlign !== 'left') {
308
+ style += `text-align: ${textProps.textAlign};`;
309
+ }
310
+
311
+ let maxLines = textProps.maxLines || Infinity;
312
+ switch (textProps.contain) {
313
+ case 'width':
314
+ if (textProps.maxWidth && textProps.maxWidth > 0) {
315
+ if (node.textAlign === 'center') {
316
+ style += `width: ${textProps.maxWidth}px;`;
317
+ } else {
318
+ style += `max-width: ${textProps.maxWidth}px;`;
319
+ }
320
+ style += `overflow: hidden;`;
321
+ } else {
322
+ style += `width: 100%;`;
323
+ }
324
+ break;
325
+ case 'both': {
326
+ let lineHeight = getNodeLineHeight(textProps);
327
+ const widthConstraint =
328
+ textProps.maxWidth && textProps.maxWidth > 0
329
+ ? `${textProps.maxWidth}px`
330
+ : `100%`;
331
+ const heightConstraint =
332
+ textProps.maxHeight && textProps.maxHeight > 0
333
+ ? textProps.maxHeight
334
+ : props.h;
335
+
336
+ let height = heightConstraint || 0;
337
+ if (height > 0) {
338
+ const maxLinesByHeight = Math.max(1, Math.floor(height / lineHeight));
339
+ maxLines = Math.min(maxLines, maxLinesByHeight);
340
+ height = Math.max(lineHeight, maxLines * lineHeight);
341
+ } else {
342
+ maxLines = Number.isFinite(maxLines) ? Math.max(1, maxLines) : 1;
343
+ height = maxLines * lineHeight;
344
+ }
345
+
346
+ style += `width: ${widthConstraint}; height: ${height}px; overflow: hidden;`;
347
+ break;
348
+ }
349
+ case 'none':
350
+ style += `width: -webkit-max-content;`;
351
+ style += `width: max-content;`;
352
+ break;
353
+ }
354
+
355
+ style += `white-space: pre-wrap;`;
356
+
357
+ if (maxLines !== Infinity) {
358
+ // https://stackoverflow.com/a/13924997
359
+ style += `display: -webkit-box;
360
+ overflow: hidden;
361
+ -webkit-line-clamp: ${maxLines};
362
+ line-clamp: ${maxLines};
363
+ -webkit-box-orient: vertical;`;
364
+ }
365
+
366
+ if (textProps.offsetY != null && textProps.offsetY !== 0) {
367
+ style += `margin-top: ${textProps.offsetY}px;`;
368
+ }
369
+
370
+ if (textProps.wordBreak) {
371
+ const wb = textProps.wordBreak as string;
372
+ if (wb !== 'normal') {
373
+ if (wb === 'break-all') {
374
+ style += `word-break: break-all;`;
375
+ } else if (wb === 'keep-all') {
376
+ style += `word-break: keep-all;`;
377
+ } else if (wb === 'break-word') {
378
+ style += `word-wrap: break-word; overflow-wrap: break-word;`;
379
+ } else {
380
+ style += `overflow-wrap: break-word;`;
381
+ }
382
+ }
383
+ }
384
+
385
+ // if (node.overflowSuffix) style += `overflow-suffix: ${node.overflowSuffix};`
386
+ // if (node.verticalAlign) style += `vertical-align: ${node.verticalAlign};`
387
+ }
388
+ // <Node>
389
+ else {
390
+ if (props.w !== 0) style += `width: ${props.w < 0 ? 0 : props.w}px;`;
391
+ if (props.h !== 0) style += `height: ${props.h}px;`;
392
+
393
+ let vGradient =
394
+ props.colorBottom !== props.colorTop
395
+ ? `linear-gradient(to bottom, ${colorToRgba(props.colorTop)}, ${colorToRgba(props.colorBottom)})`
396
+ : null;
397
+
398
+ let hGradient =
399
+ props.colorLeft !== props.colorRight
400
+ ? `linear-gradient(to right, ${colorToRgba(props.colorLeft)}, ${colorToRgba(props.colorRight)})`
401
+ : null;
402
+
403
+ let gradient =
404
+ vGradient && hGradient
405
+ ? `${vGradient}, ${hGradient}`
406
+ : vGradient || hGradient;
407
+
408
+ let srcImg: string | null = null;
409
+ let srcPos: null | InstanceType<lng.TextureMap['SubTexture']>['props'] =
410
+ null;
411
+ let rawImgSrc: string | null = null;
412
+
413
+ if (
414
+ props.texture != null &&
415
+ props.texture.type === lng.TextureType.subTexture
416
+ ) {
417
+ const texture = props.texture as InstanceType<
418
+ lng.TextureMap['SubTexture']
419
+ >;
420
+ srcPos = texture.props;
421
+ rawImgSrc = (texture.props.texture as any).props.src;
422
+ } else if (props.src) {
423
+ rawImgSrc = props.src;
424
+ }
425
+
426
+ if (rawImgSrc) {
427
+ srcImg = `url(${rawImgSrc})`;
428
+ }
429
+
430
+ let bgStyle = '';
431
+ let borderStyle = '';
432
+ let radiusStyle = '';
433
+ let maskStyle = '';
434
+ let needsBackgroundLayer = false;
435
+ let imgStyle = '';
436
+ let hasDivBgTint = false;
437
+
438
+ if (rawImgSrc) {
439
+ needsBackgroundLayer = true;
440
+
441
+ const hasTint = props.color !== 0xffffffff && props.color !== 0x00000000;
442
+
443
+ if (hasTint) {
444
+ bgStyle += `background-color: ${colorToRgba(props.color)};`;
445
+ if (srcImg) {
446
+ maskStyle += `mask-image: ${srcImg};`;
447
+ if (srcPos !== null) {
448
+ maskStyle += `mask-position: -${srcPos.x}px -${srcPos.y}px;`;
449
+ } else {
450
+ maskStyle += `mask-size: 100% 100%;`;
451
+ }
452
+ hasDivBgTint = true;
453
+ }
454
+ } else if (gradient) {
455
+ // use gradient as a mask when no tint is applied
456
+ maskStyle += `mask-image: ${gradient};`;
457
+ }
458
+
459
+ const imgStyleParts = [
460
+ 'position: absolute',
461
+ 'top: 0',
462
+ 'left: 0',
463
+ 'right: 0',
464
+ 'bottom: 0',
465
+ 'display: block',
466
+ 'pointer-events: none',
467
+ ];
468
+
469
+ if (props.textureOptions.resizeMode?.type) {
470
+ const resizeMode = props.textureOptions.resizeMode;
471
+ imgStyleParts.push('width: 100%');
472
+ imgStyleParts.push('height: 100%');
473
+ imgStyleParts.push(`object-fit: ${resizeMode.type}`);
474
+
475
+ // Handle clipX and clipY for object-position
476
+ const clipX = (resizeMode as any).clipX ?? 0.5;
477
+ const clipY = (resizeMode as any).clipY ?? 0.5;
478
+ imgStyleParts.push(`object-position: ${clipX * 100}% ${clipY * 100}%`);
479
+ } else if (srcPos !== null) {
480
+ imgStyleParts.push('width: auto');
481
+ imgStyleParts.push('height: auto');
482
+ imgStyleParts.push('object-fit: none');
483
+ imgStyleParts.push(`object-position: -${srcPos.x}px -${srcPos.y}px`);
484
+ } else if (props.w && !props.h) {
485
+ imgStyleParts.push('width: 100%');
486
+ imgStyleParts.push('height: auto');
487
+ } else if (props.h && !props.w) {
488
+ imgStyleParts.push('width: auto');
489
+ imgStyleParts.push('height: 100%');
490
+ } else {
491
+ imgStyleParts.push('width: 100%');
492
+ imgStyleParts.push('height: 100%');
493
+ imgStyleParts.push('object-fit: fill');
494
+ }
495
+ if (hasTint) {
496
+ if (supportsMixBlendMode) {
497
+ imgStyleParts.push('mix-blend-mode: multiply');
498
+ } else {
499
+ imgStyleParts.push('opacity: 0.9');
500
+ }
501
+ }
502
+
503
+ imgStyle = imgStyleParts.join('; ') + ';';
504
+ } else if (gradient) {
505
+ bgStyle += `background-image: ${gradient};`;
506
+ bgStyle += `background-repeat: no-repeat;`;
507
+ bgStyle += `background-size: 100% 100%;`;
508
+ } else if (props.color !== 0) {
509
+ bgStyle += `background-color: ${colorToRgba(props.color)};`;
510
+ }
511
+
512
+ if (props.shader?.props != null) {
513
+ let shaderProps = props.shader.props;
514
+
515
+ let borderWidth = shaderProps['border-w'];
516
+ let borderColor = shaderProps['border-color'];
517
+ let borderGap = shaderProps['border-gap'] ?? 0;
518
+ let borderAlign = shaderProps['border-align'] ?? 'inside';
519
+ let radius = shaderProps['radius'];
520
+
521
+ // Border
522
+ const borderWidthIsNumber = typeof borderWidth === 'number';
523
+ const borderWidthIsArray = Array.isArray(borderWidth);
524
+ const borderWidthHasValue =
525
+ (borderWidthIsNumber && borderWidth !== 0) ||
526
+ (borderWidthIsArray &&
527
+ (borderWidth as number[]).some(
528
+ (w) => typeof w === 'number' && w !== 0,
529
+ ));
530
+
531
+ if (
532
+ borderWidthHasValue &&
533
+ typeof borderColor === 'number' &&
534
+ borderColor !== 0
535
+ ) {
536
+ const rgbaColor = colorToRgba(borderColor);
537
+
538
+ if (borderWidthIsNumber) {
539
+ let insideWidth = 0;
540
+ let outsideWidth = 0;
541
+
542
+ if (borderAlign === 'inside') {
543
+ insideWidth = borderWidth;
544
+ } else if (borderAlign === 'center') {
545
+ insideWidth = borderWidth / 2;
546
+ outsideWidth = borderWidth / 2;
547
+ } else {
548
+ outsideWidth = borderWidth;
549
+ }
550
+
551
+ outsideWidth += borderGap;
552
+ insideWidth -= borderGap;
553
+
554
+ if (insideWidth < 0) {
555
+ outsideWidth += insideWidth;
556
+ insideWidth = 0;
557
+ }
558
+ if (outsideWidth < 0) {
559
+ insideWidth += outsideWidth;
560
+ outsideWidth = 0;
561
+ }
562
+
563
+ const shadows: string[] = [];
564
+ if (outsideWidth > 0) {
565
+ shadows.push(`0 0 0 ${outsideWidth}px ${rgbaColor}`);
566
+ }
567
+ if (insideWidth > 0) {
568
+ shadows.push(`inset 0 0 0 ${insideWidth}px ${rgbaColor}`);
569
+ }
570
+
571
+ if (shadows.length > 0) {
572
+ borderStyle += `box-shadow: ${shadows.join(', ')};`;
573
+ }
574
+ } else if (borderWidthIsArray) {
575
+ // Individual borders per side [top, right, bottom, left]
576
+ // Allow individual properties to override array values
577
+ const topWidth =
578
+ shaderProps['border-top'] ?? (borderWidth as number[])[0];
579
+ const rightWidth =
580
+ shaderProps['border-right'] ?? (borderWidth as number[])[1];
581
+ const bottomWidth =
582
+ shaderProps['border-bottom'] ?? (borderWidth as number[])[2];
583
+ const leftWidth =
584
+ shaderProps['border-left'] ?? (borderWidth as number[])[3];
585
+
586
+ const widths = [topWidth, rightWidth, bottomWidth, leftWidth];
587
+ const sides = ['top', 'right', 'bottom', 'left'] as const;
588
+
589
+ for (let i = 0; i < sides.length; i++) {
590
+ const width = widths[i];
591
+ if (typeof width === 'number' && width !== 0) {
592
+ borderStyle += `border-${sides[i]}: ${width}px solid ${rgbaColor};`;
593
+ }
594
+ }
595
+ }
596
+ }
597
+ // Rounded
598
+ if (typeof radius === 'number' && radius > 0) {
599
+ radiusStyle += `border-radius: ${radius}px;`;
600
+ } else if (Array.isArray(radius) && radius.length === 4) {
601
+ radiusStyle += `border-radius: ${radius[0]}px ${radius[1]}px ${radius[2]}px ${radius[3]}px;`;
602
+ }
603
+
604
+ if ('radial' in shaderProps) {
605
+ const rg = shaderProps.radial as
606
+ | Partial<lng.RadialGradientProps>
607
+ | undefined;
608
+ const colors = Array.isArray(rg?.colors) ? rg!.colors! : [];
609
+ const stops = Array.isArray(rg?.stops) ? rg!.stops! : undefined;
610
+ const pivot = Array.isArray(rg?.pivot) ? rg!.pivot! : [0.5, 0.5];
611
+ const width = typeof rg?.w === 'number' ? rg!.w! : props.w || 0;
612
+ const height = typeof rg?.h === 'number' ? rg!.h! : width;
613
+
614
+ if (colors.length > 0) {
615
+ const gradientStops = buildGradientStops(colors, stops);
616
+ if (gradientStops) {
617
+ if (colors.length === 1) {
618
+ // Single color -> solid fill
619
+ if (srcImg || gradient) {
620
+ maskStyle += `mask-image: linear-gradient(${gradientStops});`;
621
+ } else {
622
+ bgStyle += `background-color: ${colorToRgba(colors[0]!)};`;
623
+ }
624
+ } else {
625
+ const isEllipse = width > 0 && height > 0 && width !== height;
626
+ const pivotX = (pivot[0] ?? 0.5) * 100;
627
+ const pivotY = (pivot[1] ?? 0.5) * 100;
628
+ let sizePart = '';
629
+ if (width > 0 && height > 0) {
630
+ if (!isEllipse && width === height) {
631
+ sizePart = `${Math.round(width)}px`;
632
+ } else {
633
+ sizePart = `${Math.round(width)}px ${Math.round(height)}px`;
634
+ }
635
+ } else {
636
+ sizePart = 'closest-side';
637
+ }
638
+ const radialGradient = `radial-gradient(${isEllipse ? 'ellipse' : 'circle'} ${sizePart} at ${pivotX.toFixed(2)}% ${pivotY.toFixed(2)}%, ${gradientStops})`;
639
+ if (srcImg || gradient) {
640
+ maskStyle += `mask-image: ${radialGradient};`;
641
+ } else {
642
+ bgStyle += `background-image: ${radialGradient};`;
643
+ bgStyle += `background-repeat: no-repeat;`;
644
+ bgStyle += `background-size: 100% 100%;`;
645
+ }
646
+ }
647
+ }
648
+ }
649
+ }
650
+
651
+ if ('linear' in shaderProps) {
652
+ const lg = shaderProps.linear as
653
+ | Partial<lng.LinearGradientProps>
654
+ | undefined;
655
+ const colors = Array.isArray(lg?.colors) ? lg!.colors! : [];
656
+ const stops = Array.isArray(lg?.stops) ? lg!.stops! : undefined;
657
+ const angleRad = typeof lg?.angle === 'number' ? lg!.angle! : 0; // radians
658
+
659
+ if (colors.length > 0) {
660
+ const gradientStops = buildGradientStops(colors, stops);
661
+ if (gradientStops) {
662
+ if (colors.length === 1) {
663
+ if (srcImg || gradient) {
664
+ maskStyle += `mask-image: linear-gradient(${gradientStops});`;
665
+ } else {
666
+ bgStyle += `background-color: ${colorToRgba(colors[0]!)};`;
667
+ }
668
+ } else {
669
+ const angleDeg = 180 * (angleRad / Math.PI - 1);
670
+ const linearGradient = `linear-gradient(${angleDeg.toFixed(2)}deg, ${gradientStops})`;
671
+ if (srcImg || gradient) {
672
+ maskStyle += `mask-image: ${linearGradient};`;
673
+ } else {
674
+ bgStyle += `background-image: ${linearGradient};`;
675
+ bgStyle += `background-repeat: no-repeat;`;
676
+ bgStyle += `background-size: 100% 100%;`;
677
+ }
678
+ }
679
+ }
680
+ }
681
+ }
682
+ }
683
+
684
+ if (maskStyle !== '') {
685
+ if (!supportsStandardMask && supportsWebkitMask) {
686
+ maskStyle = maskStyle.replace(/mask-/g, '-webkit-mask-');
687
+ } else if (!supportsCssMask) {
688
+ maskStyle = '';
689
+ }
690
+ if (maskStyle !== '') {
691
+ needsBackgroundLayer = true;
692
+ }
693
+ }
694
+
695
+ style += radiusStyle;
696
+
697
+ if (needsBackgroundLayer) {
698
+ if (node.divBg == null) {
699
+ node.divBg = document.createElement('div');
700
+ node.div.insertBefore(node.divBg, node.div.firstChild);
701
+ } else if (node.divBg.parentElement !== node.div) {
702
+ node.div.insertBefore(node.divBg, node.div.firstChild);
703
+ }
704
+
705
+ let bgLayerStyle =
706
+ 'position: absolute; top:0; left:0; right:0; bottom:0; z-index: -1; pointer-events: none; overflow: hidden;';
707
+ if (bgStyle) {
708
+ bgLayerStyle += bgStyle;
709
+ }
710
+ if (maskStyle) {
711
+ bgLayerStyle += maskStyle;
712
+ }
713
+
714
+ node.divBg.setAttribute('style', bgLayerStyle + radiusStyle);
715
+
716
+ if (rawImgSrc) {
717
+ if (!node.imgEl) {
718
+ node.imgEl = document.createElement('img');
719
+ node.imgEl.alt = '';
720
+ node.imgEl.crossOrigin = 'anonymous';
721
+ node.imgEl.setAttribute('aria-hidden', 'true');
722
+ node.imgEl.setAttribute('loading', 'lazy');
723
+ node.imgEl.removeAttribute('src');
724
+
725
+ node.imgEl.addEventListener('load', () => {
726
+ const payload: lng.NodeTextureLoadedPayload = {
727
+ type: 'texture',
728
+ dimensions: {
729
+ w: node.imgEl!.naturalWidth,
730
+ h: node.imgEl!.naturalHeight,
731
+ },
732
+ };
733
+ node.imgEl!.style.display = '';
734
+ applySubTextureScaling(
735
+ node,
736
+ node.imgEl!,
737
+ node.lazyImageSubTextureProps,
738
+ );
739
+
740
+ const resizeMode = (node.props.textureOptions as any)?.resizeMode;
741
+ const clipX = resizeMode?.clipX ?? 0.5;
742
+ const clipY = resizeMode?.clipY ?? 0.5;
743
+ computeLegacyObjectFit(
744
+ node,
745
+ node.imgEl!,
746
+ resizeMode,
747
+ clipX,
748
+ clipY,
749
+ node.lazyImageSubTextureProps,
750
+ supportsObjectFit,
751
+ supportsObjectPosition,
752
+ );
753
+ node.emit('loaded', payload);
754
+ });
755
+
756
+ node.imgEl.addEventListener('error', () => {
757
+ if (node.imgEl) {
758
+ node.imgEl.removeAttribute('src');
759
+ node.imgEl.style.display = 'none';
760
+ node.imgEl.removeAttribute('data-rawSrc');
761
+ }
762
+
763
+ const failedSrc =
764
+ node.imgEl?.dataset.pendingSrc || node.lazyImagePendingSrc || '';
765
+
766
+ const payload: lng.NodeTextureFailedPayload = {
767
+ type: 'texture',
768
+ error: new Error(`Failed to load image: ${failedSrc}`),
769
+ };
770
+ node.emit('failed', payload);
771
+ });
772
+ }
773
+
774
+ node.lazyImagePendingSrc = rawImgSrc;
775
+ node.lazyImageSubTextureProps = srcPos;
776
+ node.imgEl.dataset.pendingSrc = rawImgSrc;
777
+
778
+ if (node.imgEl.parentElement !== node.divBg) {
779
+ node.divBg.appendChild(node.imgEl);
780
+ }
781
+
782
+ node.imgEl.setAttribute('style', imgStyle);
783
+
784
+ if (hasDivBgTint) {
785
+ node.imgEl.style.visibility = 'hidden';
786
+ }
787
+
788
+ if (isRenderStateInBounds(node.renderState)) {
789
+ node.applyPendingImageSrc();
790
+ } else if (!node.imgEl.dataset.rawSrc) {
791
+ node.imgEl.removeAttribute('src');
792
+ }
793
+
794
+ if (
795
+ srcPos &&
796
+ node.imgEl.complete &&
797
+ node.imgEl.dataset.rawSrc === rawImgSrc
798
+ ) {
799
+ applySubTextureScaling(node, node.imgEl, srcPos);
800
+ }
801
+ if (
802
+ !srcPos &&
803
+ node.imgEl.complete &&
804
+ (!supportsObjectFit || !supportsObjectPosition) &&
805
+ node.imgEl.dataset.rawSrc === rawImgSrc
806
+ ) {
807
+ const resizeMode = (node.props.textureOptions as any)?.resizeMode;
808
+ const clipX = resizeMode?.clipX ?? 0.5;
809
+ const clipY = resizeMode?.clipY ?? 0.5;
810
+ computeLegacyObjectFit(
811
+ node,
812
+ node.imgEl,
813
+ resizeMode,
814
+ clipX,
815
+ clipY,
816
+ srcPos,
817
+ supportsObjectFit,
818
+ supportsObjectPosition,
819
+ );
820
+ }
821
+ } else {
822
+ node.lazyImagePendingSrc = null;
823
+ node.lazyImageSubTextureProps = null;
824
+ if (node.imgEl) {
825
+ node.imgEl.remove();
826
+ node.imgEl = undefined;
827
+ }
828
+ }
829
+ } else {
830
+ node.lazyImagePendingSrc = null;
831
+ node.lazyImageSubTextureProps = null;
832
+ if (node.imgEl) {
833
+ node.imgEl.remove();
834
+ node.imgEl = undefined;
835
+ }
836
+ if (node.divBg) {
837
+ node.divBg.remove();
838
+ node.divBg = undefined;
839
+ }
840
+ style += bgStyle;
841
+ }
842
+
843
+ const needsSeparateBorderLayer = needsBackgroundLayer && maskStyle !== '';
844
+
845
+ if (needsSeparateBorderLayer) {
846
+ if (node.divBorder == null) {
847
+ node.divBorder = document.createElement('div');
848
+ node.div.appendChild(node.divBorder);
849
+ }
850
+ } else if (node.divBorder) {
851
+ node.divBorder.remove();
852
+ node.divBorder = undefined;
853
+ }
854
+
855
+ if (node.divBorder == null) {
856
+ style += borderStyle;
857
+ } else {
858
+ let borderLayerStyle =
859
+ 'position: absolute; top:0; left:0; right:0; bottom:0; z-index: -1; pointer-events: none;';
860
+ borderLayerStyle += borderStyle;
861
+ node.divBorder.setAttribute('style', borderLayerStyle + radiusStyle);
862
+ }
863
+ }
864
+
865
+ node.div.setAttribute('style', compactString(style));
866
+
867
+ if (node instanceof DOMNode && node !== node.stage.root) {
868
+ const hasTextureSrc = nodeHasTextureSource(node);
869
+ if (hasTextureSrc && node.boundsDirty) {
870
+ const next = computeRenderStateForNode(node);
871
+ if (next != null) {
872
+ node.updateRenderState(next);
873
+ }
874
+ node.boundsDirty = false;
875
+ } else if (!hasTextureSrc) {
876
+ node.boundsDirty = false;
877
+ }
878
+ }
879
+ }
880
+
881
+ const textNodesToMeasure = new Set<DOMText>();
882
+ const containTextNodes = new Set<DOMText>();
883
+ let fontLoadingListenerSetup = false;
884
+
885
+ type Size = { width: number; height: number };
886
+
887
+ function getElSize(node: DOMNode): Size {
888
+ const rawRect = node.div.getBoundingClientRect();
889
+ const dpr = Config.rendererOptions?.deviceLogicalPixelRatio ?? 1;
890
+ let width = rawRect.width / dpr;
891
+ let height = rawRect.height / dpr;
892
+
893
+ for (;;) {
894
+ if (node.props.scale != null && node.props.scale !== 1) {
895
+ width /= node.props.scale;
896
+ height /= node.props.scale;
897
+ } else {
898
+ width /= node.props.scaleX;
899
+ height /= node.props.scaleY;
900
+ }
901
+
902
+ if (node.parent instanceof DOMNode) {
903
+ node = node.parent;
904
+ } else {
905
+ break;
906
+ }
907
+ }
908
+
909
+ return { width, height };
910
+ }
911
+
912
+ /*
913
+ Text nodes with contain 'width' or 'none'
914
+ need to have their height or width calculated.
915
+ And then cause the flex layout to be recalculated.
916
+ */
917
+ function updateDOMTextSize(node: DOMText, emitLoaded = true): void {
918
+ let size: Size;
919
+ let dimensionsChanged = false;
920
+ switch (node.contain) {
921
+ case 'width':
922
+ size = getElSize(node);
923
+ if (node.props.w !== size.width) {
924
+ node.w = size.width;
925
+ dimensionsChanged = true;
926
+ }
927
+ if (node.props.h !== size.height) {
928
+ node.h = size.height;
929
+ dimensionsChanged = true;
930
+ }
931
+ break;
932
+ case 'none':
933
+ size = getElSize(node);
934
+ if (node.props.h !== size.height || node.props.w !== size.width) {
935
+ node.w = size.width;
936
+ node.h = size.height;
937
+ dimensionsChanged = true;
938
+ }
939
+ break;
940
+ }
941
+
942
+ if (emitLoaded && (!node.loaded || dimensionsChanged)) {
943
+ const payload: lng.NodeTextLoadedPayload = {
944
+ type: 'text',
945
+ dimensions: {
946
+ w: node.w,
947
+ h: node.h,
948
+ },
949
+ };
950
+ node.emit('loaded', payload);
951
+ node.loaded = true;
952
+ }
953
+ }
954
+
955
+ function updateDOMTextMeasurements() {
956
+ textNodesToMeasure.forEach((node) => updateDOMTextSize(node));
957
+ textNodesToMeasure.clear();
958
+ }
959
+
960
+ function shouldTrackContainTextNode(node: DOMText): boolean {
961
+ return node.contain === 'width' || node.contain === 'none';
962
+ }
963
+
964
+ function syncContainTextNodeTracking(node: DOMText): void {
965
+ if (shouldTrackContainTextNode(node)) {
966
+ containTextNodes.add(node);
967
+ } else {
968
+ containTextNodes.delete(node);
969
+ }
970
+ }
971
+
972
+ function scheduleContainTextNodesMeasurement(): void {
973
+ if (containTextNodes.size === 0) return;
974
+
975
+ containTextNodes.forEach((node) => {
976
+ if (node.div.isConnected) {
977
+ textNodesToMeasure.add(node);
978
+ }
979
+ });
980
+
981
+ if (textNodesToMeasure.size > 0) {
982
+ setTimeout(updateDOMTextMeasurements);
983
+ }
984
+ }
985
+
986
+ function setupFontLoadingListeners(): void {
987
+ if (fontLoadingListenerSetup) return;
988
+ if (
989
+ typeof document === 'undefined' ||
990
+ !(document.fonts as FontFaceSet | undefined)
991
+ ) {
992
+ return;
993
+ }
994
+
995
+ const fonts = document.fonts;
996
+ if (typeof fonts.addEventListener === 'function') {
997
+ fonts.addEventListener('loadingdone', scheduleContainTextNodesMeasurement);
998
+ }
999
+
1000
+ fontLoadingListenerSetup = true;
1001
+ }
1002
+
1003
+ function scheduleUpdateDOMTextMeasurement(node: DOMText) {
1004
+ /*
1005
+ Make sure the font is loaded before measuring
1006
+ */
1007
+
1008
+ setupFontLoadingListeners();
1009
+
1010
+ if (textNodesToMeasure.size === 0) {
1011
+ if (typeof document !== 'undefined' && 'fonts' in document) {
1012
+ const fonts = document.fonts;
1013
+ if (fonts.status === 'loaded') {
1014
+ setTimeout(updateDOMTextMeasurements);
1015
+ } else if (fonts.ready && typeof fonts.ready.then === 'function') {
1016
+ fonts.ready.then(updateDOMTextMeasurements);
1017
+ } else {
1018
+ setTimeout(updateDOMTextMeasurements, 500);
1019
+ }
1020
+ } else {
1021
+ setTimeout(updateDOMTextMeasurements, 500);
1022
+ }
1023
+ }
1024
+
1025
+ textNodesToMeasure.add(node);
1026
+ }
1027
+
1028
+ function updateNodeData(node: DOMNode | DOMText) {
1029
+ const data = node.data;
1030
+ for (let key in data) {
1031
+ let keyValue: unknown = data[key];
1032
+ if (keyValue === undefined) {
1033
+ node.div.removeAttribute('data-' + key);
1034
+ } else {
1035
+ node.div.dataset[key] = String(keyValue);
1036
+ }
1037
+ }
1038
+ }
1039
+
1040
+ function resolveNodeDefaults(
1041
+ props: Partial<IRendererNodeProps>,
1042
+ ): IRendererNodeProps {
1043
+ const color = props.color ?? 0x00000000;
1044
+
1045
+ return {
1046
+ x: props.x ?? 0,
1047
+ y: props.y ?? 0,
1048
+ w: props.w ?? 0,
1049
+ h: props.h ?? 0,
1050
+ alpha: props.alpha ?? 1,
1051
+ autosize: props.autosize ?? false,
1052
+ boundsMargin: props.boundsMargin ?? null,
1053
+ clipping: props.clipping ?? false,
1054
+ color,
1055
+ colorTop: props.colorTop ?? color,
1056
+ colorBottom: props.colorBottom ?? color,
1057
+ colorLeft: props.colorLeft ?? color,
1058
+ colorRight: props.colorRight ?? color,
1059
+ colorBl: props.colorBl ?? props.colorBottom ?? props.colorLeft ?? color,
1060
+ colorBr: props.colorBr ?? props.colorBottom ?? props.colorRight ?? color,
1061
+ colorTl: props.colorTl ?? props.colorTop ?? props.colorLeft ?? color,
1062
+ colorTr: props.colorTr ?? props.colorTop ?? props.colorRight ?? color,
1063
+ zIndex: props.zIndex ?? 0,
1064
+ parent: props.parent ?? null,
1065
+ texture: props.texture ?? null,
1066
+ textureOptions: props.textureOptions ?? {},
1067
+ shader: props.shader ?? defaultShader,
1068
+ // Since setting the `src` will trigger a texture load, we need to set it after
1069
+ // we set the texture. Otherwise, problems happen.
1070
+ src: props.src ?? null,
1071
+ srcHeight: props.srcHeight,
1072
+ srcWidth: props.srcWidth,
1073
+ srcX: props.srcX,
1074
+ srcY: props.srcY,
1075
+ scale: props.scale ?? null,
1076
+ scaleX: props.scaleX ?? props.scale ?? 1,
1077
+ scaleY: props.scaleY ?? props.scale ?? 1,
1078
+ mount: props.mount ?? 0,
1079
+ mountX: props.mountX ?? props.mount ?? 0,
1080
+ mountY: props.mountY ?? props.mount ?? 0,
1081
+ pivot: props.pivot ?? 0.5,
1082
+ pivotX: props.pivotX ?? props.pivot ?? 0.5,
1083
+ pivotY: props.pivotY ?? props.pivot ?? 0.5,
1084
+ rotation: props.rotation ?? 0,
1085
+ rtt: props.rtt ?? false,
1086
+ data: {},
1087
+ imageType: props.imageType,
1088
+ };
1089
+ }
1090
+
1091
+ function resolveTextNodeDefaults(
1092
+ props: Partial<IRendererTextNodeProps>,
1093
+ ): IRendererTextNodeProps {
1094
+ return {
1095
+ ...resolveNodeDefaults(props),
1096
+ text: props.text ?? '',
1097
+ textRendererOverride: props.textRendererOverride ?? null,
1098
+ fontSize: props.fontSize ?? 16,
1099
+ fontFamily: props.fontFamily ?? 'sans-serif',
1100
+ fontStyle: props.fontStyle ?? 'normal',
1101
+ fontWeight: props.fontWeight ?? 'normal',
1102
+ forceLoad: props.forceLoad ?? false,
1103
+ textAlign: props.textAlign ?? 'left',
1104
+ contain: props.contain ?? 'none',
1105
+ offsetY: props.offsetY ?? 0,
1106
+ letterSpacing: props.letterSpacing ?? 0,
1107
+ lineHeight: props.lineHeight ?? 0,
1108
+ maxLines: props.maxLines ?? 0,
1109
+ maxWidth: props.maxWidth ?? 0,
1110
+ maxHeight: props.maxHeight ?? 0,
1111
+ verticalAlign: props.verticalAlign ?? 'middle',
1112
+ overflowSuffix: props.overflowSuffix ?? '...',
1113
+ wordBreak: props.wordBreak ?? 'overflow',
1114
+ };
1115
+ }
1116
+
1117
+ const defaultShader: IRendererShader = {
1118
+ shaderType: '',
1119
+ props: undefined,
1120
+ };
1121
+
1122
+ let lastNodeId = 0;
1123
+
1124
+ const CoreNodeRenderStateMap = new Map<number, string>([
1125
+ [0, 'init'],
1126
+ [2, 'outOfBounds'],
1127
+ [4, 'inBounds'],
1128
+ [8, 'inViewport'],
1129
+ ]);
1130
+
1131
+ export class DOMNode extends EventEmitter implements IRendererNode {
1132
+ div = document.createElement('div');
1133
+ divBg: HTMLElement | undefined;
1134
+ divBorder: HTMLElement | undefined;
1135
+ imgEl: HTMLImageElement | undefined;
1136
+ lazyImagePendingSrc: string | null = null;
1137
+ lazyImageSubTextureProps:
1138
+ | InstanceType<lng.TextureMap['SubTexture']>['props']
1139
+ | null = null;
1140
+ boundsDirty = true;
1141
+ children = new Set<DOMNode>();
1142
+
1143
+ id = ++lastNodeId;
1144
+
1145
+ renderState: lng.CoreNodeRenderState = 0 /* Init */;
1146
+
1147
+ preventCleanup = true;
1148
+
1149
+ constructor(
1150
+ public stage: IRendererStage,
1151
+ public props: IRendererNodeProps,
1152
+ ) {
1153
+ super();
1154
+
1155
+ // @ts-ignore
1156
+ this.div._node = this;
1157
+ this.div.setAttribute('data-id', String(this.id));
1158
+ elMap.set(this, this.div);
1159
+
1160
+ const parent = this.props.parent;
1161
+ if (parent instanceof DOMNode) {
1162
+ parent.children.add(this);
1163
+ }
1164
+
1165
+ updateNodeParent(this);
1166
+ updateNodeStyles(this);
1167
+ updateNodeData(this);
1168
+ }
1169
+
1170
+ destroy(): void {
1171
+ elMap.delete(this);
1172
+ const parent = this.props.parent;
1173
+ if (parent instanceof DOMNode) {
1174
+ parent.children.delete(this);
1175
+ }
1176
+ this.div.parentNode!.removeChild(this.div);
1177
+ }
1178
+
1179
+ get parent() {
1180
+ return this.props.parent;
1181
+ }
1182
+ set parent(value: IRendererNode | null) {
1183
+ if (this.props.parent === value) return;
1184
+
1185
+ const prevParent = this.props.parent;
1186
+ if (prevParent instanceof DOMNode) {
1187
+ prevParent.children.delete(this);
1188
+ prevParent.markChildrenBoundsDirty();
1189
+ }
1190
+
1191
+ this.props.parent = value;
1192
+
1193
+ if (value instanceof DOMNode) {
1194
+ value.children.add(this);
1195
+ value.markChildrenBoundsDirty();
1196
+ }
1197
+
1198
+ this.boundsDirty = true;
1199
+ this.markChildrenBoundsDirty();
1200
+ updateNodeParent(this);
1201
+ }
1202
+
1203
+ public markChildrenBoundsDirty() {
1204
+ for (const child of this.children) {
1205
+ child.boundsDirty = true;
1206
+
1207
+ if (child !== child.stage.root) {
1208
+ if (nodeHasTextureSource(child)) {
1209
+ const nextState = computeRenderStateForNode(child);
1210
+
1211
+ if (nextState != null) {
1212
+ child.updateRenderState(nextState);
1213
+ }
1214
+ }
1215
+ child.boundsDirty = false;
1216
+ }
1217
+
1218
+ child.markChildrenBoundsDirty();
1219
+ }
1220
+ }
1221
+
1222
+ animate = animate;
1223
+
1224
+ updateRenderState(renderState: lng.CoreNodeRenderState) {
1225
+ if (renderState === this.renderState) return;
1226
+ const previous = this.renderState;
1227
+ this.renderState = renderState;
1228
+ const event = CoreNodeRenderStateMap.get(renderState);
1229
+ if (isRenderStateInBounds(renderState)) {
1230
+ this.applyPendingImageSrc();
1231
+ }
1232
+ if (event && event !== 'init') {
1233
+ this.emit(event, { previous, current: renderState });
1234
+ }
1235
+ if (this.imgEl) {
1236
+ this.imgEl.dataset.state = event;
1237
+ }
1238
+ }
1239
+
1240
+ applyPendingImageSrc() {
1241
+ if (!this.imgEl) return;
1242
+ const pendingSrc = this.lazyImagePendingSrc;
1243
+ if (!pendingSrc) return;
1244
+ if (this.imgEl.dataset.rawSrc === pendingSrc) return;
1245
+ this.imgEl.style.display = '';
1246
+ this.imgEl.dataset.pendingSrc = pendingSrc;
1247
+ this.imgEl.src = pendingSrc;
1248
+ this.imgEl.dataset.rawSrc = pendingSrc;
1249
+ this.imgEl.dataset.pendingSrc = '';
1250
+ }
1251
+
1252
+ get x() {
1253
+ return this.props.x;
1254
+ }
1255
+ set x(v) {
1256
+ if (this.props.x === v) return;
1257
+ this.props.x = v;
1258
+ this.boundsDirty = true;
1259
+ this.markChildrenBoundsDirty();
1260
+ updateNodeStyles(this);
1261
+ }
1262
+ get y() {
1263
+ return this.props.y;
1264
+ }
1265
+ set y(v) {
1266
+ if (this.props.y === v) return;
1267
+ this.props.y = v;
1268
+ this.boundsDirty = true;
1269
+ this.markChildrenBoundsDirty();
1270
+ updateNodeStyles(this);
1271
+ }
1272
+ get w() {
1273
+ return this.props.w;
1274
+ }
1275
+ set w(v) {
1276
+ if (this.props.w === v) return;
1277
+ this.props.w = v;
1278
+ this.boundsDirty = true;
1279
+ this.markChildrenBoundsDirty();
1280
+ updateNodeStyles(this);
1281
+ }
1282
+ get h() {
1283
+ return this.props.h;
1284
+ }
1285
+ set h(v) {
1286
+ if (this.props.h === v) return;
1287
+ this.props.h = v;
1288
+ this.boundsDirty = true;
1289
+ this.markChildrenBoundsDirty();
1290
+ updateNodeStyles(this);
1291
+ }
1292
+ get width() {
1293
+ return this.props.w;
1294
+ }
1295
+ set width(v) {
1296
+ if (this.props.w === v) return;
1297
+ this.props.w = v;
1298
+ this.boundsDirty = true;
1299
+ this.markChildrenBoundsDirty();
1300
+ updateNodeStyles(this);
1301
+ }
1302
+ get height() {
1303
+ return this.props.h;
1304
+ }
1305
+ set height(v) {
1306
+ if (this.props.h === v) return;
1307
+ this.props.h = v;
1308
+ this.boundsDirty = true;
1309
+ this.markChildrenBoundsDirty();
1310
+ updateNodeStyles(this);
1311
+ }
1312
+ get alpha() {
1313
+ return this.props.alpha;
1314
+ }
1315
+ set alpha(v) {
1316
+ this.props.alpha = v;
1317
+ updateNodeStyles(this);
1318
+ }
1319
+ get autosize() {
1320
+ return this.props.autosize;
1321
+ }
1322
+ set autosize(v) {
1323
+ this.props.autosize = v;
1324
+ updateNodeStyles(this);
1325
+ }
1326
+ get clipping() {
1327
+ return this.props.clipping;
1328
+ }
1329
+ set clipping(v) {
1330
+ this.props.clipping = v;
1331
+ updateNodeStyles(this);
1332
+ }
1333
+ get color() {
1334
+ return this.props.color;
1335
+ }
1336
+ set color(v) {
1337
+ this.props.color = v;
1338
+ updateNodeStyles(this);
1339
+ }
1340
+ get colorTop() {
1341
+ return this.props.colorTop;
1342
+ }
1343
+ set colorTop(v) {
1344
+ this.props.colorTop = v;
1345
+ updateNodeStyles(this);
1346
+ }
1347
+ get colorBottom() {
1348
+ return this.props.colorBottom;
1349
+ }
1350
+ set colorBottom(v) {
1351
+ this.props.colorBottom = v;
1352
+ updateNodeStyles(this);
1353
+ }
1354
+ get colorLeft() {
1355
+ return this.props.colorLeft;
1356
+ }
1357
+ set colorLeft(v) {
1358
+ this.props.colorLeft = v;
1359
+ updateNodeStyles(this);
1360
+ }
1361
+ get colorRight() {
1362
+ return this.props.colorRight;
1363
+ }
1364
+ set colorRight(v) {
1365
+ this.props.colorRight = v;
1366
+ updateNodeStyles(this);
1367
+ }
1368
+ get colorTl() {
1369
+ return this.props.colorTl;
1370
+ }
1371
+ set colorTl(v) {
1372
+ this.props.colorTl = v;
1373
+ updateNodeStyles(this);
1374
+ }
1375
+ get colorTr() {
1376
+ return this.props.colorTr;
1377
+ }
1378
+ set colorTr(v) {
1379
+ this.props.colorTr = v;
1380
+ updateNodeStyles(this);
1381
+ }
1382
+ get colorBr() {
1383
+ return this.props.colorBr;
1384
+ }
1385
+ set colorBr(v) {
1386
+ this.props.colorBr = v;
1387
+ updateNodeStyles(this);
1388
+ }
1389
+ get colorBl() {
1390
+ return this.props.colorBl;
1391
+ }
1392
+ set colorBl(v) {
1393
+ this.props.colorBl = v;
1394
+ updateNodeStyles(this);
1395
+ }
1396
+ get zIndex() {
1397
+ return this.props.zIndex;
1398
+ }
1399
+ set zIndex(v) {
1400
+ if (this.props.zIndex === v) return;
1401
+ this.props.zIndex = Math.ceil(v);
1402
+ updateNodeStyles(this);
1403
+ }
1404
+ get texture() {
1405
+ return this.props.texture;
1406
+ }
1407
+ set texture(v) {
1408
+ if (this.props.texture === v) return;
1409
+ this.props.texture = v;
1410
+ this.boundsDirty = true;
1411
+ updateNodeStyles(this);
1412
+ }
1413
+ get textureOptions(): IRendererNode['textureOptions'] {
1414
+ return this.props.textureOptions;
1415
+ }
1416
+ set textureOptions(v) {
1417
+ this.props.textureOptions = v;
1418
+ updateNodeStyles(this);
1419
+ }
1420
+ get src() {
1421
+ return this.props.src;
1422
+ }
1423
+ set src(v) {
1424
+ if (this.props.src === v) return;
1425
+ this.props.src = v;
1426
+ this.boundsDirty = true;
1427
+ updateNodeStyles(this);
1428
+ }
1429
+ get scale() {
1430
+ return this.props.scale ?? 1;
1431
+ }
1432
+ set scale(v) {
1433
+ if (this.props.scale === v) return;
1434
+ this.props.scale = v;
1435
+ this.boundsDirty = true;
1436
+ this.markChildrenBoundsDirty();
1437
+ updateNodeStyles(this);
1438
+ }
1439
+ get scaleX() {
1440
+ return this.props.scaleX;
1441
+ }
1442
+ set scaleX(v) {
1443
+ if (this.props.scaleX === v) return;
1444
+ this.props.scaleX = v;
1445
+ this.boundsDirty = true;
1446
+ this.markChildrenBoundsDirty();
1447
+ updateNodeStyles(this);
1448
+ }
1449
+ get scaleY() {
1450
+ return this.props.scaleY;
1451
+ }
1452
+ set scaleY(v) {
1453
+ if (this.props.scaleY === v) return;
1454
+ this.props.scaleY = v;
1455
+ this.boundsDirty = true;
1456
+ this.markChildrenBoundsDirty();
1457
+ updateNodeStyles(this);
1458
+ }
1459
+ get mount() {
1460
+ return this.props.mount;
1461
+ }
1462
+ set mount(v) {
1463
+ if (this.props.mount === v) return;
1464
+ this.props.mount = v;
1465
+ this.boundsDirty = true;
1466
+ this.markChildrenBoundsDirty();
1467
+ updateNodeStyles(this);
1468
+ }
1469
+ get mountX() {
1470
+ return this.props.mountX;
1471
+ }
1472
+ set mountX(v) {
1473
+ if (this.props.mountX === v) return;
1474
+ this.props.mountX = v;
1475
+ this.boundsDirty = true;
1476
+ this.markChildrenBoundsDirty();
1477
+ updateNodeStyles(this);
1478
+ }
1479
+ get mountY() {
1480
+ return this.props.mountY;
1481
+ }
1482
+ set mountY(v) {
1483
+ if (this.props.mountY === v) return;
1484
+ this.props.mountY = v;
1485
+ this.boundsDirty = true;
1486
+ this.markChildrenBoundsDirty();
1487
+ updateNodeStyles(this);
1488
+ }
1489
+ get pivot() {
1490
+ return this.props.pivot;
1491
+ }
1492
+ set pivot(v) {
1493
+ if (this.props.pivot === v) return;
1494
+ this.props.pivot = v;
1495
+ this.boundsDirty = true;
1496
+ this.markChildrenBoundsDirty();
1497
+ updateNodeStyles(this);
1498
+ }
1499
+ get pivotX() {
1500
+ return this.props.pivotX;
1501
+ }
1502
+ set pivotX(v) {
1503
+ if (this.props.pivotX === v) return;
1504
+ this.props.pivotX = v;
1505
+ this.boundsDirty = true;
1506
+ this.markChildrenBoundsDirty();
1507
+ updateNodeStyles(this);
1508
+ }
1509
+ get pivotY() {
1510
+ return this.props.pivotY;
1511
+ }
1512
+ set pivotY(v) {
1513
+ if (this.props.pivotY === v) return;
1514
+ this.props.pivotY = v;
1515
+ this.boundsDirty = true;
1516
+ this.markChildrenBoundsDirty();
1517
+ updateNodeStyles(this);
1518
+ }
1519
+ get rotation() {
1520
+ return this.props.rotation;
1521
+ }
1522
+ set rotation(v) {
1523
+ if (this.props.rotation === v) return;
1524
+ this.props.rotation = v;
1525
+ this.boundsDirty = true;
1526
+ this.markChildrenBoundsDirty();
1527
+ updateNodeStyles(this);
1528
+ }
1529
+ get rtt() {
1530
+ return this.props.rtt;
1531
+ }
1532
+ set rtt(v) {
1533
+ this.props.rtt = v;
1534
+ updateNodeStyles(this);
1535
+ }
1536
+ get shader() {
1537
+ return this.props.shader;
1538
+ }
1539
+ set shader(v) {
1540
+ this.props.shader = v;
1541
+ updateNodeStyles(this);
1542
+ }
1543
+
1544
+ get data(): IRendererNode['data'] {
1545
+ return this.props.data;
1546
+ }
1547
+ set data(v) {
1548
+ this.props.data = v;
1549
+ updateNodeData(this);
1550
+ }
1551
+
1552
+ get imageType() {
1553
+ return this.props.imageType;
1554
+ }
1555
+ set imageType(v) {
1556
+ this.props.imageType = v;
1557
+ }
1558
+ get srcWidth() {
1559
+ return this.props.srcWidth;
1560
+ }
1561
+ set srcWidth(v) {
1562
+ this.props.srcWidth = v;
1563
+ }
1564
+ get srcHeight() {
1565
+ return this.props.srcHeight;
1566
+ }
1567
+ set srcHeight(v) {
1568
+ this.props.srcHeight = v;
1569
+ }
1570
+ get srcX() {
1571
+ return this.props.srcX;
1572
+ }
1573
+ set srcX(v) {
1574
+ this.props.srcX = v;
1575
+ }
1576
+ get srcY() {
1577
+ return this.props.srcY;
1578
+ }
1579
+ set srcY(v) {
1580
+ this.props.srcY = v;
1581
+ }
1582
+
1583
+ get boundsMargin(): number | [number, number, number, number] | null {
1584
+ return this.props.boundsMargin;
1585
+ }
1586
+ set boundsMargin(value: number | [number, number, number, number] | null) {
1587
+ this.props.boundsMargin = value;
1588
+ this.boundsDirty = true;
1589
+ this.markChildrenBoundsDirty();
1590
+ }
1591
+
1592
+ get absX(): number {
1593
+ const parent = this.props.parent;
1594
+ return (
1595
+ this.x +
1596
+ -this.w * this.mountX +
1597
+ (parent instanceof DOMNode ? parent.absX : 0)
1598
+ );
1599
+ }
1600
+ get absY(): number {
1601
+ const parent = this.props.parent;
1602
+ return (
1603
+ this.y +
1604
+ -this.h * this.mountY +
1605
+ (parent instanceof DOMNode ? parent.absY : 0)
1606
+ );
1607
+ }
1608
+ }
1609
+
1610
+ class DOMText extends DOMNode {
1611
+ public loaded = false;
1612
+
1613
+ constructor(
1614
+ stage: IRendererStage,
1615
+ public override props: IRendererTextNodeProps,
1616
+ ) {
1617
+ super(stage, props);
1618
+ this.div.innerText = props.text;
1619
+ updateNodeStyles(this);
1620
+ updateDOMTextSize(this, false);
1621
+ syncContainTextNodeTracking(this);
1622
+ scheduleUpdateDOMTextMeasurement(this);
1623
+ }
1624
+
1625
+ override destroy(): void {
1626
+ textNodesToMeasure.delete(this);
1627
+ containTextNodes.delete(this);
1628
+ super.destroy();
1629
+ }
1630
+
1631
+ get text() {
1632
+ return this.props.text;
1633
+ }
1634
+ set text(v) {
1635
+ if (this.props.text === v) return;
1636
+ this.props.text = v;
1637
+ this.div.innerText = v;
1638
+ scheduleUpdateDOMTextMeasurement(this);
1639
+ }
1640
+ get fontFamily() {
1641
+ return this.props.fontFamily;
1642
+ }
1643
+ set fontFamily(v) {
1644
+ if (this.props.fontFamily === v) return;
1645
+ this.props.fontFamily = v;
1646
+ updateNodeStyles(this);
1647
+ scheduleUpdateDOMTextMeasurement(this);
1648
+ }
1649
+ get fontSize() {
1650
+ return this.props.fontSize;
1651
+ }
1652
+ set fontSize(v) {
1653
+ if (this.props.fontSize === v) return;
1654
+ this.props.fontSize = v;
1655
+ updateNodeStyles(this);
1656
+ scheduleUpdateDOMTextMeasurement(this);
1657
+ }
1658
+ get fontStyle() {
1659
+ return this.props.fontStyle;
1660
+ }
1661
+ set fontStyle(v) {
1662
+ if (this.props.fontStyle === v) return;
1663
+ this.props.fontStyle = v;
1664
+ updateNodeStyles(this);
1665
+ scheduleUpdateDOMTextMeasurement(this);
1666
+ }
1667
+ get fontWeight() {
1668
+ return this.props.fontWeight;
1669
+ }
1670
+ set fontWeight(v) {
1671
+ if (this.props.fontWeight === v) return;
1672
+ this.props.fontWeight = v;
1673
+ updateNodeStyles(this);
1674
+ scheduleUpdateDOMTextMeasurement(this);
1675
+ }
1676
+ get fontStretch() {
1677
+ return this.props.fontStretch;
1678
+ }
1679
+ set fontStretch(v) {
1680
+ if (this.props.fontStretch === v) return;
1681
+ this.props.fontStretch = v;
1682
+ updateNodeStyles(this);
1683
+ scheduleUpdateDOMTextMeasurement(this);
1684
+ }
1685
+ get forceLoad() {
1686
+ return this.props.forceLoad;
1687
+ }
1688
+ set forceLoad(v) {
1689
+ this.props.forceLoad = v;
1690
+ }
1691
+ get lineHeight() {
1692
+ return this.props.lineHeight;
1693
+ }
1694
+ set lineHeight(v) {
1695
+ if (this.props.lineHeight === v) return;
1696
+ this.props.lineHeight = v;
1697
+ updateNodeStyles(this);
1698
+ scheduleUpdateDOMTextMeasurement(this);
1699
+ }
1700
+ get maxWidth() {
1701
+ return this.props.maxWidth;
1702
+ }
1703
+ set maxWidth(v) {
1704
+ if (this.props.maxWidth === v) return;
1705
+ this.props.maxWidth = v;
1706
+ updateNodeStyles(this);
1707
+ scheduleUpdateDOMTextMeasurement(this);
1708
+ }
1709
+ get maxHeight() {
1710
+ return this.props.maxHeight;
1711
+ }
1712
+ set maxHeight(v) {
1713
+ if (this.props.maxHeight === v) return;
1714
+ this.props.maxHeight = v;
1715
+ updateNodeStyles(this);
1716
+ scheduleUpdateDOMTextMeasurement(this);
1717
+ }
1718
+ get letterSpacing() {
1719
+ return this.props.letterSpacing;
1720
+ }
1721
+ set letterSpacing(v) {
1722
+ if (this.props.letterSpacing === v) return;
1723
+ this.props.letterSpacing = v;
1724
+ updateNodeStyles(this);
1725
+ scheduleUpdateDOMTextMeasurement(this);
1726
+ }
1727
+ get textAlign() {
1728
+ return this.props.textAlign;
1729
+ }
1730
+ set textAlign(v) {
1731
+ if (this.props.textAlign === v) return;
1732
+ this.props.textAlign = v;
1733
+ updateNodeStyles(this);
1734
+ }
1735
+ get overflowSuffix() {
1736
+ return this.props.overflowSuffix;
1737
+ }
1738
+ set overflowSuffix(v) {
1739
+ if (this.props.overflowSuffix === v) return;
1740
+ this.props.overflowSuffix = v;
1741
+ updateNodeStyles(this);
1742
+ }
1743
+ get maxLines() {
1744
+ return this.props.maxLines;
1745
+ }
1746
+ set maxLines(v) {
1747
+ if (this.props.maxLines === v) return;
1748
+ this.props.maxLines = v;
1749
+ updateNodeStyles(this);
1750
+ scheduleUpdateDOMTextMeasurement(this);
1751
+ }
1752
+ get contain() {
1753
+ return this.props.contain;
1754
+ }
1755
+ set contain(v) {
1756
+ if (this.props.contain === v) return;
1757
+ this.props.contain = v;
1758
+ syncContainTextNodeTracking(this);
1759
+ updateNodeStyles(this);
1760
+ scheduleUpdateDOMTextMeasurement(this);
1761
+ }
1762
+ get verticalAlign() {
1763
+ return this.props.verticalAlign;
1764
+ }
1765
+ set verticalAlign(v) {
1766
+ this.props.verticalAlign = v;
1767
+ updateNodeStyles(this);
1768
+ }
1769
+ get textRendererOverride() {
1770
+ return this.props.textRendererOverride;
1771
+ }
1772
+ set textRendererOverride(v) {
1773
+ this.props.textRendererOverride = v;
1774
+ updateNodeStyles(this);
1775
+ }
1776
+ get offsetY() {
1777
+ return this.props.offsetY;
1778
+ }
1779
+ set offsetY(v) {
1780
+ this.props.offsetY = v;
1781
+ updateNodeStyles(this);
1782
+ }
1783
+ get wordBreak() {
1784
+ return this.props.wordBreak;
1785
+ }
1786
+ set wordBreak(v) {
1787
+ this.props.wordBreak = v;
1788
+ updateNodeStyles(this);
1789
+ }
1790
+ }
1791
+
1792
+ function updateRootPosition(this: DOMRendererMain) {
1793
+ let { canvas, settings } = this;
1794
+
1795
+ let rect = canvas.getBoundingClientRect();
1796
+ let top = document.documentElement.scrollTop + rect.top;
1797
+ let left = document.documentElement.scrollLeft + rect.left;
1798
+
1799
+ let dpr = settings.deviceLogicalPixelRatio ?? 1;
1800
+
1801
+ let height = Math.ceil(settings.appHeight ?? 1080 / dpr);
1802
+ let width = Math.ceil(settings.appWidth ?? 1920 / dpr);
1803
+
1804
+ this.root.div.style.left = `${left}px`;
1805
+ this.root.div.style.top = `${top}px`;
1806
+ this.root.div.style.width = `${width}px`;
1807
+ this.root.div.style.height = `${height}px`;
1808
+ this.root.div.style.position = 'absolute';
1809
+ this.root.div.style.transformOrigin = '0 0 0';
1810
+ this.root.div.style.transform = `scale(${dpr}, ${dpr})`;
1811
+ this.root.div.style.overflow = 'hidden';
1812
+ }
1813
+
1814
+ export class DOMRendererMain implements IRendererMain {
1815
+ root: DOMNode;
1816
+ canvas: HTMLCanvasElement;
1817
+ stage: IRendererStage;
1818
+ private eventListeners: Map<string, Set<(target: any, data: any) => void>> =
1819
+ new Map();
1820
+
1821
+ constructor(
1822
+ public settings: DomRendererMainSettings,
1823
+ rawTarget: string | HTMLElement,
1824
+ ) {
1825
+ let target: HTMLElement;
1826
+ if (typeof rawTarget === 'string') {
1827
+ let result = document.getElementById(rawTarget);
1828
+ if (result instanceof HTMLElement) {
1829
+ target = result;
1830
+ } else {
1831
+ throw new Error(`Target #${rawTarget} not found`);
1832
+ }
1833
+ } else {
1834
+ target = rawTarget;
1835
+ }
1836
+
1837
+ let canvas = document.body.appendChild(document.createElement('canvas'));
1838
+ canvas.style.position = 'absolute';
1839
+ canvas.style.top = '0';
1840
+ canvas.style.left = '0';
1841
+ canvas.style.width = '100vw';
1842
+ canvas.style.height = '100vh';
1843
+
1844
+ this.canvas = canvas;
1845
+
1846
+ this.stage = {
1847
+ root: null!,
1848
+ renderer: {
1849
+ mode: 'canvas',
1850
+ boundsMargin: settings.boundsMargin,
1851
+ },
1852
+ shManager: {
1853
+ registerShaderType() {},
1854
+ },
1855
+ animationManager: {
1856
+ registerAnimation(anim) {
1857
+ console.log('registerAnimation', anim);
1858
+ },
1859
+ unregisterAnimation(anim) {
1860
+ console.log('unregisterAnimation', anim);
1861
+ },
1862
+ },
1863
+ loadFont: async () => {},
1864
+ cleanup() {},
1865
+ };
1866
+
1867
+ this.root = new DOMNode(
1868
+ this.stage,
1869
+ resolveNodeDefaults({
1870
+ w: settings.appWidth ?? 1920,
1871
+ h: settings.appHeight ?? 1080,
1872
+ shader: defaultShader,
1873
+ zIndex: 1,
1874
+ }),
1875
+ );
1876
+ this.stage.root = this.root;
1877
+ target.appendChild(this.root.div);
1878
+
1879
+ if (Config.fontSettings.fontFamily) {
1880
+ this.root.div.style.fontFamily = Config.fontSettings.fontFamily;
1881
+ }
1882
+ if (Config.fontSettings.fontSize) {
1883
+ this.root.div.style.fontSize = Config.fontSettings.fontSize + 'px';
1884
+ }
1885
+ if (Config.fontSettings.lineHeight) {
1886
+ this.root.div.style.lineHeight = Config.fontSettings.lineHeight + 'px';
1887
+ } else {
1888
+ this.root.div.style.lineHeight = '1.2';
1889
+ }
1890
+ if (Config.fontSettings.fontWeight) {
1891
+ if (typeof Config.fontSettings.fontWeight === 'number') {
1892
+ this.root.div.style.fontWeight = Config.fontSettings.fontWeight + 'px';
1893
+ } else {
1894
+ this.root.div.style.fontWeight = Config.fontSettings.fontWeight;
1895
+ }
1896
+ }
1897
+
1898
+ updateRootPosition.call(this);
1899
+
1900
+ new MutationObserver(updateRootPosition.bind(this)).observe(this.canvas, {
1901
+ attributes: true,
1902
+ });
1903
+ new ResizeObserver(updateRootPosition.bind(this)).observe(this.canvas);
1904
+ window.addEventListener('resize', updateRootPosition.bind(this));
1905
+ }
1906
+
1907
+ removeAllListeners(): void {
1908
+ if (this.eventListeners.size === 0) return;
1909
+ this.eventListeners.forEach((listeners) => listeners.clear());
1910
+ this.eventListeners.clear();
1911
+ }
1912
+
1913
+ once<K extends string | number>(
1914
+ event: Extract<K, string>,
1915
+ listener: { [s: string]: (target: any, data: any) => void }[K],
1916
+ ): void {
1917
+ const wrappedListener = (target: any, data: any) => {
1918
+ this.off(event, wrappedListener);
1919
+ listener(target, data);
1920
+ };
1921
+ this.on(event, wrappedListener);
1922
+ }
1923
+
1924
+ on(name: string, callback: (target: any, data: any) => void) {
1925
+ let listeners = this.eventListeners.get(name);
1926
+ if (!listeners) {
1927
+ listeners = new Set();
1928
+ this.eventListeners.set(name, listeners);
1929
+ }
1930
+ listeners.add(callback);
1931
+ }
1932
+
1933
+ off<K extends string | number>(
1934
+ event: Extract<K, string>,
1935
+ listener: { [s: string]: (target: any, data: any) => void }[K],
1936
+ ): void {
1937
+ const listeners = this.eventListeners.get(event);
1938
+ if (listeners) {
1939
+ listeners.delete(listener);
1940
+ if (listeners.size === 0) {
1941
+ this.eventListeners.delete(event);
1942
+ }
1943
+ }
1944
+ }
1945
+
1946
+ emit<K extends string | number>(
1947
+ event: Extract<K, string>,
1948
+ data: Parameters<any>[1],
1949
+ ): void;
1950
+ emit<K extends string | number>(
1951
+ event: Extract<K, string>,
1952
+ target: any,
1953
+ data: Parameters<any>[1],
1954
+ ): void;
1955
+ emit<K extends string | number>(
1956
+ event: Extract<K, string>,
1957
+ targetOrData: any,
1958
+ maybeData?: Parameters<any>[1],
1959
+ ): void {
1960
+ const listeners = this.eventListeners.get(event);
1961
+ if (!listeners || listeners.size === 0) {
1962
+ return;
1963
+ }
1964
+
1965
+ const hasExplicitTarget = arguments.length === 3;
1966
+ const target = hasExplicitTarget ? targetOrData : this.root;
1967
+ const data = hasExplicitTarget ? maybeData : targetOrData;
1968
+
1969
+ for (const listener of Array.from(listeners)) {
1970
+ try {
1971
+ listener(target, data);
1972
+ } catch (error) {
1973
+ console.error(`Error in listener for event "${event}"`, error);
1974
+ }
1975
+ }
1976
+ }
1977
+
1978
+ createNode(props: Partial<IRendererNodeProps>): IRendererNode {
1979
+ return new DOMNode(this.stage, resolveNodeDefaults(props));
1980
+ }
1981
+
1982
+ createTextNode(props: Partial<IRendererTextNodeProps>): IRendererTextNode {
1983
+ return new DOMText(this.stage, resolveTextNodeDefaults(props));
1984
+ }
1985
+
1986
+ /** TODO: restore this */
1987
+ // createShader<ShType extends keyof ShaderMap>(
1988
+ // shType: ShType,
1989
+ // props?: OptionalShaderProps<ShType>,
1990
+ // ): InstanceType<lng.ShaderMap[ShType]> {
1991
+ // return { shaderType: shType, props, program: {} } as InstanceType<
1992
+ // lng.ShaderMap[ShType]
1993
+ // >;
1994
+ // }
1995
+
1996
+ createShader(
1997
+ ...args: Parameters<typeof lng.RendererMain.prototype.createShader>
1998
+ ): ReturnType<typeof lng.RendererMain.prototype.createShader> {
1999
+ const [shaderType, props] = args;
2000
+ return {
2001
+ // @ts-ignore
2002
+ shaderType,
2003
+ props,
2004
+ program: {},
2005
+ };
2006
+ }
2007
+
2008
+ createTexture<Type extends keyof lng.TextureMap>(
2009
+ textureType: Type,
2010
+ props: ExtractProps<lng.TextureMap[Type]>,
2011
+ ): InstanceType<lng.TextureMap[Type]> {
2012
+ let type = lng.TextureType.generic;
2013
+ switch (textureType) {
2014
+ case 'SubTexture':
2015
+ type = lng.TextureType.subTexture;
2016
+ break;
2017
+ case 'ImageTexture':
2018
+ type = lng.TextureType.image;
2019
+ break;
2020
+ case 'ColorTexture':
2021
+ type = lng.TextureType.color;
2022
+ break;
2023
+ case 'NoiseTexture':
2024
+ type = lng.TextureType.noise;
2025
+ break;
2026
+ case 'RenderTexture':
2027
+ type = lng.TextureType.renderToTexture;
2028
+ break;
2029
+ }
2030
+ return { type, props } as InstanceType<lng.TextureMap[Type]>;
2031
+ }
2032
+ }
2033
+
2034
+ export function loadFontToDom(font: FontLoadOptions): void {
2035
+ // fontFamily: string;
2036
+ // metrics?: FontMetrics;
2037
+ // fontUrl?: string;
2038
+ // atlasUrl?: string;
2039
+ // atlasDataUrl?: string;
2040
+
2041
+ if (
2042
+ typeof document === 'undefined' ||
2043
+ !('fonts' in document) ||
2044
+ typeof FontFace === 'undefined' ||
2045
+ !font.fontUrl
2046
+ ) {
2047
+ return;
2048
+ }
2049
+
2050
+ const fontFace = new FontFace(font.fontFamily, `url(${font.fontUrl})`);
2051
+ const fontSet = document.fonts as FontFaceSet & {
2052
+ add?: (font: FontFace) => FontFaceSet;
2053
+ };
2054
+ fontSet.add?.(fontFace);
2055
+
2056
+ fontFace
2057
+ .load()
2058
+ .then(scheduleContainTextNodesMeasurement)
2059
+ .catch(() => {});
2060
+ }
2061
+
2062
+ export function isDomRenderer(
2063
+ r: lng.RendererMain | DOMRendererMain,
2064
+ ): r is DOMRendererMain {
2065
+ return r instanceof DOMRendererMain;
2066
+ }