js-draw 0.1.11 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/.eslintrc.js +1 -0
  2. package/.firebaserc +5 -0
  3. package/.github/workflows/firebase-hosting-merge.yml +25 -0
  4. package/.github/workflows/firebase-hosting-pull-request.yml +22 -0
  5. package/.github/workflows/github-pages.yml +52 -0
  6. package/CHANGELOG.md +13 -0
  7. package/README.md +11 -6
  8. package/dist/bundle.js +1 -1
  9. package/dist/src/Color4.d.ts +19 -0
  10. package/dist/src/Color4.js +24 -3
  11. package/dist/src/Editor.d.ts +133 -4
  12. package/dist/src/Editor.js +124 -27
  13. package/dist/src/EditorImage.d.ts +8 -3
  14. package/dist/src/EditorImage.js +42 -26
  15. package/dist/src/EventDispatcher.d.ts +18 -0
  16. package/dist/src/EventDispatcher.js +19 -4
  17. package/dist/src/Pointer.d.ts +1 -1
  18. package/dist/src/Pointer.js +4 -3
  19. package/dist/src/SVGLoader.d.ts +1 -1
  20. package/dist/src/SVGLoader.js +14 -6
  21. package/dist/src/UndoRedoHistory.js +15 -2
  22. package/dist/src/Viewport.d.ts +8 -25
  23. package/dist/src/Viewport.js +18 -10
  24. package/dist/src/bundle/bundled.d.ts +1 -2
  25. package/dist/src/bundle/bundled.js +1 -2
  26. package/dist/src/commands/Command.d.ts +2 -2
  27. package/dist/src/commands/Command.js +4 -4
  28. package/dist/src/commands/Duplicate.d.ts +2 -2
  29. package/dist/src/commands/Duplicate.js +4 -5
  30. package/dist/src/commands/Erase.d.ts +2 -2
  31. package/dist/src/commands/Erase.js +7 -6
  32. package/dist/src/commands/SerializableCommand.d.ts +4 -5
  33. package/dist/src/commands/SerializableCommand.js +12 -4
  34. package/dist/src/commands/invertCommand.d.ts +4 -0
  35. package/dist/src/commands/invertCommand.js +44 -0
  36. package/dist/src/commands/lib.d.ts +6 -0
  37. package/dist/src/commands/lib.js +6 -0
  38. package/dist/src/commands/localization.d.ts +2 -1
  39. package/dist/src/commands/localization.js +1 -0
  40. package/dist/src/components/AbstractComponent.d.ts +16 -11
  41. package/dist/src/components/AbstractComponent.js +28 -17
  42. package/dist/src/components/SVGGlobalAttributesObject.d.ts +4 -4
  43. package/dist/src/components/SVGGlobalAttributesObject.js +8 -2
  44. package/dist/src/components/Stroke.d.ts +16 -6
  45. package/dist/src/components/Stroke.js +12 -9
  46. package/dist/src/components/Text.d.ts +5 -5
  47. package/dist/src/components/Text.js +9 -9
  48. package/dist/src/components/UnknownSVGObject.d.ts +4 -4
  49. package/dist/src/components/UnknownSVGObject.js +7 -2
  50. package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
  51. package/dist/src/components/builders/ArrowBuilder.js +1 -1
  52. package/dist/src/components/builders/FreehandLineBuilder.d.ts +8 -3
  53. package/dist/src/components/builders/FreehandLineBuilder.js +142 -71
  54. package/dist/src/components/builders/LineBuilder.d.ts +1 -1
  55. package/dist/src/components/builders/LineBuilder.js +1 -1
  56. package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
  57. package/dist/src/components/builders/RectangleBuilder.js +3 -3
  58. package/dist/src/components/builders/types.d.ts +1 -1
  59. package/dist/src/components/lib.d.ts +4 -0
  60. package/dist/src/components/lib.js +4 -0
  61. package/dist/src/lib.d.ts +25 -0
  62. package/dist/src/lib.js +25 -0
  63. package/dist/src/localization.d.ts +1 -0
  64. package/dist/src/localization.js +5 -1
  65. package/dist/src/localizations/es.js +1 -1
  66. package/dist/src/{geometry → math}/LineSegment2.d.ts +0 -0
  67. package/dist/src/{geometry → math}/LineSegment2.js +0 -0
  68. package/dist/src/math/Mat33.d.ts +78 -0
  69. package/dist/src/{geometry → math}/Mat33.js +48 -20
  70. package/dist/src/{geometry → math}/Path.d.ts +2 -1
  71. package/dist/src/{geometry → math}/Path.js +59 -52
  72. package/dist/src/{geometry → math}/Rect2.d.ts +2 -2
  73. package/dist/src/{geometry → math}/Rect2.js +0 -0
  74. package/dist/src/{geometry → math}/Vec2.d.ts +0 -0
  75. package/dist/src/{geometry → math}/Vec2.js +0 -0
  76. package/dist/src/math/Vec3.d.ts +96 -0
  77. package/dist/src/{geometry → math}/Vec3.js +63 -15
  78. package/dist/src/math/lib.d.ts +7 -0
  79. package/dist/src/math/lib.js +7 -0
  80. package/dist/src/math/rounding.d.ts +3 -0
  81. package/dist/src/math/rounding.js +121 -0
  82. package/dist/src/rendering/Display.d.ts +47 -1
  83. package/dist/src/rendering/Display.js +60 -15
  84. package/dist/src/rendering/caching/CacheRecord.d.ts +3 -2
  85. package/dist/src/rendering/caching/CacheRecord.js +4 -1
  86. package/dist/src/rendering/caching/CacheRecordManager.d.ts +5 -4
  87. package/dist/src/rendering/caching/CacheRecordManager.js +16 -4
  88. package/dist/src/rendering/caching/RenderingCache.d.ts +2 -3
  89. package/dist/src/rendering/caching/RenderingCache.js +10 -11
  90. package/dist/src/rendering/caching/RenderingCacheNode.d.ts +2 -1
  91. package/dist/src/rendering/caching/RenderingCacheNode.js +18 -7
  92. package/dist/src/rendering/caching/testUtils.js +1 -1
  93. package/dist/src/rendering/caching/types.d.ts +2 -4
  94. package/dist/src/rendering/localization.d.ts +2 -0
  95. package/dist/src/rendering/localization.js +2 -0
  96. package/dist/src/rendering/renderers/AbstractRenderer.d.ts +4 -4
  97. package/dist/src/rendering/renderers/AbstractRenderer.js +2 -2
  98. package/dist/src/rendering/renderers/CanvasRenderer.d.ts +4 -4
  99. package/dist/src/rendering/renderers/CanvasRenderer.js +2 -2
  100. package/dist/src/rendering/renderers/DummyRenderer.d.ts +4 -4
  101. package/dist/src/rendering/renderers/DummyRenderer.js +1 -1
  102. package/dist/src/rendering/renderers/SVGRenderer.d.ts +3 -3
  103. package/dist/src/rendering/renderers/SVGRenderer.js +8 -2
  104. package/dist/src/rendering/renderers/TextOnlyRenderer.d.ts +5 -3
  105. package/dist/src/rendering/renderers/TextOnlyRenderer.js +13 -3
  106. package/dist/src/toolbar/HTMLToolbar.js +1 -0
  107. package/dist/src/toolbar/icons.d.ts +3 -0
  108. package/dist/src/toolbar/icons.js +142 -132
  109. package/dist/src/toolbar/localization.d.ts +2 -1
  110. package/dist/src/toolbar/localization.js +2 -1
  111. package/dist/src/toolbar/makeColorInput.js +3 -2
  112. package/dist/src/toolbar/widgets/ActionButtonWidget.d.ts +13 -0
  113. package/dist/src/toolbar/widgets/ActionButtonWidget.js +21 -0
  114. package/dist/src/toolbar/widgets/BaseWidget.js +2 -0
  115. package/dist/src/toolbar/widgets/HandToolWidget.js +3 -3
  116. package/dist/src/toolbar/widgets/PenWidget.js +1 -0
  117. package/dist/src/toolbar/widgets/SelectionWidget.d.ts +0 -1
  118. package/dist/src/toolbar/widgets/SelectionWidget.js +23 -30
  119. package/dist/src/tools/Eraser.js +1 -1
  120. package/dist/src/tools/PanZoom.d.ts +1 -1
  121. package/dist/src/tools/PanZoom.js +24 -14
  122. package/dist/src/tools/Pen.d.ts +1 -2
  123. package/dist/src/tools/Pen.js +8 -1
  124. package/dist/src/tools/PipetteTool.js +1 -0
  125. package/dist/src/tools/SelectionTool.d.ts +3 -3
  126. package/dist/src/tools/SelectionTool.js +51 -28
  127. package/dist/src/tools/TextTool.js +1 -1
  128. package/dist/src/types.d.ts +21 -10
  129. package/dist/src/types.js +7 -5
  130. package/firebase.json +16 -0
  131. package/package.json +118 -101
  132. package/src/Color4.ts +23 -2
  133. package/src/Editor.ts +181 -37
  134. package/src/EditorImage.test.ts +2 -4
  135. package/src/EditorImage.ts +46 -28
  136. package/src/EventDispatcher.ts +21 -6
  137. package/src/Pointer.ts +4 -3
  138. package/src/SVGLoader.ts +14 -6
  139. package/src/UndoRedoHistory.ts +18 -2
  140. package/src/Viewport.ts +23 -18
  141. package/src/bundle/bundled.ts +1 -2
  142. package/src/commands/Command.ts +5 -5
  143. package/src/commands/Duplicate.ts +4 -5
  144. package/src/commands/Erase.ts +7 -6
  145. package/src/commands/SerializableCommand.ts +17 -9
  146. package/src/commands/invertCommand.ts +51 -0
  147. package/src/commands/lib.ts +14 -0
  148. package/src/commands/localization.ts +3 -1
  149. package/src/components/AbstractComponent.ts +35 -24
  150. package/src/components/SVGGlobalAttributesObject.ts +11 -4
  151. package/src/components/Stroke.test.ts +4 -6
  152. package/src/components/Stroke.ts +15 -11
  153. package/src/components/Text.test.ts +2 -2
  154. package/src/components/Text.ts +9 -10
  155. package/src/components/UnknownSVGObject.ts +10 -4
  156. package/src/components/builders/ArrowBuilder.ts +2 -2
  157. package/src/components/builders/FreehandLineBuilder.ts +190 -80
  158. package/src/components/builders/LineBuilder.ts +2 -2
  159. package/src/components/builders/RectangleBuilder.ts +3 -3
  160. package/src/components/builders/types.ts +1 -1
  161. package/src/components/lib.ts +9 -0
  162. package/src/lib.ts +28 -0
  163. package/src/localization.ts +6 -0
  164. package/src/localizations/es.ts +2 -1
  165. package/src/{geometry → math}/LineSegment2.test.ts +0 -0
  166. package/src/{geometry → math}/LineSegment2.ts +0 -0
  167. package/src/{geometry → math}/Mat33.test.ts +0 -0
  168. package/src/{geometry → math}/Mat33.ts +48 -20
  169. package/src/{geometry → math}/Path.fromString.test.ts +0 -0
  170. package/src/{geometry → math}/Path.test.ts +0 -0
  171. package/src/{geometry → math}/Path.toString.test.ts +11 -2
  172. package/src/{geometry → math}/Path.ts +61 -58
  173. package/src/{geometry → math}/Rect2.test.ts +0 -0
  174. package/src/{geometry → math}/Rect2.ts +2 -2
  175. package/src/{geometry → math}/Vec2.test.ts +0 -0
  176. package/src/{geometry → math}/Vec2.ts +0 -0
  177. package/src/{geometry → math}/Vec3.test.ts +0 -0
  178. package/src/{geometry → math}/Vec3.ts +64 -16
  179. package/src/math/lib.ts +15 -0
  180. package/src/math/rounding.test.ts +40 -0
  181. package/src/math/rounding.ts +147 -0
  182. package/src/rendering/Display.ts +63 -15
  183. package/src/rendering/caching/CacheRecord.test.ts +3 -3
  184. package/src/rendering/caching/CacheRecord.ts +6 -2
  185. package/src/rendering/caching/CacheRecordManager.ts +34 -8
  186. package/src/rendering/caching/RenderingCache.test.ts +3 -3
  187. package/src/rendering/caching/RenderingCache.ts +11 -16
  188. package/src/rendering/caching/RenderingCacheNode.ts +23 -7
  189. package/src/rendering/caching/testUtils.ts +1 -1
  190. package/src/rendering/caching/types.ts +2 -7
  191. package/src/rendering/localization.ts +4 -0
  192. package/src/rendering/renderers/AbstractRenderer.ts +4 -4
  193. package/src/rendering/renderers/CanvasRenderer.ts +5 -5
  194. package/src/rendering/renderers/DummyRenderer.test.ts +2 -2
  195. package/src/rendering/renderers/DummyRenderer.ts +4 -4
  196. package/src/rendering/renderers/SVGRenderer.ts +10 -4
  197. package/src/rendering/renderers/TextOnlyRenderer.ts +17 -6
  198. package/src/toolbar/HTMLToolbar.ts +1 -0
  199. package/src/toolbar/icons.ts +157 -137
  200. package/src/toolbar/localization.ts +4 -2
  201. package/src/toolbar/makeColorInput.ts +3 -2
  202. package/src/toolbar/toolbar.css +1 -1
  203. package/src/toolbar/widgets/ActionButtonWidget.ts +31 -0
  204. package/src/toolbar/widgets/BaseWidget.ts +2 -0
  205. package/src/toolbar/widgets/HandToolWidget.ts +3 -3
  206. package/src/toolbar/widgets/PenWidget.ts +2 -0
  207. package/src/toolbar/widgets/SelectionWidget.ts +46 -41
  208. package/src/tools/Eraser.ts +2 -2
  209. package/src/tools/PanZoom.ts +28 -17
  210. package/src/tools/Pen.ts +11 -2
  211. package/src/tools/PipetteTool.ts +2 -0
  212. package/src/tools/SelectionTool.test.ts +2 -4
  213. package/src/tools/SelectionTool.ts +52 -24
  214. package/src/tools/TextTool.ts +2 -2
  215. package/src/tools/UndoRedoShortcut.test.ts +1 -1
  216. package/src/types.ts +23 -7
  217. package/tsconfig.json +4 -1
  218. package/typedoc.json +20 -0
  219. package/dist/src/geometry/Mat33.d.ts +0 -32
  220. package/dist/src/geometry/Vec3.d.ts +0 -34
@@ -1,7 +1,21 @@
1
+ /**
2
+ * Handles `HTMLCanvasElement`s (or other drawing surfaces if being used) used to display the editor's contents.
3
+ *
4
+ * @example
5
+ * ```
6
+ * const editor = new Editor(document.body);
7
+ * const w = editor.display.width;
8
+ * const h = editor.display.height;
9
+ * const center = Vec2.of(w / 2, h / 2);
10
+ * const colorAtCenter = editor.display.getColorAt(center);
11
+ * ```
12
+ *
13
+ * @packageDocumentation
14
+ */
1
15
  import CanvasRenderer from './renderers/CanvasRenderer';
2
16
  import { EditorEventType } from '../types';
3
17
  import DummyRenderer from './renderers/DummyRenderer';
4
- import { Vec2 } from '../geometry/Vec2';
18
+ import { Vec2 } from '../math/Vec2';
5
19
  import RenderingCache from './caching/RenderingCache';
6
20
  import TextOnlyRenderer from './renderers/TextOnlyRenderer';
7
21
  import Color4 from '../Color4';
@@ -12,9 +26,15 @@ export var RenderingMode;
12
26
  // SVGRenderer is not supported by the main display
13
27
  })(RenderingMode || (RenderingMode = {}));
14
28
  export default class Display {
29
+ /** @internal */
15
30
  constructor(editor, mode, parent) {
16
31
  this.editor = editor;
17
32
  this.parent = parent;
33
+ this.textRerenderOutput = null;
34
+ /**
35
+ * @returns the color at the given point on the dry ink renderer, or `null` if `screenPos`
36
+ * is not on the display.
37
+ */
18
38
  this.getColorAt = (_screenPos) => {
19
39
  return null;
20
40
  };
@@ -51,10 +71,10 @@ export default class Display {
51
71
  return this.dryInkRenderer.canRenderFromWithoutDataLoss(renderer);
52
72
  },
53
73
  blockResolution: cacheBlockResolution,
54
- cacheSize: 500 * 500 * 4 * 200,
74
+ cacheSize: 500 * 500 * 4 * 150,
55
75
  maxScale: 1.5,
56
- minComponentsPerCache: 50,
57
- minComponentsToUseCache: 120,
76
+ minComponentsPerCache: 45,
77
+ minComponentsToUseCache: 105,
58
78
  });
59
79
  this.editor.notifier.on(EditorEventType.DisplayResized, event => {
60
80
  var _a;
@@ -64,15 +84,18 @@ export default class Display {
64
84
  (_a = this.resizeSurfacesCallback) === null || _a === void 0 ? void 0 : _a.call(this);
65
85
  });
66
86
  }
67
- // Returns the visible width of the display (e.g. how much
68
- // space the display's element takes up in the x direction
69
- // in the DOM).
87
+ /**
88
+ * @returns the visible width of the display (e.g. how much
89
+ * space the display's element takes up in the x direction
90
+ * in the DOM).
91
+ */
70
92
  get width() {
71
93
  return this.dryInkRenderer.displaySize().x;
72
94
  }
73
95
  get height() {
74
96
  return this.dryInkRenderer.displaySize().y;
75
97
  }
98
+ /** @internal */
76
99
  getCache() {
77
100
  return this.cache;
78
101
  }
@@ -126,17 +149,30 @@ export default class Display {
126
149
  const rerenderButton = document.createElement('button');
127
150
  rerenderButton.classList.add('rerenderButton');
128
151
  rerenderButton.innerText = this.editor.localization.rerenderAsText;
129
- const rerenderOutput = document.createElement('div');
130
- rerenderOutput.ariaLive = 'polite';
152
+ this.textRerenderOutput = document.createElement('div');
153
+ this.textRerenderOutput.setAttribute('aria-live', 'polite');
131
154
  rerenderButton.onclick = () => {
132
- this.textRenderer.clear();
133
- this.editor.image.render(this.textRenderer, this.editor.viewport);
134
- rerenderOutput.innerText = this.textRenderer.getDescription();
155
+ this.rerenderAsText();
135
156
  };
136
- textRendererOutputContainer.replaceChildren(rerenderButton, rerenderOutput);
157
+ textRendererOutputContainer.replaceChildren(rerenderButton, this.textRerenderOutput);
137
158
  this.editor.createHTMLOverlay(textRendererOutputContainer);
138
159
  }
139
- // Clears the drawing surfaces and otherwise prepares for a rerender.
160
+ /**
161
+ * Rerenders the text-based display.
162
+ * The text-based display is intended for screen readers and can be navigated to by pressing `tab`.
163
+ */
164
+ rerenderAsText() {
165
+ this.textRenderer.clear();
166
+ this.editor.image.render(this.textRenderer, this.editor.viewport);
167
+ if (this.textRerenderOutput) {
168
+ this.textRerenderOutput.innerText = this.textRenderer.getDescription();
169
+ }
170
+ }
171
+ /**
172
+ * Clears the drawing surfaces and otherwise prepares for a rerender.
173
+ *
174
+ * @returns the dry ink renderer.
175
+ */
140
176
  startRerender() {
141
177
  var _a;
142
178
  (_a = this.resizeSurfacesCallback) === null || _a === void 0 ? void 0 : _a.call(this);
@@ -144,16 +180,25 @@ export default class Display {
144
180
  this.dryInkRenderer.clear();
145
181
  return this.dryInkRenderer;
146
182
  }
183
+ /**
184
+ * If `draftMode`, the dry ink renderer is configured to render
185
+ * low-quality output.
186
+ */
147
187
  setDraftMode(draftMode) {
148
188
  this.dryInkRenderer.setDraftMode(draftMode);
149
189
  }
190
+ /** @internal */
150
191
  getDryInkRenderer() {
151
192
  return this.dryInkRenderer;
152
193
  }
194
+ /**
195
+ * @returns The renderer used for showing action previews (e.g. an unfinished stroke).
196
+ * The `wetInkRenderer`'s surface is stacked above the `dryInkRenderer`'s.
197
+ */
153
198
  getWetInkRenderer() {
154
199
  return this.wetInkRenderer;
155
200
  }
156
- // Re-renders the contents of the wetInkRenderer onto the dryInkRenderer
201
+ /** Re-renders the contents of the wetInkRenderer onto the dryInkRenderer. */
157
202
  flatten() {
158
203
  var _a;
159
204
  (_a = this.flattenCallback) === null || _a === void 0 ? void 0 : _a.call(this);
@@ -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 {
@@ -8,6 +8,7 @@ export default class CacheRecord {
8
8
  private renderer;
9
9
  private lastUsedCycle;
10
10
  private allocd;
11
+ allocCount: number;
11
12
  constructor(onBeforeDeallocCallback: BeforeDeallocCallback | null, cacheState: CacheState);
12
13
  startRender(): AbstractRenderer;
13
14
  dealloc(): void;
@@ -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 {
@@ -6,6 +6,8 @@ export default class CacheRecord {
6
6
  this.onBeforeDeallocCallback = onBeforeDeallocCallback;
7
7
  this.cacheState = cacheState;
8
8
  this.allocd = false;
9
+ // For debugging
10
+ this.allocCount = 0;
9
11
  this.renderer = cacheState.props.createRenderer();
10
12
  this.lastUsedCycle = -1;
11
13
  this.allocd = true;
@@ -34,6 +36,7 @@ export default class CacheRecord {
34
36
  this.allocd = true;
35
37
  this.onBeforeDeallocCallback = newDeallocCallback;
36
38
  this.lastUsedCycle = this.cacheState.currentRenderingCycle;
39
+ this.allocCount++;
37
40
  }
38
41
  getLastUsedCycle() {
39
42
  return this.lastUsedCycle;
@@ -1,11 +1,12 @@
1
- import { BeforeDeallocCallback, PartialCacheState } from './types';
1
+ import { BeforeDeallocCallback, CacheProps, CacheState } 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
- private readonly cacheState;
6
5
  private cacheRecords;
7
6
  private maxCanvases;
8
- constructor(cacheState: PartialCacheState);
7
+ private cacheState;
8
+ constructor(cacheProps: CacheProps);
9
+ setSharedState(state: CacheState): void;
9
10
  allocCanvas(drawTo: Rect2, onDealloc: BeforeDeallocCallback): CacheRecord;
10
11
  private getLeastRecentlyUsedRecord;
11
12
  }
@@ -1,25 +1,37 @@
1
1
  import CacheRecord from './CacheRecord';
2
+ const debugMode = false;
2
3
  export class CacheRecordManager {
3
- constructor(cacheState) {
4
- this.cacheState = cacheState;
4
+ constructor(cacheProps) {
5
5
  // Fixed-size array: Cache blocks are assigned indicies into [cachedCanvases].
6
6
  this.cacheRecords = [];
7
- const cacheProps = cacheState.props;
8
7
  this.maxCanvases = Math.ceil(
9
8
  // Assuming four components per pixel:
10
9
  cacheProps.cacheSize / 4 / cacheProps.blockResolution.x / cacheProps.blockResolution.y);
11
10
  }
11
+ setSharedState(state) {
12
+ this.cacheState = state;
13
+ }
12
14
  allocCanvas(drawTo, onDealloc) {
13
15
  if (this.cacheRecords.length < this.maxCanvases) {
14
- const record = new CacheRecord(onDealloc, Object.assign(Object.assign({}, this.cacheState), { recordManager: this }));
16
+ const record = new CacheRecord(onDealloc, this.cacheState);
15
17
  record.setRenderingRegion(drawTo);
16
18
  this.cacheRecords.push(record);
19
+ if (debugMode) {
20
+ console.log('[Cache] Cache spaces used: ', this.cacheRecords.length, ' of ', this.maxCanvases);
21
+ }
17
22
  return record;
18
23
  }
19
24
  else {
20
25
  const lru = this.getLeastRecentlyUsedRecord();
26
+ if (debugMode) {
27
+ console.log('[Cache] Re-alloc. Times allocated: ', lru.allocCount, '\nLast used cycle: ', lru.getLastUsedCycle(), '\nCurrent cycle: ', this.cacheState.currentRenderingCycle);
28
+ }
21
29
  lru.realloc(onDealloc);
22
30
  lru.setRenderingRegion(drawTo);
31
+ if (debugMode) {
32
+ console.log('[Cache] Now re-alloc\'d. Last used cycle: ', lru.getLastUsedCycle());
33
+ console.assert(lru['cacheState'] === this.cacheState, '[Cache] Unequal cache states! cacheState should be a shared object!');
34
+ }
23
35
  return lru;
24
36
  }
25
37
  }
@@ -1,12 +1,11 @@
1
1
  import { ImageNode } from '../../EditorImage';
2
2
  import Viewport from '../../Viewport';
3
3
  import AbstractRenderer from '../renderers/AbstractRenderer';
4
- import { CacheProps, CacheState } from './types';
4
+ import { CacheProps } from './types';
5
5
  export default class RenderingCache {
6
- private partialSharedState;
6
+ private sharedState;
7
7
  private recordManager;
8
8
  private rootNode;
9
9
  constructor(cacheProps: CacheProps);
10
- getSharedState(): CacheState;
11
10
  render(screenRenderer: AbstractRenderer, image: ImageNode, viewport: Viewport): void;
12
11
  }
@@ -1,38 +1,37 @@
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 {
5
5
  constructor(cacheProps) {
6
- this.partialSharedState = {
6
+ this.recordManager = new CacheRecordManager(cacheProps);
7
+ this.sharedState = {
7
8
  props: cacheProps,
8
9
  currentRenderingCycle: 0,
10
+ recordManager: this.recordManager,
9
11
  };
10
- this.recordManager = new CacheRecordManager(this.partialSharedState);
11
- }
12
- getSharedState() {
13
- return Object.assign(Object.assign({}, this.partialSharedState), { recordManager: this.recordManager });
12
+ this.recordManager.setSharedState(this.sharedState);
14
13
  }
15
14
  render(screenRenderer, image, viewport) {
16
15
  var _a;
17
16
  const visibleRect = viewport.visibleRect;
18
- this.partialSharedState.currentRenderingCycle++;
17
+ this.sharedState.currentRenderingCycle++;
19
18
  // If we can't use the cache,
20
- if (!this.partialSharedState.props.isOfCorrectType(screenRenderer)) {
19
+ if (!this.sharedState.props.isOfCorrectType(screenRenderer)) {
21
20
  image.render(screenRenderer, visibleRect);
22
21
  return;
23
22
  }
24
23
  if (!this.rootNode) {
25
24
  // Adjust the node so that it has the correct aspect ratio
26
- const res = this.partialSharedState.props.blockResolution;
25
+ const res = this.sharedState.props.blockResolution;
27
26
  const topLeft = visibleRect.topLeft;
28
- this.rootNode = new RenderingCacheNode(new Rect2(topLeft.x, topLeft.y, res.x, res.y), this.getSharedState());
27
+ this.rootNode = new RenderingCacheNode(new Rect2(topLeft.x, topLeft.y, res.x, res.y), this.sharedState);
29
28
  }
30
29
  while (!this.rootNode.region.containsRect(visibleRect)) {
31
30
  this.rootNode = this.rootNode.generateParent();
32
31
  }
33
32
  this.rootNode = (_a = this.rootNode.smallestChildContaining(visibleRect)) !== null && _a !== void 0 ? _a : this.rootNode;
34
33
  const visibleLeaves = image.getLeavesIntersectingRegion(viewport.visibleRect, rect => screenRenderer.isTooSmallToRender(rect));
35
- if (visibleLeaves.length > this.partialSharedState.props.minComponentsToUseCache) {
34
+ if (visibleLeaves.length > this.sharedState.props.minComponentsToUseCache) {
36
35
  this.rootNode.renderItems(screenRenderer, [image], viewport);
37
36
  }
38
37
  else {
@@ -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;
@@ -12,10 +12,8 @@ export interface CacheProps {
12
12
  minComponentsPerCache: number;
13
13
  minComponentsToUseCache: number;
14
14
  }
15
- export interface PartialCacheState {
15
+ export interface CacheState {
16
16
  currentRenderingCycle: number;
17
17
  props: CacheProps;
18
- }
19
- export interface CacheState extends PartialCacheState {
20
18
  recordManager: CacheRecordManager;
21
19
  }
@@ -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) {
@@ -43,7 +43,7 @@ export default class CanvasRenderer extends AbstractRenderer {
43
43
  }
44
44
  else {
45
45
  this.minSquareCurveApproxDist = 1;
46
- this.minRenderSizeBothDimens = 1;
46
+ this.minRenderSizeBothDimens = 0.5;
47
47
  this.minRenderSizeAnyDimen = 0;
48
48
  }
49
49
  }
@@ -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';
@@ -1,5 +1,5 @@
1
1
  // Renderer that outputs nothing. Useful for automated tests.
2
- import { Vec2 } from '../../geometry/Vec2';
2
+ import { Vec2 } from '../../math/Vec2';
3
3
  import AbstractRenderer from './AbstractRenderer';
4
4
  export default class DummyRenderer extends AbstractRenderer {
5
5
  constructor(viewport) {
@@ -1,8 +1,8 @@
1
1
  import { LoadSaveDataTable } from '../../components/AbstractComponent';
2
2
  import { TextStyle } from '../../components/Text';
3
- import Mat33 from '../../geometry/Mat33';
4
- import Rect2 from '../../geometry/Rect2';
5
- import { Point2, Vec2 } from '../../geometry/Vec2';
3
+ import Mat33 from '../../math/Mat33';
4
+ import Rect2 from '../../math/Rect2';
5
+ import { Point2, Vec2 } from '../../math/Vec2';
6
6
  import Viewport from '../../Viewport';
7
7
  import RenderingStyle from '../RenderingStyle';
8
8
  import AbstractRenderer from './AbstractRenderer';
@@ -1,5 +1,7 @@
1
- import Path, { PathCommandType } from '../../geometry/Path';
2
- import { Vec2 } from '../../geometry/Vec2';
1
+ import Mat33 from '../../math/Mat33';
2
+ import Path, { PathCommandType } from '../../math/Path';
3
+ import { toRoundedString } from '../../math/rounding';
4
+ import { Vec2 } from '../../math/Vec2';
3
5
  import { svgAttributesDataKey, svgStyleAttributesDataKey } from '../../SVGLoader';
4
6
  import AbstractRenderer from './AbstractRenderer';
5
7
  const svgNameSpace = 'http://www.w3.org/2000/svg';
@@ -89,6 +91,8 @@ export default class SVGRenderer extends AbstractRenderer {
89
91
  drawText(text, transform, style) {
90
92
  var _a, _b, _c;
91
93
  transform = this.getCanvasToScreenTransform().rightMul(transform);
94
+ const translation = transform.transformVec2(Vec2.zero);
95
+ transform = transform.rightMul(Mat33.translation(translation.times(-1)));
92
96
  const textElem = document.createElementNS(svgNameSpace, 'text');
93
97
  textElem.appendChild(document.createTextNode(text));
94
98
  textElem.style.transform = `matrix(
@@ -101,6 +105,8 @@ export default class SVGRenderer extends AbstractRenderer {
101
105
  textElem.style.fontWeight = (_b = style.fontWeight) !== null && _b !== void 0 ? _b : '';
102
106
  textElem.style.fontSize = style.size + 'px';
103
107
  textElem.style.fill = style.renderingStyle.fill.toHexString();
108
+ textElem.setAttribute('x', `${toRoundedString(translation.x)}`);
109
+ textElem.setAttribute('y', `${toRoundedString(translation.y)}`);
104
110
  if (style.renderingStyle.stroke) {
105
111
  const strokeStyle = style.renderingStyle.stroke;
106
112
  textElem.style.stroke = strokeStyle.color.toHexString();
@@ -1,7 +1,7 @@
1
1
  import { TextStyle } from '../../components/Text';
2
- import Mat33 from '../../geometry/Mat33';
3
- import Rect2 from '../../geometry/Rect2';
4
- import Vec3 from '../../geometry/Vec3';
2
+ import Mat33 from '../../math/Mat33';
3
+ import Rect2 from '../../math/Rect2';
4
+ import Vec3 from '../../math/Vec3';
5
5
  import Viewport from '../../Viewport';
6
6
  import { TextRendererLocalization } from '../localization';
7
7
  import RenderingStyle from '../RenderingStyle';
@@ -9,6 +9,8 @@ import AbstractRenderer from './AbstractRenderer';
9
9
  export default class TextOnlyRenderer extends AbstractRenderer {
10
10
  private localizationTable;
11
11
  private descriptionBuilder;
12
+ private pathCount;
13
+ private textNodeCount;
12
14
  constructor(viewport: Viewport, localizationTable: TextRendererLocalization);
13
15
  displaySize(): Vec3;
14
16
  clear(): void;
@@ -1,4 +1,4 @@
1
- import { Vec2 } from '../../geometry/Vec2';
1
+ import { Vec2 } from '../../math/Vec2';
2
2
  import AbstractRenderer from './AbstractRenderer';
3
3
  // Outputs a description of what was rendered.
4
4
  export default class TextOnlyRenderer extends AbstractRenderer {
@@ -6,6 +6,8 @@ export default class TextOnlyRenderer extends AbstractRenderer {
6
6
  super(viewport);
7
7
  this.localizationTable = localizationTable;
8
8
  this.descriptionBuilder = [];
9
+ this.pathCount = 0;
10
+ this.textNodeCount = 0;
9
11
  }
10
12
  displaySize() {
11
13
  // We don't have a graphical display, export a reasonable size.
@@ -13,13 +15,20 @@ export default class TextOnlyRenderer extends AbstractRenderer {
13
15
  }
14
16
  clear() {
15
17
  this.descriptionBuilder = [];
18
+ this.pathCount = 0;
19
+ this.textNodeCount = 0;
16
20
  }
17
21
  getDescription() {
18
- return this.descriptionBuilder.join('\n');
22
+ return [
23
+ this.localizationTable.pathNodeCount(this.pathCount),
24
+ this.localizationTable.textNodeCount(this.textNodeCount),
25
+ ...this.descriptionBuilder
26
+ ].join('\n');
19
27
  }
20
28
  beginPath(_startPoint) {
21
29
  }
22
30
  endPath(_style) {
31
+ this.pathCount++;
23
32
  }
24
33
  lineTo(_point) {
25
34
  }
@@ -31,9 +40,10 @@ export default class TextOnlyRenderer extends AbstractRenderer {
31
40
  }
32
41
  drawText(text, _transform, _style) {
33
42
  this.descriptionBuilder.push(this.localizationTable.textNode(text));
43
+ this.textNodeCount++;
34
44
  }
35
45
  isTooSmallToRender(rect) {
36
- return rect.maxDimension < 10 / this.getSizeOfCanvasPixelOnScreen();
46
+ return rect.maxDimension < 15 / this.getSizeOfCanvasPixelOnScreen();
37
47
  }
38
48
  drawPoints(..._points) {
39
49
  }
@@ -29,6 +29,7 @@ export default class HTMLToolbar {
29
29
  }
30
30
  this.setupColorPickers();
31
31
  }
32
+ // @internal
32
33
  setupColorPickers() {
33
34
  const closePickerOverlay = document.createElement('div');
34
35
  closePickerOverlay.className = `${toolbarCSSPrefix}closeColorPickerOverlay`;
@@ -15,3 +15,6 @@ export declare const makeTextIcon: (textStyle: TextStyle) => SVGSVGElement;
15
15
  export declare const makePenIcon: (tipThickness: number, color: string) => SVGSVGElement;
16
16
  export declare const makeIconFromFactory: (pen: Pen, factory: ComponentBuilderFactory) => SVGSVGElement;
17
17
  export declare const makePipetteIcon: (color?: Color4) => SVGSVGElement;
18
+ export declare const makeResizeViewportIcon: () => SVGSVGElement;
19
+ export declare const makeDuplicateSelectionIcon: () => SVGSVGElement;
20
+ export declare const makeDeleteSelectionIcon: () => SVGSVGElement;