@asyra/render 0.2.5 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -1
  3. package/dist/canvas-pipeline-debugger/index.d.ts +4 -0
  4. package/dist/canvas-pipeline-debugger/index.d.ts.map +1 -0
  5. package/dist/canvas-pipeline-debugger/index.js +3 -0
  6. package/dist/canvas-pipeline-debugger/index.js.map +1 -0
  7. package/dist/canvas-pipeline-debugger/overlay.d.ts +4 -0
  8. package/dist/canvas-pipeline-debugger/overlay.d.ts.map +1 -0
  9. package/dist/canvas-pipeline-debugger/overlay.js +90 -0
  10. package/dist/canvas-pipeline-debugger/overlay.js.map +1 -0
  11. package/dist/canvas-pipeline-debugger/trace-projector.d.ts +8 -0
  12. package/dist/canvas-pipeline-debugger/trace-projector.d.ts.map +1 -0
  13. package/dist/canvas-pipeline-debugger/trace-projector.js +329 -0
  14. package/dist/canvas-pipeline-debugger/trace-projector.js.map +1 -0
  15. package/dist/canvas-pipeline-debugger/types.d.ts +77 -0
  16. package/dist/canvas-pipeline-debugger/types.d.ts.map +1 -0
  17. package/dist/canvas-pipeline-debugger/types.js.map +1 -0
  18. package/dist/diagnostics/canvas-pipeline.d.ts +84 -0
  19. package/dist/diagnostics/canvas-pipeline.d.ts.map +1 -0
  20. package/dist/diagnostics/canvas-pipeline.js +197 -0
  21. package/dist/diagnostics/canvas-pipeline.js.map +1 -0
  22. package/dist/diagnostics/freeze-evidence.d.ts +2 -0
  23. package/dist/diagnostics/freeze-evidence.d.ts.map +1 -0
  24. package/dist/diagnostics/freeze-evidence.js +9 -0
  25. package/dist/diagnostics/freeze-evidence.js.map +1 -0
  26. package/dist/errors.d.ts +14 -0
  27. package/dist/errors.d.ts.map +1 -0
  28. package/dist/errors.js +19 -0
  29. package/dist/errors.js.map +1 -0
  30. package/dist/fills/even-odd-fill.d.ts +38 -0
  31. package/dist/fills/even-odd-fill.d.ts.map +1 -0
  32. package/dist/fills/even-odd-fill.js +369 -0
  33. package/dist/fills/even-odd-fill.js.map +1 -0
  34. package/dist/fills/gradient-fill.d.ts +27 -0
  35. package/dist/fills/gradient-fill.d.ts.map +1 -0
  36. package/dist/fills/gradient-fill.js +8 -0
  37. package/dist/fills/gradient-fill.js.map +1 -0
  38. package/dist/handlers/element-selection.d.ts +15 -2
  39. package/dist/handlers/element-selection.d.ts.map +1 -1
  40. package/dist/handlers/element-selection.js +19 -4
  41. package/dist/handlers/element-selection.js.map +1 -1
  42. package/dist/handlers/index.d.ts +1 -1
  43. package/dist/handlers/index.d.ts.map +1 -1
  44. package/dist/handlers/index.js +1 -1
  45. package/dist/handlers/index.js.map +1 -1
  46. package/dist/index.d.ts +23 -1
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +17 -3
  49. package/dist/index.js.map +1 -1
  50. package/dist/interaction/engine-interaction-bridge.d.ts +9 -0
  51. package/dist/interaction/engine-interaction-bridge.d.ts.map +1 -0
  52. package/dist/interaction/engine-interaction-bridge.js +22 -0
  53. package/dist/interaction/engine-interaction-bridge.js.map +1 -0
  54. package/dist/interaction/interaction-bridge.d.ts +26 -0
  55. package/dist/interaction/interaction-bridge.d.ts.map +1 -0
  56. package/dist/interaction/interaction-bridge.js +238 -0
  57. package/dist/interaction/interaction-bridge.js.map +1 -0
  58. package/dist/interaction/target-helpers.d.ts +31 -0
  59. package/dist/interaction/target-helpers.d.ts.map +1 -0
  60. package/dist/interaction/target-helpers.js +99 -0
  61. package/dist/interaction/target-helpers.js.map +1 -0
  62. package/dist/layers/overlay-layer.d.ts +30 -0
  63. package/dist/layers/overlay-layer.d.ts.map +1 -0
  64. package/dist/layers/overlay-layer.js +161 -0
  65. package/dist/layers/overlay-layer.js.map +1 -0
  66. package/dist/layers/scene/index.d.ts +2 -0
  67. package/dist/layers/scene/index.d.ts.map +1 -0
  68. package/dist/layers/scene/index.js +2 -0
  69. package/dist/layers/scene/index.js.map +1 -0
  70. package/dist/{render-layer → layers/scene}/render-layer.d.ts +15 -15
  71. package/dist/layers/scene/render-layer.d.ts.map +1 -0
  72. package/dist/layers/scene/render-layer.js +347 -0
  73. package/dist/layers/scene/render-layer.js.map +1 -0
  74. package/dist/layers/viewport/index.d.ts +2 -0
  75. package/dist/layers/viewport/index.d.ts.map +1 -0
  76. package/dist/layers/viewport/index.js +2 -0
  77. package/dist/layers/viewport/index.js.map +1 -0
  78. package/dist/{viewport-layer → layers/viewport}/viewport-layer.d.ts +23 -12
  79. package/dist/layers/viewport/viewport-layer.d.ts.map +1 -0
  80. package/dist/{viewport-layer → layers/viewport}/viewport-layer.js +31 -10
  81. package/dist/layers/viewport/viewport-layer.js.map +1 -0
  82. package/dist/pixi-renderer.d.ts +6 -0
  83. package/dist/pixi-renderer.d.ts.map +1 -0
  84. package/dist/pixi-renderer.js +6 -0
  85. package/dist/pixi-renderer.js.map +1 -0
  86. package/dist/projections/mesh-projection.d.ts +32 -0
  87. package/dist/projections/mesh-projection.d.ts.map +1 -0
  88. package/dist/projections/mesh-projection.js +192 -0
  89. package/dist/projections/mesh-projection.js.map +1 -0
  90. package/dist/registries/interaction-target.d.ts +31 -0
  91. package/dist/registries/interaction-target.d.ts.map +1 -0
  92. package/dist/registries/interaction-target.js +126 -0
  93. package/dist/registries/interaction-target.js.map +1 -0
  94. package/dist/registries/render-interaction-handler.d.ts +12 -0
  95. package/dist/registries/render-interaction-handler.d.ts.map +1 -0
  96. package/dist/registries/render-interaction-handler.js +81 -0
  97. package/dist/registries/render-interaction-handler.js.map +1 -0
  98. package/dist/registries/render-layer.d.ts +14 -0
  99. package/dist/registries/render-layer.d.ts.map +1 -0
  100. package/dist/registries/render-layer.js +44 -0
  101. package/dist/registries/render-layer.js.map +1 -0
  102. package/dist/registries/render-strategy.d.ts +12 -0
  103. package/dist/registries/render-strategy.d.ts.map +1 -0
  104. package/dist/registries/render-strategy.js +29 -0
  105. package/dist/registries/render-strategy.js.map +1 -0
  106. package/dist/render.d.ts +105 -39
  107. package/dist/render.d.ts.map +1 -1
  108. package/dist/render.js +662 -68
  109. package/dist/render.js.map +1 -1
  110. package/dist/renderer.d.ts +33 -0
  111. package/dist/renderer.d.ts.map +1 -0
  112. package/dist/renderer.js +88 -0
  113. package/dist/renderer.js.map +1 -0
  114. package/dist/stores/scene-tree.d.ts +58 -7
  115. package/dist/stores/scene-tree.d.ts.map +1 -1
  116. package/dist/stores/scene-tree.js +1270 -28
  117. package/dist/stores/scene-tree.js.map +1 -1
  118. package/dist/stores/selection.d.ts +3 -2
  119. package/dist/stores/selection.d.ts.map +1 -1
  120. package/dist/stores/selection.js +9 -4
  121. package/dist/stores/selection.js.map +1 -1
  122. package/dist/strategies/default-strategy.d.ts +4 -0
  123. package/dist/strategies/default-strategy.d.ts.map +1 -0
  124. package/dist/strategies/default-strategy.js +15 -0
  125. package/dist/strategies/default-strategy.js.map +1 -0
  126. package/dist/types/render-interaction.d.ts +25 -0
  127. package/dist/types/render-interaction.d.ts.map +1 -0
  128. package/dist/types/render-interaction.js +2 -0
  129. package/dist/types/render-interaction.js.map +1 -0
  130. package/dist/types/render-layer.d.ts +8 -0
  131. package/dist/types/render-layer.d.ts.map +1 -0
  132. package/dist/types/render-layer.js +2 -0
  133. package/dist/types/render-layer.js.map +1 -0
  134. package/dist/types/render-object.d.ts +194 -0
  135. package/dist/types/render-object.d.ts.map +1 -0
  136. package/dist/types/render-object.js +889 -0
  137. package/dist/types/render-object.js.map +1 -0
  138. package/dist/types/render-strategy.compatibility.d.ts +2 -0
  139. package/dist/types/render-strategy.compatibility.d.ts.map +1 -0
  140. package/dist/types/render-strategy.compatibility.js +2 -0
  141. package/dist/types/render-strategy.compatibility.js.map +1 -0
  142. package/dist/types/render-strategy.d.ts +35 -0
  143. package/dist/types/render-strategy.d.ts.map +1 -0
  144. package/dist/types/render-strategy.js +2 -0
  145. package/dist/types/render-strategy.js.map +1 -0
  146. package/dist/types/renderer.d.ts +86 -0
  147. package/dist/types/renderer.d.ts.map +1 -0
  148. package/dist/types/renderer.js +2 -0
  149. package/dist/types/renderer.js.map +1 -0
  150. package/dist/types.d.ts +2 -2
  151. package/dist/types.d.ts.map +1 -1
  152. package/package.json +33 -8
  153. package/.turbo/turbo-test$colon$local.log +0 -48
  154. package/CHANGELOG.md +0 -58
  155. package/dist/__tests__/render.test.d.ts +0 -2
  156. package/dist/__tests__/render.test.d.ts.map +0 -1
  157. package/dist/__tests__/render.test.js +0 -148
  158. package/dist/__tests__/render.test.js.map +0 -1
  159. package/dist/render-layer/element-interaction-handler.d.ts +0 -8
  160. package/dist/render-layer/element-interaction-handler.d.ts.map +0 -1
  161. package/dist/render-layer/element-interaction-handler.js +0 -20
  162. package/dist/render-layer/element-interaction-handler.js.map +0 -1
  163. package/dist/render-layer/index.d.ts +0 -2
  164. package/dist/render-layer/index.d.ts.map +0 -1
  165. package/dist/render-layer/index.js +0 -2
  166. package/dist/render-layer/index.js.map +0 -1
  167. package/dist/render-layer/render-layer.d.ts.map +0 -1
  168. package/dist/render-layer/render-layer.js +0 -189
  169. package/dist/render-layer/render-layer.js.map +0 -1
  170. package/dist/selection-layer/index.d.ts +0 -3
  171. package/dist/selection-layer/index.d.ts.map +0 -1
  172. package/dist/selection-layer/index.js +0 -3
  173. package/dist/selection-layer/index.js.map +0 -1
  174. package/dist/selection-layer/selection-layer.d.ts +0 -29
  175. package/dist/selection-layer/selection-layer.d.ts.map +0 -1
  176. package/dist/selection-layer/selection-layer.js +0 -63
  177. package/dist/selection-layer/selection-layer.js.map +0 -1
  178. package/dist/selection-layer/utils.d.ts +0 -13
  179. package/dist/selection-layer/utils.d.ts.map +0 -1
  180. package/dist/selection-layer/utils.js +0 -27
  181. package/dist/selection-layer/utils.js.map +0 -1
  182. package/dist/subscribes/index.d.ts +0 -2
  183. package/dist/subscribes/index.d.ts.map +0 -1
  184. package/dist/subscribes/index.js +0 -11
  185. package/dist/subscribes/index.js.map +0 -1
  186. package/dist/subscribes/scene-tree.d.ts +0 -4
  187. package/dist/subscribes/scene-tree.d.ts.map +0 -1
  188. package/dist/subscribes/scene-tree.js +0 -56
  189. package/dist/subscribes/scene-tree.js.map +0 -1
  190. package/dist/subscribes/selection.d.ts +0 -3
  191. package/dist/subscribes/selection.d.ts.map +0 -1
  192. package/dist/subscribes/selection.js +0 -31
  193. package/dist/subscribes/selection.js.map +0 -1
  194. package/dist/subscribes/system.d.ts +0 -2
  195. package/dist/subscribes/system.d.ts.map +0 -1
  196. package/dist/subscribes/system.js +0 -15
  197. package/dist/subscribes/system.js.map +0 -1
  198. package/dist/subscribes/viewport.d.ts +0 -2
  199. package/dist/subscribes/viewport.d.ts.map +0 -1
  200. package/dist/subscribes/viewport.js +0 -36
  201. package/dist/subscribes/viewport.js.map +0 -1
  202. package/dist/viewport-layer/index.d.ts +0 -4
  203. package/dist/viewport-layer/index.d.ts.map +0 -1
  204. package/dist/viewport-layer/index.js +0 -4
  205. package/dist/viewport-layer/index.js.map +0 -1
  206. package/dist/viewport-layer/types.d.ts +0 -17
  207. package/dist/viewport-layer/types.d.ts.map +0 -1
  208. package/dist/viewport-layer/types.js.map +0 -1
  209. package/dist/viewport-layer/utils.d.ts +0 -8
  210. package/dist/viewport-layer/utils.d.ts.map +0 -1
  211. package/dist/viewport-layer/utils.js +0 -49
  212. package/dist/viewport-layer/utils.js.map +0 -1
  213. package/dist/viewport-layer/viewport-layer.d.ts.map +0 -1
  214. package/dist/viewport-layer/viewport-layer.js.map +0 -1
  215. package/src/__tests__/render.test.ts +0 -209
  216. package/src/handlers/element-selection.ts +0 -7
  217. package/src/handlers/index.ts +0 -1
  218. package/src/index.ts +0 -7
  219. package/src/render-layer/element-interaction-handler.ts +0 -31
  220. package/src/render-layer/index.ts +0 -1
  221. package/src/render-layer/render-layer.ts +0 -238
  222. package/src/render.ts +0 -161
  223. package/src/selection-layer/index.ts +0 -2
  224. package/src/selection-layer/selection-layer.ts +0 -90
  225. package/src/selection-layer/utils.ts +0 -37
  226. package/src/stores/scene-tree.ts +0 -79
  227. package/src/stores/selection.ts +0 -40
  228. package/src/subscribes/index.ts +0 -11
  229. package/src/subscribes/scene-tree.ts +0 -72
  230. package/src/subscribes/selection.ts +0 -40
  231. package/src/subscribes/system.ts +0 -18
  232. package/src/subscribes/viewport.ts +0 -54
  233. package/src/types.ts +0 -15
  234. package/src/viewport-layer/index.ts +0 -3
  235. package/src/viewport-layer/types.ts +0 -18
  236. package/src/viewport-layer/utils.ts +0 -55
  237. package/src/viewport-layer/viewport-layer.ts +0 -172
  238. package/test-global.d.ts +0 -11
  239. package/test-setup.ts +0 -28
  240. package/tsconfig.json +0 -22
  241. package/vitest.config.ts +0 -8
  242. /package/dist/{viewport-layer → canvas-pipeline-debugger}/types.js +0 -0
@@ -0,0 +1,889 @@
1
+ export const createRenderResourceStyle = (descriptor) => {
2
+ const listeners = new Set();
3
+ let released = false;
4
+ const style = {
5
+ __renderResourceDescriptor: descriptor
6
+ };
7
+ Object.defineProperties(style, {
8
+ __subscribeRenderResourceRelease: {
9
+ enumerable: false,
10
+ value: (listener) => {
11
+ if (released) {
12
+ listener();
13
+ return () => undefined;
14
+ }
15
+ listeners.add(listener);
16
+ return () => listeners.delete(listener);
17
+ }
18
+ },
19
+ __isRenderResourceReleased: {
20
+ enumerable: false,
21
+ value: () => released
22
+ }
23
+ });
24
+ return {
25
+ style,
26
+ dispose: () => {
27
+ if (released) {
28
+ return;
29
+ }
30
+ released = true;
31
+ listeners.forEach((listener) => listener());
32
+ listeners.clear();
33
+ }
34
+ };
35
+ };
36
+ export class RenderMatrix {
37
+ constructor(a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0) {
38
+ this.a = a;
39
+ this.b = b;
40
+ this.c = c;
41
+ this.d = d;
42
+ this.tx = tx;
43
+ this.ty = ty;
44
+ }
45
+ clone() {
46
+ return new RenderMatrix(this.a, this.b, this.c, this.d, this.tx, this.ty);
47
+ }
48
+ apply(point) {
49
+ return {
50
+ x: this.a * point.x + this.c * point.y + this.tx,
51
+ y: this.b * point.x + this.d * point.y + this.ty
52
+ };
53
+ }
54
+ applyInverse(point) {
55
+ const determinant = this.a * this.d - this.b * this.c;
56
+ if (Math.abs(determinant) <= Number.EPSILON) {
57
+ return { x: 0, y: 0 };
58
+ }
59
+ const x = point.x - this.tx;
60
+ const y = point.y - this.ty;
61
+ return {
62
+ x: (this.d * x - this.c * y) / determinant,
63
+ y: (-this.b * x + this.a * y) / determinant
64
+ };
65
+ }
66
+ }
67
+ const multiplyMatrices = (parent, local) => new RenderMatrix(parent.a * local.a + parent.c * local.b, parent.b * local.a + parent.d * local.b, parent.a * local.c + parent.c * local.d, parent.b * local.c + parent.d * local.d, parent.a * local.tx + parent.c * local.ty + parent.tx, parent.b * local.tx + parent.d * local.ty + parent.ty);
68
+ export class RenderPoint {
69
+ constructor(onChange, x = 0, y = 0) {
70
+ this.onChange = onChange;
71
+ this.x = x;
72
+ this.y = y;
73
+ }
74
+ set(x, y = x) {
75
+ this.x = x;
76
+ this.y = y;
77
+ this.onChange(x, y);
78
+ }
79
+ }
80
+ const normalizePaint = (value) => {
81
+ if (typeof value === 'number' || typeof value === 'string') {
82
+ return { color: value };
83
+ }
84
+ if (typeof value === 'object' && value !== null) {
85
+ const style = value;
86
+ if (style.fill !== undefined) {
87
+ return normalizePaint(style.fill);
88
+ }
89
+ return {
90
+ color: typeof style.color === 'number' || typeof style.color === 'string'
91
+ ? style.color
92
+ : undefined,
93
+ alpha: typeof style.alpha === 'number' ? style.alpha : undefined
94
+ };
95
+ }
96
+ return {};
97
+ };
98
+ export class RenderObjectRuntime {
99
+ constructor(engine, root, beforeExecute) {
100
+ this.engine = engine;
101
+ this.root = root;
102
+ this.beforeExecute = beforeExecute;
103
+ this.objectByHandle = new Map();
104
+ this.dirtyGraphics = new Set();
105
+ this.resourceByStyle = new WeakMap();
106
+ this.resourceStylesByGraphics = new Map();
107
+ this.resourceReleaseSubscriptions = new Set();
108
+ }
109
+ execute(command, node, relatedNode) {
110
+ this.beforeExecute?.(command, node, relatedNode);
111
+ return this.engine.execute(command);
112
+ }
113
+ attachRoot(node) {
114
+ const handle = this.attachNode(node);
115
+ this.execute({
116
+ type: 'append-child',
117
+ parent: this.root,
118
+ child: handle
119
+ }, node);
120
+ }
121
+ detachRoot(node) {
122
+ const handle = node.getEngineHandle();
123
+ if (handle) {
124
+ this.execute({
125
+ type: 'remove-child',
126
+ parent: this.root,
127
+ child: handle
128
+ }, node);
129
+ }
130
+ }
131
+ attachNode(node) {
132
+ const existingHandle = node.getEngineHandle();
133
+ if (existingHandle) {
134
+ return existingHandle;
135
+ }
136
+ const result = this.execute({
137
+ type: 'create-object',
138
+ requestId: node.label || node.objectType,
139
+ objectType: node.objectType,
140
+ properties: node.getEngineProperties()
141
+ }, node);
142
+ if (!result.object) {
143
+ throw new Error('Render engine did not return an object handle');
144
+ }
145
+ node.bindRuntime(this, result.object);
146
+ this.objectByHandle.set(result.object, node);
147
+ node.children.forEach((child) => {
148
+ const childHandle = this.attachNode(child);
149
+ this.execute({
150
+ type: 'append-child',
151
+ parent: result.object,
152
+ child: childHandle
153
+ }, child, node);
154
+ });
155
+ if (node instanceof RenderGraphics) {
156
+ this.dirtyGraphics.add(node);
157
+ }
158
+ return result.object;
159
+ }
160
+ appendChild(parent, child, index) {
161
+ const parentHandle = parent.getEngineHandle();
162
+ if (!parentHandle) {
163
+ return;
164
+ }
165
+ const childHandle = this.attachNode(child);
166
+ this.execute({
167
+ type: 'append-child',
168
+ parent: parentHandle,
169
+ child: childHandle
170
+ }, child, parent);
171
+ if (index !== undefined) {
172
+ this.execute({
173
+ type: 'set-child-index',
174
+ parent: parentHandle,
175
+ child: childHandle,
176
+ index
177
+ }, child, parent);
178
+ }
179
+ }
180
+ removeChild(parent, child) {
181
+ const parentHandle = parent.getEngineHandle();
182
+ const childHandle = child.getEngineHandle();
183
+ if (parentHandle && childHandle) {
184
+ this.execute({
185
+ type: 'remove-child',
186
+ parent: parentHandle,
187
+ child: childHandle
188
+ }, child, parent);
189
+ }
190
+ }
191
+ setChildIndex(parent, child, index) {
192
+ const parentHandle = parent.getEngineHandle();
193
+ const childHandle = child.getEngineHandle();
194
+ if (parentHandle && childHandle) {
195
+ this.execute({
196
+ type: 'set-child-index',
197
+ parent: parentHandle,
198
+ child: childHandle,
199
+ index
200
+ }, child, parent);
201
+ }
202
+ }
203
+ updateObject(node, properties) {
204
+ const handle = node.getEngineHandle();
205
+ if (handle) {
206
+ this.execute({ type: 'update-object', object: handle, properties }, node);
207
+ }
208
+ }
209
+ destroyObject(node) {
210
+ const handle = node.getEngineHandle();
211
+ if (!handle) {
212
+ return;
213
+ }
214
+ this.execute({ type: 'destroy-object', object: handle }, node);
215
+ this.objectByHandle.delete(handle);
216
+ node.unbindRuntime();
217
+ }
218
+ markDrawDirty(graphics) {
219
+ if (graphics.getEngineHandle()) {
220
+ this.dirtyGraphics.add(graphics);
221
+ }
222
+ }
223
+ flushDraws() {
224
+ if (this.dirtyGraphics.size === 0) {
225
+ return false;
226
+ }
227
+ for (const graphics of this.dirtyGraphics) {
228
+ const handle = graphics.getEngineHandle();
229
+ if (handle) {
230
+ this.execute({
231
+ type: 'draw',
232
+ object: handle,
233
+ operations: graphics.getDrawOperations()
234
+ }, graphics);
235
+ graphics.markDrawClean();
236
+ }
237
+ }
238
+ this.dirtyGraphics.clear();
239
+ return true;
240
+ }
241
+ createPaint(style, owner) {
242
+ const resourceCandidate = typeof style === 'object' && style !== null && 'fill' in style
243
+ ? style.fill
244
+ : style;
245
+ if (typeof resourceCandidate === 'object' &&
246
+ resourceCandidate !== null &&
247
+ '__renderResourceDescriptor' in resourceCandidate) {
248
+ const resourceStyle = resourceCandidate;
249
+ if (resourceStyle.__isRenderResourceReleased?.()) {
250
+ throw new Error('Cannot use a disposed render resource style');
251
+ }
252
+ let record = this.resourceByStyle.get(resourceCandidate);
253
+ if (!record) {
254
+ const result = this.execute({
255
+ type: 'create-resource',
256
+ requestId: resourceStyle.__renderResourceDescriptor.kind,
257
+ descriptor: resourceStyle.__renderResourceDescriptor
258
+ }, owner);
259
+ if (!result.resource) {
260
+ throw new Error('Render engine did not return a resource handle');
261
+ }
262
+ const createdRecord = {
263
+ handle: result.resource,
264
+ owners: new Set()
265
+ };
266
+ record = createdRecord;
267
+ this.resourceByStyle.set(resourceCandidate, createdRecord);
268
+ const unsubscribe = resourceStyle.__subscribeRenderResourceRelease?.(() => {
269
+ this.destroyResourceRecord(resourceCandidate, createdRecord);
270
+ });
271
+ if (unsubscribe) {
272
+ createdRecord.unsubscribe = unsubscribe;
273
+ this.resourceReleaseSubscriptions.add(unsubscribe);
274
+ }
275
+ }
276
+ if (owner) {
277
+ record.owners.add(owner);
278
+ const styles = this.resourceStylesByGraphics.get(owner) ?? new Set();
279
+ styles.add(resourceCandidate);
280
+ this.resourceStylesByGraphics.set(owner, styles);
281
+ }
282
+ return { resource: record.handle };
283
+ }
284
+ return normalizePaint(style);
285
+ }
286
+ releaseGraphicResources(graphics) {
287
+ const styles = this.resourceStylesByGraphics.get(graphics);
288
+ if (!styles) {
289
+ return;
290
+ }
291
+ styles.forEach((style) => {
292
+ const record = this.resourceByStyle.get(style);
293
+ if (!record) {
294
+ return;
295
+ }
296
+ record.owners.delete(graphics);
297
+ if (record.owners.size === 0) {
298
+ this.destroyResourceRecord(style, record);
299
+ }
300
+ });
301
+ this.resourceStylesByGraphics.delete(graphics);
302
+ }
303
+ getObject(handle) {
304
+ return handle ? (this.objectByHandle.get(handle) ?? null) : null;
305
+ }
306
+ detachResourceLifecycles() {
307
+ this.resourceReleaseSubscriptions.forEach((unsubscribe) => unsubscribe());
308
+ this.resourceReleaseSubscriptions.clear();
309
+ }
310
+ destroyResourceRecord(style, record) {
311
+ if (this.resourceByStyle.get(style) !== record) {
312
+ return;
313
+ }
314
+ this.execute({
315
+ type: 'destroy-resource',
316
+ resource: record.handle
317
+ });
318
+ this.resourceByStyle.delete(style);
319
+ record.owners.forEach((owner) => {
320
+ const styles = this.resourceStylesByGraphics.get(owner);
321
+ styles?.delete(style);
322
+ if (styles?.size === 0) {
323
+ this.resourceStylesByGraphics.delete(owner);
324
+ }
325
+ });
326
+ record.owners.clear();
327
+ if (record.unsubscribe) {
328
+ record.unsubscribe();
329
+ this.resourceReleaseSubscriptions.delete(record.unsubscribe);
330
+ }
331
+ }
332
+ queryPoint(type, node, point) {
333
+ const handle = node.getEngineHandle();
334
+ if (!handle) {
335
+ return null;
336
+ }
337
+ const result = this.engine.query({
338
+ type,
339
+ object: handle,
340
+ point
341
+ });
342
+ return result.type === 'point' ? result.point : null;
343
+ }
344
+ }
345
+ export class RenderNode {
346
+ constructor(objectType) {
347
+ this.objectType = objectType;
348
+ this.children = [];
349
+ this.parent = null;
350
+ this.hitArea = null;
351
+ this.context = { batchMode: 'auto' };
352
+ this.geometry = { clear: () => undefined };
353
+ this._transform = { updateLocalTransform: () => undefined };
354
+ this.listeners = new Map();
355
+ this._x = 0;
356
+ this._y = 0;
357
+ this._width = 0;
358
+ this._height = 0;
359
+ this._visible = true;
360
+ this._renderable = true;
361
+ this._label = '';
362
+ this._eventMode = 'auto';
363
+ this._cursor = 'default';
364
+ this._alpha = 1;
365
+ this._angle = 0;
366
+ this._rotation = 0;
367
+ this._zIndex = 0;
368
+ this._batched = false;
369
+ this.sourceSpaceOrigin = { x: 0, y: 0 };
370
+ this.handle = null;
371
+ this.runtime = null;
372
+ this.position = new RenderPoint((x, y) => {
373
+ this._x = x;
374
+ this._y = y;
375
+ this.updateEngineProperties({ x, y });
376
+ });
377
+ this.scale = new RenderPoint((scaleX, scaleY) => {
378
+ this.updateEngineProperties({
379
+ scaleX: scaleX * this.getDimensionScaleX(),
380
+ scaleY: scaleY * this.getDimensionScaleY()
381
+ });
382
+ }, 1, 1);
383
+ this.skew = new RenderPoint((skewX, skewY) => {
384
+ this.updateEngineProperties({ skewX, skewY });
385
+ });
386
+ }
387
+ setSourceSpaceOrigin(origin) {
388
+ if (!Number.isFinite(origin.x) || !Number.isFinite(origin.y)) {
389
+ throw new Error('Render source-space origin must be finite');
390
+ }
391
+ this.sourceSpaceOrigin = { x: origin.x, y: origin.y };
392
+ }
393
+ sourceToLocal(point) {
394
+ return {
395
+ x: point.x - this.sourceSpaceOrigin.x,
396
+ y: point.y - this.sourceSpaceOrigin.y
397
+ };
398
+ }
399
+ localToSource(point) {
400
+ return {
401
+ x: point.x + this.sourceSpaceOrigin.x,
402
+ y: point.y + this.sourceSpaceOrigin.y
403
+ };
404
+ }
405
+ get x() {
406
+ return this._x;
407
+ }
408
+ set x(value) {
409
+ this._x = value;
410
+ this.position.x = value;
411
+ this.updateEngineProperties({ x: value });
412
+ }
413
+ get y() {
414
+ return this._y;
415
+ }
416
+ set y(value) {
417
+ this._y = value;
418
+ this.position.y = value;
419
+ this.updateEngineProperties({ y: value });
420
+ }
421
+ get width() {
422
+ return this._width || this.getLocalBounds().width;
423
+ }
424
+ set width(value) {
425
+ this._width = value;
426
+ this.updateEngineProperties({
427
+ width: value,
428
+ scaleX: this.scale.x * this.getDimensionScaleX()
429
+ });
430
+ }
431
+ get height() {
432
+ return this._height || this.getLocalBounds().height;
433
+ }
434
+ set height(value) {
435
+ this._height = value;
436
+ this.updateEngineProperties({
437
+ height: value,
438
+ scaleY: this.scale.y * this.getDimensionScaleY()
439
+ });
440
+ }
441
+ get visible() {
442
+ return this._visible;
443
+ }
444
+ set visible(value) {
445
+ this._visible = value;
446
+ this.updateEngineProperties({ visible: value });
447
+ }
448
+ get renderable() {
449
+ return this._renderable;
450
+ }
451
+ set renderable(value) {
452
+ this._renderable = value;
453
+ this.updateEngineProperties({ renderable: value });
454
+ }
455
+ get label() {
456
+ return this._label;
457
+ }
458
+ set label(value) {
459
+ this._label = value;
460
+ this.updateEngineProperties({ label: value });
461
+ }
462
+ get eventMode() {
463
+ return this._eventMode;
464
+ }
465
+ set eventMode(value) {
466
+ this._eventMode = value;
467
+ this.updateEngineProperties({ eventMode: value });
468
+ }
469
+ get cursor() {
470
+ return this._cursor;
471
+ }
472
+ set cursor(value) {
473
+ this._cursor = value;
474
+ this.updateEngineProperties({ cursor: value });
475
+ }
476
+ get alpha() {
477
+ return this._alpha;
478
+ }
479
+ set alpha(value) {
480
+ this._alpha = value;
481
+ this.updateEngineProperties({ alpha: value });
482
+ }
483
+ get angle() {
484
+ return this._angle;
485
+ }
486
+ set angle(value) {
487
+ this._angle = value;
488
+ this.updateEngineProperties({ angle: value });
489
+ }
490
+ get rotation() {
491
+ return this._rotation;
492
+ }
493
+ set rotation(value) {
494
+ this._rotation = value;
495
+ this.updateEngineProperties({ rotation: value });
496
+ }
497
+ get zIndex() {
498
+ return this._zIndex;
499
+ }
500
+ set zIndex(value) {
501
+ this._zIndex = value;
502
+ this.updateEngineProperties({ zIndex: value });
503
+ }
504
+ get batched() {
505
+ return this._batched;
506
+ }
507
+ set batched(value) {
508
+ this._batched = value;
509
+ this.updateEngineProperties({ batched: value });
510
+ }
511
+ get worldTransform() {
512
+ const rotationPlusSkewY = this.rotation + this.skew.y;
513
+ const rotationMinusSkewX = this.rotation - this.skew.x;
514
+ const scaleX = this.scale.x * this.getDimensionScaleX();
515
+ const scaleY = this.scale.y * this.getDimensionScaleY();
516
+ const local = new RenderMatrix(Math.cos(rotationPlusSkewY) * scaleX, Math.sin(rotationPlusSkewY) * scaleX, -Math.sin(rotationMinusSkewX) * scaleY, Math.cos(rotationMinusSkewX) * scaleY, this.x, this.y);
517
+ return this.parent
518
+ ? multiplyMatrices(this.parent.worldTransform, local)
519
+ : local;
520
+ }
521
+ getEngineProperties() {
522
+ return {
523
+ x: this.x,
524
+ y: this.y,
525
+ width: this._width,
526
+ height: this._height,
527
+ scaleX: this.scale.x * this.getDimensionScaleX(),
528
+ scaleY: this.scale.y * this.getDimensionScaleY(),
529
+ skewX: this.skew.x,
530
+ skewY: this.skew.y,
531
+ visible: this.visible,
532
+ renderable: this.renderable,
533
+ label: this.label,
534
+ alpha: this.alpha,
535
+ angle: this.angle,
536
+ rotation: this.rotation,
537
+ zIndex: this.zIndex,
538
+ eventMode: this.eventMode,
539
+ cursor: this.cursor,
540
+ batched: this.batched
541
+ };
542
+ }
543
+ getDimensionScaleX() {
544
+ const geometryBounds = this.__geometryLocalBounds;
545
+ return geometryBounds &&
546
+ geometryBounds.width > 0 &&
547
+ Number.isFinite(geometryBounds.width) &&
548
+ this._width > 0
549
+ ? this._width / geometryBounds.width
550
+ : 1;
551
+ }
552
+ getDimensionScaleY() {
553
+ const geometryBounds = this.__geometryLocalBounds;
554
+ return geometryBounds &&
555
+ geometryBounds.height > 0 &&
556
+ Number.isFinite(geometryBounds.height) &&
557
+ this._height > 0
558
+ ? this._height / geometryBounds.height
559
+ : 1;
560
+ }
561
+ bindRuntime(runtime, handle) {
562
+ this.runtime = runtime;
563
+ this.handle = handle;
564
+ }
565
+ unbindRuntime() {
566
+ this.runtime = null;
567
+ this.handle = null;
568
+ }
569
+ releaseRuntime() {
570
+ this.children.forEach((child) => child.releaseRuntime());
571
+ this.unbindRuntime();
572
+ }
573
+ getEngineHandle() {
574
+ return this.handle;
575
+ }
576
+ addChild(child) {
577
+ return this.addChildAt(child, this.children.length);
578
+ }
579
+ addChildAt(child, index) {
580
+ if (child.parent === this) {
581
+ this.setChildIndex(child, index);
582
+ return child;
583
+ }
584
+ const previousParent = child.parent;
585
+ const previousIndex = previousParent?.children.indexOf(child) ?? -1;
586
+ const boundedIndex = Math.max(0, Math.min(index, this.children.length));
587
+ if (previousParent && previousIndex >= 0) {
588
+ previousParent.runtime?.removeChild(previousParent, child);
589
+ }
590
+ try {
591
+ this.runtime?.appendChild(this, child, boundedIndex);
592
+ }
593
+ catch (error) {
594
+ if (previousParent && previousIndex >= 0) {
595
+ try {
596
+ this.runtime?.removeChild(this, child);
597
+ }
598
+ catch {
599
+ // The target append may have failed before attaching the child.
600
+ }
601
+ try {
602
+ previousParent.runtime?.appendChild(previousParent, child, previousIndex);
603
+ }
604
+ catch {
605
+ // Preserve the target handoff failure after rollback effort.
606
+ }
607
+ }
608
+ throw error;
609
+ }
610
+ if (previousParent && previousIndex >= 0) {
611
+ previousParent.children.splice(previousIndex, 1);
612
+ }
613
+ this.children.splice(boundedIndex, 0, child);
614
+ child.parent = this;
615
+ return child;
616
+ }
617
+ removeChild(child) {
618
+ const index = this.children.indexOf(child);
619
+ if (index >= 0) {
620
+ this.runtime?.removeChild(this, child);
621
+ this.children.splice(index, 1);
622
+ child.parent = null;
623
+ }
624
+ return child;
625
+ }
626
+ removeChildren() {
627
+ const removed = [...this.children];
628
+ removed.forEach((child) => this.removeChild(child));
629
+ return removed;
630
+ }
631
+ setChildIndex(child, index) {
632
+ const currentIndex = this.children.indexOf(child);
633
+ if (currentIndex < 0) {
634
+ return;
635
+ }
636
+ const boundedIndex = Math.max(0, Math.min(index, this.children.length - 1));
637
+ this.runtime?.setChildIndex(this, child, boundedIndex);
638
+ this.children.splice(currentIndex, 1);
639
+ this.children.splice(boundedIndex, 0, child);
640
+ }
641
+ toGlobal(point) {
642
+ return this.worldTransform.apply(point);
643
+ }
644
+ toLocal(point) {
645
+ return this.worldTransform.applyInverse(point);
646
+ }
647
+ getLocalBounds() {
648
+ if (this.children.length === 0) {
649
+ return { x: 0, y: 0, width: this._width, height: this._height };
650
+ }
651
+ const childBounds = this.children.map((child) => child.getBounds());
652
+ const minX = Math.min(...childBounds.map((bounds) => bounds.x));
653
+ const minY = Math.min(...childBounds.map((bounds) => bounds.y));
654
+ const maxX = Math.max(...childBounds.map((bounds) => bounds.x + bounds.width));
655
+ const maxY = Math.max(...childBounds.map((bounds) => bounds.y + bounds.height));
656
+ return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
657
+ }
658
+ getBounds() {
659
+ const local = this.getLocalBounds();
660
+ const corners = [
661
+ this.toGlobal({ x: local.x, y: local.y }),
662
+ this.toGlobal({ x: local.x + local.width, y: local.y }),
663
+ this.toGlobal({ x: local.x + local.width, y: local.y + local.height }),
664
+ this.toGlobal({ x: local.x, y: local.y + local.height })
665
+ ];
666
+ const minX = Math.min(...corners.map((point) => point.x));
667
+ const minY = Math.min(...corners.map((point) => point.y));
668
+ const maxX = Math.max(...corners.map((point) => point.x));
669
+ const maxY = Math.max(...corners.map((point) => point.y));
670
+ return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
671
+ }
672
+ setTransform(matrix) {
673
+ this.x = matrix.tx;
674
+ this.y = matrix.ty;
675
+ this.rotation = Math.atan2(matrix.b, matrix.a);
676
+ this.scale.set(Math.hypot(matrix.a, matrix.b), Math.hypot(matrix.c, matrix.d));
677
+ this.updateEngineProperties({
678
+ x: this.x,
679
+ y: this.y,
680
+ rotation: this.rotation,
681
+ scaleX: this.scale.x * this.getDimensionScaleX(),
682
+ scaleY: this.scale.y * this.getDimensionScaleY(),
683
+ skewX: this.skew.x,
684
+ skewY: this.skew.y
685
+ });
686
+ }
687
+ on(type, listener) {
688
+ const listeners = this.listeners.get(type) ?? new Set();
689
+ listeners.add(listener);
690
+ this.listeners.set(type, listeners);
691
+ return this;
692
+ }
693
+ off(type, listener) {
694
+ this.listeners.get(type)?.delete(listener);
695
+ return this;
696
+ }
697
+ removeAllListeners() {
698
+ this.listeners.clear();
699
+ }
700
+ emit(type, event) {
701
+ this.listeners.get(type)?.forEach((listener) => listener(event));
702
+ }
703
+ destroy(options) {
704
+ if (options?.children !== false) {
705
+ ;
706
+ [...this.children].reverse().forEach((child) => child.destroy(options));
707
+ }
708
+ this.parent?.removeChild(this);
709
+ this.runtime?.destroyObject(this);
710
+ this.removeAllListeners();
711
+ }
712
+ updateEngineProperties(properties) {
713
+ this.runtime?.updateObject(this, properties);
714
+ }
715
+ }
716
+ export class RenderContainer extends RenderNode {
717
+ constructor(properties = {}, objectType = 'container') {
718
+ super(objectType);
719
+ if (typeof properties.label === 'string')
720
+ this.label = properties.label;
721
+ if (typeof properties.x === 'number')
722
+ this.x = properties.x;
723
+ if (typeof properties.y === 'number')
724
+ this.y = properties.y;
725
+ }
726
+ }
727
+ export class RenderGraphics extends RenderContainer {
728
+ constructor() {
729
+ super({}, 'graphics');
730
+ this.drawOperations = [];
731
+ this.paintStyles = new Map();
732
+ this.drawDirty = false;
733
+ this.bounds = { x: 0, y: 0, width: 0, height: 0 };
734
+ this.lineStyleValue = null;
735
+ }
736
+ clear() {
737
+ this.runtime?.releaseGraphicResources(this);
738
+ this.drawOperations = [{ type: 'clear' }];
739
+ this.paintStyles.clear();
740
+ this.bounds = { x: 0, y: 0, width: 0, height: 0 };
741
+ return this.markDrawDirty();
742
+ }
743
+ rect(x, y, width, height) {
744
+ this.drawOperations.push({ type: 'rect', x, y, width, height });
745
+ this.includeBounds(x, y, width, height);
746
+ return this.markDrawDirty();
747
+ }
748
+ drawRect(x, y, width, height) {
749
+ this.rect(x, y, width, height);
750
+ if (this.lineStyleValue) {
751
+ this.stroke(this.lineStyleValue);
752
+ }
753
+ return this;
754
+ }
755
+ ellipse(x, y, radiusX, radiusY) {
756
+ this.drawOperations.push({ type: 'ellipse', x, y, radiusX, radiusY });
757
+ this.includeBounds(x - radiusX, y - radiusY, radiusX * 2, radiusY * 2);
758
+ return this.markDrawDirty();
759
+ }
760
+ circle(x, y, radius) {
761
+ this.drawOperations.push({ type: 'circle', x, y, radius });
762
+ this.includeBounds(x - radius, y - radius, radius * 2, radius * 2);
763
+ return this.markDrawDirty();
764
+ }
765
+ poly(points, close = false) {
766
+ const ownedPoints = points.map(({ x, y }) => ({ x, y }));
767
+ this.drawOperations.push({ type: 'poly', points: ownedPoints, close });
768
+ ownedPoints.forEach(({ x, y }) => this.includePoint(x, y));
769
+ return this.markDrawDirty();
770
+ }
771
+ moveTo(x, y) {
772
+ this.drawOperations.push({ type: 'move-to', x, y });
773
+ this.includePoint(x, y);
774
+ return this.markDrawDirty();
775
+ }
776
+ lineTo(x, y) {
777
+ this.drawOperations.push({ type: 'line-to', x, y });
778
+ this.includePoint(x, y);
779
+ return this.markDrawDirty();
780
+ }
781
+ bezierCurveTo(controlPoint1X, controlPoint1Y, controlPoint2X, controlPoint2Y, destinationX, destinationY) {
782
+ this.drawOperations.push({
783
+ type: 'bezier-curve-to',
784
+ controlPoint1: { x: controlPoint1X, y: controlPoint1Y },
785
+ controlPoint2: { x: controlPoint2X, y: controlPoint2Y },
786
+ destination: { x: destinationX, y: destinationY }
787
+ });
788
+ this.includePoint(controlPoint1X, controlPoint1Y);
789
+ this.includePoint(controlPoint2X, controlPoint2Y);
790
+ this.includePoint(destinationX, destinationY);
791
+ return this.markDrawDirty();
792
+ }
793
+ closePath() {
794
+ this.drawOperations.push({ type: 'close-path' });
795
+ return this.markDrawDirty();
796
+ }
797
+ fill(style) {
798
+ const operationIndex = this.drawOperations.length;
799
+ this.drawOperations.push({
800
+ type: 'fill',
801
+ paint: normalizePaint(style)
802
+ });
803
+ this.paintStyles.set(operationIndex, style);
804
+ return this.markDrawDirty();
805
+ }
806
+ stroke(style) {
807
+ const value = typeof style === 'object' && style !== null
808
+ ? style
809
+ : {};
810
+ const width = typeof value.width === 'number' ? value.width : 1;
811
+ const operationIndex = this.drawOperations.length;
812
+ this.drawOperations.push({
813
+ type: 'stroke',
814
+ paint: normalizePaint(value),
815
+ width
816
+ });
817
+ this.paintStyles.set(operationIndex, value);
818
+ return this.markDrawDirty();
819
+ }
820
+ lineStyle(width, color, alpha = 1) {
821
+ this.lineStyleValue = { width, color, alpha };
822
+ return this;
823
+ }
824
+ getLocalBounds() {
825
+ return { ...this.bounds };
826
+ }
827
+ getDrawOperations() {
828
+ return this.drawOperations.map((operation, index) => {
829
+ if (!this.paintStyles.has(index)) {
830
+ return operation;
831
+ }
832
+ const style = this.paintStyles.get(index);
833
+ const paint = this.runtime?.createPaint(style, this) ?? normalizePaint(style);
834
+ if (operation.type === 'fill' || operation.type === 'stroke') {
835
+ return { ...operation, paint };
836
+ }
837
+ return operation;
838
+ });
839
+ }
840
+ isDrawDirty() {
841
+ return this.drawDirty;
842
+ }
843
+ markDrawClean() {
844
+ this.drawDirty = false;
845
+ }
846
+ destroy(options) {
847
+ this.runtime?.releaseGraphicResources(this);
848
+ super.destroy(options);
849
+ }
850
+ markDrawDirty() {
851
+ this.drawDirty = true;
852
+ this.runtime?.markDrawDirty(this);
853
+ return this;
854
+ }
855
+ includePoint(x, y) {
856
+ this.includeBounds(x, y, 0, 0);
857
+ }
858
+ includeBounds(x, y, width, height) {
859
+ if (this.bounds.width === 0 && this.bounds.height === 0) {
860
+ this.bounds = { x, y, width, height };
861
+ return;
862
+ }
863
+ const minX = Math.min(this.bounds.x, x);
864
+ const minY = Math.min(this.bounds.y, y);
865
+ const maxX = Math.max(this.bounds.x + this.bounds.width, x + width);
866
+ const maxY = Math.max(this.bounds.y + this.bounds.height, y + height);
867
+ this.bounds = { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
868
+ }
869
+ }
870
+ export class RenderMesh extends RenderContainer {
871
+ constructor(properties = {}) {
872
+ super(properties, 'mesh');
873
+ this.meshProperties = { ...properties };
874
+ }
875
+ getEngineProperties() {
876
+ return {
877
+ ...super.getEngineProperties(),
878
+ ...this.meshProperties
879
+ };
880
+ }
881
+ update(properties) {
882
+ this.meshProperties = {
883
+ ...this.meshProperties,
884
+ ...properties
885
+ };
886
+ this.updateEngineProperties(properties);
887
+ }
888
+ }
889
+ //# sourceMappingURL=render-object.js.map