@twick/2d 0.13.0 → 0.14.2

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 (171) hide show
  1. package/LICENSE +21 -0
  2. package/editor/editor/tsconfig.build.tsbuildinfo +1 -1
  3. package/lib/components/Audio.js +3 -3
  4. package/lib/components/Img.js +23 -23
  5. package/lib/components/Line.js +31 -31
  6. package/lib/components/Media.d.ts +1 -1
  7. package/lib/components/Media.d.ts.map +1 -1
  8. package/lib/components/Media.js +26 -26
  9. package/lib/components/Spline.js +25 -25
  10. package/lib/components/Video.js +3 -3
  11. package/lib/tsconfig.build.tsbuildinfo +1 -1
  12. package/package.json +5 -4
  13. package/src/editor/NodeInspectorConfig.tsx +76 -76
  14. package/src/editor/PreviewOverlayConfig.tsx +67 -67
  15. package/src/editor/Provider.tsx +93 -93
  16. package/src/editor/SceneGraphTabConfig.tsx +81 -81
  17. package/src/editor/icons/CircleIcon.tsx +7 -7
  18. package/src/editor/icons/CodeBlockIcon.tsx +8 -8
  19. package/src/editor/icons/CurveIcon.tsx +7 -7
  20. package/src/editor/icons/GridIcon.tsx +7 -7
  21. package/src/editor/icons/IconMap.ts +35 -35
  22. package/src/editor/icons/ImgIcon.tsx +8 -8
  23. package/src/editor/icons/LayoutIcon.tsx +9 -9
  24. package/src/editor/icons/LineIcon.tsx +7 -7
  25. package/src/editor/icons/NodeIcon.tsx +7 -7
  26. package/src/editor/icons/RayIcon.tsx +7 -7
  27. package/src/editor/icons/RectIcon.tsx +7 -7
  28. package/src/editor/icons/ShapeIcon.tsx +7 -7
  29. package/src/editor/icons/TxtIcon.tsx +8 -8
  30. package/src/editor/icons/VideoIcon.tsx +7 -7
  31. package/src/editor/icons/View2DIcon.tsx +10 -10
  32. package/src/editor/index.ts +17 -17
  33. package/src/editor/tree/DetachedRoot.tsx +23 -23
  34. package/src/editor/tree/NodeElement.tsx +74 -74
  35. package/src/editor/tree/TreeElement.tsx +72 -72
  36. package/src/editor/tree/TreeRoot.tsx +10 -10
  37. package/src/editor/tree/ViewRoot.tsx +20 -20
  38. package/src/editor/tree/index.module.scss +38 -38
  39. package/src/editor/tree/index.ts +3 -3
  40. package/src/editor/tsconfig.build.json +5 -5
  41. package/src/editor/tsconfig.json +12 -12
  42. package/src/editor/tsdoc.json +4 -4
  43. package/src/editor/vite-env.d.ts +1 -1
  44. package/src/lib/code/CodeCursor.ts +445 -445
  45. package/src/lib/code/CodeDiffer.ts +78 -78
  46. package/src/lib/code/CodeFragment.ts +97 -97
  47. package/src/lib/code/CodeHighlighter.ts +75 -75
  48. package/src/lib/code/CodeMetrics.ts +47 -47
  49. package/src/lib/code/CodeRange.test.ts +74 -74
  50. package/src/lib/code/CodeRange.ts +216 -216
  51. package/src/lib/code/CodeScope.ts +101 -101
  52. package/src/lib/code/CodeSelection.ts +24 -24
  53. package/src/lib/code/CodeSignal.ts +327 -327
  54. package/src/lib/code/CodeTokenizer.ts +54 -54
  55. package/src/lib/code/DefaultHighlightStyle.ts +98 -98
  56. package/src/lib/code/LezerHighlighter.ts +113 -113
  57. package/src/lib/code/diff.test.ts +311 -311
  58. package/src/lib/code/diff.ts +319 -319
  59. package/src/lib/code/extractRange.ts +126 -126
  60. package/src/lib/code/index.ts +13 -13
  61. package/src/lib/components/Audio.ts +168 -168
  62. package/src/lib/components/Bezier.ts +105 -105
  63. package/src/lib/components/Circle.ts +266 -266
  64. package/src/lib/components/Code.ts +526 -526
  65. package/src/lib/components/CodeBlock.ts +576 -576
  66. package/src/lib/components/CubicBezier.ts +112 -112
  67. package/src/lib/components/Curve.ts +455 -455
  68. package/src/lib/components/Grid.ts +135 -135
  69. package/src/lib/components/Icon.ts +96 -96
  70. package/src/lib/components/Img.ts +319 -319
  71. package/src/lib/components/Knot.ts +157 -157
  72. package/src/lib/components/Latex.ts +122 -122
  73. package/src/lib/components/Layout.ts +1092 -1092
  74. package/src/lib/components/Line.ts +429 -429
  75. package/src/lib/components/Media.ts +576 -576
  76. package/src/lib/components/Node.ts +1940 -1940
  77. package/src/lib/components/Path.ts +137 -137
  78. package/src/lib/components/Polygon.ts +171 -171
  79. package/src/lib/components/QuadBezier.ts +100 -100
  80. package/src/lib/components/Ray.ts +125 -125
  81. package/src/lib/components/Rect.ts +187 -187
  82. package/src/lib/components/Rive.ts +156 -156
  83. package/src/lib/components/SVG.ts +797 -797
  84. package/src/lib/components/Shape.ts +143 -143
  85. package/src/lib/components/Spline.ts +344 -344
  86. package/src/lib/components/Txt.test.tsx +81 -81
  87. package/src/lib/components/Txt.ts +203 -203
  88. package/src/lib/components/TxtLeaf.ts +205 -205
  89. package/src/lib/components/Video.ts +461 -461
  90. package/src/lib/components/View2D.ts +98 -98
  91. package/src/lib/components/__tests__/children.test.tsx +142 -142
  92. package/src/lib/components/__tests__/clone.test.tsx +126 -126
  93. package/src/lib/components/__tests__/generatorTest.ts +28 -28
  94. package/src/lib/components/__tests__/mockScene2D.ts +45 -45
  95. package/src/lib/components/__tests__/query.test.tsx +122 -122
  96. package/src/lib/components/__tests__/state.test.tsx +60 -60
  97. package/src/lib/components/index.ts +28 -28
  98. package/src/lib/components/types.ts +35 -35
  99. package/src/lib/curves/ArcSegment.ts +159 -159
  100. package/src/lib/curves/CircleSegment.ts +77 -77
  101. package/src/lib/curves/CubicBezierSegment.ts +78 -78
  102. package/src/lib/curves/CurveDrawingInfo.ts +11 -11
  103. package/src/lib/curves/CurvePoint.ts +15 -15
  104. package/src/lib/curves/CurveProfile.ts +7 -7
  105. package/src/lib/curves/KnotInfo.ts +10 -10
  106. package/src/lib/curves/LineSegment.ts +62 -62
  107. package/src/lib/curves/Polynomial.ts +355 -355
  108. package/src/lib/curves/Polynomial2D.ts +62 -62
  109. package/src/lib/curves/PolynomialSegment.ts +124 -124
  110. package/src/lib/curves/QuadBezierSegment.ts +64 -64
  111. package/src/lib/curves/Segment.ts +17 -17
  112. package/src/lib/curves/UniformPolynomialCurveSampler.ts +94 -94
  113. package/src/lib/curves/createCurveProfileLerp.ts +471 -471
  114. package/src/lib/curves/getBezierSplineProfile.ts +223 -223
  115. package/src/lib/curves/getCircleProfile.ts +86 -86
  116. package/src/lib/curves/getPathProfile.ts +178 -178
  117. package/src/lib/curves/getPointAtDistance.ts +21 -21
  118. package/src/lib/curves/getPolylineProfile.test.ts +21 -21
  119. package/src/lib/curves/getPolylineProfile.ts +89 -89
  120. package/src/lib/curves/getRectProfile.ts +139 -139
  121. package/src/lib/curves/index.ts +16 -16
  122. package/src/lib/decorators/canvasStyleSignal.ts +16 -16
  123. package/src/lib/decorators/colorSignal.ts +9 -9
  124. package/src/lib/decorators/compound.ts +72 -72
  125. package/src/lib/decorators/computed.ts +18 -18
  126. package/src/lib/decorators/defaultStyle.ts +18 -18
  127. package/src/lib/decorators/filtersSignal.ts +136 -136
  128. package/src/lib/decorators/index.ts +10 -10
  129. package/src/lib/decorators/initializers.ts +32 -32
  130. package/src/lib/decorators/nodeName.ts +13 -13
  131. package/src/lib/decorators/signal.test.ts +90 -90
  132. package/src/lib/decorators/signal.ts +345 -345
  133. package/src/lib/decorators/spacingSignal.ts +15 -15
  134. package/src/lib/decorators/vector2Signal.ts +30 -30
  135. package/src/lib/globals.d.ts +2 -2
  136. package/src/lib/index.ts +8 -8
  137. package/src/lib/jsx-dev-runtime.ts +2 -2
  138. package/src/lib/jsx-runtime.ts +46 -46
  139. package/src/lib/parse-svg-path.d.ts +14 -14
  140. package/src/lib/partials/Filter.ts +180 -180
  141. package/src/lib/partials/Gradient.ts +102 -102
  142. package/src/lib/partials/Pattern.ts +34 -34
  143. package/src/lib/partials/ShaderConfig.ts +117 -117
  144. package/src/lib/partials/index.ts +4 -4
  145. package/src/lib/partials/types.ts +58 -58
  146. package/src/lib/scenes/Scene2D.ts +242 -242
  147. package/src/lib/scenes/index.ts +3 -3
  148. package/src/lib/scenes/makeScene2D.ts +16 -16
  149. package/src/lib/scenes/useScene2D.ts +6 -6
  150. package/src/lib/tsconfig.build.json +5 -5
  151. package/src/lib/tsconfig.json +10 -10
  152. package/src/lib/tsdoc.json +4 -4
  153. package/src/lib/utils/CanvasUtils.ts +306 -306
  154. package/src/lib/utils/diff.test.ts +453 -453
  155. package/src/lib/utils/diff.ts +148 -148
  156. package/src/lib/utils/index.ts +2 -2
  157. package/src/lib/utils/is.ts +11 -11
  158. package/src/lib/utils/makeSignalExtensions.ts +30 -30
  159. package/src/lib/utils/video/declarations.d.ts +1 -1
  160. package/src/lib/utils/video/ffmpeg-client.ts +50 -50
  161. package/src/lib/utils/video/mp4-parser-manager.ts +72 -72
  162. package/src/lib/utils/video/parser/index.ts +1 -1
  163. package/src/lib/utils/video/parser/parser.ts +257 -257
  164. package/src/lib/utils/video/parser/sampler.ts +72 -72
  165. package/src/lib/utils/video/parser/segment.ts +302 -302
  166. package/src/lib/utils/video/parser/sink.ts +29 -29
  167. package/src/lib/utils/video/parser/utils.ts +31 -31
  168. package/src/tsconfig.base.json +19 -19
  169. package/src/tsconfig.build.json +8 -8
  170. package/src/tsconfig.json +5 -5
  171. package/tsconfig.project.json +7 -7
@@ -1,1092 +1,1092 @@
1
- import type {
2
- InterpolationFunction,
3
- PossibleSpacing,
4
- PossibleVector2,
5
- SerializedVector2,
6
- Signal,
7
- SignalValue,
8
- SimpleSignal,
9
- SimpleVector2Signal,
10
- SpacingSignal,
11
- ThreadGenerator,
12
- TimingFunction,
13
- Vector2Signal,
14
- } from '@twick/core';
15
- import {
16
- BBox,
17
- DependencyContext,
18
- Origin,
19
- Vector2,
20
- boolLerp,
21
- modify,
22
- originToOffset,
23
- threadable,
24
- transformVector,
25
- transformVectorAsPoint,
26
- tween,
27
- } from '@twick/core';
28
- import type {Vector2LengthSignal} from '../decorators';
29
- import {
30
- addInitializer,
31
- cloneable,
32
- computed,
33
- defaultStyle,
34
- getPropertyMeta,
35
- initial,
36
- interpolation,
37
- nodeName,
38
- signal,
39
- vector2Signal,
40
- } from '../decorators';
41
- import {spacingSignal} from '../decorators/spacingSignal';
42
- import type {
43
- DesiredLength,
44
- FlexBasis,
45
- FlexContent,
46
- FlexDirection,
47
- FlexItems,
48
- FlexWrap,
49
- LayoutMode,
50
- Length,
51
- LengthLimit,
52
- TextWrap,
53
- } from '../partials';
54
- import {drawLine, drawPivot, is} from '../utils';
55
- import type {NodeProps} from './Node';
56
- import {Node} from './Node';
57
-
58
- export interface LayoutProps extends NodeProps {
59
- layout?: LayoutMode;
60
- tagName?: keyof HTMLElementTagNameMap;
61
-
62
- width?: SignalValue<Length>;
63
- height?: SignalValue<Length>;
64
- maxWidth?: SignalValue<LengthLimit>;
65
- maxHeight?: SignalValue<LengthLimit>;
66
- minWidth?: SignalValue<LengthLimit>;
67
- minHeight?: SignalValue<LengthLimit>;
68
- ratio?: SignalValue<number>;
69
-
70
- marginTop?: SignalValue<number>;
71
- marginBottom?: SignalValue<number>;
72
- marginLeft?: SignalValue<number>;
73
- marginRight?: SignalValue<number>;
74
- margin?: SignalValue<PossibleSpacing>;
75
-
76
- paddingTop?: SignalValue<number>;
77
- paddingBottom?: SignalValue<number>;
78
- paddingLeft?: SignalValue<number>;
79
- paddingRight?: SignalValue<number>;
80
- padding?: SignalValue<PossibleSpacing>;
81
-
82
- direction?: SignalValue<FlexDirection>;
83
- basis?: SignalValue<FlexBasis>;
84
- grow?: SignalValue<number>;
85
- shrink?: SignalValue<number>;
86
- wrap?: SignalValue<FlexWrap>;
87
-
88
- justifyContent?: SignalValue<FlexContent>;
89
- alignContent?: SignalValue<FlexContent>;
90
- alignItems?: SignalValue<FlexItems>;
91
- alignSelf?: SignalValue<FlexItems>;
92
- rowGap?: SignalValue<Length>;
93
- columnGap?: SignalValue<Length>;
94
- gap?: SignalValue<Length>;
95
-
96
- fontFamily?: SignalValue<string>;
97
- fontSize?: SignalValue<number>;
98
- fontStyle?: SignalValue<string>;
99
- fontWeight?: SignalValue<number>;
100
- lineHeight?: SignalValue<Length>;
101
- letterSpacing?: SignalValue<number>;
102
- textWrap?: SignalValue<TextWrap>;
103
- textDirection?: SignalValue<CanvasDirection>;
104
- textAlign?: SignalValue<CanvasTextAlign>;
105
-
106
- size?: SignalValue<PossibleVector2<Length>>;
107
- offsetX?: SignalValue<number>;
108
- offsetY?: SignalValue<number>;
109
- offset?: SignalValue<PossibleVector2>;
110
- /**
111
- * The position of the center of this node.
112
- *
113
- * @remarks
114
- * This shortcut property will set the node's position so that the center ends
115
- * up in the given place.
116
- * If present, overrides the {@link NodeProps.position} property.
117
- * When {@link offset} is not set, this will be the same as the
118
- * {@link NodeProps.position}.
119
- */
120
- middle?: SignalValue<PossibleVector2>;
121
- /**
122
- * The position of the top edge of this node.
123
- *
124
- * @remarks
125
- * This shortcut property will set the node's position so that the top edge
126
- * ends up in the given place.
127
- * If present, overrides the {@link NodeProps.position} property.
128
- */
129
- top?: SignalValue<PossibleVector2>;
130
- /**
131
- * The position of the bottom edge of this node.
132
- *
133
- * @remarks
134
- * This shortcut property will set the node's position so that the bottom edge
135
- * ends up in the given place.
136
- * If present, overrides the {@link NodeProps.position} property.
137
- */
138
- bottom?: SignalValue<PossibleVector2>;
139
- /**
140
- * The position of the left edge of this node.
141
- *
142
- * @remarks
143
- * This shortcut property will set the node's position so that the left edge
144
- * ends up in the given place.
145
- * If present, overrides the {@link NodeProps.position} property.
146
- */
147
- left?: SignalValue<PossibleVector2>;
148
- /**
149
- * The position of the right edge of this node.
150
- *
151
- * @remarks
152
- * This shortcut property will set the node's position so that the right edge
153
- * ends up in the given place.
154
- * If present, overrides the {@link NodeProps.position} property.
155
- */
156
- right?: SignalValue<PossibleVector2>;
157
- /**
158
- * The position of the top left corner of this node.
159
- *
160
- * @remarks
161
- * This shortcut property will set the node's position so that the top left
162
- * corner ends up in the given place.
163
- * If present, overrides the {@link NodeProps.position} property.
164
- */
165
- topLeft?: SignalValue<PossibleVector2>;
166
- /**
167
- * The position of the top right corner of this node.
168
- *
169
- * @remarks
170
- * This shortcut property will set the node's position so that the top right
171
- * corner ends up in the given place.
172
- * If present, overrides the {@link NodeProps.position} property.
173
- */
174
- topRight?: SignalValue<PossibleVector2>;
175
- /**
176
- * The position of the bottom left corner of this node.
177
- *
178
- * @remarks
179
- * This shortcut property will set the node's position so that the bottom left
180
- * corner ends up in the given place.
181
- * If present, overrides the {@link NodeProps.position} property.
182
- */
183
- bottomLeft?: SignalValue<PossibleVector2>;
184
- /**
185
- * The position of the bottom right corner of this node.
186
- *
187
- * @remarks
188
- * This shortcut property will set the node's position so that the bottom
189
- * right corner ends up in the given place.
190
- * If present, overrides the {@link NodeProps.position} property.
191
- */
192
- bottomRight?: SignalValue<PossibleVector2>;
193
- clip?: SignalValue<boolean>;
194
- }
195
-
196
- @nodeName('Layout')
197
- export class Layout extends Node {
198
- @initial(null)
199
- @interpolation(boolLerp)
200
- @signal()
201
- public declare readonly layout: SimpleSignal<LayoutMode, this>;
202
-
203
- @initial(null)
204
- @signal()
205
- public declare readonly maxWidth: SimpleSignal<LengthLimit, this>;
206
- @initial(null)
207
- @signal()
208
- public declare readonly maxHeight: SimpleSignal<LengthLimit, this>;
209
- @initial(null)
210
- @signal()
211
- public declare readonly minWidth: SimpleSignal<LengthLimit, this>;
212
- @initial(null)
213
- @signal()
214
- public declare readonly minHeight: SimpleSignal<LengthLimit, this>;
215
- @initial(null)
216
- @signal()
217
- public declare readonly ratio: SimpleSignal<number | null, this>;
218
-
219
- @spacingSignal('margin')
220
- public declare readonly margin: SpacingSignal<this>;
221
-
222
- @spacingSignal('padding')
223
- public declare readonly padding: SpacingSignal<this>;
224
-
225
- @initial('row')
226
- @signal()
227
- public declare readonly direction: SimpleSignal<FlexDirection, this>;
228
- @initial(null)
229
- @signal()
230
- public declare readonly basis: SimpleSignal<FlexBasis, this>;
231
- @initial(0)
232
- @signal()
233
- public declare readonly grow: SimpleSignal<number, this>;
234
- @initial(1)
235
- @signal()
236
- public declare readonly shrink: SimpleSignal<number, this>;
237
- @initial('nowrap')
238
- @signal()
239
- public declare readonly wrap: SimpleSignal<FlexWrap, this>;
240
-
241
- @initial('start')
242
- @signal()
243
- public declare readonly justifyContent: SimpleSignal<FlexContent, this>;
244
- @initial('normal')
245
- @signal()
246
- public declare readonly alignContent: SimpleSignal<FlexContent, this>;
247
- @initial('stretch')
248
- @signal()
249
- public declare readonly alignItems: SimpleSignal<FlexItems, this>;
250
- @initial('auto')
251
- @signal()
252
- public declare readonly alignSelf: SimpleSignal<FlexItems, this>;
253
- @initial(0)
254
- @vector2Signal({x: 'columnGap', y: 'rowGap'})
255
- public declare readonly gap: Vector2LengthSignal<this>;
256
- public get columnGap(): Signal<Length, number, this> {
257
- return this.gap.x;
258
- }
259
- public get rowGap(): Signal<Length, number, this> {
260
- return this.gap.y;
261
- }
262
-
263
- @defaultStyle('font-family')
264
- @signal()
265
- public declare readonly fontFamily: SimpleSignal<string, this>;
266
- @defaultStyle('font-size', parseFloat)
267
- @signal()
268
- public declare readonly fontSize: SimpleSignal<number, this>;
269
- @defaultStyle('font-style')
270
- @signal()
271
- public declare readonly fontStyle: SimpleSignal<string, this>;
272
- @defaultStyle('font-weight', parseInt)
273
- @signal()
274
- public declare readonly fontWeight: SimpleSignal<number, this>;
275
- @defaultStyle('line-height', parseFloat)
276
- @signal()
277
- public declare readonly lineHeight: SimpleSignal<Length, this>;
278
- @defaultStyle('letter-spacing', i => (i === 'normal' ? 0 : parseFloat(i)))
279
- @signal()
280
- public declare readonly letterSpacing: SimpleSignal<number, this>;
281
-
282
- @defaultStyle('white-space', i => (i === 'pre' ? 'pre' : i === 'normal'))
283
- @signal()
284
- public declare readonly textWrap: SimpleSignal<TextWrap, this>;
285
- @initial('inherit')
286
- @signal()
287
- public declare readonly textDirection: SimpleSignal<CanvasDirection, this>;
288
- @defaultStyle('text-align')
289
- @signal()
290
- public declare readonly textAlign: SimpleSignal<CanvasTextAlign, this>;
291
-
292
- protected getX(): number {
293
- if (this.isLayoutRoot()) {
294
- return this.x.context.getter();
295
- }
296
-
297
- return this.computedPosition().x;
298
- }
299
- protected setX(value: SignalValue<number>) {
300
- this.x.context.setter(value);
301
- }
302
-
303
- protected getY(): number {
304
- if (this.isLayoutRoot()) {
305
- return this.y.context.getter();
306
- }
307
-
308
- return this.computedPosition().y;
309
- }
310
- protected setY(value: SignalValue<number>) {
311
- this.y.context.setter(value);
312
- }
313
-
314
- /**
315
- * Represents the size of this node.
316
- *
317
- * @remarks
318
- * A size is a two-dimensional vector, where `x` represents the `width`, and `y`
319
- * represents the `height`.
320
- *
321
- * The value of both x and y is of type {@link partials.Length} which is
322
- * either:
323
- * - `number` - the desired length in pixels
324
- * - `${number}%` - a string with the desired length in percents, for example
325
- * `'50%'`
326
- * - `null` - an automatic length
327
- *
328
- * When retrieving the size, all units are converted to pixels, using the
329
- * current state of the layout. For example, retrieving the width set to
330
- * `'50%'`, while the parent has a width of `200px` will result in the number
331
- * `100` being returned.
332
- *
333
- * When the node is not part of the layout, setting its size using percents
334
- * refers to the size of the entire scene.
335
- *
336
- * @example
337
- * Initializing the size:
338
- * ```tsx
339
- * // with a possible vector:
340
- * <Node size={['50%', 200]} />
341
- * // with individual components:
342
- * <Node width={'50%'} height={200} />
343
- * ```
344
- *
345
- * Accessing the size:
346
- * ```tsx
347
- * // retrieving the vector:
348
- * const size = node.size();
349
- * // retrieving an individual component:
350
- * const width = node.size.x();
351
- * ```
352
- *
353
- * Setting the size:
354
- * ```tsx
355
- * // with a possible vector:
356
- * node.size(['50%', 200]);
357
- * node.size(() => ['50%', 200]);
358
- * // with individual components:
359
- * node.size.x('50%');
360
- * node.size.x(() => '50%');
361
- * ```
362
- */
363
- @initial({x: null, y: null})
364
- @vector2Signal({x: 'width', y: 'height'})
365
- public declare readonly size: Vector2LengthSignal<this>;
366
- public get width(): Signal<Length, number, this> {
367
- return this.size.x;
368
- }
369
- public get height(): Signal<Length, number, this> {
370
- return this.size.y;
371
- }
372
-
373
- protected getWidth(): number {
374
- return this.computedSize().width;
375
- }
376
- protected setWidth(value: SignalValue<Length>) {
377
- this.width.context.setter(value);
378
- }
379
-
380
- @threadable()
381
- protected *tweenWidth(
382
- value: SignalValue<Length>,
383
- time: number,
384
- timingFunction: TimingFunction,
385
- interpolationFunction: InterpolationFunction<Length>,
386
- ): ThreadGenerator {
387
- const width = this.desiredSize().x;
388
- const lock = typeof width !== 'number' || typeof value !== 'number';
389
- let from: number;
390
- if (lock) {
391
- from = this.size.x();
392
- } else {
393
- from = width;
394
- }
395
-
396
- let to: number;
397
- if (lock) {
398
- this.size.x(value);
399
- to = this.size.x();
400
- } else {
401
- to = value;
402
- }
403
-
404
- this.size.x(from);
405
- lock && this.lockSize();
406
- yield* tween(time, value =>
407
- this.size.x(interpolationFunction(from, to, timingFunction(value))),
408
- );
409
- this.size.x(value);
410
- lock && this.releaseSize();
411
- }
412
-
413
- protected getHeight(): number {
414
- return this.computedSize().height;
415
- }
416
- protected setHeight(value: SignalValue<Length>) {
417
- this.height.context.setter(value);
418
- }
419
-
420
- @threadable()
421
- protected *tweenHeight(
422
- value: SignalValue<Length>,
423
- time: number,
424
- timingFunction: TimingFunction,
425
- interpolationFunction: InterpolationFunction<Length>,
426
- ): ThreadGenerator {
427
- const height = this.desiredSize().y;
428
- const lock = typeof height !== 'number' || typeof value !== 'number';
429
-
430
- let from: number;
431
- if (lock) {
432
- from = this.size.y();
433
- } else {
434
- from = height;
435
- }
436
-
437
- let to: number;
438
- if (lock) {
439
- this.size.y(value);
440
- to = this.size.y();
441
- } else {
442
- to = value;
443
- }
444
-
445
- this.size.y(from);
446
- lock && this.lockSize();
447
- yield* tween(time, value =>
448
- this.size.y(interpolationFunction(from, to, timingFunction(value))),
449
- );
450
- this.size.y(value);
451
- lock && this.releaseSize();
452
- }
453
-
454
- /**
455
- * Get the desired size of this node.
456
- *
457
- * @remarks
458
- * This method can be used to control the size using external factors.
459
- * By default, the returned size is the same as the one declared by the user.
460
- */
461
- @computed()
462
- protected desiredSize(): SerializedVector2<DesiredLength> {
463
- return {
464
- x: this.width.context.getter(),
465
- y: this.height.context.getter(),
466
- };
467
- }
468
-
469
- @threadable()
470
- protected *tweenSize(
471
- value: SignalValue<SerializedVector2<Length>>,
472
- time: number,
473
- timingFunction: TimingFunction,
474
- interpolationFunction: InterpolationFunction<Vector2>,
475
- ): ThreadGenerator {
476
- const size = this.desiredSize();
477
- let from: Vector2;
478
- if (typeof size.x !== 'number' || typeof size.y !== 'number') {
479
- from = this.size();
480
- } else {
481
- from = new Vector2(<Vector2>size);
482
- }
483
-
484
- let to: Vector2;
485
- if (
486
- typeof value === 'object' &&
487
- typeof value.x === 'number' &&
488
- typeof value.y === 'number'
489
- ) {
490
- to = new Vector2(<Vector2>value);
491
- } else {
492
- this.size(value);
493
- to = this.size();
494
- }
495
-
496
- this.size(from);
497
- this.lockSize();
498
- yield* tween(time, value =>
499
- this.size(interpolationFunction(from, to, timingFunction(value))),
500
- );
501
- this.releaseSize();
502
- this.size(value);
503
- }
504
-
505
- /**
506
- * Represents the offset of this node's origin.
507
- *
508
- * @remarks
509
- * By default, the origin of a node is located at its center. The origin
510
- * serves as the pivot point when rotating and scaling a node, but it doesn't
511
- * affect the placement of its children.
512
- *
513
- * The value is relative to the size of this node. A value of `1` means as far
514
- * to the right/bottom as possible. Here are a few examples of offsets:
515
- * - `[-1, -1]` - top left corner
516
- * - `[1, -1]` - top right corner
517
- * - `[0, 1]` - bottom edge
518
- * - `[-1, 1]` - bottom left corner
519
- */
520
- @vector2Signal('offset')
521
- public declare readonly offset: Vector2Signal<this>;
522
-
523
- /**
524
- * The position of the center of this node.
525
- *
526
- * @remarks
527
- * When set, this shortcut property will modify the node's position so that
528
- * the center ends up in the given place.
529
- *
530
- * If the {@link offset} has not been changed, this will be the same as the
531
- * {@link position}.
532
- *
533
- * When retrieved, it will return the position of the center in the parent
534
- * space.
535
- */
536
- @originSignal(Origin.Middle)
537
- public declare readonly middle: SimpleVector2Signal<this>;
538
-
539
- /**
540
- * The position of the top edge of this node.
541
- *
542
- * @remarks
543
- * When set, this shortcut property will modify the node's position so that
544
- * the top edge ends up in the given place.
545
- *
546
- * When retrieved, it will return the position of the top edge in the parent
547
- * space.
548
- */
549
- @originSignal(Origin.Top)
550
- public declare readonly top: SimpleVector2Signal<this>;
551
- /**
552
- * The position of the bottom edge of this node.
553
- *
554
- * @remarks
555
- * When set, this shortcut property will modify the node's position so that
556
- * the bottom edge ends up in the given place.
557
- *
558
- * When retrieved, it will return the position of the bottom edge in the
559
- * parent space.
560
- */
561
- @originSignal(Origin.Bottom)
562
- public declare readonly bottom: SimpleVector2Signal<this>;
563
- /**
564
- * The position of the left edge of this node.
565
- *
566
- * @remarks
567
- * When set, this shortcut property will modify the node's position so that
568
- * the left edge ends up in the given place.
569
- *
570
- * When retrieved, it will return the position of the left edge in the parent
571
- * space.
572
- */
573
- @originSignal(Origin.Left)
574
- public declare readonly left: SimpleVector2Signal<this>;
575
- /**
576
- * The position of the right edge of this node.
577
- *
578
- * @remarks
579
- * When set, this shortcut property will modify the node's position so that
580
- * the right edge ends up in the given place.
581
- *
582
- * When retrieved, it will return the position of the right edge in the parent
583
- * space.
584
- */
585
- @originSignal(Origin.Right)
586
- public declare readonly right: SimpleVector2Signal<this>;
587
- /**
588
- * The position of the top left corner of this node.
589
- *
590
- * @remarks
591
- * When set, this shortcut property will modify the node's position so that
592
- * the top left corner ends up in the given place.
593
- *
594
- * When retrieved, it will return the position of the top left corner in the
595
- * parent space.
596
- */
597
- @originSignal(Origin.TopLeft)
598
- public declare readonly topLeft: SimpleVector2Signal<this>;
599
- /**
600
- * The position of the top right corner of this node.
601
- *
602
- * @remarks
603
- * When set, this shortcut property will modify the node's position so that
604
- * the top right corner ends up in the given place.
605
- *
606
- * When retrieved, it will return the position of the top right corner in the
607
- * parent space.
608
- */
609
- @originSignal(Origin.TopRight)
610
- public declare readonly topRight: SimpleVector2Signal<this>;
611
- /**
612
- * The position of the bottom left corner of this node.
613
- *
614
- * @remarks
615
- * When set, this shortcut property will modify the node's position so that
616
- * the bottom left corner ends up in the given place.
617
- *
618
- * When retrieved, it will return the position of the bottom left corner in
619
- * the parent space.
620
- */
621
- @originSignal(Origin.BottomLeft)
622
- public declare readonly bottomLeft: SimpleVector2Signal<this>;
623
- /**
624
- * The position of the bottom right corner of this node.
625
- *
626
- * @remarks
627
- * When set, this shortcut property will modify the node's position so that
628
- * the bottom right corner ends up in the given place.
629
- *
630
- * When retrieved, it will return the position of the bottom right corner in
631
- * the parent space.
632
- */
633
- @originSignal(Origin.BottomRight)
634
- public declare readonly bottomRight: SimpleVector2Signal<this>;
635
-
636
- @initial(false)
637
- @signal()
638
- public declare readonly clip: SimpleSignal<boolean, this>;
639
-
640
- public declare element: HTMLElement;
641
- public declare styles: CSSStyleDeclaration;
642
-
643
- @initial(0)
644
- @signal()
645
- protected declare readonly sizeLockCounter: SimpleSignal<number, this>;
646
-
647
- public constructor(props: LayoutProps) {
648
- super(props);
649
- this.element.dataset.motionCanvasKey = this.key;
650
- }
651
-
652
- public lockSize() {
653
- this.sizeLockCounter(this.sizeLockCounter() + 1);
654
- }
655
-
656
- public releaseSize() {
657
- this.sizeLockCounter(this.sizeLockCounter() - 1);
658
- }
659
-
660
- @computed()
661
- protected parentTransform(): Layout | null {
662
- return this.findAncestor(is(Layout));
663
- }
664
-
665
- @computed()
666
- public anchorPosition() {
667
- const size = this.computedSize();
668
- const offset = this.offset();
669
-
670
- return size.scale(0.5).mul(offset);
671
- }
672
-
673
- /**
674
- * Get the resolved layout mode of this node.
675
- *
676
- * @remarks
677
- * When the mode is `null`, its value will be inherited from the parent.
678
- *
679
- * Use {@link layout} to get the raw mode set for this node (without
680
- * inheritance).
681
- */
682
- @computed()
683
- public layoutEnabled(): boolean {
684
- return this.layout() ?? this.parentTransform()?.layoutEnabled() ?? false;
685
- }
686
-
687
- @computed()
688
- public isLayoutRoot(): boolean {
689
- return !this.layoutEnabled() || !this.parentTransform()?.layoutEnabled();
690
- }
691
-
692
- public override localToParent(): DOMMatrix {
693
- const matrix = super.localToParent();
694
- const offset = this.offset();
695
- if (!offset.exactlyEquals(Vector2.zero)) {
696
- const translate = this.size().mul(offset).scale(-0.5);
697
- matrix.translateSelf(translate.x, translate.y);
698
- }
699
-
700
- return matrix;
701
- }
702
-
703
- /**
704
- * A simplified version of {@link localToParent} matrix used for transforming
705
- * direction vectors.
706
- *
707
- * @internal
708
- */
709
- @computed()
710
- protected scalingRotationMatrix(): DOMMatrix {
711
- const matrix = new DOMMatrix();
712
-
713
- matrix.rotateSelf(0, 0, this.rotation());
714
- matrix.scaleSelf(this.scale.x(), this.scale.y());
715
-
716
- const offset = this.offset();
717
- if (!offset.exactlyEquals(Vector2.zero)) {
718
- const translate = this.size().mul(offset).scale(-0.5);
719
- matrix.translateSelf(translate.x, translate.y);
720
- }
721
-
722
- return matrix;
723
- }
724
-
725
- protected getComputedLayout(): BBox {
726
- return new BBox(this.element.getBoundingClientRect());
727
- }
728
-
729
- @computed()
730
- public computedPosition(): Vector2 {
731
- this.requestLayoutUpdate();
732
- const box = this.getComputedLayout();
733
-
734
- const position = new Vector2(
735
- box.x + (box.width / 2) * this.offset.x(),
736
- box.y + (box.height / 2) * this.offset.y(),
737
- );
738
-
739
- const parent = this.parentTransform();
740
- if (parent) {
741
- const parentRect = parent.getComputedLayout();
742
- position.x -= parentRect.x + (parentRect.width - box.width) / 2;
743
- position.y -= parentRect.y + (parentRect.height - box.height) / 2;
744
- }
745
-
746
- return position;
747
- }
748
-
749
- @computed()
750
- protected computedSize(): Vector2 {
751
- this.requestLayoutUpdate();
752
- return this.getComputedLayout().size;
753
- }
754
-
755
- /**
756
- * Find the closest layout root and apply any new layout changes.
757
- */
758
- @computed()
759
- protected requestLayoutUpdate() {
760
- const parent = this.parentTransform();
761
- if (this.appendedToView()) {
762
- parent?.requestFontUpdate();
763
- this.updateLayout();
764
- } else {
765
- parent!.requestLayoutUpdate();
766
- }
767
- }
768
-
769
- @computed()
770
- protected appendedToView() {
771
- const root = this.isLayoutRoot();
772
- if (root) {
773
- this.view().element.append(this.element);
774
- }
775
-
776
- return root;
777
- }
778
-
779
- /**
780
- * Apply any new layout changes to this node and its children.
781
- */
782
- @computed()
783
- protected updateLayout() {
784
- this.applyFont();
785
- this.applyFlex();
786
- if (this.layoutEnabled()) {
787
- const children = this.layoutChildren();
788
- for (const child of children) {
789
- child.updateLayout();
790
- }
791
- }
792
- }
793
-
794
- @computed()
795
- protected layoutChildren(): Layout[] {
796
- const queue = [...this.children()];
797
- const result: Layout[] = [];
798
- const elements: HTMLElement[] = [];
799
- while (queue.length) {
800
- const child = queue.shift();
801
- if (child instanceof Layout) {
802
- if (child.layoutEnabled()) {
803
- result.push(child);
804
- elements.push(child.element);
805
- }
806
- } else if (child) {
807
- queue.unshift(...child.children());
808
- }
809
- }
810
- this.element.replaceChildren(...elements);
811
-
812
- return result;
813
- }
814
-
815
- /**
816
- * Apply any new font changes to this node and all of its ancestors.
817
- */
818
- @computed()
819
- protected requestFontUpdate() {
820
- this.appendedToView();
821
- this.parentTransform()?.requestFontUpdate();
822
- this.applyFont();
823
- }
824
-
825
- protected override getCacheBBox(): BBox {
826
- return BBox.fromSizeCentered(this.computedSize());
827
- }
828
-
829
- protected override async draw(context: CanvasRenderingContext2D) {
830
- await document.fonts?.ready;
831
- if (this.clip()) {
832
- const size = this.computedSize();
833
- if (size.width === 0 || size.height === 0) {
834
- return;
835
- }
836
-
837
- context.beginPath();
838
- context.rect(size.width / -2, size.height / -2, size.width, size.height);
839
- context.closePath();
840
- context.clip();
841
- }
842
-
843
- await this.drawChildren(context);
844
- }
845
-
846
- public override drawOverlay(
847
- context: CanvasRenderingContext2D,
848
- matrix: DOMMatrix,
849
- ) {
850
- const size = this.computedSize();
851
- const offsetVector = size.mul(this.offset()).scale(0.5);
852
- const offset = transformVectorAsPoint(offsetVector, matrix);
853
- const box = BBox.fromSizeCentered(size);
854
- const layout = box.transformCorners(matrix);
855
- const padding = box
856
- .addSpacing(this.padding().scale(-1))
857
- .transformCorners(matrix);
858
- const margin = box.addSpacing(this.margin()).transformCorners(matrix);
859
-
860
- context.beginPath();
861
- drawLine(context, margin);
862
- drawLine(context, layout);
863
- context.closePath();
864
- context.fillStyle = 'rgba(255,193,125,0.6)';
865
- context.fill('evenodd');
866
-
867
- context.beginPath();
868
- drawLine(context, layout);
869
- drawLine(context, padding);
870
- context.closePath();
871
- context.fillStyle = 'rgba(180,255,147,0.6)';
872
- context.fill('evenodd');
873
-
874
- context.beginPath();
875
- drawLine(context, layout);
876
- context.closePath();
877
- context.lineWidth = 1;
878
- context.strokeStyle = 'white';
879
- context.stroke();
880
-
881
- context.beginPath();
882
- drawPivot(context, offset);
883
- context.stroke();
884
- }
885
-
886
- public getOriginDelta(origin: Origin) {
887
- const size = this.computedSize().scale(0.5);
888
- const offset = this.offset().mul(size);
889
- if (origin === Origin.Middle) {
890
- return offset.flipped;
891
- }
892
-
893
- const newOffset = originToOffset(origin).mul(size);
894
- return newOffset.sub(offset);
895
- }
896
-
897
- /**
898
- * Update the offset of this node and adjust the position to keep it in the
899
- * same place.
900
- *
901
- * @param offset - The new offset.
902
- */
903
- public moveOffset(offset: Vector2) {
904
- const size = this.computedSize().scale(0.5);
905
- const oldOffset = this.offset().mul(size);
906
- const newOffset = offset.mul(size);
907
- this.offset(offset);
908
- this.position(this.position().add(newOffset).sub(oldOffset));
909
- }
910
-
911
- protected parsePixels(value: number | null): string {
912
- return value === null ? '' : `${value}px`;
913
- }
914
-
915
- protected parseLength(value: number | string | null): string {
916
- if (value === null) {
917
- return '';
918
- }
919
- if (typeof value === 'string') {
920
- return value;
921
- }
922
- return `${value}px`;
923
- }
924
-
925
- @computed()
926
- protected applyFlex() {
927
- this.element.style.position = this.isLayoutRoot() ? 'absolute' : 'relative';
928
-
929
- const size = this.desiredSize();
930
- this.element.style.width = this.parseLength(size.x);
931
- this.element.style.height = this.parseLength(size.y);
932
- this.element.style.maxWidth = this.parseLength(this.maxWidth());
933
- this.element.style.minWidth = this.parseLength(this.minWidth());
934
- this.element.style.maxHeight = this.parseLength(this.maxHeight());
935
- this.element.style.minHeight = this.parseLength(this.minHeight()!);
936
- this.element.style.aspectRatio =
937
- this.ratio() === null ? '' : this.ratio()!.toString();
938
-
939
- this.element.style.marginTop = this.parsePixels(this.margin.top());
940
- this.element.style.marginBottom = this.parsePixels(this.margin.bottom());
941
- this.element.style.marginLeft = this.parsePixels(this.margin.left());
942
- this.element.style.marginRight = this.parsePixels(this.margin.right());
943
-
944
- this.element.style.paddingTop = this.parsePixels(this.padding.top());
945
- this.element.style.paddingBottom = this.parsePixels(this.padding.bottom());
946
- this.element.style.paddingLeft = this.parsePixels(this.padding.left());
947
- this.element.style.paddingRight = this.parsePixels(this.padding.right());
948
-
949
- this.element.style.flexDirection = this.direction();
950
- this.element.style.flexBasis = this.parseLength(this.basis()!);
951
- this.element.style.flexWrap = this.wrap();
952
-
953
- this.element.style.justifyContent = this.justifyContent();
954
- this.element.style.alignContent = this.alignContent();
955
- this.element.style.alignItems = this.alignItems();
956
- this.element.style.alignSelf = this.alignSelf();
957
- this.element.style.columnGap = this.parseLength(this.gap.x());
958
- this.element.style.rowGap = this.parseLength(this.gap.y());
959
-
960
- if (this.sizeLockCounter() > 0) {
961
- this.element.style.flexGrow = '0';
962
- this.element.style.flexShrink = '0';
963
- } else {
964
- this.element.style.flexGrow = this.grow().toString();
965
- this.element.style.flexShrink = this.shrink().toString();
966
- }
967
- }
968
-
969
- @computed()
970
- protected applyFont() {
971
- const loadingFonts = document.fonts
972
- ? Array.from(document.fonts).filter(font => font.status === 'loading')
973
- : [];
974
- if (loadingFonts.length > 0) {
975
- DependencyContext.collectPromise(
976
- (async () => {
977
- await document.fonts?.ready;
978
- })(),
979
- );
980
- }
981
-
982
- this.element.style.fontFamily = this.fontFamily.isInitial()
983
- ? ''
984
- : this.fontFamily();
985
- this.element.style.fontSize = this.fontSize.isInitial()
986
- ? ''
987
- : `${this.fontSize()}px`;
988
- this.element.style.fontStyle = this.fontStyle.isInitial()
989
- ? ''
990
- : this.fontStyle();
991
- if (this.lineHeight.isInitial()) {
992
- this.element.style.lineHeight = '';
993
- } else {
994
- const lineHeight = this.lineHeight();
995
- this.element.style.lineHeight =
996
- typeof lineHeight === 'string'
997
- ? (parseFloat(lineHeight as string) / 100).toString()
998
- : `${lineHeight}px`;
999
- }
1000
- this.element.style.fontWeight = this.fontWeight.isInitial()
1001
- ? ''
1002
- : this.fontWeight().toString();
1003
- this.element.style.letterSpacing = this.letterSpacing.isInitial()
1004
- ? ''
1005
- : `${this.letterSpacing()}px`;
1006
-
1007
- this.element.style.textAlign = this.textAlign.isInitial()
1008
- ? ''
1009
- : this.textAlign();
1010
-
1011
- if (this.textWrap.isInitial()) {
1012
- this.element.style.whiteSpace = '';
1013
- return;
1014
- }
1015
-
1016
- const wrap = this.textWrap();
1017
-
1018
- if (typeof wrap === 'boolean') {
1019
- this.element.style.whiteSpace = wrap ? 'normal' : 'nowrap';
1020
- return;
1021
- }
1022
-
1023
- if (wrap === 'pre') {
1024
- this.element.style.whiteSpace = wrap;
1025
- return;
1026
- }
1027
-
1028
- if (wrap === 'balance') {
1029
- this.element.style.whiteSpace = 'normal';
1030
- this.element.style.textWrap = wrap;
1031
- return;
1032
- }
1033
- }
1034
-
1035
- public override dispose() {
1036
- super.dispose();
1037
- this.sizeLockCounter?.context.dispose();
1038
- if (this.element) {
1039
- this.element.remove();
1040
- this.element.innerHTML = '';
1041
- }
1042
- this.element = null as unknown as HTMLElement;
1043
- this.styles = null as unknown as CSSStyleDeclaration;
1044
- }
1045
-
1046
- public override hit(position: Vector2): Node | null {
1047
- const local = transformVectorAsPoint(
1048
- position,
1049
- this.localToParent().inverse(),
1050
- );
1051
- if (this.cacheBBox().includes(local)) {
1052
- return super.hit(position) ?? this;
1053
- }
1054
-
1055
- return null;
1056
- }
1057
- }
1058
-
1059
- function originSignal(origin: Origin): PropertyDecorator {
1060
- return (target, key) => {
1061
- signal()(target, key);
1062
- cloneable(false)(target, key);
1063
- const meta = getPropertyMeta<any>(target, key);
1064
- meta!.parser = value => new Vector2(value);
1065
- meta!.getter = function (this: Layout) {
1066
- const originOffset = this.computedSize().getOriginOffset(origin);
1067
- return transformVectorAsPoint(originOffset, this.localToParent());
1068
- };
1069
- meta!.setter = function (
1070
- this: Layout,
1071
- value: SignalValue<PossibleVector2>,
1072
- ) {
1073
- this.position(
1074
- modify(value, unwrapped => {
1075
- const originDelta = this.getOriginDelta(origin);
1076
- return transformVector(
1077
- originDelta,
1078
- this.scalingRotationMatrix(),
1079
- ).flipped.add(unwrapped);
1080
- }),
1081
- );
1082
- return this;
1083
- };
1084
- };
1085
- }
1086
-
1087
- addInitializer<Layout>(Layout.prototype, instance => {
1088
- instance.element = document.createElement('div');
1089
- instance.element.style.display = 'flex';
1090
- instance.element.style.boxSizing = 'border-box';
1091
- instance.styles = getComputedStyle(instance.element);
1092
- });
1
+ import type {
2
+ InterpolationFunction,
3
+ PossibleSpacing,
4
+ PossibleVector2,
5
+ SerializedVector2,
6
+ Signal,
7
+ SignalValue,
8
+ SimpleSignal,
9
+ SimpleVector2Signal,
10
+ SpacingSignal,
11
+ ThreadGenerator,
12
+ TimingFunction,
13
+ Vector2Signal,
14
+ } from '@twick/core';
15
+ import {
16
+ BBox,
17
+ DependencyContext,
18
+ Origin,
19
+ Vector2,
20
+ boolLerp,
21
+ modify,
22
+ originToOffset,
23
+ threadable,
24
+ transformVector,
25
+ transformVectorAsPoint,
26
+ tween,
27
+ } from '@twick/core';
28
+ import type {Vector2LengthSignal} from '../decorators';
29
+ import {
30
+ addInitializer,
31
+ cloneable,
32
+ computed,
33
+ defaultStyle,
34
+ getPropertyMeta,
35
+ initial,
36
+ interpolation,
37
+ nodeName,
38
+ signal,
39
+ vector2Signal,
40
+ } from '../decorators';
41
+ import {spacingSignal} from '../decorators/spacingSignal';
42
+ import type {
43
+ DesiredLength,
44
+ FlexBasis,
45
+ FlexContent,
46
+ FlexDirection,
47
+ FlexItems,
48
+ FlexWrap,
49
+ LayoutMode,
50
+ Length,
51
+ LengthLimit,
52
+ TextWrap,
53
+ } from '../partials';
54
+ import {drawLine, drawPivot, is} from '../utils';
55
+ import type {NodeProps} from './Node';
56
+ import {Node} from './Node';
57
+
58
+ export interface LayoutProps extends NodeProps {
59
+ layout?: LayoutMode;
60
+ tagName?: keyof HTMLElementTagNameMap;
61
+
62
+ width?: SignalValue<Length>;
63
+ height?: SignalValue<Length>;
64
+ maxWidth?: SignalValue<LengthLimit>;
65
+ maxHeight?: SignalValue<LengthLimit>;
66
+ minWidth?: SignalValue<LengthLimit>;
67
+ minHeight?: SignalValue<LengthLimit>;
68
+ ratio?: SignalValue<number>;
69
+
70
+ marginTop?: SignalValue<number>;
71
+ marginBottom?: SignalValue<number>;
72
+ marginLeft?: SignalValue<number>;
73
+ marginRight?: SignalValue<number>;
74
+ margin?: SignalValue<PossibleSpacing>;
75
+
76
+ paddingTop?: SignalValue<number>;
77
+ paddingBottom?: SignalValue<number>;
78
+ paddingLeft?: SignalValue<number>;
79
+ paddingRight?: SignalValue<number>;
80
+ padding?: SignalValue<PossibleSpacing>;
81
+
82
+ direction?: SignalValue<FlexDirection>;
83
+ basis?: SignalValue<FlexBasis>;
84
+ grow?: SignalValue<number>;
85
+ shrink?: SignalValue<number>;
86
+ wrap?: SignalValue<FlexWrap>;
87
+
88
+ justifyContent?: SignalValue<FlexContent>;
89
+ alignContent?: SignalValue<FlexContent>;
90
+ alignItems?: SignalValue<FlexItems>;
91
+ alignSelf?: SignalValue<FlexItems>;
92
+ rowGap?: SignalValue<Length>;
93
+ columnGap?: SignalValue<Length>;
94
+ gap?: SignalValue<Length>;
95
+
96
+ fontFamily?: SignalValue<string>;
97
+ fontSize?: SignalValue<number>;
98
+ fontStyle?: SignalValue<string>;
99
+ fontWeight?: SignalValue<number>;
100
+ lineHeight?: SignalValue<Length>;
101
+ letterSpacing?: SignalValue<number>;
102
+ textWrap?: SignalValue<TextWrap>;
103
+ textDirection?: SignalValue<CanvasDirection>;
104
+ textAlign?: SignalValue<CanvasTextAlign>;
105
+
106
+ size?: SignalValue<PossibleVector2<Length>>;
107
+ offsetX?: SignalValue<number>;
108
+ offsetY?: SignalValue<number>;
109
+ offset?: SignalValue<PossibleVector2>;
110
+ /**
111
+ * The position of the center of this node.
112
+ *
113
+ * @remarks
114
+ * This shortcut property will set the node's position so that the center ends
115
+ * up in the given place.
116
+ * If present, overrides the {@link NodeProps.position} property.
117
+ * When {@link offset} is not set, this will be the same as the
118
+ * {@link NodeProps.position}.
119
+ */
120
+ middle?: SignalValue<PossibleVector2>;
121
+ /**
122
+ * The position of the top edge of this node.
123
+ *
124
+ * @remarks
125
+ * This shortcut property will set the node's position so that the top edge
126
+ * ends up in the given place.
127
+ * If present, overrides the {@link NodeProps.position} property.
128
+ */
129
+ top?: SignalValue<PossibleVector2>;
130
+ /**
131
+ * The position of the bottom edge of this node.
132
+ *
133
+ * @remarks
134
+ * This shortcut property will set the node's position so that the bottom edge
135
+ * ends up in the given place.
136
+ * If present, overrides the {@link NodeProps.position} property.
137
+ */
138
+ bottom?: SignalValue<PossibleVector2>;
139
+ /**
140
+ * The position of the left edge of this node.
141
+ *
142
+ * @remarks
143
+ * This shortcut property will set the node's position so that the left edge
144
+ * ends up in the given place.
145
+ * If present, overrides the {@link NodeProps.position} property.
146
+ */
147
+ left?: SignalValue<PossibleVector2>;
148
+ /**
149
+ * The position of the right edge of this node.
150
+ *
151
+ * @remarks
152
+ * This shortcut property will set the node's position so that the right edge
153
+ * ends up in the given place.
154
+ * If present, overrides the {@link NodeProps.position} property.
155
+ */
156
+ right?: SignalValue<PossibleVector2>;
157
+ /**
158
+ * The position of the top left corner of this node.
159
+ *
160
+ * @remarks
161
+ * This shortcut property will set the node's position so that the top left
162
+ * corner ends up in the given place.
163
+ * If present, overrides the {@link NodeProps.position} property.
164
+ */
165
+ topLeft?: SignalValue<PossibleVector2>;
166
+ /**
167
+ * The position of the top right corner of this node.
168
+ *
169
+ * @remarks
170
+ * This shortcut property will set the node's position so that the top right
171
+ * corner ends up in the given place.
172
+ * If present, overrides the {@link NodeProps.position} property.
173
+ */
174
+ topRight?: SignalValue<PossibleVector2>;
175
+ /**
176
+ * The position of the bottom left corner of this node.
177
+ *
178
+ * @remarks
179
+ * This shortcut property will set the node's position so that the bottom left
180
+ * corner ends up in the given place.
181
+ * If present, overrides the {@link NodeProps.position} property.
182
+ */
183
+ bottomLeft?: SignalValue<PossibleVector2>;
184
+ /**
185
+ * The position of the bottom right corner of this node.
186
+ *
187
+ * @remarks
188
+ * This shortcut property will set the node's position so that the bottom
189
+ * right corner ends up in the given place.
190
+ * If present, overrides the {@link NodeProps.position} property.
191
+ */
192
+ bottomRight?: SignalValue<PossibleVector2>;
193
+ clip?: SignalValue<boolean>;
194
+ }
195
+
196
+ @nodeName('Layout')
197
+ export class Layout extends Node {
198
+ @initial(null)
199
+ @interpolation(boolLerp)
200
+ @signal()
201
+ public declare readonly layout: SimpleSignal<LayoutMode, this>;
202
+
203
+ @initial(null)
204
+ @signal()
205
+ public declare readonly maxWidth: SimpleSignal<LengthLimit, this>;
206
+ @initial(null)
207
+ @signal()
208
+ public declare readonly maxHeight: SimpleSignal<LengthLimit, this>;
209
+ @initial(null)
210
+ @signal()
211
+ public declare readonly minWidth: SimpleSignal<LengthLimit, this>;
212
+ @initial(null)
213
+ @signal()
214
+ public declare readonly minHeight: SimpleSignal<LengthLimit, this>;
215
+ @initial(null)
216
+ @signal()
217
+ public declare readonly ratio: SimpleSignal<number | null, this>;
218
+
219
+ @spacingSignal('margin')
220
+ public declare readonly margin: SpacingSignal<this>;
221
+
222
+ @spacingSignal('padding')
223
+ public declare readonly padding: SpacingSignal<this>;
224
+
225
+ @initial('row')
226
+ @signal()
227
+ public declare readonly direction: SimpleSignal<FlexDirection, this>;
228
+ @initial(null)
229
+ @signal()
230
+ public declare readonly basis: SimpleSignal<FlexBasis, this>;
231
+ @initial(0)
232
+ @signal()
233
+ public declare readonly grow: SimpleSignal<number, this>;
234
+ @initial(1)
235
+ @signal()
236
+ public declare readonly shrink: SimpleSignal<number, this>;
237
+ @initial('nowrap')
238
+ @signal()
239
+ public declare readonly wrap: SimpleSignal<FlexWrap, this>;
240
+
241
+ @initial('start')
242
+ @signal()
243
+ public declare readonly justifyContent: SimpleSignal<FlexContent, this>;
244
+ @initial('normal')
245
+ @signal()
246
+ public declare readonly alignContent: SimpleSignal<FlexContent, this>;
247
+ @initial('stretch')
248
+ @signal()
249
+ public declare readonly alignItems: SimpleSignal<FlexItems, this>;
250
+ @initial('auto')
251
+ @signal()
252
+ public declare readonly alignSelf: SimpleSignal<FlexItems, this>;
253
+ @initial(0)
254
+ @vector2Signal({x: 'columnGap', y: 'rowGap'})
255
+ public declare readonly gap: Vector2LengthSignal<this>;
256
+ public get columnGap(): Signal<Length, number, this> {
257
+ return this.gap.x;
258
+ }
259
+ public get rowGap(): Signal<Length, number, this> {
260
+ return this.gap.y;
261
+ }
262
+
263
+ @defaultStyle('font-family')
264
+ @signal()
265
+ public declare readonly fontFamily: SimpleSignal<string, this>;
266
+ @defaultStyle('font-size', parseFloat)
267
+ @signal()
268
+ public declare readonly fontSize: SimpleSignal<number, this>;
269
+ @defaultStyle('font-style')
270
+ @signal()
271
+ public declare readonly fontStyle: SimpleSignal<string, this>;
272
+ @defaultStyle('font-weight', parseInt)
273
+ @signal()
274
+ public declare readonly fontWeight: SimpleSignal<number, this>;
275
+ @defaultStyle('line-height', parseFloat)
276
+ @signal()
277
+ public declare readonly lineHeight: SimpleSignal<Length, this>;
278
+ @defaultStyle('letter-spacing', i => (i === 'normal' ? 0 : parseFloat(i)))
279
+ @signal()
280
+ public declare readonly letterSpacing: SimpleSignal<number, this>;
281
+
282
+ @defaultStyle('white-space', i => (i === 'pre' ? 'pre' : i === 'normal'))
283
+ @signal()
284
+ public declare readonly textWrap: SimpleSignal<TextWrap, this>;
285
+ @initial('inherit')
286
+ @signal()
287
+ public declare readonly textDirection: SimpleSignal<CanvasDirection, this>;
288
+ @defaultStyle('text-align')
289
+ @signal()
290
+ public declare readonly textAlign: SimpleSignal<CanvasTextAlign, this>;
291
+
292
+ protected getX(): number {
293
+ if (this.isLayoutRoot()) {
294
+ return this.x.context.getter();
295
+ }
296
+
297
+ return this.computedPosition().x;
298
+ }
299
+ protected setX(value: SignalValue<number>) {
300
+ this.x.context.setter(value);
301
+ }
302
+
303
+ protected getY(): number {
304
+ if (this.isLayoutRoot()) {
305
+ return this.y.context.getter();
306
+ }
307
+
308
+ return this.computedPosition().y;
309
+ }
310
+ protected setY(value: SignalValue<number>) {
311
+ this.y.context.setter(value);
312
+ }
313
+
314
+ /**
315
+ * Represents the size of this node.
316
+ *
317
+ * @remarks
318
+ * A size is a two-dimensional vector, where `x` represents the `width`, and `y`
319
+ * represents the `height`.
320
+ *
321
+ * The value of both x and y is of type {@link partials.Length} which is
322
+ * either:
323
+ * - `number` - the desired length in pixels
324
+ * - `${number}%` - a string with the desired length in percents, for example
325
+ * `'50%'`
326
+ * - `null` - an automatic length
327
+ *
328
+ * When retrieving the size, all units are converted to pixels, using the
329
+ * current state of the layout. For example, retrieving the width set to
330
+ * `'50%'`, while the parent has a width of `200px` will result in the number
331
+ * `100` being returned.
332
+ *
333
+ * When the node is not part of the layout, setting its size using percents
334
+ * refers to the size of the entire scene.
335
+ *
336
+ * @example
337
+ * Initializing the size:
338
+ * ```tsx
339
+ * // with a possible vector:
340
+ * <Node size={['50%', 200]} />
341
+ * // with individual components:
342
+ * <Node width={'50%'} height={200} />
343
+ * ```
344
+ *
345
+ * Accessing the size:
346
+ * ```tsx
347
+ * // retrieving the vector:
348
+ * const size = node.size();
349
+ * // retrieving an individual component:
350
+ * const width = node.size.x();
351
+ * ```
352
+ *
353
+ * Setting the size:
354
+ * ```tsx
355
+ * // with a possible vector:
356
+ * node.size(['50%', 200]);
357
+ * node.size(() => ['50%', 200]);
358
+ * // with individual components:
359
+ * node.size.x('50%');
360
+ * node.size.x(() => '50%');
361
+ * ```
362
+ */
363
+ @initial({x: null, y: null})
364
+ @vector2Signal({x: 'width', y: 'height'})
365
+ public declare readonly size: Vector2LengthSignal<this>;
366
+ public get width(): Signal<Length, number, this> {
367
+ return this.size.x;
368
+ }
369
+ public get height(): Signal<Length, number, this> {
370
+ return this.size.y;
371
+ }
372
+
373
+ protected getWidth(): number {
374
+ return this.computedSize().width;
375
+ }
376
+ protected setWidth(value: SignalValue<Length>) {
377
+ this.width.context.setter(value);
378
+ }
379
+
380
+ @threadable()
381
+ protected *tweenWidth(
382
+ value: SignalValue<Length>,
383
+ time: number,
384
+ timingFunction: TimingFunction,
385
+ interpolationFunction: InterpolationFunction<Length>,
386
+ ): ThreadGenerator {
387
+ const width = this.desiredSize().x;
388
+ const lock = typeof width !== 'number' || typeof value !== 'number';
389
+ let from: number;
390
+ if (lock) {
391
+ from = this.size.x();
392
+ } else {
393
+ from = width;
394
+ }
395
+
396
+ let to: number;
397
+ if (lock) {
398
+ this.size.x(value);
399
+ to = this.size.x();
400
+ } else {
401
+ to = value;
402
+ }
403
+
404
+ this.size.x(from);
405
+ lock && this.lockSize();
406
+ yield* tween(time, value =>
407
+ this.size.x(interpolationFunction(from, to, timingFunction(value))),
408
+ );
409
+ this.size.x(value);
410
+ lock && this.releaseSize();
411
+ }
412
+
413
+ protected getHeight(): number {
414
+ return this.computedSize().height;
415
+ }
416
+ protected setHeight(value: SignalValue<Length>) {
417
+ this.height.context.setter(value);
418
+ }
419
+
420
+ @threadable()
421
+ protected *tweenHeight(
422
+ value: SignalValue<Length>,
423
+ time: number,
424
+ timingFunction: TimingFunction,
425
+ interpolationFunction: InterpolationFunction<Length>,
426
+ ): ThreadGenerator {
427
+ const height = this.desiredSize().y;
428
+ const lock = typeof height !== 'number' || typeof value !== 'number';
429
+
430
+ let from: number;
431
+ if (lock) {
432
+ from = this.size.y();
433
+ } else {
434
+ from = height;
435
+ }
436
+
437
+ let to: number;
438
+ if (lock) {
439
+ this.size.y(value);
440
+ to = this.size.y();
441
+ } else {
442
+ to = value;
443
+ }
444
+
445
+ this.size.y(from);
446
+ lock && this.lockSize();
447
+ yield* tween(time, value =>
448
+ this.size.y(interpolationFunction(from, to, timingFunction(value))),
449
+ );
450
+ this.size.y(value);
451
+ lock && this.releaseSize();
452
+ }
453
+
454
+ /**
455
+ * Get the desired size of this node.
456
+ *
457
+ * @remarks
458
+ * This method can be used to control the size using external factors.
459
+ * By default, the returned size is the same as the one declared by the user.
460
+ */
461
+ @computed()
462
+ protected desiredSize(): SerializedVector2<DesiredLength> {
463
+ return {
464
+ x: this.width.context.getter(),
465
+ y: this.height.context.getter(),
466
+ };
467
+ }
468
+
469
+ @threadable()
470
+ protected *tweenSize(
471
+ value: SignalValue<SerializedVector2<Length>>,
472
+ time: number,
473
+ timingFunction: TimingFunction,
474
+ interpolationFunction: InterpolationFunction<Vector2>,
475
+ ): ThreadGenerator {
476
+ const size = this.desiredSize();
477
+ let from: Vector2;
478
+ if (typeof size.x !== 'number' || typeof size.y !== 'number') {
479
+ from = this.size();
480
+ } else {
481
+ from = new Vector2(<Vector2>size);
482
+ }
483
+
484
+ let to: Vector2;
485
+ if (
486
+ typeof value === 'object' &&
487
+ typeof value.x === 'number' &&
488
+ typeof value.y === 'number'
489
+ ) {
490
+ to = new Vector2(<Vector2>value);
491
+ } else {
492
+ this.size(value);
493
+ to = this.size();
494
+ }
495
+
496
+ this.size(from);
497
+ this.lockSize();
498
+ yield* tween(time, value =>
499
+ this.size(interpolationFunction(from, to, timingFunction(value))),
500
+ );
501
+ this.releaseSize();
502
+ this.size(value);
503
+ }
504
+
505
+ /**
506
+ * Represents the offset of this node's origin.
507
+ *
508
+ * @remarks
509
+ * By default, the origin of a node is located at its center. The origin
510
+ * serves as the pivot point when rotating and scaling a node, but it doesn't
511
+ * affect the placement of its children.
512
+ *
513
+ * The value is relative to the size of this node. A value of `1` means as far
514
+ * to the right/bottom as possible. Here are a few examples of offsets:
515
+ * - `[-1, -1]` - top left corner
516
+ * - `[1, -1]` - top right corner
517
+ * - `[0, 1]` - bottom edge
518
+ * - `[-1, 1]` - bottom left corner
519
+ */
520
+ @vector2Signal('offset')
521
+ public declare readonly offset: Vector2Signal<this>;
522
+
523
+ /**
524
+ * The position of the center of this node.
525
+ *
526
+ * @remarks
527
+ * When set, this shortcut property will modify the node's position so that
528
+ * the center ends up in the given place.
529
+ *
530
+ * If the {@link offset} has not been changed, this will be the same as the
531
+ * {@link position}.
532
+ *
533
+ * When retrieved, it will return the position of the center in the parent
534
+ * space.
535
+ */
536
+ @originSignal(Origin.Middle)
537
+ public declare readonly middle: SimpleVector2Signal<this>;
538
+
539
+ /**
540
+ * The position of the top edge of this node.
541
+ *
542
+ * @remarks
543
+ * When set, this shortcut property will modify the node's position so that
544
+ * the top edge ends up in the given place.
545
+ *
546
+ * When retrieved, it will return the position of the top edge in the parent
547
+ * space.
548
+ */
549
+ @originSignal(Origin.Top)
550
+ public declare readonly top: SimpleVector2Signal<this>;
551
+ /**
552
+ * The position of the bottom edge of this node.
553
+ *
554
+ * @remarks
555
+ * When set, this shortcut property will modify the node's position so that
556
+ * the bottom edge ends up in the given place.
557
+ *
558
+ * When retrieved, it will return the position of the bottom edge in the
559
+ * parent space.
560
+ */
561
+ @originSignal(Origin.Bottom)
562
+ public declare readonly bottom: SimpleVector2Signal<this>;
563
+ /**
564
+ * The position of the left edge of this node.
565
+ *
566
+ * @remarks
567
+ * When set, this shortcut property will modify the node's position so that
568
+ * the left edge ends up in the given place.
569
+ *
570
+ * When retrieved, it will return the position of the left edge in the parent
571
+ * space.
572
+ */
573
+ @originSignal(Origin.Left)
574
+ public declare readonly left: SimpleVector2Signal<this>;
575
+ /**
576
+ * The position of the right edge of this node.
577
+ *
578
+ * @remarks
579
+ * When set, this shortcut property will modify the node's position so that
580
+ * the right edge ends up in the given place.
581
+ *
582
+ * When retrieved, it will return the position of the right edge in the parent
583
+ * space.
584
+ */
585
+ @originSignal(Origin.Right)
586
+ public declare readonly right: SimpleVector2Signal<this>;
587
+ /**
588
+ * The position of the top left corner of this node.
589
+ *
590
+ * @remarks
591
+ * When set, this shortcut property will modify the node's position so that
592
+ * the top left corner ends up in the given place.
593
+ *
594
+ * When retrieved, it will return the position of the top left corner in the
595
+ * parent space.
596
+ */
597
+ @originSignal(Origin.TopLeft)
598
+ public declare readonly topLeft: SimpleVector2Signal<this>;
599
+ /**
600
+ * The position of the top right corner of this node.
601
+ *
602
+ * @remarks
603
+ * When set, this shortcut property will modify the node's position so that
604
+ * the top right corner ends up in the given place.
605
+ *
606
+ * When retrieved, it will return the position of the top right corner in the
607
+ * parent space.
608
+ */
609
+ @originSignal(Origin.TopRight)
610
+ public declare readonly topRight: SimpleVector2Signal<this>;
611
+ /**
612
+ * The position of the bottom left corner of this node.
613
+ *
614
+ * @remarks
615
+ * When set, this shortcut property will modify the node's position so that
616
+ * the bottom left corner ends up in the given place.
617
+ *
618
+ * When retrieved, it will return the position of the bottom left corner in
619
+ * the parent space.
620
+ */
621
+ @originSignal(Origin.BottomLeft)
622
+ public declare readonly bottomLeft: SimpleVector2Signal<this>;
623
+ /**
624
+ * The position of the bottom right corner of this node.
625
+ *
626
+ * @remarks
627
+ * When set, this shortcut property will modify the node's position so that
628
+ * the bottom right corner ends up in the given place.
629
+ *
630
+ * When retrieved, it will return the position of the bottom right corner in
631
+ * the parent space.
632
+ */
633
+ @originSignal(Origin.BottomRight)
634
+ public declare readonly bottomRight: SimpleVector2Signal<this>;
635
+
636
+ @initial(false)
637
+ @signal()
638
+ public declare readonly clip: SimpleSignal<boolean, this>;
639
+
640
+ public declare element: HTMLElement;
641
+ public declare styles: CSSStyleDeclaration;
642
+
643
+ @initial(0)
644
+ @signal()
645
+ protected declare readonly sizeLockCounter: SimpleSignal<number, this>;
646
+
647
+ public constructor(props: LayoutProps) {
648
+ super(props);
649
+ this.element.dataset.motionCanvasKey = this.key;
650
+ }
651
+
652
+ public lockSize() {
653
+ this.sizeLockCounter(this.sizeLockCounter() + 1);
654
+ }
655
+
656
+ public releaseSize() {
657
+ this.sizeLockCounter(this.sizeLockCounter() - 1);
658
+ }
659
+
660
+ @computed()
661
+ protected parentTransform(): Layout | null {
662
+ return this.findAncestor(is(Layout));
663
+ }
664
+
665
+ @computed()
666
+ public anchorPosition() {
667
+ const size = this.computedSize();
668
+ const offset = this.offset();
669
+
670
+ return size.scale(0.5).mul(offset);
671
+ }
672
+
673
+ /**
674
+ * Get the resolved layout mode of this node.
675
+ *
676
+ * @remarks
677
+ * When the mode is `null`, its value will be inherited from the parent.
678
+ *
679
+ * Use {@link layout} to get the raw mode set for this node (without
680
+ * inheritance).
681
+ */
682
+ @computed()
683
+ public layoutEnabled(): boolean {
684
+ return this.layout() ?? this.parentTransform()?.layoutEnabled() ?? false;
685
+ }
686
+
687
+ @computed()
688
+ public isLayoutRoot(): boolean {
689
+ return !this.layoutEnabled() || !this.parentTransform()?.layoutEnabled();
690
+ }
691
+
692
+ public override localToParent(): DOMMatrix {
693
+ const matrix = super.localToParent();
694
+ const offset = this.offset();
695
+ if (!offset.exactlyEquals(Vector2.zero)) {
696
+ const translate = this.size().mul(offset).scale(-0.5);
697
+ matrix.translateSelf(translate.x, translate.y);
698
+ }
699
+
700
+ return matrix;
701
+ }
702
+
703
+ /**
704
+ * A simplified version of {@link localToParent} matrix used for transforming
705
+ * direction vectors.
706
+ *
707
+ * @internal
708
+ */
709
+ @computed()
710
+ protected scalingRotationMatrix(): DOMMatrix {
711
+ const matrix = new DOMMatrix();
712
+
713
+ matrix.rotateSelf(0, 0, this.rotation());
714
+ matrix.scaleSelf(this.scale.x(), this.scale.y());
715
+
716
+ const offset = this.offset();
717
+ if (!offset.exactlyEquals(Vector2.zero)) {
718
+ const translate = this.size().mul(offset).scale(-0.5);
719
+ matrix.translateSelf(translate.x, translate.y);
720
+ }
721
+
722
+ return matrix;
723
+ }
724
+
725
+ protected getComputedLayout(): BBox {
726
+ return new BBox(this.element.getBoundingClientRect());
727
+ }
728
+
729
+ @computed()
730
+ public computedPosition(): Vector2 {
731
+ this.requestLayoutUpdate();
732
+ const box = this.getComputedLayout();
733
+
734
+ const position = new Vector2(
735
+ box.x + (box.width / 2) * this.offset.x(),
736
+ box.y + (box.height / 2) * this.offset.y(),
737
+ );
738
+
739
+ const parent = this.parentTransform();
740
+ if (parent) {
741
+ const parentRect = parent.getComputedLayout();
742
+ position.x -= parentRect.x + (parentRect.width - box.width) / 2;
743
+ position.y -= parentRect.y + (parentRect.height - box.height) / 2;
744
+ }
745
+
746
+ return position;
747
+ }
748
+
749
+ @computed()
750
+ protected computedSize(): Vector2 {
751
+ this.requestLayoutUpdate();
752
+ return this.getComputedLayout().size;
753
+ }
754
+
755
+ /**
756
+ * Find the closest layout root and apply any new layout changes.
757
+ */
758
+ @computed()
759
+ protected requestLayoutUpdate() {
760
+ const parent = this.parentTransform();
761
+ if (this.appendedToView()) {
762
+ parent?.requestFontUpdate();
763
+ this.updateLayout();
764
+ } else {
765
+ parent!.requestLayoutUpdate();
766
+ }
767
+ }
768
+
769
+ @computed()
770
+ protected appendedToView() {
771
+ const root = this.isLayoutRoot();
772
+ if (root) {
773
+ this.view().element.append(this.element);
774
+ }
775
+
776
+ return root;
777
+ }
778
+
779
+ /**
780
+ * Apply any new layout changes to this node and its children.
781
+ */
782
+ @computed()
783
+ protected updateLayout() {
784
+ this.applyFont();
785
+ this.applyFlex();
786
+ if (this.layoutEnabled()) {
787
+ const children = this.layoutChildren();
788
+ for (const child of children) {
789
+ child.updateLayout();
790
+ }
791
+ }
792
+ }
793
+
794
+ @computed()
795
+ protected layoutChildren(): Layout[] {
796
+ const queue = [...this.children()];
797
+ const result: Layout[] = [];
798
+ const elements: HTMLElement[] = [];
799
+ while (queue.length) {
800
+ const child = queue.shift();
801
+ if (child instanceof Layout) {
802
+ if (child.layoutEnabled()) {
803
+ result.push(child);
804
+ elements.push(child.element);
805
+ }
806
+ } else if (child) {
807
+ queue.unshift(...child.children());
808
+ }
809
+ }
810
+ this.element.replaceChildren(...elements);
811
+
812
+ return result;
813
+ }
814
+
815
+ /**
816
+ * Apply any new font changes to this node and all of its ancestors.
817
+ */
818
+ @computed()
819
+ protected requestFontUpdate() {
820
+ this.appendedToView();
821
+ this.parentTransform()?.requestFontUpdate();
822
+ this.applyFont();
823
+ }
824
+
825
+ protected override getCacheBBox(): BBox {
826
+ return BBox.fromSizeCentered(this.computedSize());
827
+ }
828
+
829
+ protected override async draw(context: CanvasRenderingContext2D) {
830
+ await document.fonts?.ready;
831
+ if (this.clip()) {
832
+ const size = this.computedSize();
833
+ if (size.width === 0 || size.height === 0) {
834
+ return;
835
+ }
836
+
837
+ context.beginPath();
838
+ context.rect(size.width / -2, size.height / -2, size.width, size.height);
839
+ context.closePath();
840
+ context.clip();
841
+ }
842
+
843
+ await this.drawChildren(context);
844
+ }
845
+
846
+ public override drawOverlay(
847
+ context: CanvasRenderingContext2D,
848
+ matrix: DOMMatrix,
849
+ ) {
850
+ const size = this.computedSize();
851
+ const offsetVector = size.mul(this.offset()).scale(0.5);
852
+ const offset = transformVectorAsPoint(offsetVector, matrix);
853
+ const box = BBox.fromSizeCentered(size);
854
+ const layout = box.transformCorners(matrix);
855
+ const padding = box
856
+ .addSpacing(this.padding().scale(-1))
857
+ .transformCorners(matrix);
858
+ const margin = box.addSpacing(this.margin()).transformCorners(matrix);
859
+
860
+ context.beginPath();
861
+ drawLine(context, margin);
862
+ drawLine(context, layout);
863
+ context.closePath();
864
+ context.fillStyle = 'rgba(255,193,125,0.6)';
865
+ context.fill('evenodd');
866
+
867
+ context.beginPath();
868
+ drawLine(context, layout);
869
+ drawLine(context, padding);
870
+ context.closePath();
871
+ context.fillStyle = 'rgba(180,255,147,0.6)';
872
+ context.fill('evenodd');
873
+
874
+ context.beginPath();
875
+ drawLine(context, layout);
876
+ context.closePath();
877
+ context.lineWidth = 1;
878
+ context.strokeStyle = 'white';
879
+ context.stroke();
880
+
881
+ context.beginPath();
882
+ drawPivot(context, offset);
883
+ context.stroke();
884
+ }
885
+
886
+ public getOriginDelta(origin: Origin) {
887
+ const size = this.computedSize().scale(0.5);
888
+ const offset = this.offset().mul(size);
889
+ if (origin === Origin.Middle) {
890
+ return offset.flipped;
891
+ }
892
+
893
+ const newOffset = originToOffset(origin).mul(size);
894
+ return newOffset.sub(offset);
895
+ }
896
+
897
+ /**
898
+ * Update the offset of this node and adjust the position to keep it in the
899
+ * same place.
900
+ *
901
+ * @param offset - The new offset.
902
+ */
903
+ public moveOffset(offset: Vector2) {
904
+ const size = this.computedSize().scale(0.5);
905
+ const oldOffset = this.offset().mul(size);
906
+ const newOffset = offset.mul(size);
907
+ this.offset(offset);
908
+ this.position(this.position().add(newOffset).sub(oldOffset));
909
+ }
910
+
911
+ protected parsePixels(value: number | null): string {
912
+ return value === null ? '' : `${value}px`;
913
+ }
914
+
915
+ protected parseLength(value: number | string | null): string {
916
+ if (value === null) {
917
+ return '';
918
+ }
919
+ if (typeof value === 'string') {
920
+ return value;
921
+ }
922
+ return `${value}px`;
923
+ }
924
+
925
+ @computed()
926
+ protected applyFlex() {
927
+ this.element.style.position = this.isLayoutRoot() ? 'absolute' : 'relative';
928
+
929
+ const size = this.desiredSize();
930
+ this.element.style.width = this.parseLength(size.x);
931
+ this.element.style.height = this.parseLength(size.y);
932
+ this.element.style.maxWidth = this.parseLength(this.maxWidth());
933
+ this.element.style.minWidth = this.parseLength(this.minWidth());
934
+ this.element.style.maxHeight = this.parseLength(this.maxHeight());
935
+ this.element.style.minHeight = this.parseLength(this.minHeight()!);
936
+ this.element.style.aspectRatio =
937
+ this.ratio() === null ? '' : this.ratio()!.toString();
938
+
939
+ this.element.style.marginTop = this.parsePixels(this.margin.top());
940
+ this.element.style.marginBottom = this.parsePixels(this.margin.bottom());
941
+ this.element.style.marginLeft = this.parsePixels(this.margin.left());
942
+ this.element.style.marginRight = this.parsePixels(this.margin.right());
943
+
944
+ this.element.style.paddingTop = this.parsePixels(this.padding.top());
945
+ this.element.style.paddingBottom = this.parsePixels(this.padding.bottom());
946
+ this.element.style.paddingLeft = this.parsePixels(this.padding.left());
947
+ this.element.style.paddingRight = this.parsePixels(this.padding.right());
948
+
949
+ this.element.style.flexDirection = this.direction();
950
+ this.element.style.flexBasis = this.parseLength(this.basis()!);
951
+ this.element.style.flexWrap = this.wrap();
952
+
953
+ this.element.style.justifyContent = this.justifyContent();
954
+ this.element.style.alignContent = this.alignContent();
955
+ this.element.style.alignItems = this.alignItems();
956
+ this.element.style.alignSelf = this.alignSelf();
957
+ this.element.style.columnGap = this.parseLength(this.gap.x());
958
+ this.element.style.rowGap = this.parseLength(this.gap.y());
959
+
960
+ if (this.sizeLockCounter() > 0) {
961
+ this.element.style.flexGrow = '0';
962
+ this.element.style.flexShrink = '0';
963
+ } else {
964
+ this.element.style.flexGrow = this.grow().toString();
965
+ this.element.style.flexShrink = this.shrink().toString();
966
+ }
967
+ }
968
+
969
+ @computed()
970
+ protected applyFont() {
971
+ const loadingFonts = document.fonts
972
+ ? Array.from(document.fonts).filter(font => font.status === 'loading')
973
+ : [];
974
+ if (loadingFonts.length > 0) {
975
+ DependencyContext.collectPromise(
976
+ (async () => {
977
+ await document.fonts?.ready;
978
+ })(),
979
+ );
980
+ }
981
+
982
+ this.element.style.fontFamily = this.fontFamily.isInitial()
983
+ ? ''
984
+ : this.fontFamily();
985
+ this.element.style.fontSize = this.fontSize.isInitial()
986
+ ? ''
987
+ : `${this.fontSize()}px`;
988
+ this.element.style.fontStyle = this.fontStyle.isInitial()
989
+ ? ''
990
+ : this.fontStyle();
991
+ if (this.lineHeight.isInitial()) {
992
+ this.element.style.lineHeight = '';
993
+ } else {
994
+ const lineHeight = this.lineHeight();
995
+ this.element.style.lineHeight =
996
+ typeof lineHeight === 'string'
997
+ ? (parseFloat(lineHeight as string) / 100).toString()
998
+ : `${lineHeight}px`;
999
+ }
1000
+ this.element.style.fontWeight = this.fontWeight.isInitial()
1001
+ ? ''
1002
+ : this.fontWeight().toString();
1003
+ this.element.style.letterSpacing = this.letterSpacing.isInitial()
1004
+ ? ''
1005
+ : `${this.letterSpacing()}px`;
1006
+
1007
+ this.element.style.textAlign = this.textAlign.isInitial()
1008
+ ? ''
1009
+ : this.textAlign();
1010
+
1011
+ if (this.textWrap.isInitial()) {
1012
+ this.element.style.whiteSpace = '';
1013
+ return;
1014
+ }
1015
+
1016
+ const wrap = this.textWrap();
1017
+
1018
+ if (typeof wrap === 'boolean') {
1019
+ this.element.style.whiteSpace = wrap ? 'normal' : 'nowrap';
1020
+ return;
1021
+ }
1022
+
1023
+ if (wrap === 'pre') {
1024
+ this.element.style.whiteSpace = wrap;
1025
+ return;
1026
+ }
1027
+
1028
+ if (wrap === 'balance') {
1029
+ this.element.style.whiteSpace = 'normal';
1030
+ this.element.style.textWrap = wrap;
1031
+ return;
1032
+ }
1033
+ }
1034
+
1035
+ public override dispose() {
1036
+ super.dispose();
1037
+ this.sizeLockCounter?.context.dispose();
1038
+ if (this.element) {
1039
+ this.element.remove();
1040
+ this.element.innerHTML = '';
1041
+ }
1042
+ this.element = null as unknown as HTMLElement;
1043
+ this.styles = null as unknown as CSSStyleDeclaration;
1044
+ }
1045
+
1046
+ public override hit(position: Vector2): Node | null {
1047
+ const local = transformVectorAsPoint(
1048
+ position,
1049
+ this.localToParent().inverse(),
1050
+ );
1051
+ if (this.cacheBBox().includes(local)) {
1052
+ return super.hit(position) ?? this;
1053
+ }
1054
+
1055
+ return null;
1056
+ }
1057
+ }
1058
+
1059
+ function originSignal(origin: Origin): PropertyDecorator {
1060
+ return (target, key) => {
1061
+ signal()(target, key);
1062
+ cloneable(false)(target, key);
1063
+ const meta = getPropertyMeta<any>(target, key);
1064
+ meta!.parser = value => new Vector2(value);
1065
+ meta!.getter = function (this: Layout) {
1066
+ const originOffset = this.computedSize().getOriginOffset(origin);
1067
+ return transformVectorAsPoint(originOffset, this.localToParent());
1068
+ };
1069
+ meta!.setter = function (
1070
+ this: Layout,
1071
+ value: SignalValue<PossibleVector2>,
1072
+ ) {
1073
+ this.position(
1074
+ modify(value, unwrapped => {
1075
+ const originDelta = this.getOriginDelta(origin);
1076
+ return transformVector(
1077
+ originDelta,
1078
+ this.scalingRotationMatrix(),
1079
+ ).flipped.add(unwrapped);
1080
+ }),
1081
+ );
1082
+ return this;
1083
+ };
1084
+ };
1085
+ }
1086
+
1087
+ addInitializer<Layout>(Layout.prototype, instance => {
1088
+ instance.element = document.createElement('div');
1089
+ instance.element.style.display = 'flex';
1090
+ instance.element.style.boxSizing = 'border-box';
1091
+ instance.styles = getComputedStyle(instance.element);
1092
+ });