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,4 +1,5 @@
1
1
  import { Bezier } from 'bezier-js';
2
+ import { toRoundedString, toStringOfSamePrecision } from './rounding';
2
3
  import LineSegment2 from './LineSegment2';
3
4
  import Rect2 from './Rect2';
4
5
  import { Vec2 } from './Vec2';
@@ -109,8 +110,8 @@ export default class Path {
109
110
  }
110
111
  return result;
111
112
  }
112
- transformedBy(affineTransfm) {
113
- const startPoint = affineTransfm.transformVec2(this.startPoint);
113
+ mapPoints(mapping) {
114
+ const startPoint = mapping(this.startPoint);
114
115
  const newParts = [];
115
116
  let exhaustivenessCheck;
116
117
  for (const part of this.parts) {
@@ -119,22 +120,22 @@ export default class Path {
119
120
  case PathCommandType.LineTo:
120
121
  newParts.push({
121
122
  kind: part.kind,
122
- point: affineTransfm.transformVec2(part.point),
123
+ point: mapping(part.point),
123
124
  });
124
125
  break;
125
126
  case PathCommandType.CubicBezierTo:
126
127
  newParts.push({
127
128
  kind: part.kind,
128
- controlPoint1: affineTransfm.transformVec2(part.controlPoint1),
129
- controlPoint2: affineTransfm.transformVec2(part.controlPoint2),
130
- endPoint: affineTransfm.transformVec2(part.endPoint),
129
+ controlPoint1: mapping(part.controlPoint1),
130
+ controlPoint2: mapping(part.controlPoint2),
131
+ endPoint: mapping(part.endPoint),
131
132
  });
132
133
  break;
133
134
  case PathCommandType.QuadraticBezierTo:
134
135
  newParts.push({
135
136
  kind: part.kind,
136
- controlPoint: affineTransfm.transformVec2(part.controlPoint),
137
- endPoint: affineTransfm.transformVec2(part.endPoint),
137
+ controlPoint: mapping(part.controlPoint),
138
+ endPoint: mapping(part.endPoint),
138
139
  });
139
140
  break;
140
141
  default:
@@ -144,6 +145,9 @@ export default class Path {
144
145
  }
145
146
  return new Path(startPoint, newParts);
146
147
  }
148
+ transformedBy(affineTransfm) {
149
+ return this.mapPoints(point => affineTransfm.transformVec2(point));
150
+ }
147
151
  // Creates a new path by joining [other] to the end of this path
148
152
  union(other) {
149
153
  if (!other) {
@@ -201,58 +205,61 @@ export default class Path {
201
205
  };
202
206
  }
203
207
  toString() {
204
- return Path.toString(this.startPoint, this.parts);
208
+ // Hueristic: Try to determine whether converting absolute to relative commands is worth it.
209
+ // If we're near (0, 0), it probably isn't worth it and if bounding boxes are large,
210
+ // it also probably isn't worth it.
211
+ const makeRelativeCommands = Math.abs(this.bbox.topLeft.x) > 10 && Math.abs(this.bbox.size.x) < 2
212
+ && Math.abs(this.bbox.topLeft.y) > 10 && Math.abs(this.bbox.size.y) < 2;
213
+ return Path.toString(this.startPoint, this.parts, !makeRelativeCommands);
205
214
  }
206
215
  serialize() {
207
216
  return this.toString();
208
217
  }
209
- static toString(startPoint, parts) {
218
+ // [onlyAbsCommands]: True if we should avoid converting absolute coordinates to relative offsets -- such
219
+ // conversions can lead to smaller output strings, but also take time.
220
+ static toString(startPoint, parts, onlyAbsCommands = true) {
210
221
  const result = [];
211
- const toRoundedString = (num) => {
212
- // Try to remove rounding errors. If the number ends in at least three/four zeroes
213
- // (or nines) just one or two digits, it's probably a rounding error.
214
- const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d$/;
215
- const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,}\d)$/;
216
- let text = num.toString();
217
- if (text.indexOf('.') === -1) {
218
- return text;
219
- }
220
- const roundingDownMatch = hasRoundingDownExp.exec(text);
221
- if (roundingDownMatch) {
222
- const negativeSign = roundingDownMatch[1];
223
- const lastDigit = parseInt(text.charAt(text.length - 1), 10);
224
- const postDecimal = parseInt(roundingDownMatch[3], 10);
225
- const preDecimal = parseInt(roundingDownMatch[2], 10);
226
- const origPostDecimalString = roundingDownMatch[3];
227
- let newPostDecimal = (postDecimal + 10 - lastDigit).toString();
228
- let carry = 0;
229
- if (newPostDecimal.length > postDecimal.toString().length) {
230
- // Left-shift
231
- newPostDecimal = newPostDecimal.substring(1);
232
- carry = 1;
222
+ let prevPoint;
223
+ const addCommand = (command, ...points) => {
224
+ const absoluteCommandParts = [];
225
+ const relativeCommandParts = [];
226
+ const makeAbsCommand = !prevPoint || onlyAbsCommands;
227
+ const roundedPrevX = prevPoint ? toRoundedString(prevPoint.x) : '';
228
+ const roundedPrevY = prevPoint ? toRoundedString(prevPoint.y) : '';
229
+ for (const point of points) {
230
+ // Relative commands are often shorter as strings than absolute commands.
231
+ if (!makeAbsCommand) {
232
+ const xComponentRelative = toStringOfSamePrecision(point.x - prevPoint.x, roundedPrevX, roundedPrevY);
233
+ const yComponentRelative = toStringOfSamePrecision(point.y - prevPoint.y, roundedPrevX, roundedPrevY);
234
+ // No need for an additional separator if it starts with a '-'
235
+ if (yComponentRelative.charAt(0) === '-') {
236
+ relativeCommandParts.push(`${xComponentRelative}${yComponentRelative}`);
237
+ }
238
+ else {
239
+ relativeCommandParts.push(`${xComponentRelative},${yComponentRelative}`);
240
+ }
233
241
  }
234
- // parseInt(...).toString() removes leading zeroes. Add them back.
235
- while (newPostDecimal.length < origPostDecimalString.length) {
236
- newPostDecimal = carry.toString(10) + newPostDecimal;
237
- carry = 0;
242
+ else {
243
+ const xComponent = toRoundedString(point.x);
244
+ const yComponent = toRoundedString(point.y);
245
+ absoluteCommandParts.push(`${xComponent},${yComponent}`);
238
246
  }
239
- text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
240
247
  }
241
- text = text.replace(fixRoundingUpExp, '$1');
242
- // Remove trailing zeroes
243
- text = text.replace(/([.]\d*[^0]+)0+$/, '$1');
244
- text = text.replace(/[.]0+$/, '.');
245
- // Remove trailing period
246
- return text.replace(/[.]$/, '');
247
- };
248
- const addCommand = (command, ...points) => {
249
- const parts = [];
250
- for (const point of points) {
251
- const xComponent = toRoundedString(point.x);
252
- const yComponent = toRoundedString(point.y);
253
- parts.push(`${xComponent},${yComponent}`);
248
+ let commandString;
249
+ if (makeAbsCommand) {
250
+ commandString = `${command}${absoluteCommandParts.join(' ')}`;
251
+ }
252
+ else {
253
+ commandString = `${command.toLowerCase()}${relativeCommandParts.join(' ')}`;
254
+ }
255
+ // Don't add no-ops.
256
+ if (commandString === 'l0,0') {
257
+ return;
258
+ }
259
+ result.push(commandString);
260
+ if (points.length > 0) {
261
+ prevPoint = points[points.length - 1];
254
262
  }
255
- result.push(`${command}${parts.join(' ')}`);
256
263
  };
257
264
  addCommand('M', startPoint);
258
265
  let exhaustivenessCheck;
@@ -30,6 +30,7 @@ export default class Rect2 {
30
30
  divideIntoGrid(columns: number, rows: number): Rect2[];
31
31
  grownToPoint(point: Point2, margin?: number): Rect2;
32
32
  grownBy(margin: number): Rect2;
33
+ getClosestPointOnBoundaryTo(target: Point2): import("./Vec3").default;
33
34
  get corners(): Point2[];
34
35
  get maxDimension(): number;
35
36
  get topRight(): import("./Vec3").default;
@@ -25,6 +25,7 @@ export default class Rect2 {
25
25
  translatedBy(vec) {
26
26
  return new Rect2(vec.x + this.x, vec.y + this.y, this.w, this.h);
27
27
  }
28
+ // Returns a copy of this with the given size (but same top-left).
28
29
  resizedTo(size) {
29
30
  return new Rect2(this.x, this.y, size.x, size.y);
30
31
  }
@@ -109,6 +110,21 @@ export default class Rect2 {
109
110
  grownBy(margin) {
110
111
  return new Rect2(this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2);
111
112
  }
113
+ getClosestPointOnBoundaryTo(target) {
114
+ const closestEdgePoints = this.getEdges().map(edge => {
115
+ return edge.closestPointTo(target);
116
+ });
117
+ let closest = null;
118
+ let closestDist = null;
119
+ for (const point of closestEdgePoints) {
120
+ const dist = point.minus(target).length();
121
+ if (closestDist === null || dist < closestDist) {
122
+ closest = point;
123
+ closestDist = dist;
124
+ }
125
+ }
126
+ return closest;
127
+ }
112
128
  get corners() {
113
129
  return [
114
130
  this.bottomRight,
File without changes
File without changes
@@ -23,7 +23,7 @@ export default class Vec3 {
23
23
  extend(distance: number, direction: Vec3): Vec3;
24
24
  lerp(target: Vec3, fractionTo: number): Vec3;
25
25
  zip(other: Vec3, zip: (componentInThis: number, componentInOther: number) => number): Vec3;
26
- map(fn: (component: number) => number): Vec3;
26
+ map(fn: (component: number, index: number) => number): Vec3;
27
27
  asArray(): number[];
28
28
  eq(other: Vec3, fuzz: number): boolean;
29
29
  toString(): string;
@@ -86,7 +86,7 @@ export default class Vec3 {
86
86
  }
87
87
  // Returns a vector with each component acted on by [fn]
88
88
  map(fn) {
89
- return Vec3.of(fn(this.x), fn(this.y), fn(this.z));
89
+ return Vec3.of(fn(this.x, 0), fn(this.y, 1), fn(this.z, 2));
90
90
  }
91
91
  asArray() {
92
92
  return [this.x, this.y, this.z];
@@ -0,0 +1,3 @@
1
+ export declare const toRoundedString: (num: number) => string;
2
+ export declare const getLenAfterDecimal: (numberAsString: string) => number;
3
+ export declare const toStringOfSamePrecision: (num: number, ...references: string[]) => string;
@@ -0,0 +1,120 @@
1
+ // Clean up stringified numbers
2
+ const cleanUpNumber = (text) => {
3
+ // Regular expression substitions can be somewhat expensive. Only do them
4
+ // if necessary.
5
+ const lastChar = text.charAt(text.length - 1);
6
+ if (lastChar === '0' || lastChar === '.') {
7
+ // Remove trailing zeroes
8
+ text = text.replace(/([.]\d*[^0]+)0+$/, '$1');
9
+ text = text.replace(/[.]0+$/, '.');
10
+ // Remove trailing period
11
+ text = text.replace(/[.]$/, '');
12
+ if (text === '-0') {
13
+ return '0';
14
+ }
15
+ }
16
+ const firstChar = text.charAt(0);
17
+ if (firstChar === '0' || firstChar === '-') {
18
+ // Remove unnecessary leading zeroes.
19
+ text = text.replace(/^(0+)[.]/, '.');
20
+ text = text.replace(/^-(0+)[.]/, '-.');
21
+ }
22
+ return text;
23
+ };
24
+ export const toRoundedString = (num) => {
25
+ // Try to remove rounding errors. If the number ends in at least three/four zeroes
26
+ // (or nines) just one or two digits, it's probably a rounding error.
27
+ const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d{1,2}$/;
28
+ const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,})\d{1,2}$/;
29
+ let text = num.toString(10);
30
+ if (text.indexOf('.') === -1) {
31
+ return text;
32
+ }
33
+ const roundingDownMatch = hasRoundingDownExp.exec(text);
34
+ if (roundingDownMatch) {
35
+ const negativeSign = roundingDownMatch[1];
36
+ const postDecimalString = roundingDownMatch[3];
37
+ const lastDigit = parseInt(postDecimalString.charAt(postDecimalString.length - 1), 10);
38
+ const postDecimal = parseInt(postDecimalString, 10);
39
+ const preDecimal = parseInt(roundingDownMatch[2], 10);
40
+ const origPostDecimalString = roundingDownMatch[3];
41
+ let newPostDecimal = (postDecimal + 10 - lastDigit).toString();
42
+ let carry = 0;
43
+ if (newPostDecimal.length > postDecimal.toString().length) {
44
+ // Left-shift
45
+ newPostDecimal = newPostDecimal.substring(1);
46
+ carry = 1;
47
+ }
48
+ // parseInt(...).toString() removes leading zeroes. Add them back.
49
+ while (newPostDecimal.length < origPostDecimalString.length) {
50
+ newPostDecimal = carry.toString(10) + newPostDecimal;
51
+ carry = 0;
52
+ }
53
+ text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
54
+ }
55
+ text = text.replace(fixRoundingUpExp, '$1');
56
+ return cleanUpNumber(text);
57
+ };
58
+ const numberExp = /^([-]?)(\d*)[.](\d+)$/;
59
+ export const getLenAfterDecimal = (numberAsString) => {
60
+ const numberMatch = numberExp.exec(numberAsString);
61
+ if (!numberMatch) {
62
+ // If not a match, either the number is exponential notation (or is something
63
+ // like NaN or Infinity)
64
+ if (numberAsString.search(/[eE]/) !== -1 || /^[a-zA-Z]+$/.exec(numberAsString)) {
65
+ return -1;
66
+ // Or it has no decimal point
67
+ }
68
+ else {
69
+ return 0;
70
+ }
71
+ }
72
+ const afterDecimalLen = numberMatch[3].length;
73
+ return afterDecimalLen;
74
+ };
75
+ // [reference] should be a string representation of a base-10 number (no exponential (e.g. 10e10))
76
+ export const toStringOfSamePrecision = (num, ...references) => {
77
+ const text = num.toString(10);
78
+ const textMatch = numberExp.exec(text);
79
+ if (!textMatch) {
80
+ return text;
81
+ }
82
+ let decimalPlaces = -1;
83
+ for (const reference of references) {
84
+ decimalPlaces = Math.max(getLenAfterDecimal(reference), decimalPlaces);
85
+ }
86
+ if (decimalPlaces === -1) {
87
+ return toRoundedString(num);
88
+ }
89
+ // Make text's after decimal length match [afterDecimalLen].
90
+ let postDecimal = textMatch[3].substring(0, decimalPlaces);
91
+ let preDecimal = textMatch[2];
92
+ const nextDigit = textMatch[3].charAt(decimalPlaces);
93
+ if (nextDigit !== '') {
94
+ const asNumber = parseInt(nextDigit, 10);
95
+ if (asNumber >= 5) {
96
+ // Don't attempt to parseInt() an empty string.
97
+ if (postDecimal.length > 0) {
98
+ const leadingZeroMatch = /^(0+)(\d*)$/.exec(postDecimal);
99
+ let leadingZeroes = '';
100
+ let postLeading = postDecimal;
101
+ if (leadingZeroMatch) {
102
+ leadingZeroes = leadingZeroMatch[1];
103
+ postLeading = leadingZeroMatch[2];
104
+ }
105
+ postDecimal = (parseInt(postDecimal) + 1).toString();
106
+ // If postDecimal got longer, remove leading zeroes if possible
107
+ if (postDecimal.length > postLeading.length && leadingZeroes.length > 0) {
108
+ leadingZeroes = leadingZeroes.substring(1);
109
+ }
110
+ postDecimal = leadingZeroes + postDecimal;
111
+ }
112
+ if (postDecimal.length === 0 || postDecimal.length > decimalPlaces) {
113
+ preDecimal = (parseInt(preDecimal) + 1).toString();
114
+ postDecimal = postDecimal.substring(1);
115
+ }
116
+ }
117
+ }
118
+ const negativeSign = textMatch[1];
119
+ return cleanUpNumber(`${negativeSign}${preDecimal}.${postDecimal}`);
120
+ };
@@ -1,6 +1,6 @@
1
1
  import AbstractRenderer from './renderers/AbstractRenderer';
2
2
  import { Editor } from '../Editor';
3
- import { Point2 } from '../geometry/Vec2';
3
+ import { Point2 } from '../math/Vec2';
4
4
  import RenderingCache from './caching/RenderingCache';
5
5
  import Color4 from '../Color4';
6
6
  export declare enum RenderingMode {
@@ -13,6 +13,7 @@ export default class Display {
13
13
  private dryInkRenderer;
14
14
  private wetInkRenderer;
15
15
  private textRenderer;
16
+ private textRerenderOutput;
16
17
  private cache;
17
18
  private resizeSurfacesCallback?;
18
19
  private flattenCallback?;
@@ -23,6 +24,7 @@ export default class Display {
23
24
  getColorAt: (_screenPos: Point2) => Color4 | null;
24
25
  private initializeCanvasRendering;
25
26
  private initializeTextRendering;
27
+ rerenderAsText(): void;
26
28
  startRerender(): AbstractRenderer;
27
29
  setDraftMode(draftMode: boolean): void;
28
30
  getDryInkRenderer(): AbstractRenderer;
@@ -1,7 +1,7 @@
1
1
  import CanvasRenderer from './renderers/CanvasRenderer';
2
2
  import { EditorEventType } from '../types';
3
3
  import DummyRenderer from './renderers/DummyRenderer';
4
- import { Vec2 } from '../geometry/Vec2';
4
+ import { Vec2 } from '../math/Vec2';
5
5
  import RenderingCache from './caching/RenderingCache';
6
6
  import TextOnlyRenderer from './renderers/TextOnlyRenderer';
7
7
  import Color4 from '../Color4';
@@ -15,6 +15,7 @@ export default class Display {
15
15
  constructor(editor, mode, parent) {
16
16
  this.editor = editor;
17
17
  this.parent = parent;
18
+ this.textRerenderOutput = null;
18
19
  this.getColorAt = (_screenPos) => {
19
20
  return null;
20
21
  };
@@ -51,10 +52,10 @@ export default class Display {
51
52
  return this.dryInkRenderer.canRenderFromWithoutDataLoss(renderer);
52
53
  },
53
54
  blockResolution: cacheBlockResolution,
54
- cacheSize: 500 * 500 * 4 * 200,
55
+ cacheSize: 500 * 500 * 4 * 220,
55
56
  maxScale: 1.5,
56
- minComponentsPerCache: 50,
57
- minComponentsToUseCache: 120,
57
+ minComponentsPerCache: 45,
58
+ minComponentsToUseCache: 105,
58
59
  });
59
60
  this.editor.notifier.on(EditorEventType.DisplayResized, event => {
60
61
  var _a;
@@ -126,16 +127,21 @@ export default class Display {
126
127
  const rerenderButton = document.createElement('button');
127
128
  rerenderButton.classList.add('rerenderButton');
128
129
  rerenderButton.innerText = this.editor.localization.rerenderAsText;
129
- const rerenderOutput = document.createElement('div');
130
- rerenderOutput.ariaLive = 'polite';
130
+ this.textRerenderOutput = document.createElement('div');
131
+ this.textRerenderOutput.setAttribute('aria-live', 'polite');
131
132
  rerenderButton.onclick = () => {
132
- this.textRenderer.clear();
133
- this.editor.image.render(this.textRenderer, this.editor.viewport);
134
- rerenderOutput.innerText = this.textRenderer.getDescription();
133
+ this.rerenderAsText();
135
134
  };
136
- textRendererOutputContainer.replaceChildren(rerenderButton, rerenderOutput);
135
+ textRendererOutputContainer.replaceChildren(rerenderButton, this.textRerenderOutput);
137
136
  this.editor.createHTMLOverlay(textRendererOutputContainer);
138
137
  }
138
+ rerenderAsText() {
139
+ this.textRenderer.clear();
140
+ this.editor.image.render(this.textRenderer, this.editor.viewport);
141
+ if (this.textRerenderOutput) {
142
+ this.textRerenderOutput.innerText = this.textRenderer.getDescription();
143
+ }
144
+ }
139
145
  // Clears the drawing surfaces and otherwise prepares for a rerender.
140
146
  startRerender() {
141
147
  var _a;
@@ -1,5 +1,5 @@
1
- import Mat33 from '../../geometry/Mat33';
2
- import Rect2 from '../../geometry/Rect2';
1
+ import Mat33 from '../../math/Mat33';
2
+ import Rect2 from '../../math/Rect2';
3
3
  import AbstractRenderer from '../renderers/AbstractRenderer';
4
4
  import { BeforeDeallocCallback, CacheState } from './types';
5
5
  export default class CacheRecord {
@@ -1,4 +1,4 @@
1
- import Mat33 from '../../geometry/Mat33';
1
+ import Mat33 from '../../math/Mat33';
2
2
  // Represents a cached renderer/canvas
3
3
  // This is not a [CacheNode] -- it handles cached renderers and does not have sub-renderers.
4
4
  export default class CacheRecord {
@@ -1,6 +1,6 @@
1
1
  import { BeforeDeallocCallback, PartialCacheState } from './types';
2
2
  import CacheRecord from './CacheRecord';
3
- import Rect2 from '../../geometry/Rect2';
3
+ import Rect2 from '../../math/Rect2';
4
4
  export declare class CacheRecordManager {
5
5
  private readonly cacheState;
6
6
  private cacheRecords;
@@ -1,4 +1,4 @@
1
- import Rect2 from '../../geometry/Rect2';
1
+ import Rect2 from '../../math/Rect2';
2
2
  import RenderingCacheNode from './RenderingCacheNode';
3
3
  import { CacheRecordManager } from './CacheRecordManager';
4
4
  export default class RenderingCache {
@@ -1,5 +1,5 @@
1
1
  import { ImageNode } from '../../EditorImage';
2
- import Rect2 from '../../geometry/Rect2';
2
+ import Rect2 from '../../math/Rect2';
3
3
  import Viewport from '../../Viewport';
4
4
  import AbstractRenderer from '../renderers/AbstractRenderer';
5
5
  import { CacheState } from './types';
@@ -20,6 +20,7 @@ export default class RenderingCacheNode {
20
20
  private allChildrenCanRender;
21
21
  private computeSortedByLeafIds;
22
22
  private idsOfIntersecting;
23
+ private allRenderedIdsIn;
23
24
  private renderingIsUpToDate;
24
25
  renderItems(screenRenderer: AbstractRenderer, items: ImageNode[], viewport: Viewport): void;
25
26
  private isEmpty;
@@ -1,7 +1,7 @@
1
1
  // A cache record with sub-nodes.
2
2
  import Color4 from '../../Color4';
3
3
  import { sortLeavesByZIndex } from '../../EditorImage';
4
- import Rect2 from '../../geometry/Rect2';
4
+ import Rect2 from '../../math/Rect2';
5
5
  // 3x3 divisions for each node.
6
6
  const cacheDivisionSize = 3;
7
7
  // True: Show rendering updates.
@@ -116,17 +116,26 @@ export default class RenderingCacheNode {
116
116
  }
117
117
  return result;
118
118
  }
119
- renderingIsUpToDate(sortedIds) {
120
- if (this.cachedRenderer === null || sortedIds.length !== this.renderedIds.length) {
119
+ // Returns true iff all elems of this.renderedIds are in sortedIds.
120
+ // sortedIds should be sorted by z-index (or some other order, so long as they are
121
+ // sorted by the same thing as this.renderedIds.)
122
+ allRenderedIdsIn(sortedIds) {
123
+ if (this.renderedIds.length > sortedIds.length) {
121
124
  return false;
122
125
  }
123
- for (let i = 0; i < sortedIds.length; i++) {
126
+ for (let i = 0; i < this.renderedIds.length; i++) {
124
127
  if (sortedIds[i] !== this.renderedIds[i]) {
125
128
  return false;
126
129
  }
127
130
  }
128
131
  return true;
129
132
  }
133
+ renderingIsUpToDate(sortedIds) {
134
+ if (this.cachedRenderer === null || sortedIds.length !== this.renderedIds.length) {
135
+ return false;
136
+ }
137
+ return this.allRenderedIdsIn(sortedIds);
138
+ }
130
139
  // Render all [items] within [viewport]
131
140
  renderItems(screenRenderer, items, viewport) {
132
141
  var _a, _b;
@@ -188,14 +197,13 @@ export default class RenderingCacheNode {
188
197
  return;
189
198
  }
190
199
  // Is it worth it to render the items?
191
- // TODO: Replace this with something performace based.
192
- // TODO: Determine whether it is 'worth it' to cache this depending on rendering time.
200
+ // TODO: Consider replacing this with something performace based.
193
201
  if (leavesByIds.length > this.cacheState.props.minComponentsPerCache) {
194
202
  let fullRerenderNeeded = true;
195
203
  if (!this.cachedRenderer) {
196
204
  this.cachedRenderer = this.cacheState.recordManager.allocCanvas(this.region, () => this.onRegionDealloc());
197
205
  }
198
- else if (leavesByIds.length > this.renderedIds.length && this.renderedMaxZIndex !== null) {
206
+ else if (leavesByIds.length > this.renderedIds.length && this.allRenderedIdsIn(leafIds) && this.renderedMaxZIndex !== null) {
199
207
  // We often don't need to do a full re-render even if something's changed.
200
208
  // Check whether we can just draw on top of the existing cache.
201
209
  const newLeaves = [];
@@ -228,6 +236,9 @@ export default class RenderingCacheNode {
228
236
  }
229
237
  }
230
238
  }
239
+ else if (debugMode) {
240
+ console.log('Decided on a full re-render. Reason: At least one of the following is false:', '\n leafIds.length > this.renderedIds.length: ', leafIds.length > this.renderedIds.length, '\n this.allRenderedIdsIn(leafIds): ', this.allRenderedIdsIn(leafIds), '\n this.renderedMaxZIndex !== null: ', this.renderedMaxZIndex !== null, '\n\nthis.rerenderedIds: ', this.renderedIds, ', leafIds: ', leafIds);
241
+ }
231
242
  if (fullRerenderNeeded) {
232
243
  thisRenderer = this.cachedRenderer.startRender();
233
244
  thisRenderer.clear();
@@ -1,4 +1,4 @@
1
- import { Vec2 } from '../../geometry/Vec2';
1
+ import { Vec2 } from '../../math/Vec2';
2
2
  import DummyRenderer from '../renderers/DummyRenderer';
3
3
  import createEditor from '../../testing/createEditor';
4
4
  import RenderingCache from './RenderingCache';
@@ -1,4 +1,4 @@
1
- import { Vec2 } from '../../geometry/Vec2';
1
+ import { Vec2 } from '../../math/Vec2';
2
2
  import AbstractRenderer from '../renderers/AbstractRenderer';
3
3
  import { CacheRecordManager } from './CacheRecordManager';
4
4
  export declare type CacheAddress = number;
@@ -1,4 +1,6 @@
1
1
  export interface TextRendererLocalization {
2
+ pathNodeCount(pathCount: number): string;
3
+ textNodeCount(nodeCount: number): string;
2
4
  textNode(content: string): string;
3
5
  rerenderAsText: string;
4
6
  }
@@ -1,4 +1,6 @@
1
1
  export const defaultTextRendererLocalization = {
2
+ pathNodeCount: (count) => `There are ${count} visible path objects.`,
3
+ textNodeCount: (count) => `There are ${count} visible text nodes.`,
2
4
  textNode: (content) => `Text: ${content}`,
3
5
  rerenderAsText: 'Re-render as text',
4
6
  };
@@ -1,9 +1,9 @@
1
1
  import { LoadSaveDataTable } from '../../components/AbstractComponent';
2
2
  import { TextStyle } from '../../components/Text';
3
- import Mat33 from '../../geometry/Mat33';
4
- import { PathCommand } from '../../geometry/Path';
5
- import Rect2 from '../../geometry/Rect2';
6
- import { Point2, Vec2 } from '../../geometry/Vec2';
3
+ import Mat33 from '../../math/Mat33';
4
+ import { PathCommand } from '../../math/Path';
5
+ import Rect2 from '../../math/Rect2';
6
+ import { Point2, Vec2 } from '../../math/Vec2';
7
7
  import Viewport from '../../Viewport';
8
8
  import RenderingStyle from '../RenderingStyle';
9
9
  export interface RenderablePathSpec {
@@ -1,5 +1,5 @@
1
- import Path, { PathCommandType } from '../../geometry/Path';
2
- import { Vec2 } from '../../geometry/Vec2';
1
+ import Path, { PathCommandType } from '../../math/Path';
2
+ import { Vec2 } from '../../math/Vec2';
3
3
  import { stylesEqual } from '../RenderingStyle';
4
4
  export default class AbstractRenderer {
5
5
  constructor(viewport) {
@@ -1,8 +1,8 @@
1
1
  import { TextStyle } from '../../components/Text';
2
- import Mat33 from '../../geometry/Mat33';
3
- import Rect2 from '../../geometry/Rect2';
4
- import { Point2, Vec2 } from '../../geometry/Vec2';
5
- import Vec3 from '../../geometry/Vec3';
2
+ import Mat33 from '../../math/Mat33';
3
+ import Rect2 from '../../math/Rect2';
4
+ import { Point2, Vec2 } from '../../math/Vec2';
5
+ import Vec3 from '../../math/Vec3';
6
6
  import Viewport from '../../Viewport';
7
7
  import RenderingStyle from '../RenderingStyle';
8
8
  import AbstractRenderer, { RenderablePathSpec } from './AbstractRenderer';
@@ -1,6 +1,6 @@
1
1
  import Color4 from '../../Color4';
2
2
  import Text from '../../components/Text';
3
- import { Vec2 } from '../../geometry/Vec2';
3
+ import { Vec2 } from '../../math/Vec2';
4
4
  import AbstractRenderer from './AbstractRenderer';
5
5
  export default class CanvasRenderer extends AbstractRenderer {
6
6
  constructor(ctx, viewport) {
@@ -1,8 +1,8 @@
1
1
  import { TextStyle } from '../../components/Text';
2
- import Mat33 from '../../geometry/Mat33';
3
- import Rect2 from '../../geometry/Rect2';
4
- import { Point2, Vec2 } from '../../geometry/Vec2';
5
- import Vec3 from '../../geometry/Vec3';
2
+ import Mat33 from '../../math/Mat33';
3
+ import Rect2 from '../../math/Rect2';
4
+ import { Point2, Vec2 } from '../../math/Vec2';
5
+ import Vec3 from '../../math/Vec3';
6
6
  import Viewport from '../../Viewport';
7
7
  import RenderingStyle from '../RenderingStyle';
8
8
  import AbstractRenderer from './AbstractRenderer';