js-draw 0.1.9 → 0.1.12

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 (193) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +15 -3
  3. package/dist/bundle.js +1 -1
  4. package/dist/src/Editor.d.ts +7 -2
  5. package/dist/src/Editor.js +74 -25
  6. package/dist/src/EditorImage.d.ts +1 -1
  7. package/dist/src/EditorImage.js +2 -2
  8. package/dist/src/Pointer.d.ts +1 -1
  9. package/dist/src/Pointer.js +1 -1
  10. package/dist/src/SVGLoader.d.ts +1 -1
  11. package/dist/src/SVGLoader.js +14 -6
  12. package/dist/src/UndoRedoHistory.js +3 -0
  13. package/dist/src/Viewport.d.ts +8 -25
  14. package/dist/src/Viewport.js +17 -10
  15. package/dist/src/bundle/bundled.d.ts +2 -1
  16. package/dist/src/bundle/bundled.js +2 -1
  17. package/dist/src/commands/Command.d.ts +2 -2
  18. package/dist/src/commands/Command.js +4 -4
  19. package/dist/src/commands/Duplicate.d.ts +1 -1
  20. package/dist/src/commands/Duplicate.js +1 -1
  21. package/dist/src/commands/Erase.d.ts +1 -1
  22. package/dist/src/commands/Erase.js +1 -1
  23. package/dist/src/commands/localization.d.ts +1 -1
  24. package/dist/src/components/AbstractComponent.d.ts +3 -3
  25. package/dist/src/components/AbstractComponent.js +2 -2
  26. package/dist/src/components/SVGGlobalAttributesObject.d.ts +3 -3
  27. package/dist/src/components/SVGGlobalAttributesObject.js +1 -1
  28. package/dist/src/components/Stroke.d.ts +4 -4
  29. package/dist/src/components/Stroke.js +2 -2
  30. package/dist/src/components/Text.d.ts +3 -3
  31. package/dist/src/components/Text.js +3 -3
  32. package/dist/src/components/UnknownSVGObject.d.ts +3 -3
  33. package/dist/src/components/UnknownSVGObject.js +1 -1
  34. package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
  35. package/dist/src/components/builders/ArrowBuilder.js +1 -1
  36. package/dist/src/components/builders/FreehandLineBuilder.d.ts +8 -3
  37. package/dist/src/components/builders/FreehandLineBuilder.js +142 -71
  38. package/dist/src/components/builders/LineBuilder.d.ts +1 -1
  39. package/dist/src/components/builders/LineBuilder.js +1 -1
  40. package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
  41. package/dist/src/components/builders/RectangleBuilder.js +3 -3
  42. package/dist/src/components/builders/types.d.ts +1 -1
  43. package/dist/src/localization.d.ts +1 -0
  44. package/dist/src/localization.js +5 -1
  45. package/dist/src/localizations/en.d.ts +3 -0
  46. package/dist/src/localizations/en.js +4 -0
  47. package/dist/src/localizations/es.d.ts +3 -0
  48. package/dist/src/localizations/es.js +18 -0
  49. package/dist/src/localizations/getLocalizationTable.d.ts +3 -0
  50. package/dist/src/localizations/getLocalizationTable.js +43 -0
  51. package/dist/src/{geometry → math}/LineSegment2.d.ts +1 -0
  52. package/dist/src/{geometry → math}/LineSegment2.js +16 -0
  53. package/dist/src/{geometry → math}/Mat33.d.ts +0 -0
  54. package/dist/src/{geometry → math}/Mat33.js +0 -0
  55. package/dist/src/{geometry → math}/Path.d.ts +2 -1
  56. package/dist/src/{geometry → math}/Path.js +58 -51
  57. package/dist/src/{geometry → math}/Rect2.d.ts +1 -0
  58. package/dist/src/{geometry → math}/Rect2.js +16 -0
  59. package/dist/src/{geometry → math}/Vec2.d.ts +0 -0
  60. package/dist/src/{geometry → math}/Vec2.js +0 -0
  61. package/dist/src/{geometry → math}/Vec3.d.ts +1 -1
  62. package/dist/src/{geometry → math}/Vec3.js +1 -1
  63. package/dist/src/math/rounding.d.ts +3 -0
  64. package/dist/src/math/rounding.js +120 -0
  65. package/dist/src/rendering/Display.d.ts +3 -1
  66. package/dist/src/rendering/Display.js +16 -10
  67. package/dist/src/rendering/caching/CacheRecord.d.ts +2 -2
  68. package/dist/src/rendering/caching/CacheRecord.js +1 -1
  69. package/dist/src/rendering/caching/CacheRecordManager.d.ts +1 -1
  70. package/dist/src/rendering/caching/RenderingCache.js +1 -1
  71. package/dist/src/rendering/caching/RenderingCacheNode.d.ts +2 -1
  72. package/dist/src/rendering/caching/RenderingCacheNode.js +18 -7
  73. package/dist/src/rendering/caching/testUtils.js +1 -1
  74. package/dist/src/rendering/caching/types.d.ts +1 -1
  75. package/dist/src/rendering/localization.d.ts +2 -0
  76. package/dist/src/rendering/localization.js +2 -0
  77. package/dist/src/rendering/renderers/AbstractRenderer.d.ts +4 -4
  78. package/dist/src/rendering/renderers/AbstractRenderer.js +2 -2
  79. package/dist/src/rendering/renderers/CanvasRenderer.d.ts +4 -4
  80. package/dist/src/rendering/renderers/CanvasRenderer.js +1 -1
  81. package/dist/src/rendering/renderers/DummyRenderer.d.ts +4 -4
  82. package/dist/src/rendering/renderers/DummyRenderer.js +1 -1
  83. package/dist/src/rendering/renderers/SVGRenderer.d.ts +3 -3
  84. package/dist/src/rendering/renderers/SVGRenderer.js +8 -2
  85. package/dist/src/rendering/renderers/TextOnlyRenderer.d.ts +5 -3
  86. package/dist/src/rendering/renderers/TextOnlyRenderer.js +13 -3
  87. package/dist/src/toolbar/HTMLToolbar.js +5 -7
  88. package/dist/src/toolbar/icons.d.ts +3 -0
  89. package/dist/src/toolbar/icons.js +152 -142
  90. package/dist/src/toolbar/localization.d.ts +4 -1
  91. package/dist/src/toolbar/localization.js +4 -1
  92. package/dist/src/toolbar/makeColorInput.js +2 -1
  93. package/dist/src/toolbar/widgets/ActionButtonWidget.d.ts +13 -0
  94. package/dist/src/toolbar/widgets/ActionButtonWidget.js +21 -0
  95. package/dist/src/toolbar/widgets/BaseWidget.js +31 -0
  96. package/dist/src/toolbar/widgets/HandToolWidget.js +10 -3
  97. package/dist/src/toolbar/widgets/SelectionWidget.d.ts +0 -1
  98. package/dist/src/toolbar/widgets/SelectionWidget.js +23 -30
  99. package/dist/src/tools/BaseTool.d.ts +2 -1
  100. package/dist/src/tools/BaseTool.js +3 -0
  101. package/dist/src/tools/Eraser.js +1 -1
  102. package/dist/src/tools/PanZoom.d.ts +3 -2
  103. package/dist/src/tools/PanZoom.js +32 -16
  104. package/dist/src/tools/SelectionTool.d.ts +11 -4
  105. package/dist/src/tools/SelectionTool.js +135 -23
  106. package/dist/src/tools/TextTool.js +1 -1
  107. package/dist/src/tools/ToolController.js +6 -2
  108. package/dist/src/tools/localization.d.ts +1 -0
  109. package/dist/src/tools/localization.js +1 -0
  110. package/dist/src/types.d.ts +13 -6
  111. package/dist/src/types.js +1 -0
  112. package/package.json +9 -1
  113. package/src/Editor.ts +86 -24
  114. package/src/EditorImage.test.ts +2 -4
  115. package/src/EditorImage.ts +2 -2
  116. package/src/Pointer.ts +1 -1
  117. package/src/SVGLoader.ts +14 -6
  118. package/src/UndoRedoHistory.ts +4 -0
  119. package/src/Viewport.ts +21 -17
  120. package/src/bundle/bundled.ts +2 -1
  121. package/src/commands/Command.ts +5 -5
  122. package/src/commands/Duplicate.ts +1 -1
  123. package/src/commands/Erase.ts +1 -1
  124. package/src/commands/localization.ts +1 -1
  125. package/src/components/AbstractComponent.ts +4 -4
  126. package/src/components/SVGGlobalAttributesObject.ts +3 -3
  127. package/src/components/Stroke.test.ts +3 -5
  128. package/src/components/Stroke.ts +4 -4
  129. package/src/components/Text.test.ts +2 -2
  130. package/src/components/Text.ts +3 -3
  131. package/src/components/UnknownSVGObject.ts +3 -3
  132. package/src/components/builders/ArrowBuilder.ts +2 -2
  133. package/src/components/builders/FreehandLineBuilder.ts +190 -80
  134. package/src/components/builders/LineBuilder.ts +2 -2
  135. package/src/components/builders/RectangleBuilder.ts +3 -3
  136. package/src/components/builders/types.ts +1 -1
  137. package/src/localization.ts +6 -0
  138. package/src/localizations/en.ts +8 -0
  139. package/src/localizations/es.ts +63 -0
  140. package/src/localizations/getLocalizationTable.test.ts +27 -0
  141. package/src/localizations/getLocalizationTable.ts +53 -0
  142. package/src/{geometry → math}/LineSegment2.test.ts +15 -0
  143. package/src/{geometry → math}/LineSegment2.ts +20 -0
  144. package/src/{geometry → math}/Mat33.test.ts +0 -0
  145. package/src/{geometry → math}/Mat33.ts +0 -0
  146. package/src/{geometry → math}/Path.fromString.test.ts +0 -0
  147. package/src/{geometry → math}/Path.test.ts +0 -0
  148. package/src/{geometry → math}/Path.toString.test.ts +11 -2
  149. package/src/{geometry → math}/Path.ts +60 -57
  150. package/src/{geometry → math}/Rect2.test.ts +20 -7
  151. package/src/{geometry → math}/Rect2.ts +19 -1
  152. package/src/{geometry → math}/Vec2.test.ts +0 -0
  153. package/src/{geometry → math}/Vec2.ts +0 -0
  154. package/src/{geometry → math}/Vec3.test.ts +0 -0
  155. package/src/{geometry → math}/Vec3.ts +2 -2
  156. package/src/math/rounding.test.ts +40 -0
  157. package/src/math/rounding.ts +145 -0
  158. package/src/rendering/Display.ts +18 -10
  159. package/src/rendering/caching/CacheRecord.test.ts +2 -2
  160. package/src/rendering/caching/CacheRecord.ts +2 -2
  161. package/src/rendering/caching/CacheRecordManager.ts +1 -1
  162. package/src/rendering/caching/RenderingCache.test.ts +3 -3
  163. package/src/rendering/caching/RenderingCache.ts +1 -1
  164. package/src/rendering/caching/RenderingCacheNode.ts +23 -7
  165. package/src/rendering/caching/testUtils.ts +1 -1
  166. package/src/rendering/caching/types.ts +1 -1
  167. package/src/rendering/localization.ts +4 -0
  168. package/src/rendering/renderers/AbstractRenderer.ts +4 -4
  169. package/src/rendering/renderers/CanvasRenderer.ts +4 -4
  170. package/src/rendering/renderers/DummyRenderer.test.ts +2 -2
  171. package/src/rendering/renderers/DummyRenderer.ts +4 -4
  172. package/src/rendering/renderers/SVGRenderer.ts +10 -4
  173. package/src/rendering/renderers/TextOnlyRenderer.ts +17 -6
  174. package/src/toolbar/HTMLToolbar.ts +5 -8
  175. package/src/toolbar/icons.ts +167 -147
  176. package/src/toolbar/localization.ts +8 -2
  177. package/src/toolbar/makeColorInput.ts +2 -1
  178. package/src/toolbar/toolbar.css +7 -3
  179. package/src/toolbar/widgets/ActionButtonWidget.ts +31 -0
  180. package/src/toolbar/widgets/BaseWidget.ts +36 -0
  181. package/src/toolbar/widgets/HandToolWidget.ts +14 -3
  182. package/src/toolbar/widgets/SelectionWidget.ts +46 -41
  183. package/src/tools/BaseTool.ts +5 -1
  184. package/src/tools/Eraser.ts +2 -2
  185. package/src/tools/PanZoom.ts +39 -18
  186. package/src/tools/SelectionTool.test.ts +26 -5
  187. package/src/tools/SelectionTool.ts +162 -27
  188. package/src/tools/TextTool.ts +2 -2
  189. package/src/tools/ToolController.ts +6 -2
  190. package/src/tools/UndoRedoShortcut.test.ts +1 -1
  191. package/src/tools/localization.ts +2 -0
  192. package/src/types.ts +14 -5
  193. package/dist-test/test-dist-bundle.html +0 -42
@@ -1,6 +1,6 @@
1
- import LineSegment2 from '../geometry/LineSegment2';
2
- import Mat33 from '../geometry/Mat33';
3
- import Rect2 from '../geometry/Rect2';
1
+ import LineSegment2 from '../math/LineSegment2';
2
+ import Mat33 from '../math/Mat33';
3
+ import Rect2 from '../math/Rect2';
4
4
  import AbstractRenderer from '../rendering/renderers/AbstractRenderer';
5
5
  import AbstractComponent from './AbstractComponent';
6
6
  import { ImageComponentLocalization } from './localization';
@@ -1,4 +1,4 @@
1
- import Rect2 from '../geometry/Rect2';
1
+ import Rect2 from '../math/Rect2';
2
2
  import SVGRenderer from '../rendering/renderers/SVGRenderer';
3
3
  import AbstractComponent from './AbstractComponent';
4
4
  const componentId = 'unknown-svg-object';
@@ -1,4 +1,4 @@
1
- import Rect2 from '../../geometry/Rect2';
1
+ import Rect2 from '../../math/Rect2';
2
2
  import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
3
  import { StrokeDataPoint } from '../../types';
4
4
  import AbstractComponent from '../AbstractComponent';
@@ -1,4 +1,4 @@
1
- import { PathCommandType } from '../../geometry/Path';
1
+ import { PathCommandType } from '../../math/Path';
2
2
  import Stroke from '../Stroke';
3
3
  export const makeArrowBuilder = (initialPoint, _viewport) => {
4
4
  return new ArrowBuilder(initialPoint);
@@ -1,5 +1,5 @@
1
1
  import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
2
- import Rect2 from '../../geometry/Rect2';
2
+ import Rect2 from '../../math/Rect2';
3
3
  import Stroke from '../Stroke';
4
4
  import { StrokeDataPoint } from '../../types';
5
5
  import { ComponentBuilder, ComponentBuilderFactory } from './types';
@@ -8,7 +8,11 @@ export default class FreehandLineBuilder implements ComponentBuilder {
8
8
  private startPoint;
9
9
  private minFitAllowed;
10
10
  private maxFitAllowed;
11
- private segments;
11
+ private isFirstSegment;
12
+ private pathStartConnector;
13
+ private mostRecentConnector;
14
+ private upperSegments;
15
+ private lowerSegments;
12
16
  private buffer;
13
17
  private lastPoint;
14
18
  private lastExitingVec;
@@ -20,7 +24,8 @@ export default class FreehandLineBuilder implements ComponentBuilder {
20
24
  constructor(startPoint: StrokeDataPoint, minFitAllowed: number, maxFitAllowed: number);
21
25
  getBBox(): Rect2;
22
26
  private getRenderingStyle;
23
- private getPreview;
27
+ private previewPath;
28
+ private previewStroke;
24
29
  preview(renderer: AbstractRenderer): void;
25
30
  build(): Stroke;
26
31
  private roundPoint;
@@ -1,8 +1,8 @@
1
1
  import { Bezier } from 'bezier-js';
2
- import { Vec2 } from '../../geometry/Vec2';
3
- import Rect2 from '../../geometry/Rect2';
4
- import { PathCommandType } from '../../geometry/Path';
5
- import LineSegment2 from '../../geometry/LineSegment2';
2
+ import { Vec2 } from '../../math/Vec2';
3
+ import Rect2 from '../../math/Rect2';
4
+ import { PathCommandType } from '../../math/Path';
5
+ import LineSegment2 from '../../math/LineSegment2';
6
6
  import Stroke from '../Stroke';
7
7
  import Viewport from '../../Viewport';
8
8
  export const makeFreehandLineBuilder = (initialPoint, viewport) => {
@@ -24,9 +24,13 @@ export default class FreehandLineBuilder {
24
24
  this.startPoint = startPoint;
25
25
  this.minFitAllowed = minFitAllowed;
26
26
  this.maxFitAllowed = maxFitAllowed;
27
+ this.isFirstSegment = true;
28
+ this.pathStartConnector = null;
29
+ this.mostRecentConnector = null;
27
30
  this.currentCurve = null;
28
31
  this.lastPoint = this.startPoint;
29
- this.segments = [];
32
+ this.upperSegments = [];
33
+ this.lowerSegments = [];
30
34
  this.buffer = [this.startPoint.pos];
31
35
  this.momentum = Vec2.zero;
32
36
  this.currentCurve = null;
@@ -41,24 +45,81 @@ export default class FreehandLineBuilder {
41
45
  fill: (_a = this.lastPoint.color) !== null && _a !== void 0 ? _a : null,
42
46
  };
43
47
  }
44
- // Get the segments that make up this' path. Can be called after calling build()
45
- getPreview() {
46
- if (this.currentCurve && this.lastPoint) {
47
- const currentPath = this.currentSegmentToPath();
48
- return this.segments.concat(currentPath);
48
+ previewPath() {
49
+ var _a;
50
+ let upperPath;
51
+ let lowerPath;
52
+ let lowerToUpperCap;
53
+ let pathStartConnector;
54
+ if (this.currentCurve) {
55
+ const { upperCurve, lowerToUpperConnector, upperToLowerConnector, lowerCurve } = this.currentSegmentToPath();
56
+ upperPath = this.upperSegments.concat(upperCurve);
57
+ lowerPath = this.lowerSegments.concat(lowerCurve);
58
+ lowerToUpperCap = lowerToUpperConnector;
59
+ pathStartConnector = (_a = this.pathStartConnector) !== null && _a !== void 0 ? _a : upperToLowerConnector;
60
+ }
61
+ else {
62
+ if (this.mostRecentConnector === null || this.pathStartConnector === null) {
63
+ return null;
64
+ }
65
+ upperPath = this.upperSegments.slice();
66
+ lowerPath = this.lowerSegments.slice();
67
+ lowerToUpperCap = this.mostRecentConnector;
68
+ pathStartConnector = this.pathStartConnector;
69
+ }
70
+ const startPoint = lowerPath[lowerPath.length - 1].endPoint;
71
+ return {
72
+ // Start at the end of the lower curve:
73
+ // Start point
74
+ // ↓
75
+ // __/ __/ ← Most recent points on this end
76
+ // /___ /
77
+ // ↑
78
+ // Oldest points
79
+ startPoint,
80
+ commands: [
81
+ // Move to the most recent point on the upperPath:
82
+ // ----→•
83
+ // __/ __/
84
+ // /___ /
85
+ lowerToUpperCap,
86
+ // Move to the beginning of the upperPath:
87
+ // __/ __/
88
+ // /___ /
89
+ // • ←-
90
+ ...upperPath.reverse(),
91
+ // Move to the beginning of the lowerPath:
92
+ // __/ __/
93
+ // /___ /
94
+ // •
95
+ pathStartConnector,
96
+ // Move back to the start point:
97
+ // •
98
+ // __/ __/
99
+ // /___ /
100
+ ...lowerPath,
101
+ ],
102
+ style: this.getRenderingStyle(),
103
+ };
104
+ }
105
+ previewStroke() {
106
+ const pathPreview = this.previewPath();
107
+ if (pathPreview) {
108
+ return new Stroke([pathPreview]);
49
109
  }
50
- return this.segments;
110
+ return null;
51
111
  }
52
112
  preview(renderer) {
53
- for (const part of this.getPreview()) {
54
- renderer.drawPath(part);
113
+ const path = this.previewPath();
114
+ if (path) {
115
+ renderer.drawPath(path);
55
116
  }
56
117
  }
57
118
  build() {
58
119
  if (this.lastPoint) {
59
120
  this.finalizeCurrentCurve();
60
121
  }
61
- return new Stroke(this.segments);
122
+ return this.previewStroke();
62
123
  }
63
124
  roundPoint(point) {
64
125
  return Viewport.roundPoint(point, this.minFitAllowed);
@@ -67,53 +128,61 @@ export default class FreehandLineBuilder {
67
128
  // Case where no points have been added
68
129
  if (!this.currentCurve) {
69
130
  // Don't create a circle around the initial point if the stroke has more than one point.
70
- if (this.segments.length > 0) {
131
+ if (!this.isFirstSegment) {
71
132
  return;
72
133
  }
73
- const width = Viewport.roundPoint(this.startPoint.width / 3, this.minFitAllowed);
134
+ const width = Viewport.roundPoint(this.startPoint.width / 3.5, this.minFitAllowed);
74
135
  const center = this.roundPoint(this.startPoint.pos);
136
+ // Start on the right, cycle clockwise:
137
+ // |
138
+ // ----- ←
139
+ // |
140
+ const startPoint = this.startPoint.pos.plus(Vec2.of(width, 0));
75
141
  // Draw a circle-ish shape around the start point
76
- this.segments.push({
77
- // Start on the right, cycle clockwise:
142
+ this.lowerSegments.push({
143
+ kind: PathCommandType.QuadraticBezierTo,
144
+ controlPoint: center.plus(Vec2.of(width, width)),
145
+ // Bottom of the circle
78
146
  // |
79
- // -----
147
+ // -----
80
148
  // |
81
- startPoint: this.startPoint.pos.plus(Vec2.of(width, 0)),
82
- commands: [
83
- {
84
- kind: PathCommandType.QuadraticBezierTo,
85
- controlPoint: center.plus(Vec2.of(width, width)),
86
- // Bottom of the circle
87
- // |
88
- // -----
89
- // |
90
- // ↑
91
- endPoint: center.plus(Vec2.of(0, width)),
92
- },
93
- {
94
- kind: PathCommandType.QuadraticBezierTo,
95
- controlPoint: center.plus(Vec2.of(-width, width)),
96
- endPoint: center.plus(Vec2.of(-width, 0)),
97
- },
98
- {
99
- kind: PathCommandType.QuadraticBezierTo,
100
- controlPoint: center.plus(Vec2.of(-width, -width)),
101
- endPoint: center.plus(Vec2.of(0, -width)),
102
- },
103
- {
104
- kind: PathCommandType.QuadraticBezierTo,
105
- controlPoint: center.plus(Vec2.of(width, -width)),
106
- endPoint: center.plus(Vec2.of(width, 0)),
107
- },
108
- ],
109
- style: this.getRenderingStyle(),
149
+ // ↑
150
+ endPoint: center.plus(Vec2.of(0, width)),
151
+ }, {
152
+ kind: PathCommandType.QuadraticBezierTo,
153
+ controlPoint: center.plus(Vec2.of(-width, width)),
154
+ endPoint: center.plus(Vec2.of(-width, 0)),
155
+ }, {
156
+ kind: PathCommandType.QuadraticBezierTo,
157
+ controlPoint: center.plus(Vec2.of(-width, -width)),
158
+ endPoint: center.plus(Vec2.of(0, -width)),
159
+ }, {
160
+ kind: PathCommandType.QuadraticBezierTo,
161
+ controlPoint: center.plus(Vec2.of(width, -width)),
162
+ endPoint: center.plus(Vec2.of(width, 0)),
110
163
  });
164
+ this.pathStartConnector = {
165
+ kind: PathCommandType.LineTo,
166
+ point: startPoint,
167
+ };
168
+ this.mostRecentConnector = this.pathStartConnector;
111
169
  return;
112
170
  }
113
- this.segments.push(this.currentSegmentToPath());
171
+ const { upperCurve, lowerToUpperConnector, upperToLowerConnector, lowerCurve } = this.currentSegmentToPath();
172
+ if (this.isFirstSegment) {
173
+ // We draw the upper path (reversed), then the lower path, so we need the
174
+ // upperToLowerConnector to join the two paths.
175
+ this.pathStartConnector = upperToLowerConnector;
176
+ this.isFirstSegment = false;
177
+ }
178
+ // With the most recent connector, we're joining the end of the lowerPath to the most recent
179
+ // upperPath:
180
+ this.mostRecentConnector = lowerToUpperConnector;
181
+ this.upperSegments.push(upperCurve);
182
+ this.lowerSegments.push(lowerCurve);
114
183
  const lastPoint = this.buffer[this.buffer.length - 1];
115
184
  this.lastExitingVec = Vec2.ofXY(this.currentCurve.points[2]).minus(Vec2.ofXY(this.currentCurve.points[1]));
116
- console.assert(this.lastExitingVec.magnitude() !== 0);
185
+ console.assert(this.lastExitingVec.magnitude() !== 0, 'lastExitingVec has zero length!');
117
186
  // Use the last two points to start a new curve (the last point isn't used
118
187
  // in the current curve and we want connected curves to share end points)
119
188
  this.buffer = [
@@ -121,6 +190,7 @@ export default class FreehandLineBuilder {
121
190
  ];
122
191
  this.currentCurve = null;
123
192
  }
193
+ // Returns [upper curve, connector, lower curve]
124
194
  currentSegmentToPath() {
125
195
  if (this.currentCurve == null) {
126
196
  throw new Error('Invalid State: currentCurve is null!');
@@ -163,27 +233,28 @@ export default class FreehandLineBuilder {
163
233
  if (upperBoundary.intersects(lowerBoundary).length > 0) {
164
234
  halfVec = halfVec.times(2);
165
235
  }
166
- const pathCommands = [
167
- {
168
- kind: PathCommandType.QuadraticBezierTo,
169
- controlPoint: this.roundPoint(controlPoint.plus(halfVec)),
170
- endPoint: this.roundPoint(endPt.plus(endVec)),
171
- },
172
- {
173
- kind: PathCommandType.LineTo,
174
- point: this.roundPoint(endPt.minus(endVec)),
175
- },
176
- {
177
- kind: PathCommandType.QuadraticBezierTo,
178
- controlPoint: this.roundPoint(controlPoint.minus(halfVec)),
179
- endPoint: this.roundPoint(startPt.minus(startVec)),
180
- },
181
- ];
182
- return {
183
- startPoint: this.roundPoint(startPt.plus(startVec)),
184
- commands: pathCommands,
185
- style: this.getRenderingStyle(),
236
+ // Each starts at startPt ± startVec
237
+ const lowerCurve = {
238
+ kind: PathCommandType.QuadraticBezierTo,
239
+ controlPoint: this.roundPoint(controlPoint.plus(halfVec)),
240
+ endPoint: this.roundPoint(endPt.plus(endVec)),
241
+ };
242
+ // From the end of the upperCurve to the start of the lowerCurve:
243
+ const upperToLowerConnector = {
244
+ kind: PathCommandType.LineTo,
245
+ point: this.roundPoint(startPt.plus(startVec)),
246
+ };
247
+ // From the end of lowerCurve to the start of upperCurve:
248
+ const lowerToUpperConnector = {
249
+ kind: PathCommandType.LineTo,
250
+ point: this.roundPoint(endPt.minus(endVec))
251
+ };
252
+ const upperCurve = {
253
+ kind: PathCommandType.QuadraticBezierTo,
254
+ controlPoint: this.roundPoint(controlPoint.minus(halfVec)),
255
+ endPoint: this.roundPoint(startPt.minus(startVec)),
186
256
  };
257
+ return { upperCurve, upperToLowerConnector, lowerToUpperConnector, lowerCurve };
187
258
  }
188
259
  // Compute the direction of the velocity at the end of this.buffer
189
260
  computeExitingVec() {
@@ -205,7 +276,7 @@ export default class FreehandLineBuilder {
205
276
  }
206
277
  const threshold = Math.min(this.lastPoint.width, newPoint.width) / 4;
207
278
  const shouldSnapToInitial = this.startPoint.pos.minus(newPoint.pos).magnitude() < threshold
208
- && this.segments.length === 0;
279
+ && this.isFirstSegment;
209
280
  // Snap to the starting point if the stroke is contained within a small ball centered
210
281
  // at the starting point.
211
282
  // This allows us to create a circle/dot at the start of the stroke.
@@ -1,4 +1,4 @@
1
- import Rect2 from '../../geometry/Rect2';
1
+ import Rect2 from '../../math/Rect2';
2
2
  import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
3
  import { StrokeDataPoint } from '../../types';
4
4
  import AbstractComponent from '../AbstractComponent';
@@ -1,4 +1,4 @@
1
- import { PathCommandType } from '../../geometry/Path';
1
+ import { PathCommandType } from '../../math/Path';
2
2
  import Stroke from '../Stroke';
3
3
  export const makeLineBuilder = (initialPoint, _viewport) => {
4
4
  return new LineBuilder(initialPoint);
@@ -1,4 +1,4 @@
1
- import Rect2 from '../../geometry/Rect2';
1
+ import Rect2 from '../../math/Rect2';
2
2
  import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
3
  import { StrokeDataPoint } from '../../types';
4
4
  import Viewport from '../../Viewport';
@@ -1,6 +1,6 @@
1
- import Mat33 from '../../geometry/Mat33';
2
- import Path from '../../geometry/Path';
3
- import Rect2 from '../../geometry/Rect2';
1
+ import Mat33 from '../../math/Mat33';
2
+ import Path from '../../math/Path';
3
+ import Rect2 from '../../math/Rect2';
4
4
  import Stroke from '../Stroke';
5
5
  export const makeFilledRectangleBuilder = (initialPoint, viewport) => {
6
6
  return new RectangleBuilder(initialPoint, true, viewport);
@@ -1,4 +1,4 @@
1
- import Rect2 from '../../geometry/Rect2';
1
+ import Rect2 from '../../math/Rect2';
2
2
  import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
3
  import { StrokeDataPoint } from '../../types';
4
4
  import Viewport from '../../Viewport';
@@ -4,6 +4,7 @@ import { TextRendererLocalization } from './rendering/localization';
4
4
  import { ToolbarLocalization } from './toolbar/localization';
5
5
  import { ToolLocalization } from './tools/localization';
6
6
  export interface EditorLocalization extends ToolbarLocalization, ToolLocalization, CommandLocalization, ImageComponentLocalization, TextRendererLocalization {
7
+ accessibilityInputInstructions: string;
7
8
  undoAnnouncement: (actionDescription: string) => string;
8
9
  redoAnnouncement: (actionDescription: string) => string;
9
10
  doneLoading: string;
@@ -3,4 +3,8 @@ import { defaultComponentLocalization } from './components/localization';
3
3
  import { defaultTextRendererLocalization } from './rendering/localization';
4
4
  import { defaultToolbarLocalization } from './toolbar/localization';
5
5
  import { defaultToolLocalization } from './tools/localization';
6
- export const defaultEditorLocalization = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, defaultToolbarLocalization), defaultToolLocalization), defaultCommandLocalization), defaultComponentLocalization), defaultTextRendererLocalization), { loading: (percentage) => `Loading ${percentage}%...`, imageEditor: 'Image Editor', doneLoading: 'Done loading', undoAnnouncement: (commandDescription) => `Undid ${commandDescription}`, redoAnnouncement: (commandDescription) => `Redid ${commandDescription}` });
6
+ export const defaultEditorLocalization = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, defaultToolbarLocalization), defaultToolLocalization), defaultCommandLocalization), defaultComponentLocalization), defaultTextRendererLocalization), { accessibilityInputInstructions: [
7
+ 'Press "t" to read the contents of the viewport as text.',
8
+ 'Use the arrow keys to move the viewport, click and drag to draw strokes.',
9
+ 'Press "w" to zoom in and "s" to zoom out.',
10
+ ].join(' '), loading: (percentage) => `Loading ${percentage}%...`, imageEditor: 'Image Editor', doneLoading: 'Done loading', undoAnnouncement: (commandDescription) => `Undid ${commandDescription}`, redoAnnouncement: (commandDescription) => `Redid ${commandDescription}` });
@@ -0,0 +1,3 @@
1
+ import { EditorLocalization } from '../localization';
2
+ declare const localization: EditorLocalization;
3
+ export default localization;
@@ -0,0 +1,4 @@
1
+ import { defaultEditorLocalization } from '../localization';
2
+ // Default localizations are already in English.
3
+ const localization = Object.assign({}, defaultEditorLocalization);
4
+ export default localization;
@@ -0,0 +1,3 @@
1
+ import { EditorLocalization } from '../localization';
2
+ declare const localization: EditorLocalization;
3
+ export default localization;
@@ -0,0 +1,18 @@
1
+ import { defaultEditorLocalization } from '../localization';
2
+ // A partial Spanish localization.
3
+ const localization = Object.assign(Object.assign({}, defaultEditorLocalization), {
4
+ // Strings for the main editor interface
5
+ // (see src/localization.ts)
6
+ loading: (percentage) => `Cargando: ${percentage}%...`, imageEditor: 'Editor de dibujos', undoAnnouncement: (commandDescription) => `${commandDescription} fue deshecho`, redoAnnouncement: (commandDescription) => `${commandDescription} fue rehecho`, undo: 'Deshace', redo: 'Rehace',
7
+ // Strings for the toolbar
8
+ // (see src/toolbar/localization.ts)
9
+ pen: 'Lapiz', eraser: 'Borrador', select: 'Selecciona', thicknessLabel: 'Tamaño: ', colorLabel: 'Color: ', doneLoading: 'El cargado terminó', fontLabel: 'Fuente: ', anyDevicePanning: 'Mover la pantalla con todo dispotivo', touchPanning: 'Mover la pantalla con un dedo', touchPanTool: 'Instrumento de mover la pantalla con un dedo', outlinedRectanglePen: 'Rectángulo con nada más que un borde', filledRectanglePen: 'Rectángulo sin borde', linePen: 'Línea', arrowPen: 'Flecha', freehandPen: 'Dibuja sin restricción de forma', selectObjectType: 'Forma de dibuja:', handTool: 'Mover', zoom: 'Zoom', resetView: 'Reiniciar vista', resizeImageToSelection: 'Redimensionar la imagen a lo que está seleccionado', deleteSelection: 'Borra la selección', duplicateSelection: 'Duplica la selección', pickColorFromScreen: 'Selecciona un color de la pantalla', clickToPickColorAnnouncement: 'Haga un clic en la pantalla para seleccionar un color', dropdownShown(toolName) {
10
+ return `Menú por ${toolName} es visible`;
11
+ }, dropdownHidden: function (toolName) {
12
+ return `Menú por ${toolName} fue ocultado`;
13
+ }, colorChangedAnnouncement: function (color) {
14
+ return `Color fue cambiado a ${color}`;
15
+ }, keyboardPanZoom: 'Mover la pantalla con el teclado', penTool: function (penId) {
16
+ return `Lapiz ${penId}`;
17
+ }, selectionTool: 'Selecciona', eraserTool: 'Borrador', textTool: 'Texto', enterTextToInsert: 'Entra texto', rerenderAsText: 'Redibuja la pantalla al texto' });
18
+ export default localization;
@@ -0,0 +1,3 @@
1
+ import { EditorLocalization } from '../localization';
2
+ declare const getLocalizationTable: (userLocales?: readonly string[]) => EditorLocalization;
3
+ export default getLocalizationTable;
@@ -0,0 +1,43 @@
1
+ import { defaultEditorLocalization } from '../localization';
2
+ import en from './en';
3
+ import es from './es';
4
+ const allLocales = {
5
+ en,
6
+ es,
7
+ };
8
+ // [locale]: A string in the format languageCode_Region or just languageCode. For example, en_US.
9
+ const languageFromLocale = (locale) => {
10
+ const matches = /^(\w+)[_-](\w+)$/.exec(locale);
11
+ if (!matches) {
12
+ // If not in languageCode_region format, the locale should be the
13
+ // languageCode. Return that.
14
+ return locale;
15
+ }
16
+ return matches[1];
17
+ };
18
+ const getLocalizationTable = (userLocales) => {
19
+ userLocales !== null && userLocales !== void 0 ? userLocales : (userLocales = navigator.languages);
20
+ let prevLanguage;
21
+ for (const locale of userLocales) {
22
+ const language = languageFromLocale(locale);
23
+ // If the specific localization of the language is not available, but
24
+ // a localization for the language is,
25
+ if (prevLanguage && language !== prevLanguage) {
26
+ if (prevLanguage in allLocales) {
27
+ return allLocales[prevLanguage];
28
+ }
29
+ }
30
+ // If the full locale (e.g. en_US) is available,
31
+ if (locale in allLocales) {
32
+ return allLocales[locale];
33
+ }
34
+ prevLanguage = language;
35
+ }
36
+ if (prevLanguage && prevLanguage in allLocales) {
37
+ return allLocales[prevLanguage];
38
+ }
39
+ else {
40
+ return defaultEditorLocalization;
41
+ }
42
+ };
43
+ export default getLocalizationTable;
@@ -15,5 +15,6 @@ export default class LineSegment2 {
15
15
  get p2(): Point2;
16
16
  get(t: number): Point2;
17
17
  intersection(other: LineSegment2): IntersectionResult | null;
18
+ closestPointTo(target: Point2): import("./Vec3").default;
18
19
  }
19
20
  export {};
@@ -97,4 +97,20 @@ export default class LineSegment2 {
97
97
  t: resultT,
98
98
  };
99
99
  }
100
+ // Returns the closest point on this to [target]
101
+ closestPointTo(target) {
102
+ // Distance from P1 along this' direction.
103
+ const projectedDistFromP1 = target.minus(this.p1).dot(this.direction);
104
+ const projectedDistFromP2 = this.length - projectedDistFromP1;
105
+ const projection = this.p1.plus(this.direction.times(projectedDistFromP1));
106
+ if (projectedDistFromP1 > 0 && projectedDistFromP1 < this.length) {
107
+ return projection;
108
+ }
109
+ if (Math.abs(projectedDistFromP2) < Math.abs(projectedDistFromP1)) {
110
+ return this.p2;
111
+ }
112
+ else {
113
+ return this.p1;
114
+ }
115
+ }
100
116
  }
File without changes
File without changes
@@ -45,6 +45,7 @@ export default class Path {
45
45
  get geometry(): Array<LineSegment2 | Bezier>;
46
46
  static computeBBoxForSegment(startPoint: Point2, part: PathCommand): Rect2;
47
47
  intersection(line: LineSegment2): IntersectionResult[];
48
+ mapPoints(mapping: (point: Point2) => Point2): Path;
48
49
  transformedBy(affineTransfm: Mat33): Path;
49
50
  union(other: Path | null): Path;
50
51
  static fromRect(rect: Rect2, lineWidth?: number | null): Path;
@@ -52,7 +53,7 @@ export default class Path {
52
53
  toRenderable(fill: RenderingStyle): RenderablePathSpec;
53
54
  toString(): string;
54
55
  serialize(): string;
55
- static toString(startPoint: Point2, parts: PathCommand[]): string;
56
+ static toString(startPoint: Point2, parts: PathCommand[], onlyAbsCommands?: boolean): string;
56
57
  static fromString(pathString: string): Path;
57
58
  static empty: Path;
58
59
  }