@canvas-commons/2d 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/LICENSE +22 -0
  2. package/editor/index.css +40 -0
  3. package/editor/index.js +525 -0
  4. package/editor/index.js.map +1 -0
  5. package/lib/index-OvpZ-GsA.d.ts +5673 -0
  6. package/lib/index-OvpZ-GsA.d.ts.map +1 -0
  7. package/lib/index.d.ts +2 -0
  8. package/lib/index.js +10326 -0
  9. package/lib/index.js.map +1 -0
  10. package/lib/jsx-dev-runtime.d.ts +2 -0
  11. package/lib/jsx-dev-runtime.js +2 -0
  12. package/lib/jsx-runtime.d.ts +2 -0
  13. package/lib/jsx-runtime.js +28 -0
  14. package/lib/jsx-runtime.js.map +1 -0
  15. package/package.json +79 -0
  16. package/src/editor/NodeInspectorConfig.tsx +76 -0
  17. package/src/editor/PreviewOverlayConfig.tsx +65 -0
  18. package/src/editor/Provider.tsx +109 -0
  19. package/src/editor/SceneGraphTabConfig.tsx +87 -0
  20. package/src/editor/icons/CircleIcon.tsx +7 -0
  21. package/src/editor/icons/CodeIcon.tsx +8 -0
  22. package/src/editor/icons/CurveIcon.tsx +7 -0
  23. package/src/editor/icons/GridIcon.tsx +7 -0
  24. package/src/editor/icons/IconMap.ts +35 -0
  25. package/src/editor/icons/ImgIcon.tsx +8 -0
  26. package/src/editor/icons/LayoutIcon.tsx +9 -0
  27. package/src/editor/icons/LineIcon.tsx +7 -0
  28. package/src/editor/icons/NodeIcon.tsx +7 -0
  29. package/src/editor/icons/RayIcon.tsx +7 -0
  30. package/src/editor/icons/RectIcon.tsx +7 -0
  31. package/src/editor/icons/ShapeIcon.tsx +7 -0
  32. package/src/editor/icons/TxtIcon.tsx +8 -0
  33. package/src/editor/icons/VideoIcon.tsx +7 -0
  34. package/src/editor/icons/View2DIcon.tsx +10 -0
  35. package/src/editor/index.css +0 -0
  36. package/src/editor/index.ts +19 -0
  37. package/src/editor/shortcuts.ts +27 -0
  38. package/src/editor/tree/DetachedRoot.tsx +27 -0
  39. package/src/editor/tree/NodeElement.tsx +72 -0
  40. package/src/editor/tree/TreeElement.tsx +70 -0
  41. package/src/editor/tree/TreeRoot.tsx +10 -0
  42. package/src/editor/tree/ViewRoot.tsx +20 -0
  43. package/src/editor/tree/index.module.scss +45 -0
  44. package/src/editor/tree/index.ts +4 -0
  45. package/src/editor/tree/navigation.ts +145 -0
  46. package/src/editor/tsconfig.build.json +5 -0
  47. package/src/editor/tsconfig.json +12 -0
  48. package/src/editor/tsdoc.json +4 -0
  49. package/src/editor/utils/SignalSet.ts +37 -0
  50. package/src/editor/utils/index.ts +1 -0
  51. package/src/editor/vite-env.d.ts +1 -0
  52. package/src/lib/code/CodeCursor.ts +468 -0
  53. package/src/lib/code/CodeDiffer.ts +77 -0
  54. package/src/lib/code/CodeFragment.ts +96 -0
  55. package/src/lib/code/CodeHighlighter.ts +73 -0
  56. package/src/lib/code/CodeMetrics.ts +47 -0
  57. package/src/lib/code/CodeRange.test.ts +113 -0
  58. package/src/lib/code/CodeRange.ts +222 -0
  59. package/src/lib/code/CodeScope.ts +100 -0
  60. package/src/lib/code/CodeSelection.ts +28 -0
  61. package/src/lib/code/CodeSignal.ts +348 -0
  62. package/src/lib/code/CodeTokenizer.ts +54 -0
  63. package/src/lib/code/DefaultHighlightStyle.ts +98 -0
  64. package/src/lib/code/LezerHighlighter.ts +113 -0
  65. package/src/lib/code/diff.test.ts +311 -0
  66. package/src/lib/code/diff.ts +319 -0
  67. package/src/lib/code/extractRange.ts +125 -0
  68. package/src/lib/code/index.ts +13 -0
  69. package/src/lib/components/Bezier.ts +103 -0
  70. package/src/lib/components/Camera.ts +401 -0
  71. package/src/lib/components/Circle.ts +310 -0
  72. package/src/lib/components/Code.ts +532 -0
  73. package/src/lib/components/CubicBezier.ts +115 -0
  74. package/src/lib/components/Curve.ts +460 -0
  75. package/src/lib/components/Grid.ts +134 -0
  76. package/src/lib/components/Icon.ts +153 -0
  77. package/src/lib/components/Img.ts +328 -0
  78. package/src/lib/components/Knot.ts +156 -0
  79. package/src/lib/components/Latex.ts +537 -0
  80. package/src/lib/components/Layout.ts +1101 -0
  81. package/src/lib/components/Line.ts +394 -0
  82. package/src/lib/components/Node.ts +1941 -0
  83. package/src/lib/components/Path.ts +132 -0
  84. package/src/lib/components/Polygon.ts +266 -0
  85. package/src/lib/components/QuadBezier.ts +103 -0
  86. package/src/lib/components/Ray.ts +126 -0
  87. package/src/lib/components/Rect.ts +219 -0
  88. package/src/lib/components/SVG.ts +853 -0
  89. package/src/lib/components/Shape.ts +322 -0
  90. package/src/lib/components/Spline.ts +318 -0
  91. package/src/lib/components/Txt.test.tsx +81 -0
  92. package/src/lib/components/Txt.ts +204 -0
  93. package/src/lib/components/TxtLeaf.ts +210 -0
  94. package/src/lib/components/Video.ts +368 -0
  95. package/src/lib/components/View2D.ts +85 -0
  96. package/src/lib/components/__logs__/image-without-source.ts +18 -0
  97. package/src/lib/components/__logs__/line-without-points.ts +31 -0
  98. package/src/lib/components/__logs__/reactive-playback-rate.ts +22 -0
  99. package/src/lib/components/__logs__/spline-with-insufficient-knots.ts +25 -0
  100. package/src/lib/components/__tests__/Camera.test.tsx +73 -0
  101. package/src/lib/components/__tests__/children.test.tsx +142 -0
  102. package/src/lib/components/__tests__/clone.test.tsx +126 -0
  103. package/src/lib/components/__tests__/fontInheritance.test.tsx +102 -0
  104. package/src/lib/components/__tests__/generatorTest.ts +27 -0
  105. package/src/lib/components/__tests__/mockScene2D.ts +50 -0
  106. package/src/lib/components/__tests__/query.test.tsx +122 -0
  107. package/src/lib/components/__tests__/state.test.tsx +60 -0
  108. package/src/lib/components/index.ts +26 -0
  109. package/src/lib/components/types.ts +35 -0
  110. package/src/lib/curves/ArcSegment.ts +169 -0
  111. package/src/lib/curves/CircleSegment.ts +99 -0
  112. package/src/lib/curves/CubicBezierSegment.ts +80 -0
  113. package/src/lib/curves/CurveDrawingInfo.ts +11 -0
  114. package/src/lib/curves/CurvePoint.ts +15 -0
  115. package/src/lib/curves/CurveProfile.ts +28 -0
  116. package/src/lib/curves/KnotInfo.ts +10 -0
  117. package/src/lib/curves/LineSegment.ts +75 -0
  118. package/src/lib/curves/Polynomial.ts +355 -0
  119. package/src/lib/curves/Polynomial2D.ts +62 -0
  120. package/src/lib/curves/PolynomialSegment.ts +151 -0
  121. package/src/lib/curves/QuadBezierSegment.ts +66 -0
  122. package/src/lib/curves/Segment.ts +31 -0
  123. package/src/lib/curves/UniformPolynomialCurveSampler.ts +93 -0
  124. package/src/lib/curves/createCurveProfileLerp.ts +471 -0
  125. package/src/lib/curves/getBezierSplineProfile.ts +227 -0
  126. package/src/lib/curves/getCircleProfile.ts +86 -0
  127. package/src/lib/curves/getPathProfile.ts +177 -0
  128. package/src/lib/curves/getPointAtDistance.ts +21 -0
  129. package/src/lib/curves/getPolylineProfile.test.ts +21 -0
  130. package/src/lib/curves/getPolylineProfile.ts +88 -0
  131. package/src/lib/curves/getRectProfile.ts +138 -0
  132. package/src/lib/curves/index.ts +16 -0
  133. package/src/lib/decorators/canvasStyleSignal.ts +15 -0
  134. package/src/lib/decorators/colorSignal.ts +9 -0
  135. package/src/lib/decorators/compound.ts +85 -0
  136. package/src/lib/decorators/computed.ts +18 -0
  137. package/src/lib/decorators/defaultStyle.ts +15 -0
  138. package/src/lib/decorators/filtersSignal.ts +133 -0
  139. package/src/lib/decorators/index.ts +10 -0
  140. package/src/lib/decorators/initializers.ts +32 -0
  141. package/src/lib/decorators/nodeName.ts +13 -0
  142. package/src/lib/decorators/signal.test.ts +89 -0
  143. package/src/lib/decorators/signal.ts +348 -0
  144. package/src/lib/decorators/spacingSignal.ts +15 -0
  145. package/src/lib/decorators/transformSignals.test.ts +858 -0
  146. package/src/lib/decorators/transformSignals.ts +1633 -0
  147. package/src/lib/decorators/vector2Signal.ts +35 -0
  148. package/src/lib/globals.d.ts +2 -0
  149. package/src/lib/index.ts +9 -0
  150. package/src/lib/jsx-dev-runtime.ts +2 -0
  151. package/src/lib/jsx-runtime.ts +45 -0
  152. package/src/lib/morphers/PathMorpher.ts +8 -0
  153. package/src/lib/morphers/defaultMorpher.ts +43 -0
  154. package/src/lib/morphers/index.ts +4 -0
  155. package/src/lib/morphers/manimMorpher.ts +658 -0
  156. package/src/lib/parse-svg-path.d.ts +14 -0
  157. package/src/lib/partials/Filter.ts +185 -0
  158. package/src/lib/partials/Gradient.ts +103 -0
  159. package/src/lib/partials/Pattern.ts +35 -0
  160. package/src/lib/partials/RoughConfig.ts +180 -0
  161. package/src/lib/partials/ShaderConfig.ts +122 -0
  162. package/src/lib/partials/index.ts +5 -0
  163. package/src/lib/partials/types.ts +58 -0
  164. package/src/lib/scenes/Scene2D.ts +167 -0
  165. package/src/lib/scenes/index.ts +3 -0
  166. package/src/lib/scenes/makeScene2D.ts +19 -0
  167. package/src/lib/scenes/useScene2D.ts +6 -0
  168. package/src/lib/tsconfig.build.json +5 -0
  169. package/src/lib/tsconfig.json +11 -0
  170. package/src/lib/tsdoc.json +4 -0
  171. package/src/lib/utils/CanvasUtils.ts +304 -0
  172. package/src/lib/utils/PathDataBuilder.ts +320 -0
  173. package/src/lib/utils/diff.test.ts +453 -0
  174. package/src/lib/utils/diff.ts +148 -0
  175. package/src/lib/utils/index.ts +5 -0
  176. package/src/lib/utils/is.ts +11 -0
  177. package/src/lib/utils/makeSignalExtensions.ts +29 -0
  178. package/src/lib/utils/rough.ts +513 -0
  179. package/src/lib/utils/withDefaults.tsx +26 -0
  180. package/src/tsconfig.base.json +18 -0
  181. package/src/tsconfig.build.json +8 -0
  182. package/src/tsconfig.json +5 -0
  183. package/tsconfig.project.json +7 -0
@@ -0,0 +1,394 @@
1
+ import {
2
+ BBox,
3
+ createSignal,
4
+ PossibleVector2,
5
+ SignalValue,
6
+ SimpleSignal,
7
+ threadable,
8
+ ThreadGenerator,
9
+ TimingFunction,
10
+ tween,
11
+ unwrap,
12
+ useLogger,
13
+ Vector2,
14
+ } from '@canvas-commons/core';
15
+ import {CurveProfile, getPolylineProfile} from '../curves';
16
+ import {
17
+ calculateLerpDistance,
18
+ polygonLength,
19
+ polygonPointsLerp,
20
+ } from '../curves/createCurveProfileLerp';
21
+ import {computed, initial, nodeName, signal} from '../decorators';
22
+ import {arc, drawLine, drawPivot, lineTo, moveTo} from '../utils';
23
+ import lineWithoutPoints from './__logs__/line-without-points';
24
+ import {Curve, CurveProps} from './Curve';
25
+ import {Layout} from './Layout';
26
+
27
+ export interface LineProps extends CurveProps {
28
+ /**
29
+ * {@inheritDoc Line.radius}
30
+ */
31
+ radius?: SignalValue<number>;
32
+ /**
33
+ * {@inheritDoc Line.points}
34
+ */
35
+ points?: SignalValue<SignalValue<PossibleVector2>[]>;
36
+ }
37
+
38
+ /**
39
+ * A node for drawing lines and polygons.
40
+ *
41
+ * @remarks
42
+ * This node can be used to render any polygonal shape defined by a set of
43
+ * points.
44
+ *
45
+ * @preview
46
+ * ```tsx editor
47
+ * // snippet Simple line
48
+ * import {makeScene2D, Line} from '@canvas-commons/2d';
49
+ *
50
+ * export default makeScene2D(function* (view) {
51
+ * view.add(
52
+ * <Line
53
+ * points={[
54
+ * [150, 50],
55
+ * [0, -50],
56
+ * [-150, 50],
57
+ * ]}
58
+ * stroke={'lightseagreen'}
59
+ * lineWidth={8}
60
+ * radius={40}
61
+ * startArrow
62
+ * />,
63
+ * );
64
+ * });
65
+ *
66
+ * // snippet Polygon
67
+ * import {makeScene2D, Line} from '@canvas-commons/2d';
68
+ *
69
+ * export default makeScene2D(function* (view) {
70
+ * view.add(
71
+ * <Line
72
+ * points={[
73
+ * [-200, 70],
74
+ * [150, 70],
75
+ * [100, -70],
76
+ * [-100, -70],
77
+ * ]}
78
+ * fill={'lightseagreen'}
79
+ * closed
80
+ * />,
81
+ * );
82
+ * });
83
+ *
84
+ * // snippet Using signals
85
+ * import {makeScene2D, Line} from '@canvas-commons/2d';
86
+ * import {createSignal} from '@canvas-commons/core';
87
+ *
88
+ * export default makeScene2D(function* (view) {
89
+ * const tip = createSignal(-150);
90
+ * view.add(
91
+ * <Line
92
+ * points={[
93
+ * [-150, 70],
94
+ * [150, 70],
95
+ * // this point is dynamically calculated based on the signal:
96
+ * () => [tip(), -70],
97
+ * ]}
98
+ * stroke={'lightseagreen'}
99
+ * lineWidth={8}
100
+ * closed
101
+ * />,
102
+ * );
103
+ *
104
+ * yield* tip(150, 1).back(1);
105
+ * });
106
+ *
107
+ * // snippet Tweening points
108
+ * import {makeScene2D, Line} from '@canvas-commons/2d';
109
+ * import {createRef} from '@canvas-commons/core';
110
+ *
111
+ * export default makeScene2D(function* (view) {
112
+ * const line = createRef<Line>();
113
+ * view.add(
114
+ * <Line
115
+ * ref={line}
116
+ * points={[
117
+ * [-150, 70],
118
+ * [150, 70],
119
+ * [0, -70],
120
+ * ]}
121
+ * stroke={'lightseagreen'}
122
+ * lineWidth={8}
123
+ * radius={20}
124
+ * closed
125
+ * />,
126
+ * );
127
+ *
128
+ * yield* line()
129
+ * .points(
130
+ * [
131
+ * [-150, 0],
132
+ * [0, 100],
133
+ * [150, 0],
134
+ * [150, -70],
135
+ * [-150, -70],
136
+ * ],
137
+ * 2,
138
+ * )
139
+ * .back(2);
140
+ * });
141
+ * ```
142
+ */
143
+ @nodeName('Line')
144
+ export class Line extends Curve {
145
+ /**
146
+ * Rotate the points to minimize the overall distance traveled when tweening.
147
+ *
148
+ * @param points - The points to rotate.
149
+ * @param reference - The reference points to which the distance is measured.
150
+ * @param closed - Whether the points form a closed polygon.
151
+ */
152
+ private static rotatePoints(
153
+ points: Vector2[],
154
+ reference: Vector2[],
155
+ closed: boolean,
156
+ ) {
157
+ if (closed) {
158
+ let minDistance = Infinity;
159
+ let bestOffset = 0;
160
+ for (let offset = 0; offset < points.length; offset += 1) {
161
+ const distance = calculateLerpDistance(points, reference, offset);
162
+ if (distance < minDistance) {
163
+ minDistance = distance;
164
+ bestOffset = offset;
165
+ }
166
+ }
167
+
168
+ if (bestOffset) {
169
+ const spliced = points.splice(0, bestOffset);
170
+ points.splice(points.length, 0, ...spliced);
171
+ }
172
+ } else {
173
+ const originalDistance = calculateLerpDistance(points, reference, 0);
174
+ const reversedPoints = [...points].reverse();
175
+ const reversedDistance = calculateLerpDistance(
176
+ reversedPoints,
177
+ reference,
178
+ 0,
179
+ );
180
+ if (reversedDistance < originalDistance) {
181
+ points.reverse();
182
+ }
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Distribute additional points along the polyline.
188
+ *
189
+ * @param points - The points of a polyline along which new points should be
190
+ * distributed.
191
+ * @param count - The number of points to add.
192
+ */
193
+ private static distributePoints(points: Vector2[], count: number) {
194
+ if (points.length === 0) {
195
+ for (let j = 0; j < count; j++) {
196
+ points.push(Vector2.zero);
197
+ }
198
+ return;
199
+ }
200
+
201
+ if (points.length === 1) {
202
+ const point = points[0];
203
+ for (let j = 0; j < count; j++) {
204
+ points.push(point);
205
+ }
206
+ return;
207
+ }
208
+
209
+ const desiredLength = points.length + count;
210
+ const arcLength = polygonLength(points);
211
+ let density = arcLength === 0 ? 0 : count / arcLength;
212
+
213
+ let i = 0;
214
+ while (points.length < desiredLength) {
215
+ const pointsLeft = desiredLength - points.length;
216
+
217
+ if (i + 1 >= points.length) {
218
+ density = arcLength === 0 ? 0 : pointsLeft / arcLength;
219
+ i = 0;
220
+ continue;
221
+ }
222
+
223
+ const a = points[i];
224
+ const b = points[i + 1];
225
+ const length = a.sub(b).magnitude;
226
+ let pointCount = Math.min(Math.round(length * density), pointsLeft) + 1;
227
+
228
+ if (arcLength === 0) {
229
+ pointCount = 2;
230
+ }
231
+
232
+ for (let j = 1; j < pointCount; j++) {
233
+ points.splice(++i, 0, Vector2.lerp(a, b, j / pointCount));
234
+ }
235
+
236
+ i++;
237
+ }
238
+ }
239
+
240
+ /**
241
+ * The radius of the line's corners.
242
+ */
243
+ @initial(0)
244
+ @signal()
245
+ declare public readonly radius: SimpleSignal<number, this>;
246
+
247
+ /**
248
+ * The points of the line.
249
+ *
250
+ * @remarks
251
+ * When set to `null`, the Line will use the positions of its children as
252
+ * points.
253
+ */
254
+ @initial(null)
255
+ @signal()
256
+ declare public readonly points: SimpleSignal<
257
+ SignalValue<PossibleVector2>[] | null,
258
+ this
259
+ >;
260
+
261
+ @threadable()
262
+ protected *tweenPoints(
263
+ value: SignalValue<SignalValue<PossibleVector2>[] | null>,
264
+ time: number,
265
+ timingFunction: TimingFunction,
266
+ ): ThreadGenerator {
267
+ const fromPoints = [...this.parsedPoints()];
268
+ const toPoints = this.parsePoints(unwrap(value));
269
+ const closed = this.closed();
270
+
271
+ const diff = fromPoints.length - toPoints.length;
272
+ Line.distributePoints(diff < 0 ? fromPoints : toPoints, Math.abs(diff));
273
+ Line.rotatePoints(toPoints, fromPoints, closed);
274
+
275
+ this.tweenedPoints(fromPoints);
276
+ yield* tween(
277
+ time,
278
+ value => {
279
+ const progress = timingFunction(value);
280
+ this.tweenedPoints(polygonPointsLerp(fromPoints, toPoints, progress));
281
+ },
282
+ () => {
283
+ this.tweenedPoints(null);
284
+ this.points(value);
285
+ },
286
+ );
287
+ }
288
+
289
+ private tweenedPoints = createSignal<Vector2[] | null>(null);
290
+
291
+ public constructor(props: LineProps) {
292
+ super(props);
293
+
294
+ if (props.children === undefined && props.points === undefined) {
295
+ useLogger().warn({
296
+ message: 'No points specified for the line',
297
+ remarks: lineWithoutPoints,
298
+ inspect: this.key,
299
+ });
300
+ }
301
+ }
302
+
303
+ @computed()
304
+ protected childrenBBox() {
305
+ let points = this.tweenedPoints();
306
+ if (!points) {
307
+ const custom = this.points();
308
+ points = custom
309
+ ? custom.map(signal => new Vector2(unwrap(signal)))
310
+ : this.children()
311
+ .filter(child => !(child instanceof Layout) || child.isLayoutRoot())
312
+ .map(child => child.position());
313
+ }
314
+
315
+ return BBox.fromPoints(...points);
316
+ }
317
+
318
+ @computed()
319
+ public parsedPoints(): Vector2[] {
320
+ return this.parsePoints(this.points());
321
+ }
322
+
323
+ @computed()
324
+ public override profile(): CurveProfile {
325
+ return getPolylineProfile(
326
+ this.tweenedPoints() ?? this.parsedPoints(),
327
+ this.radius(),
328
+ this.closed(),
329
+ );
330
+ }
331
+
332
+ protected override lineWidthCoefficient(): number {
333
+ const radius = this.radius();
334
+ const join = this.lineJoin();
335
+
336
+ let coefficient = super.lineWidthCoefficient();
337
+
338
+ if (radius === 0 && join === 'miter') {
339
+ const {minSin} = this.profile();
340
+ if (minSin > 0) {
341
+ coefficient = Math.max(coefficient, 0.5 / minSin);
342
+ }
343
+ }
344
+
345
+ return coefficient;
346
+ }
347
+
348
+ public override drawOverlay(
349
+ context: CanvasRenderingContext2D,
350
+ matrix: DOMMatrix,
351
+ ) {
352
+ const box = this.childrenBBox().transformCorners(matrix);
353
+ const size = this.computedSize();
354
+ const offset = size.mul(this.anchor()).scale(0.5).transformAsPoint(matrix);
355
+
356
+ context.fillStyle = 'white';
357
+ context.strokeStyle = 'black';
358
+ context.lineWidth = 1;
359
+
360
+ const path = new Path2D();
361
+ const points = (this.tweenedPoints() ?? this.parsedPoints()).map(point =>
362
+ point.transformAsPoint(matrix),
363
+ );
364
+ if (points.length > 0) {
365
+ moveTo(path, points[0]);
366
+ for (const point of points) {
367
+ lineTo(path, point);
368
+ context.beginPath();
369
+ arc(context, point, 4);
370
+ context.closePath();
371
+ context.fill();
372
+ context.stroke();
373
+ }
374
+ }
375
+
376
+ context.strokeStyle = 'white';
377
+ context.stroke(path);
378
+
379
+ context.beginPath();
380
+ drawPivot(context, offset);
381
+ context.stroke();
382
+
383
+ context.beginPath();
384
+ drawLine(context, box);
385
+ context.closePath();
386
+ context.stroke();
387
+ }
388
+
389
+ private parsePoints(points: SignalValue<PossibleVector2>[] | null) {
390
+ return points
391
+ ? points.map(signal => new Vector2(unwrap(signal)))
392
+ : this.children().map(child => child.position());
393
+ }
394
+ }