js-draw 0.18.2 → 0.20.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 (269) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +10 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/bundledStyles.js +1 -0
  5. package/dist/cjs/src/Color4.d.ts +8 -0
  6. package/dist/cjs/src/Color4.js +67 -0
  7. package/dist/cjs/src/Editor.d.ts +2 -2
  8. package/dist/cjs/src/Editor.js +7 -7
  9. package/dist/cjs/src/SVGLoader.js +77 -12
  10. package/dist/cjs/src/Viewport.d.ts +2 -0
  11. package/dist/cjs/src/Viewport.js +6 -2
  12. package/dist/cjs/src/components/AbstractComponent.d.ts +2 -2
  13. package/dist/cjs/src/components/AbstractComponent.js +3 -3
  14. package/dist/cjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  15. package/dist/cjs/src/components/BackgroundComponent.js +309 -0
  16. package/dist/cjs/src/components/RestylableComponent.d.ts +21 -2
  17. package/dist/cjs/src/components/Stroke.d.ts +35 -0
  18. package/dist/cjs/src/components/Stroke.js +37 -3
  19. package/dist/cjs/src/components/TextComponent.d.ts +27 -17
  20. package/dist/cjs/src/components/TextComponent.js +23 -1
  21. package/dist/cjs/src/components/lib.d.ts +4 -3
  22. package/dist/cjs/src/components/lib.js +2 -2
  23. package/dist/cjs/src/components/util/StrokeSmoother.js +25 -15
  24. package/dist/cjs/src/lib.d.ts +30 -0
  25. package/dist/cjs/src/lib.js +30 -0
  26. package/dist/cjs/src/localizations/de.js +1 -1
  27. package/dist/cjs/src/localizations/es.js +1 -1
  28. package/dist/cjs/src/math/Path.js +1 -1
  29. package/dist/cjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  30. package/dist/cjs/src/math/polynomial/QuadraticBezier.js +115 -0
  31. package/dist/cjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  32. package/dist/cjs/src/math/polynomial/solveQuadratic.js +36 -0
  33. package/dist/cjs/src/rendering/RenderingStyle.d.ts +4 -4
  34. package/dist/cjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  35. package/dist/cjs/src/rendering/lib.d.ts +2 -0
  36. package/dist/cjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  37. package/dist/cjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  38. package/dist/cjs/src/rendering/renderers/CanvasRenderer.js +5 -3
  39. package/dist/cjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  40. package/dist/cjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  41. package/dist/cjs/src/rendering/renderers/SVGRenderer.js +15 -6
  42. package/dist/cjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  43. package/dist/cjs/src/toolbar/IconProvider.d.ts +2 -2
  44. package/dist/cjs/src/toolbar/localization.d.ts +2 -1
  45. package/dist/cjs/src/toolbar/localization.js +3 -2
  46. package/dist/cjs/src/toolbar/widgets/BaseWidget.js +1 -1
  47. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  48. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.js +77 -2
  49. package/dist/cjs/src/toolbar/widgets/PenToolWidget.js +1 -1
  50. package/dist/cjs/src/tools/FindTool.js +1 -1
  51. package/dist/cjs/src/tools/SoundUITool.d.ts +24 -0
  52. package/dist/cjs/src/tools/SoundUITool.js +164 -0
  53. package/dist/cjs/src/tools/TextTool.d.ts +2 -2
  54. package/dist/cjs/src/tools/ToolController.js +6 -1
  55. package/dist/cjs/src/tools/lib.d.ts +1 -0
  56. package/dist/cjs/src/tools/lib.js +3 -1
  57. package/dist/cjs/src/tools/localization.d.ts +3 -0
  58. package/dist/cjs/src/tools/localization.js +3 -0
  59. package/dist/mjs/src/Color4.d.ts +8 -0
  60. package/dist/mjs/src/Color4.mjs +64 -0
  61. package/dist/mjs/src/Editor.d.ts +2 -2
  62. package/dist/mjs/src/Editor.mjs +6 -6
  63. package/dist/mjs/src/SVGLoader.mjs +76 -11
  64. package/dist/mjs/src/Viewport.d.ts +2 -0
  65. package/dist/mjs/src/Viewport.mjs +6 -2
  66. package/dist/mjs/src/components/AbstractComponent.d.ts +2 -2
  67. package/dist/mjs/src/components/AbstractComponent.mjs +3 -3
  68. package/dist/mjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  69. package/dist/mjs/src/components/BackgroundComponent.mjs +279 -0
  70. package/dist/mjs/src/components/RestylableComponent.d.ts +21 -2
  71. package/dist/mjs/src/components/Stroke.d.ts +35 -0
  72. package/dist/mjs/src/components/Stroke.mjs +37 -3
  73. package/dist/mjs/src/components/TextComponent.d.ts +27 -17
  74. package/dist/mjs/src/components/TextComponent.mjs +23 -1
  75. package/dist/mjs/src/components/lib.d.ts +4 -3
  76. package/dist/mjs/src/components/lib.mjs +2 -2
  77. package/dist/mjs/src/components/util/StrokeSmoother.mjs +25 -15
  78. package/dist/mjs/src/lib.d.ts +30 -0
  79. package/dist/mjs/src/lib.mjs +30 -0
  80. package/dist/mjs/src/localizations/de.mjs +1 -1
  81. package/dist/mjs/src/localizations/es.mjs +1 -1
  82. package/dist/mjs/src/math/Path.mjs +1 -1
  83. package/dist/mjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  84. package/dist/mjs/src/math/polynomial/QuadraticBezier.mjs +109 -0
  85. package/dist/mjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  86. package/dist/mjs/src/math/polynomial/solveQuadratic.mjs +34 -0
  87. package/dist/mjs/src/rendering/RenderingStyle.d.ts +4 -4
  88. package/dist/mjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  89. package/dist/mjs/src/rendering/lib.d.ts +2 -0
  90. package/dist/mjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  91. package/dist/mjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  92. package/dist/mjs/src/rendering/renderers/CanvasRenderer.mjs +5 -3
  93. package/dist/mjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  94. package/dist/mjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  95. package/dist/mjs/src/rendering/renderers/SVGRenderer.mjs +15 -6
  96. package/dist/mjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  97. package/dist/mjs/src/toolbar/IconProvider.d.ts +2 -2
  98. package/dist/mjs/src/toolbar/localization.d.ts +2 -1
  99. package/dist/mjs/src/toolbar/localization.mjs +3 -2
  100. package/dist/mjs/src/toolbar/widgets/BaseWidget.mjs +1 -1
  101. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  102. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.mjs +54 -2
  103. package/dist/mjs/src/toolbar/widgets/PenToolWidget.mjs +1 -1
  104. package/dist/mjs/src/tools/FindTool.mjs +1 -1
  105. package/dist/mjs/src/tools/SoundUITool.d.ts +24 -0
  106. package/dist/mjs/src/tools/SoundUITool.mjs +158 -0
  107. package/dist/mjs/src/tools/TextTool.d.ts +2 -2
  108. package/dist/mjs/src/tools/ToolController.mjs +6 -1
  109. package/dist/mjs/src/tools/lib.d.ts +1 -0
  110. package/dist/mjs/src/tools/lib.mjs +1 -0
  111. package/dist/mjs/src/tools/localization.d.ts +3 -0
  112. package/dist/mjs/src/tools/localization.mjs +3 -0
  113. package/jest.config.js +1 -1
  114. package/package.json +19 -17
  115. package/src/Editor.css +2 -2
  116. package/src/tools/SoundUITool.css +15 -0
  117. package/src/tools/tools.css +4 -0
  118. package/dist/cjs/src/components/ImageBackground.js +0 -146
  119. package/dist/mjs/src/components/ImageBackground.mjs +0 -139
  120. package/src/Color4.test.ts +0 -40
  121. package/src/Color4.ts +0 -236
  122. package/src/Editor.loadFrom.test.ts +0 -24
  123. package/src/Editor.toSVG.test.ts +0 -111
  124. package/src/Editor.ts +0 -1122
  125. package/src/EditorImage.test.ts +0 -120
  126. package/src/EditorImage.ts +0 -603
  127. package/src/EventDispatcher.test.ts +0 -123
  128. package/src/EventDispatcher.ts +0 -71
  129. package/src/Pointer.ts +0 -127
  130. package/src/SVGLoader.test.ts +0 -114
  131. package/src/SVGLoader.ts +0 -511
  132. package/src/UndoRedoHistory.test.ts +0 -33
  133. package/src/UndoRedoHistory.ts +0 -102
  134. package/src/Viewport.ts +0 -319
  135. package/src/bundle/bundled.ts +0 -7
  136. package/src/commands/Command.ts +0 -45
  137. package/src/commands/Duplicate.ts +0 -48
  138. package/src/commands/Erase.ts +0 -74
  139. package/src/commands/SerializableCommand.ts +0 -49
  140. package/src/commands/UnresolvedCommand.ts +0 -37
  141. package/src/commands/invertCommand.ts +0 -51
  142. package/src/commands/lib.ts +0 -16
  143. package/src/commands/localization.ts +0 -47
  144. package/src/commands/uniteCommands.test.ts +0 -23
  145. package/src/commands/uniteCommands.ts +0 -135
  146. package/src/components/AbstractComponent.transformBy.test.ts +0 -22
  147. package/src/components/AbstractComponent.ts +0 -364
  148. package/src/components/ImageBackground.test.ts +0 -35
  149. package/src/components/ImageBackground.ts +0 -176
  150. package/src/components/ImageComponent.ts +0 -171
  151. package/src/components/RestylableComponent.ts +0 -142
  152. package/src/components/SVGGlobalAttributesObject.ts +0 -81
  153. package/src/components/Stroke.test.ts +0 -139
  154. package/src/components/Stroke.ts +0 -245
  155. package/src/components/TextComponent.test.ts +0 -99
  156. package/src/components/TextComponent.ts +0 -315
  157. package/src/components/UnknownSVGObject.test.ts +0 -10
  158. package/src/components/UnknownSVGObject.ts +0 -60
  159. package/src/components/builders/ArrowBuilder.ts +0 -107
  160. package/src/components/builders/FreehandLineBuilder.ts +0 -212
  161. package/src/components/builders/LineBuilder.ts +0 -77
  162. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
  163. package/src/components/builders/RectangleBuilder.ts +0 -74
  164. package/src/components/builders/types.ts +0 -15
  165. package/src/components/lib.ts +0 -25
  166. package/src/components/localization.ts +0 -22
  167. package/src/components/util/StrokeSmoother.ts +0 -293
  168. package/src/components/util/describeComponentList.ts +0 -18
  169. package/src/lib.ts +0 -37
  170. package/src/localization.ts +0 -34
  171. package/src/localizations/de.ts +0 -98
  172. package/src/localizations/en.ts +0 -8
  173. package/src/localizations/es.ts +0 -74
  174. package/src/localizations/getLocalizationTable.test.ts +0 -27
  175. package/src/localizations/getLocalizationTable.ts +0 -55
  176. package/src/math/LineSegment2.test.ts +0 -99
  177. package/src/math/LineSegment2.ts +0 -160
  178. package/src/math/Mat33.test.ts +0 -244
  179. package/src/math/Mat33.ts +0 -437
  180. package/src/math/Path.fromString.test.ts +0 -223
  181. package/src/math/Path.test.ts +0 -198
  182. package/src/math/Path.toString.test.ts +0 -77
  183. package/src/math/Path.ts +0 -790
  184. package/src/math/Rect2.test.ts +0 -204
  185. package/src/math/Rect2.ts +0 -315
  186. package/src/math/Triangle.ts +0 -29
  187. package/src/math/Vec2.test.ts +0 -30
  188. package/src/math/Vec2.ts +0 -18
  189. package/src/math/Vec3.test.ts +0 -44
  190. package/src/math/Vec3.ts +0 -218
  191. package/src/math/lib.ts +0 -15
  192. package/src/math/rounding.test.ts +0 -65
  193. package/src/math/rounding.ts +0 -156
  194. package/src/rendering/Display.ts +0 -249
  195. package/src/rendering/RenderingStyle.test.ts +0 -68
  196. package/src/rendering/RenderingStyle.ts +0 -55
  197. package/src/rendering/TextRenderingStyle.ts +0 -45
  198. package/src/rendering/caching/CacheRecord.test.ts +0 -49
  199. package/src/rendering/caching/CacheRecord.ts +0 -77
  200. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  201. package/src/rendering/caching/RenderingCache.test.ts +0 -44
  202. package/src/rendering/caching/RenderingCache.ts +0 -66
  203. package/src/rendering/caching/RenderingCacheNode.ts +0 -405
  204. package/src/rendering/caching/testUtils.ts +0 -35
  205. package/src/rendering/caching/types.ts +0 -34
  206. package/src/rendering/lib.ts +0 -6
  207. package/src/rendering/localization.ts +0 -20
  208. package/src/rendering/renderers/AbstractRenderer.ts +0 -222
  209. package/src/rendering/renderers/CanvasRenderer.ts +0 -296
  210. package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
  211. package/src/rendering/renderers/DummyRenderer.ts +0 -136
  212. package/src/rendering/renderers/SVGRenderer.ts +0 -354
  213. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
  214. package/src/testing/beforeEachFile.ts +0 -8
  215. package/src/testing/createEditor.ts +0 -11
  216. package/src/testing/global.d.ts +0 -17
  217. package/src/testing/lib.ts +0 -3
  218. package/src/testing/loadExpectExtensions.ts +0 -25
  219. package/src/testing/sendPenEvent.ts +0 -31
  220. package/src/testing/sendTouchEvent.ts +0 -78
  221. package/src/toolbar/HTMLToolbar.ts +0 -492
  222. package/src/toolbar/IconProvider.ts +0 -736
  223. package/src/toolbar/lib.ts +0 -4
  224. package/src/toolbar/localization.ts +0 -106
  225. package/src/toolbar/makeColorInput.ts +0 -145
  226. package/src/toolbar/types.ts +0 -5
  227. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  228. package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
  229. package/src/toolbar/widgets/BaseWidget.ts +0 -377
  230. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
  231. package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
  232. package/src/toolbar/widgets/HandToolWidget.ts +0 -250
  233. package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
  234. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  235. package/src/toolbar/widgets/PenToolWidget.ts +0 -288
  236. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
  237. package/src/toolbar/widgets/TextToolWidget.ts +0 -145
  238. package/src/toolbar/widgets/lib.ts +0 -13
  239. package/src/tools/BaseTool.ts +0 -76
  240. package/src/tools/Eraser.test.ts +0 -103
  241. package/src/tools/Eraser.ts +0 -139
  242. package/src/tools/FindTool.ts +0 -152
  243. package/src/tools/PanZoom.test.ts +0 -310
  244. package/src/tools/PanZoom.ts +0 -520
  245. package/src/tools/PasteHandler.ts +0 -95
  246. package/src/tools/Pen.test.ts +0 -194
  247. package/src/tools/Pen.ts +0 -226
  248. package/src/tools/PipetteTool.ts +0 -55
  249. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
  250. package/src/tools/SelectionTool/Selection.ts +0 -607
  251. package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
  252. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
  253. package/src/tools/SelectionTool/SelectionTool.ts +0 -480
  254. package/src/tools/SelectionTool/TransformMode.ts +0 -114
  255. package/src/tools/SelectionTool/types.ts +0 -11
  256. package/src/tools/TextTool.ts +0 -326
  257. package/src/tools/ToolController.ts +0 -178
  258. package/src/tools/ToolEnabledGroup.ts +0 -14
  259. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  260. package/src/tools/ToolbarShortcutHandler.ts +0 -34
  261. package/src/tools/UndoRedoShortcut.test.ts +0 -56
  262. package/src/tools/UndoRedoShortcut.ts +0 -25
  263. package/src/tools/lib.ts +0 -21
  264. package/src/tools/localization.ts +0 -66
  265. package/src/types.ts +0 -234
  266. package/src/util/assertions.ts +0 -55
  267. package/src/util/fileToBase64.ts +0 -18
  268. package/src/util/untilNextAnimationFrame.ts +0 -9
  269. package/src/util/waitForTimeout.ts +0 -9
@@ -1,198 +0,0 @@
1
- import { Bezier } from 'bezier-js';
2
- import LineSegment2 from './LineSegment2';
3
- import Path, { PathCommandType } from './Path';
4
- import Rect2 from './Rect2';
5
- import { Vec2 } from './Vec2';
6
-
7
- describe('Path', () => {
8
- it('should instantiate Beziers from cubic and quatratic commands', () => {
9
- const path = new Path(Vec2.zero, [
10
- {
11
- kind: PathCommandType.CubicBezierTo,
12
- controlPoint1: Vec2.of(1, 1),
13
- controlPoint2: Vec2.of(-1, -1),
14
- endPoint: Vec2.of(3, 3),
15
- },
16
- {
17
- kind: PathCommandType.QuadraticBezierTo,
18
- controlPoint: Vec2.of(1, 1),
19
- endPoint: Vec2.of(0, 0),
20
- },
21
- ]);
22
-
23
- expect(path.geometry.length).toBe(2);
24
-
25
- const firstItem = path.geometry[0];
26
- const secondItem = path.geometry[1];
27
- expect(firstItem).toBeInstanceOf(Bezier);
28
- expect(secondItem).toBeInstanceOf(Bezier);
29
-
30
- // Force TypeScript to do type narrowing.
31
- if (!(firstItem instanceof Bezier) || !(secondItem instanceof Bezier)) {
32
- throw new Error('Invalid state! .toBeInstanceOf should have caused test to fail!');
33
- }
34
-
35
- // Make sure the control points (and start/end points) match what was set
36
- expect(firstItem.points).toMatchObject([
37
- { x: 0, y: 0 }, { x: 1, y: 1 }, { x: -1, y: -1 }, { x: 3, y: 3 }
38
- ]);
39
- expect(secondItem.points).toMatchObject([
40
- { x: 3, y: 3 }, { x: 1, y: 1 }, { x: 0, y: 0 },
41
- ]);
42
- });
43
-
44
- it('should create LineSegments from line commands', () => {
45
- const lineStart = Vec2.zero;
46
- const lineEnd = Vec2.of(100, 100);
47
-
48
- const path = new Path(lineStart, [
49
- {
50
- kind: PathCommandType.LineTo,
51
- point: lineEnd,
52
- },
53
- ]);
54
-
55
- expect(path.geometry.length).toBe(1);
56
- expect(path.geometry[0]).toBeInstanceOf(LineSegment2);
57
- expect(path.geometry[0]).toMatchObject(
58
- new LineSegment2(lineStart, lineEnd)
59
- );
60
- });
61
-
62
- it('should give all intersections for a path made up of lines', () => {
63
- const lineStart = Vec2.of(100, 100);
64
- const path = new Path(lineStart, [
65
- {
66
- kind: PathCommandType.LineTo,
67
- point: Vec2.of(-100, 100),
68
- },
69
- {
70
- kind: PathCommandType.LineTo,
71
- point: Vec2.of(0, 0),
72
- },
73
- {
74
- kind: PathCommandType.LineTo,
75
- point: Vec2.of(100, -100),
76
- },
77
- ]);
78
-
79
- const intersections = path.intersection(
80
- new LineSegment2(Vec2.of(-50, 200), Vec2.of(-50, -200))
81
- );
82
-
83
- // Should only have intersections in quadrants II and III.
84
- expect(intersections.length).toBe(2);
85
-
86
- // First intersection should be with the first curve
87
- const firstIntersection = intersections[0];
88
- expect(firstIntersection.point.xy).toMatchObject({
89
- x: -50,
90
- y: 100,
91
- });
92
- expect(firstIntersection.curve.get(firstIntersection.parameterValue)).toMatchObject({
93
- x: -50,
94
- y: 100,
95
- });
96
- });
97
-
98
- describe('polylineApproximation', () => {
99
- it('should approximate Bézier curves with polylines', () => {
100
- const path = Path.fromString('m0,0 l4,4 Q 1,4 4,1z');
101
-
102
- expect(path.polylineApproximation()).toMatchObject([
103
- new LineSegment2(Vec2.of(0, 0), Vec2.of(4, 4)),
104
- new LineSegment2(Vec2.of(4, 4), Vec2.of(1, 4)),
105
- new LineSegment2(Vec2.of(1, 4), Vec2.of(4, 1)),
106
- new LineSegment2(Vec2.of(4, 1), Vec2.of(0, 0)),
107
- ]);
108
- });
109
- });
110
-
111
- describe('roughlyIntersectsClosed', () => {
112
- it('small, line-only path', () => {
113
- const path = Path.fromString('m0,0 l10,10 L0,10 z');
114
- expect(
115
- path.closedRoughlyIntersects(Rect2.fromCorners(Vec2.zero, Vec2.of(20, 20)))
116
- ).toBe(true);
117
- expect(
118
- path.closedRoughlyIntersects(Rect2.fromCorners(Vec2.zero, Vec2.of(2, 2)))
119
- ).toBe(true);
120
- expect(
121
- path.closedRoughlyIntersects(new Rect2(10, 1, 1, 1))
122
- ).toBe(false);
123
- expect(
124
- path.closedRoughlyIntersects(new Rect2(1, 5, 1, 1))
125
- ).toBe(true);
126
- });
127
-
128
- it('path with Bézier curves', () => {
129
- const path = Path.fromString(`
130
- M1090,2560
131
- L1570,2620
132
- Q1710,1300 1380,720
133
- Q980,100 -460,-640
134
- L-680,-200
135
- Q670,470 960,980
136
- Q1230,1370 1090,2560
137
- `);
138
- expect(
139
- path.closedRoughlyIntersects(new Rect2(0, 0, 500, 500))
140
- ).toBe(true);
141
- expect(
142
- path.closedRoughlyIntersects(new Rect2(0, 0, 5, 5))
143
- ).toBe(true);
144
- expect(
145
- path.closedRoughlyIntersects(new Rect2(-10000, 0, 500, 500))
146
- ).toBe(false);
147
- });
148
- });
149
-
150
- describe('roughlyIntersects', () => {
151
- it('should consider parts outside bbox of individual parts of a line as not intersecting', () => {
152
- const path = Path.fromString(`
153
- M10,10
154
- L20,20
155
- L100,21
156
- `);
157
- expect(
158
- path.roughlyIntersects(new Rect2(0, 0, 50, 50))
159
- ).toBe(true);
160
- expect(
161
- path.roughlyIntersects(new Rect2(0, 0, 5, 5))
162
- ).toBe(false);
163
- expect(
164
- path.roughlyIntersects(new Rect2(8, 22, 1, 1))
165
- ).toBe(false);
166
- expect(
167
- path.roughlyIntersects(new Rect2(21, 11, 1, 1))
168
- ).toBe(false);
169
- expect(
170
- path.roughlyIntersects(new Rect2(50, 19, 1, 2))
171
- ).toBe(true);
172
- });
173
- });
174
-
175
- describe('fromRect', () => {
176
- const filledRect = Path.fromRect(Rect2.unitSquare);
177
- const strokedRect = Path.fromRect(Rect2.unitSquare, 0.1);
178
-
179
- it('filled should be closed shape', () => {
180
- const lastSegment = filledRect.parts[filledRect.parts.length - 1];
181
-
182
- if (lastSegment.kind !== PathCommandType.LineTo) {
183
- throw new Error('Rectangles should only be made up of lines');
184
- }
185
-
186
- expect(filledRect.startPoint).objEq(lastSegment.point);
187
- });
188
-
189
- it('stroked should be closed shape', () => {
190
- const lastSegment = strokedRect.parts[strokedRect.parts.length - 1];
191
- if (lastSegment.kind !== PathCommandType.LineTo) {
192
- throw new Error('Rectangles should only be made up of lines');
193
- }
194
-
195
- expect(strokedRect.startPoint).objEq(lastSegment.point);
196
- });
197
- });
198
- });
@@ -1,77 +0,0 @@
1
- import Path, { PathCommandType } from './Path';
2
- import { Vec2 } from './Vec2';
3
-
4
-
5
- describe('Path.toString', () => {
6
- it('a single-point path should produce a move-to command', () => {
7
- const path = new Path(Vec2.of(0, 0), []);
8
- expect(path.toString()).toBe('M0,0');
9
- });
10
-
11
- it('should convert lineTo commands to L SVG commands', () => {
12
- const path = new Path(Vec2.of(0.1, 0.2), [
13
- {
14
- kind: PathCommandType.LineTo,
15
- point: Vec2.of(0.3, 0.4),
16
- },
17
- ]);
18
- expect(path.toString()).toBe('M.1,.2L.3,.4');
19
- });
20
-
21
- it('should fix rounding errors', () => {
22
- const path = new Path(Vec2.of(0.100000001, 0.199999999), [
23
- {
24
- kind: PathCommandType.QuadraticBezierTo,
25
- controlPoint: Vec2.of(9999, -10.999999995),
26
- endPoint: Vec2.of(0.000300001, 1.400000002),
27
- },
28
- {
29
- kind: PathCommandType.LineTo,
30
- point: Vec2.of(184.00482359999998, 1)
31
- }
32
- ]);
33
-
34
- expect(path.toString()).toBe('M.1,.2Q9999,-11 .0003,1.4L184.0048236,1');
35
- });
36
-
37
- it('should not remove trailing zeroes before decimal points', () => {
38
- const path = new Path(Vec2.of(1000, 2_000_000), [
39
- {
40
- kind: PathCommandType.LineTo,
41
- point: Vec2.of(30.00000001, 40.000000001),
42
- },
43
- ]);
44
-
45
- expect(path.toString()).toBe('M1000,2000000l-970-1999960');
46
- });
47
-
48
- it('deserialized path should serialize to the same/similar path, but with rounded components', () => {
49
- const path1 = Path.fromString('M100,100 L101,101 Q102,102 90.000000001,89.99999999 Z');
50
- path1['cachedStringVersion'] = null; // Clear the cache.
51
-
52
- expect(path1.toString()).toBe([
53
- 'M100,100', 'l1,1', 'q1,1 -11-11', 'l10,10'
54
- ].join(''));
55
- });
56
-
57
- it('should not lose precision when saving', () => {
58
- const pathStr = 'M184.2,52.3l-.2-.2q-2.7,2.4 -3.2,3.5q-2.8,7 -.9,6.1q4.3-2.6 4.8-6.1q1.2-8.8 .4-8.3q-4.2,5.2 -3.9,3.9q.2-1.6 .3-2.1q.2-1.3 -.2-1q-3.8,6.5 -3.2,3.3q.6-4.1 1.1-5.3q4.1-10 3.3-8.3q-5.3,13.1 -6.6,14.1q-3.3,2.8 -1.8-1.5q2.8-9.7 2.7-8.4q0,.3 0,.4q-1.4,7.1 -2.7,8.5q-2.6,3.2 -2.5,2.9q-.3-1.9 -.7-1.9q-4.1,4.4 -2.9,1.9q1.1-3 .3-2.6q-1.8,2 -2.5,2.4q-4.5,2.8 -4.2,1.9q.3-1.6 .2-1.4q1.5,2.2 1.3,2.9q-.8,3.9 -.5,3.3q.8-7.6 2.5-13.3q2.6-9.2 2.9-6.9q.3,1.4 .3,1.2q-.7-.4 -.9,0q-2.2,11.6 -7.6,13.6q-3.9,1.6 -2.1-1.3q3-5.5 2.6-3.4q-.2,1.8 -.5,1.8q-3.2,.5 -4.1,1.2q-2.6,2.6 -1.9,2.5q4.7-4.4 3.7-5.5q-1.1-.9 -1.6-.6q-7.2,7.5 -3.9,6.5q.3-.1 .4-.4q.6-5.3 -.2-4.9q-2.8,2.3 -3.1,2.4q-3.7,1.5 -3.5,.5q.3-3.6 1.4-3.3q3.5,.7 1.9,2.4q-1.7,2.3 -1.6,.8q0-3.5 -.9-3.1q-5.1,3.3 -4.9,2.8q.1-4 -.8-3.5q-4.3,3.4 -4.6,2.5q-1-2.1 .5-8.7l-.2,0q-1.6,6.6 -.7,8.9q.7,1.2 5.2-2.3q.4-.5 .2,3.1q.1,1 5.5-2.4q.4-.4 .3,2.7q.1,2 2.4-.4q1.7-2.3 -2.1-3.2q-1.7-.3 -2,3.7q0,1.4 4.1-.1q.3-.1 3.1-2.4q.3-.5 -.4,4.5q0-.1 -.2,0q-2.6,1.2 4.5-5.7q0-.2 .8,.6q.9,.6 -3.7,4.7q-.5,1 2.7-1.7q.6-.7 3.7-1.2q.7-.2 .9-2.2q.1-2.7 -3.4,3.2q-1.8,3.4 2.7,1.9q5.6-2.1 7.8-14q-.1,.1 .3,.4q.6,.1 .3-1.6q-.7-2.8 -3.7,6.7q-1.8,5.8 -2.5,13.5q.1,1.1 1.3-3.1q.2-1 -1.3-3.3q-.5-.5 -1,1.6q-.1,1.3 4.8-1.5q1-1 3-2q.1-.4 -1.1,2q-1.1,3.1 3.7-1.3q-.4,0 -.1,1.5q.3,.8 3.3-2.5q1.3-1.6 2.7-8.9q0-.1 0-.4q-.3-1.9 -3.5,8.2q-1.3,4.9 2.4,2.1q1.4-1.2 6.6-14.3q.8-2.4 -3.9,7.9q-.6,1.3 -1.1,5.5q-.3,3.7 4-3.1q-.2,0 -.6,.6q-.2,.6 -.3,2.3q0,1.8 4.7-3.5q.1-.5 -1.2,7.9q-.5,3.2 -4.6,5.7q-1.3,1 1.5-5.5q.4-1.1 3.01-3.5';
59
-
60
- const path1 = Path.fromString(pathStr);
61
- path1['cachedStringVersion'] = null; // Clear the cache.
62
- const path = Path.fromString(path1.toString(true));
63
- path1['cachedStringVersion'] = null; // Clear the cache.
64
-
65
- expect(path.toString(true)).toBe(path1.toString(true));
66
- });
67
-
68
- it('should remove no-op move-tos', () => {
69
- const path1 = Path.fromString('M50,75m0,0q0,12.5 0,50q0,6.3 25,0');
70
- path1['cachedStringVersion'] = null;
71
- const path2 = Path.fromString('M150,175M150,175q0,12.5 0,50q0,6.3 25,0');
72
- path2['cachedStringVersion'] = null;
73
-
74
- expect(path1.toString()).toBe('M50,75q0,12.5 0,50q0,6.3 25,0');
75
- expect(path2.toString()).toBe('M150,175q0,12.5 0,50q0,6.3 25,0');
76
- });
77
- });