@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,227 @@
1
+ import {Vector2, clamp} from '@canvas-commons/core';
2
+ import {CubicBezierSegment} from './CubicBezierSegment';
3
+ import {CurveProfile} from './CurveProfile';
4
+ import {KnotInfo} from './KnotInfo';
5
+ import {PolynomialSegment} from './PolynomialSegment';
6
+ import {QuadBezierSegment} from './QuadBezierSegment';
7
+
8
+ function isCubicSegment(
9
+ segment: PolynomialSegment,
10
+ ): segment is CubicBezierSegment {
11
+ return segment instanceof CubicBezierSegment;
12
+ }
13
+
14
+ /**
15
+ * Update a given knot's handles to be a blend between the user provided handles
16
+ * and a set of auto calculated handles that smoothly connect the knot to its
17
+ * two neighboring knots.
18
+ *
19
+ * @param knot - The knot for which to calculate the handles
20
+ * @param previous - The previous knot in the spline, relative to the provided
21
+ * knot.
22
+ * @param next - The next knot in the spline, relative to the provided knot.
23
+ * @param smoothness - The desired smoothness of the spline. Affects the scaling
24
+ * of the auto calculated handles.
25
+ */
26
+ function calculateSmoothHandles(
27
+ knot: KnotInfo,
28
+ previous: KnotInfo,
29
+ next: KnotInfo,
30
+ smoothness: number,
31
+ ) {
32
+ if (knot.auto.start === 0 && knot.auto.end === 0) {
33
+ return;
34
+ }
35
+
36
+ // See for reference:
37
+ // http://scaledinnovation.com/analytics/splines/aboutSplines.html
38
+ const distanceToPrev = knot.position.sub(previous.position).magnitude;
39
+ const distanceToNext = next.position.sub(knot.position).magnitude;
40
+ const fa = (smoothness * distanceToPrev) / (distanceToPrev + distanceToNext);
41
+ const fb = smoothness - fa;
42
+ const startHandle = new Vector2(
43
+ knot.position.x - fa * (next.position.x - previous.position.x),
44
+ knot.position.y - fa * (next.position.y - previous.position.y),
45
+ );
46
+ const endHandle = new Vector2(
47
+ knot.position.x + fb * (next.position.x - previous.position.x),
48
+ knot.position.y + fb * (next.position.y - previous.position.y),
49
+ );
50
+
51
+ knot.startHandle = knot.startHandle.lerp(startHandle, knot.auto.start);
52
+ knot.endHandle = knot.endHandle.lerp(endHandle, knot.auto.end);
53
+ }
54
+
55
+ /**
56
+ * Calculate the `minSin` value of the curve profile so that miter joins get
57
+ * taken into account properly.
58
+ */
59
+ function updateMinSin(profile: CurveProfile) {
60
+ for (let i = 0; i < profile.segments.length; i++) {
61
+ const segmentA = profile.segments[i] as PolynomialSegment;
62
+ const segmentB = profile.segments[
63
+ (i + 1) % profile.segments.length
64
+ ] as PolynomialSegment;
65
+
66
+ // Quadratic Bézier segments will always join smoothly with the previous
67
+ // segment. This means that we can skip the segment since it's impossible
68
+ // to have a miter join between the two segments.
69
+ if (!isCubicSegment(segmentA) || !isCubicSegment(segmentB)) {
70
+ continue;
71
+ }
72
+
73
+ const startVector = segmentA.p2.sub(segmentA.p3).normalized.safe;
74
+ const endVector = segmentB.p1.sub(segmentB.p0).normalized.safe;
75
+ const dot = startVector.dot(endVector);
76
+
77
+ // A miter join can only occur if the handle is broken, so we can skip the
78
+ // segment if the handles are mirrored.
79
+ const isBroken = 1 - Math.abs(dot) > 0.0001;
80
+ if (!isBroken) {
81
+ continue;
82
+ }
83
+
84
+ const angleBetween = Math.acos(clamp(-1, 1, dot));
85
+ const angleSin = Math.sin(angleBetween / 2);
86
+
87
+ profile.minSin = Math.min(profile.minSin, Math.abs(angleSin));
88
+ }
89
+ }
90
+
91
+ function addSegmentToProfile(
92
+ profile: CurveProfile,
93
+ p0: Vector2,
94
+ p1: Vector2,
95
+ p2: Vector2,
96
+ p3?: Vector2,
97
+ ) {
98
+ const segment =
99
+ p3 !== undefined
100
+ ? new CubicBezierSegment(p0, p1, p2, p3)
101
+ : new QuadBezierSegment(p0, p1, p2);
102
+ profile.segments.push(segment);
103
+ profile.arcLength += segment.arcLength;
104
+ }
105
+
106
+ /**
107
+ * Calculate the curve profile of a spline based on a set of knots.
108
+ *
109
+ * @param knots - The knots defining the spline
110
+ * @param closed - Whether the spline should be closed or not
111
+ * @param smoothness - The desired smoothness of the spline when using auto
112
+ * calculated handles.
113
+ */
114
+ export function getBezierSplineProfile(
115
+ knots: KnotInfo[],
116
+ closed: boolean,
117
+ smoothness: number,
118
+ ): CurveProfile {
119
+ const profile: CurveProfile = {
120
+ segments: [],
121
+ arcLength: 0,
122
+ minSin: 1,
123
+ };
124
+
125
+ if (knots.length < 2) {
126
+ return profile;
127
+ }
128
+
129
+ // First, we want to calculate the actual handle positions for each knot. We
130
+ // do so using the knot's `auto` value to blend between the user-provided
131
+ // handles and the auto calculated smooth handles.
132
+ const numberOfKnots = knots.length;
133
+ for (let i = 0; i < numberOfKnots; i++) {
134
+ // Calculating the auto handles for a given knot requires both of the knot's
135
+ // neighboring knots. To make sure that this works properly for the first
136
+ // and last knots of the spline, we want to make sure to wrap around to the
137
+ // beginning and end of the array, respectively.
138
+ const prevIndex = (i - 1 + numberOfKnots) % numberOfKnots;
139
+ const nextIndex = (i + 1) % numberOfKnots;
140
+ calculateSmoothHandles(
141
+ knots[i],
142
+ knots[prevIndex],
143
+ knots[nextIndex],
144
+ smoothness,
145
+ );
146
+ }
147
+
148
+ const firstKnot = knots[0];
149
+ const secondKnot = knots[1];
150
+
151
+ // Drawing the first and last segments of a spline has a few edge cases we
152
+ // need to consider:
153
+ // If the spline is not closed and the first knot should use the auto
154
+ // calculated handles, we want to draw a quadratic Bézier curve instead of a
155
+ // cubic one.
156
+ if (!closed && firstKnot.auto.start === 1 && firstKnot.auto.end === 1) {
157
+ addSegmentToProfile(
158
+ profile,
159
+ firstKnot.position,
160
+ secondKnot.startHandle,
161
+ secondKnot.position,
162
+ );
163
+ } else {
164
+ // Otherwise, draw a cubic Bézier segment like we do for the other segments.
165
+ addSegmentToProfile(
166
+ profile,
167
+ firstKnot.position,
168
+ firstKnot.endHandle,
169
+ secondKnot.startHandle,
170
+ secondKnot.position,
171
+ );
172
+ }
173
+
174
+ // Add all intermediate spline segments as cubic Bézier curve segments.
175
+ for (let i = 1; i < numberOfKnots - 2; i++) {
176
+ const start = knots[i];
177
+ const end = knots[i + 1];
178
+ addSegmentToProfile(
179
+ profile,
180
+ start.position,
181
+ start.endHandle,
182
+ end.startHandle,
183
+ end.position,
184
+ );
185
+ }
186
+
187
+ const lastKnot = knots.at(-1)!;
188
+ const secondToLastKnot = knots.at(-2)!;
189
+
190
+ if (knots.length > 2) {
191
+ // Similar to the first segment, we also want to draw the last segment as a
192
+ // quadratic Bézier curve if the curve is not closed and the knot should
193
+ // use the auto calculated handles.
194
+ if (!closed && lastKnot.auto.start === 1 && lastKnot.auto.end === 1) {
195
+ addSegmentToProfile(
196
+ profile,
197
+ secondToLastKnot.position,
198
+ secondToLastKnot.endHandle,
199
+ lastKnot.position,
200
+ );
201
+ } else {
202
+ addSegmentToProfile(
203
+ profile,
204
+ secondToLastKnot.position,
205
+ secondToLastKnot.endHandle,
206
+ lastKnot.startHandle,
207
+ lastKnot.position,
208
+ );
209
+ }
210
+ }
211
+
212
+ // If the spline should be closed, add one final cubic Bézier segment
213
+ // connecting the last and first knots.
214
+ if (closed) {
215
+ addSegmentToProfile(
216
+ profile,
217
+ lastKnot.position,
218
+ lastKnot.endHandle,
219
+ firstKnot.startHandle,
220
+ firstKnot.position,
221
+ );
222
+ }
223
+
224
+ updateMinSin(profile);
225
+
226
+ return profile;
227
+ }
@@ -0,0 +1,86 @@
1
+ import {Vector2} from '@canvas-commons/core';
2
+ import {ArcSegment} from './ArcSegment';
3
+ import {CurveProfile} from './CurveProfile';
4
+ import {LineSegment} from './LineSegment';
5
+ import {Segment} from './Segment';
6
+
7
+ export function getCircleProfile(
8
+ size: Vector2,
9
+ startAngle: number,
10
+ endAngle: number,
11
+ closed: boolean,
12
+ counterclockwise = false,
13
+ ): CurveProfile {
14
+ const profile: CurveProfile = {
15
+ arcLength: 0,
16
+ minSin: 1,
17
+ segments: [],
18
+ };
19
+
20
+ if (endAngle < startAngle) {
21
+ const loops = Math.floor((startAngle - endAngle) / (Math.PI * 2)) + 1;
22
+ endAngle += Math.PI * 2 * loops;
23
+ } else if (endAngle > startAngle + Math.PI * 2) {
24
+ const loops = Math.floor((endAngle - startAngle) / (Math.PI * 2));
25
+ endAngle -= Math.PI * 2 * loops;
26
+ }
27
+
28
+ const middleAngle = (startAngle + endAngle) / 2;
29
+ const from = size.mul(Vector2.fromRadians(startAngle));
30
+ const to = size.mul(Vector2.fromRadians(endAngle));
31
+ const middle = size
32
+ .mul(Vector2.fromRadians(middleAngle))
33
+ .scale(counterclockwise ? -1 : 1);
34
+
35
+ if (closed) {
36
+ addSegment(profile, new LineSegment(Vector2.zero, from));
37
+ }
38
+
39
+ addArcSegment(
40
+ profile,
41
+ size,
42
+ from,
43
+ middle,
44
+ startAngle,
45
+ middleAngle,
46
+ counterclockwise,
47
+ );
48
+ addArcSegment(
49
+ profile,
50
+ size,
51
+ middle,
52
+ to,
53
+ middleAngle,
54
+ endAngle,
55
+ counterclockwise,
56
+ );
57
+
58
+ if (closed) {
59
+ addSegment(profile, new LineSegment(to, Vector2.zero));
60
+ }
61
+
62
+ return profile;
63
+ }
64
+
65
+ function addSegment(profile: CurveProfile, segment: Segment) {
66
+ profile.segments.push(segment);
67
+ profile.arcLength += segment.arcLength;
68
+ }
69
+
70
+ function addArcSegment(
71
+ profile: CurveProfile,
72
+ size: Vector2,
73
+ from: Vector2,
74
+ to: Vector2,
75
+ fromAngle: number,
76
+ toAngle: number,
77
+ counterclockwise: boolean,
78
+ ) {
79
+ const small = Math.abs(fromAngle - toAngle) <= 180 ? 1 : 0;
80
+ const flip = fromAngle > toAngle ? 0 : 1;
81
+ const counter = counterclockwise ? 0 : 1;
82
+ addSegment(
83
+ profile,
84
+ new ArcSegment(from, size, 0, 0, small ^ counter ^ flip, to),
85
+ );
86
+ }
@@ -0,0 +1,177 @@
1
+ import {Vector2, clamp} from '@canvas-commons/core';
2
+ import parse, {PathCommand} from 'parse-svg-path';
3
+ import {ArcSegment} from './ArcSegment';
4
+ import {CubicBezierSegment} from './CubicBezierSegment';
5
+ import {CurveProfile} from './CurveProfile';
6
+ import {LineSegment} from './LineSegment';
7
+ import {QuadBezierSegment} from './QuadBezierSegment';
8
+ import {Segment} from './Segment';
9
+
10
+ function addSegmentToProfile(profile: CurveProfile, segment: Segment) {
11
+ profile.segments.push(segment);
12
+ profile.arcLength += segment.arcLength;
13
+ }
14
+
15
+ function getArg(command: PathCommand, argumentIndex: number) {
16
+ return command[argumentIndex + 1] as number;
17
+ }
18
+
19
+ function getVector2(command: PathCommand, argumentIndex: number) {
20
+ return new Vector2(
21
+ command[argumentIndex + 1] as number,
22
+ command[argumentIndex + 2] as number,
23
+ );
24
+ }
25
+
26
+ function getPoint(
27
+ command: PathCommand,
28
+ argumentIndex: number,
29
+ isRelative: boolean,
30
+ currentPoint: Vector2,
31
+ ) {
32
+ const point = getVector2(command, argumentIndex);
33
+ return isRelative ? currentPoint.add(point) : point;
34
+ }
35
+
36
+ function reflectControlPoint(control: Vector2, currentPoint: Vector2) {
37
+ return currentPoint.add(currentPoint.sub(control));
38
+ }
39
+
40
+ function updateMinSin(profile: CurveProfile) {
41
+ for (let i = 0; i < profile.segments.length; i++) {
42
+ const segmentA = profile.segments[i];
43
+ const segmentB = profile.segments[(i + 1) % profile.segments.length];
44
+
45
+ // In cubic bezier this equal p2.sub(p3)
46
+ const startVector = segmentA.getPoint(1).tangent.scale(-1);
47
+ // In cubic bezier this equal p1.sub(p0)
48
+ const endVector = segmentB.getPoint(0).tangent;
49
+ const dot = startVector.dot(endVector);
50
+
51
+ const angleBetween = Math.acos(clamp(-1, 1, dot));
52
+ const angleSin = Math.sin(angleBetween / 2);
53
+
54
+ profile.minSin = Math.min(profile.minSin, Math.abs(angleSin));
55
+ }
56
+ }
57
+
58
+ export function getPathProfile(data: string): CurveProfile {
59
+ const profile: CurveProfile = {
60
+ segments: [],
61
+ arcLength: 0,
62
+ minSin: 1,
63
+ };
64
+
65
+ const segments = parse(data);
66
+ let currentPoint = new Vector2(0, 0);
67
+ let firstPoint: Vector2 | null = null;
68
+
69
+ for (const segment of segments) {
70
+ const command = segment[0].toLowerCase();
71
+ const isRelative = segment[0] === command;
72
+
73
+ if (command === 'm') {
74
+ currentPoint = getPoint(segment, 0, isRelative, currentPoint);
75
+ firstPoint = currentPoint;
76
+ } else if (command === 'l') {
77
+ const nextPoint = getPoint(segment, 0, isRelative, currentPoint);
78
+ addSegmentToProfile(profile, new LineSegment(currentPoint, nextPoint));
79
+ currentPoint = nextPoint;
80
+ } else if (command === 'h') {
81
+ const x = getArg(segment, 0);
82
+ const nextPoint = isRelative
83
+ ? currentPoint.addX(x)
84
+ : new Vector2(x, currentPoint.y);
85
+ addSegmentToProfile(profile, new LineSegment(currentPoint, nextPoint));
86
+ currentPoint = nextPoint;
87
+ } else if (command === 'v') {
88
+ const y = getArg(segment, 0);
89
+ const nextPoint = isRelative
90
+ ? currentPoint.addY(y)
91
+ : new Vector2(currentPoint.x, y);
92
+ addSegmentToProfile(profile, new LineSegment(currentPoint, nextPoint));
93
+ currentPoint = nextPoint;
94
+ } else if (command === 'q') {
95
+ const controlPoint = getPoint(segment, 0, isRelative, currentPoint);
96
+ const nextPoint = getPoint(segment, 2, isRelative, currentPoint);
97
+ addSegmentToProfile(
98
+ profile,
99
+ new QuadBezierSegment(currentPoint, controlPoint, nextPoint),
100
+ );
101
+ currentPoint = nextPoint;
102
+ } else if (command === 't') {
103
+ const lastSegment = profile.segments.at(-1);
104
+ const controlPoint =
105
+ lastSegment instanceof QuadBezierSegment
106
+ ? reflectControlPoint(lastSegment.p1, currentPoint)
107
+ : currentPoint;
108
+
109
+ const nextPoint = getPoint(segment, 0, isRelative, currentPoint);
110
+ addSegmentToProfile(
111
+ profile,
112
+ new QuadBezierSegment(currentPoint, controlPoint, nextPoint),
113
+ );
114
+ currentPoint = nextPoint;
115
+ } else if (command === 'c') {
116
+ const startControlPoint = getPoint(segment, 0, isRelative, currentPoint);
117
+ const endControlPoint = getPoint(segment, 2, isRelative, currentPoint);
118
+ const nextPoint = getPoint(segment, 4, isRelative, currentPoint);
119
+ addSegmentToProfile(
120
+ profile,
121
+ new CubicBezierSegment(
122
+ currentPoint,
123
+ startControlPoint,
124
+ endControlPoint,
125
+ nextPoint,
126
+ ),
127
+ );
128
+ currentPoint = nextPoint;
129
+ } else if (command === 's') {
130
+ const lastSegment = profile.segments.at(-1);
131
+ const startControlPoint =
132
+ lastSegment instanceof CubicBezierSegment
133
+ ? reflectControlPoint(lastSegment.p2, currentPoint)
134
+ : currentPoint;
135
+
136
+ const endControlPoint = getPoint(segment, 0, isRelative, currentPoint);
137
+ const nextPoint = getPoint(segment, 2, isRelative, currentPoint);
138
+ addSegmentToProfile(
139
+ profile,
140
+ new CubicBezierSegment(
141
+ currentPoint,
142
+ startControlPoint,
143
+ endControlPoint,
144
+ nextPoint,
145
+ ),
146
+ );
147
+ currentPoint = nextPoint;
148
+ } else if (command === 'a') {
149
+ const radius = getVector2(segment, 0);
150
+ const angle = getArg(segment, 2);
151
+ const largeArcFlag = getArg(segment, 3);
152
+ const sweepFlag = getArg(segment, 4);
153
+ const nextPoint = getPoint(segment, 5, isRelative, currentPoint);
154
+ addSegmentToProfile(
155
+ profile,
156
+ new ArcSegment(
157
+ currentPoint,
158
+ radius,
159
+ angle,
160
+ largeArcFlag,
161
+ sweepFlag,
162
+ nextPoint,
163
+ ),
164
+ );
165
+ currentPoint = nextPoint;
166
+ } else if (command === 'z') {
167
+ if (!firstPoint) continue;
168
+ if (currentPoint.equals(firstPoint)) continue;
169
+
170
+ addSegmentToProfile(profile, new LineSegment(currentPoint, firstPoint));
171
+ currentPoint = firstPoint;
172
+ }
173
+ }
174
+ updateMinSin(profile);
175
+
176
+ return profile;
177
+ }
@@ -0,0 +1,21 @@
1
+ import {Vector2, clamp} from '@canvas-commons/core';
2
+ import {CurvePoint} from './CurvePoint';
3
+ import {CurveProfile} from './CurveProfile';
4
+
5
+ export function getPointAtDistance(
6
+ profile: CurveProfile,
7
+ distance: number,
8
+ ): CurvePoint {
9
+ const clamped = clamp(0, profile.arcLength, distance);
10
+ let length = 0;
11
+ for (const segment of profile.segments) {
12
+ const previousLength = length;
13
+ length += segment.arcLength;
14
+ if (length >= clamped) {
15
+ const relative = (clamped - previousLength) / segment.arcLength;
16
+ return segment.getPoint(clamp(0, 1, relative));
17
+ }
18
+ }
19
+
20
+ return {position: Vector2.zero, tangent: Vector2.up, normal: Vector2.up};
21
+ }
@@ -0,0 +1,21 @@
1
+ import {Vector2} from '@canvas-commons/core';
2
+ import {describe, expect, test} from 'vitest';
3
+ import {getPolylineProfile} from './getPolylineProfile';
4
+
5
+ describe('getPolylineProfile', () => {
6
+ test('Correct arc length', () => {
7
+ const profile = getPolylineProfile(
8
+ [
9
+ Vector2.zero,
10
+ Vector2.zero,
11
+ new Vector2(100, 0),
12
+ new Vector2(100, 100),
13
+ new Vector2(0, 100),
14
+ ],
15
+ 0,
16
+ true,
17
+ );
18
+
19
+ expect(profile.arcLength).toBe(400);
20
+ });
21
+ });
@@ -0,0 +1,88 @@
1
+ import {Vector2, clamp} from '@canvas-commons/core';
2
+ import {CircleSegment} from './CircleSegment';
3
+ import {CurveProfile} from './CurveProfile';
4
+ import {LineSegment} from './LineSegment';
5
+
6
+ export function getPolylineProfile(
7
+ points: readonly Vector2[],
8
+ radius: number,
9
+ closed: boolean,
10
+ ): CurveProfile {
11
+ const profile: CurveProfile = {
12
+ arcLength: 0,
13
+ segments: [],
14
+ minSin: 1,
15
+ };
16
+
17
+ if (points.length === 0) {
18
+ return profile;
19
+ }
20
+
21
+ if (closed) {
22
+ const middle = points[0].add(points[points.length - 1]).scale(0.5);
23
+ points = [middle, ...points, middle];
24
+ }
25
+
26
+ let last = points[0];
27
+ for (let i = 2; i < points.length; i++) {
28
+ const start = points[i - 2];
29
+ const center = points[i - 1];
30
+ const end = points[i];
31
+
32
+ const centerToStart = start.sub(center);
33
+ const centerToEnd = end.sub(center);
34
+ const startVector = centerToStart.normalized.safe;
35
+ const endVector = centerToEnd.normalized.safe;
36
+ const angleBetween = Math.acos(clamp(-1, 1, startVector.dot(endVector)));
37
+ const angleTan = Math.tan(angleBetween / 2);
38
+ const angleSin = Math.sin(angleBetween / 2);
39
+
40
+ const safeRadius = Math.min(
41
+ radius,
42
+ angleTan * centerToStart.magnitude * (i === 2 ? 1 : 0.5),
43
+ angleTan * centerToEnd.magnitude * (i === points.length - 1 ? 1 : 0.5),
44
+ );
45
+
46
+ const circleOffsetDistance = angleSin === 0 ? 0 : safeRadius / angleSin;
47
+ const pointOffsetDistance = angleTan === 0 ? 0 : safeRadius / angleTan;
48
+ const circleDistance = startVector
49
+ .add(endVector)
50
+ .scale(1 / 2)
51
+ .normalized.safe.scale(circleOffsetDistance)
52
+ .add(center);
53
+
54
+ const counter = startVector.perpendicular.dot(endVector) < 0;
55
+ const line = new LineSegment(
56
+ last,
57
+ center.add(startVector.scale(pointOffsetDistance)),
58
+ );
59
+ const circle = new CircleSegment(
60
+ circleDistance,
61
+ safeRadius,
62
+ startVector.perpendicular.scale(counter ? 1 : -1),
63
+ endVector.perpendicular.scale(counter ? -1 : 1),
64
+ counter,
65
+ );
66
+
67
+ if (line.arcLength > 0) {
68
+ profile.segments.push(line);
69
+ profile.arcLength += line.arcLength;
70
+ }
71
+ if (circle.arcLength > 0) {
72
+ profile.segments.push(circle);
73
+ profile.arcLength += circle.arcLength;
74
+ }
75
+
76
+ profile.minSin = Math.min(profile.minSin, Math.abs(angleSin));
77
+
78
+ last = center.add(endVector.scale(pointOffsetDistance));
79
+ }
80
+
81
+ const line = new LineSegment(last, points[points.length - 1]);
82
+ if (line.arcLength > 0) {
83
+ profile.segments.push(line);
84
+ profile.arcLength += line.arcLength;
85
+ }
86
+
87
+ return profile;
88
+ }