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.
- package/.eslintrc.js +1 -0
- package/.firebaserc +5 -0
- package/.github/workflows/firebase-hosting-merge.yml +25 -0
- package/.github/workflows/firebase-hosting-pull-request.yml +22 -0
- package/.github/workflows/github-pages.yml +52 -0
- package/CHANGELOG.md +13 -0
- package/README.md +11 -6
- package/dist/bundle.js +1 -1
- package/dist/src/Color4.d.ts +19 -0
- package/dist/src/Color4.js +24 -3
- package/dist/src/Editor.d.ts +133 -4
- package/dist/src/Editor.js +124 -27
- package/dist/src/EditorImage.d.ts +8 -3
- package/dist/src/EditorImage.js +42 -26
- package/dist/src/EventDispatcher.d.ts +18 -0
- package/dist/src/EventDispatcher.js +19 -4
- package/dist/src/Pointer.d.ts +1 -1
- package/dist/src/Pointer.js +4 -3
- package/dist/src/SVGLoader.d.ts +1 -1
- package/dist/src/SVGLoader.js +14 -6
- package/dist/src/UndoRedoHistory.js +15 -2
- package/dist/src/Viewport.d.ts +8 -25
- package/dist/src/Viewport.js +18 -10
- package/dist/src/bundle/bundled.d.ts +1 -2
- package/dist/src/bundle/bundled.js +1 -2
- package/dist/src/commands/Command.d.ts +2 -2
- package/dist/src/commands/Command.js +4 -4
- package/dist/src/commands/Duplicate.d.ts +2 -2
- package/dist/src/commands/Duplicate.js +4 -5
- package/dist/src/commands/Erase.d.ts +2 -2
- package/dist/src/commands/Erase.js +7 -6
- package/dist/src/commands/SerializableCommand.d.ts +4 -5
- package/dist/src/commands/SerializableCommand.js +12 -4
- package/dist/src/commands/invertCommand.d.ts +4 -0
- package/dist/src/commands/invertCommand.js +44 -0
- package/dist/src/commands/lib.d.ts +6 -0
- package/dist/src/commands/lib.js +6 -0
- package/dist/src/commands/localization.d.ts +2 -1
- package/dist/src/commands/localization.js +1 -0
- package/dist/src/components/AbstractComponent.d.ts +16 -11
- package/dist/src/components/AbstractComponent.js +28 -17
- package/dist/src/components/SVGGlobalAttributesObject.d.ts +4 -4
- package/dist/src/components/SVGGlobalAttributesObject.js +8 -2
- package/dist/src/components/Stroke.d.ts +16 -6
- package/dist/src/components/Stroke.js +12 -9
- package/dist/src/components/Text.d.ts +5 -5
- package/dist/src/components/Text.js +9 -9
- package/dist/src/components/UnknownSVGObject.d.ts +4 -4
- package/dist/src/components/UnknownSVGObject.js +7 -2
- package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
- package/dist/src/components/builders/ArrowBuilder.js +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.d.ts +8 -3
- package/dist/src/components/builders/FreehandLineBuilder.js +142 -71
- package/dist/src/components/builders/LineBuilder.d.ts +1 -1
- package/dist/src/components/builders/LineBuilder.js +1 -1
- package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
- package/dist/src/components/builders/RectangleBuilder.js +3 -3
- package/dist/src/components/builders/types.d.ts +1 -1
- package/dist/src/components/lib.d.ts +4 -0
- package/dist/src/components/lib.js +4 -0
- package/dist/src/lib.d.ts +25 -0
- package/dist/src/lib.js +25 -0
- package/dist/src/localization.d.ts +1 -0
- package/dist/src/localization.js +5 -1
- package/dist/src/localizations/es.js +1 -1
- package/dist/src/{geometry → math}/LineSegment2.d.ts +0 -0
- package/dist/src/{geometry → math}/LineSegment2.js +0 -0
- package/dist/src/math/Mat33.d.ts +78 -0
- package/dist/src/{geometry → math}/Mat33.js +48 -20
- package/dist/src/{geometry → math}/Path.d.ts +2 -1
- package/dist/src/{geometry → math}/Path.js +59 -52
- package/dist/src/{geometry → math}/Rect2.d.ts +2 -2
- package/dist/src/{geometry → math}/Rect2.js +0 -0
- package/dist/src/{geometry → math}/Vec2.d.ts +0 -0
- package/dist/src/{geometry → math}/Vec2.js +0 -0
- package/dist/src/math/Vec3.d.ts +96 -0
- package/dist/src/{geometry → math}/Vec3.js +63 -15
- package/dist/src/math/lib.d.ts +7 -0
- package/dist/src/math/lib.js +7 -0
- package/dist/src/math/rounding.d.ts +3 -0
- package/dist/src/math/rounding.js +121 -0
- package/dist/src/rendering/Display.d.ts +47 -1
- package/dist/src/rendering/Display.js +60 -15
- package/dist/src/rendering/caching/CacheRecord.d.ts +3 -2
- package/dist/src/rendering/caching/CacheRecord.js +4 -1
- package/dist/src/rendering/caching/CacheRecordManager.d.ts +5 -4
- package/dist/src/rendering/caching/CacheRecordManager.js +16 -4
- package/dist/src/rendering/caching/RenderingCache.d.ts +2 -3
- package/dist/src/rendering/caching/RenderingCache.js +10 -11
- package/dist/src/rendering/caching/RenderingCacheNode.d.ts +2 -1
- package/dist/src/rendering/caching/RenderingCacheNode.js +18 -7
- package/dist/src/rendering/caching/testUtils.js +1 -1
- package/dist/src/rendering/caching/types.d.ts +2 -4
- package/dist/src/rendering/localization.d.ts +2 -0
- package/dist/src/rendering/localization.js +2 -0
- package/dist/src/rendering/renderers/AbstractRenderer.d.ts +4 -4
- package/dist/src/rendering/renderers/AbstractRenderer.js +2 -2
- package/dist/src/rendering/renderers/CanvasRenderer.d.ts +4 -4
- package/dist/src/rendering/renderers/CanvasRenderer.js +2 -2
- package/dist/src/rendering/renderers/DummyRenderer.d.ts +4 -4
- package/dist/src/rendering/renderers/DummyRenderer.js +1 -1
- package/dist/src/rendering/renderers/SVGRenderer.d.ts +3 -3
- package/dist/src/rendering/renderers/SVGRenderer.js +8 -2
- package/dist/src/rendering/renderers/TextOnlyRenderer.d.ts +5 -3
- package/dist/src/rendering/renderers/TextOnlyRenderer.js +13 -3
- package/dist/src/toolbar/HTMLToolbar.js +1 -0
- package/dist/src/toolbar/icons.d.ts +3 -0
- package/dist/src/toolbar/icons.js +142 -132
- package/dist/src/toolbar/localization.d.ts +2 -1
- package/dist/src/toolbar/localization.js +2 -1
- package/dist/src/toolbar/makeColorInput.js +3 -2
- package/dist/src/toolbar/widgets/ActionButtonWidget.d.ts +13 -0
- package/dist/src/toolbar/widgets/ActionButtonWidget.js +21 -0
- package/dist/src/toolbar/widgets/BaseWidget.js +2 -0
- package/dist/src/toolbar/widgets/HandToolWidget.js +3 -3
- package/dist/src/toolbar/widgets/PenWidget.js +1 -0
- package/dist/src/toolbar/widgets/SelectionWidget.d.ts +0 -1
- package/dist/src/toolbar/widgets/SelectionWidget.js +23 -30
- package/dist/src/tools/Eraser.js +1 -1
- package/dist/src/tools/PanZoom.d.ts +1 -1
- package/dist/src/tools/PanZoom.js +24 -14
- package/dist/src/tools/Pen.d.ts +1 -2
- package/dist/src/tools/Pen.js +8 -1
- package/dist/src/tools/PipetteTool.js +1 -0
- package/dist/src/tools/SelectionTool.d.ts +3 -3
- package/dist/src/tools/SelectionTool.js +51 -28
- package/dist/src/tools/TextTool.js +1 -1
- package/dist/src/types.d.ts +21 -10
- package/dist/src/types.js +7 -5
- package/firebase.json +16 -0
- package/package.json +118 -101
- package/src/Color4.ts +23 -2
- package/src/Editor.ts +181 -37
- package/src/EditorImage.test.ts +2 -4
- package/src/EditorImage.ts +46 -28
- package/src/EventDispatcher.ts +21 -6
- package/src/Pointer.ts +4 -3
- package/src/SVGLoader.ts +14 -6
- package/src/UndoRedoHistory.ts +18 -2
- package/src/Viewport.ts +23 -18
- package/src/bundle/bundled.ts +1 -2
- package/src/commands/Command.ts +5 -5
- package/src/commands/Duplicate.ts +4 -5
- package/src/commands/Erase.ts +7 -6
- package/src/commands/SerializableCommand.ts +17 -9
- package/src/commands/invertCommand.ts +51 -0
- package/src/commands/lib.ts +14 -0
- package/src/commands/localization.ts +3 -1
- package/src/components/AbstractComponent.ts +35 -24
- package/src/components/SVGGlobalAttributesObject.ts +11 -4
- package/src/components/Stroke.test.ts +4 -6
- package/src/components/Stroke.ts +15 -11
- package/src/components/Text.test.ts +2 -2
- package/src/components/Text.ts +9 -10
- package/src/components/UnknownSVGObject.ts +10 -4
- package/src/components/builders/ArrowBuilder.ts +2 -2
- package/src/components/builders/FreehandLineBuilder.ts +190 -80
- package/src/components/builders/LineBuilder.ts +2 -2
- package/src/components/builders/RectangleBuilder.ts +3 -3
- package/src/components/builders/types.ts +1 -1
- package/src/components/lib.ts +9 -0
- package/src/lib.ts +28 -0
- package/src/localization.ts +6 -0
- package/src/localizations/es.ts +2 -1
- package/src/{geometry → math}/LineSegment2.test.ts +0 -0
- package/src/{geometry → math}/LineSegment2.ts +0 -0
- package/src/{geometry → math}/Mat33.test.ts +0 -0
- package/src/{geometry → math}/Mat33.ts +48 -20
- package/src/{geometry → math}/Path.fromString.test.ts +0 -0
- package/src/{geometry → math}/Path.test.ts +0 -0
- package/src/{geometry → math}/Path.toString.test.ts +11 -2
- package/src/{geometry → math}/Path.ts +61 -58
- package/src/{geometry → math}/Rect2.test.ts +0 -0
- package/src/{geometry → math}/Rect2.ts +2 -2
- package/src/{geometry → math}/Vec2.test.ts +0 -0
- package/src/{geometry → math}/Vec2.ts +0 -0
- package/src/{geometry → math}/Vec3.test.ts +0 -0
- package/src/{geometry → math}/Vec3.ts +64 -16
- package/src/math/lib.ts +15 -0
- package/src/math/rounding.test.ts +40 -0
- package/src/math/rounding.ts +147 -0
- package/src/rendering/Display.ts +63 -15
- package/src/rendering/caching/CacheRecord.test.ts +3 -3
- package/src/rendering/caching/CacheRecord.ts +6 -2
- package/src/rendering/caching/CacheRecordManager.ts +34 -8
- package/src/rendering/caching/RenderingCache.test.ts +3 -3
- package/src/rendering/caching/RenderingCache.ts +11 -16
- package/src/rendering/caching/RenderingCacheNode.ts +23 -7
- package/src/rendering/caching/testUtils.ts +1 -1
- package/src/rendering/caching/types.ts +2 -7
- package/src/rendering/localization.ts +4 -0
- package/src/rendering/renderers/AbstractRenderer.ts +4 -4
- package/src/rendering/renderers/CanvasRenderer.ts +5 -5
- package/src/rendering/renderers/DummyRenderer.test.ts +2 -2
- package/src/rendering/renderers/DummyRenderer.ts +4 -4
- package/src/rendering/renderers/SVGRenderer.ts +10 -4
- package/src/rendering/renderers/TextOnlyRenderer.ts +17 -6
- package/src/toolbar/HTMLToolbar.ts +1 -0
- package/src/toolbar/icons.ts +157 -137
- package/src/toolbar/localization.ts +4 -2
- package/src/toolbar/makeColorInput.ts +3 -2
- package/src/toolbar/toolbar.css +1 -1
- package/src/toolbar/widgets/ActionButtonWidget.ts +31 -0
- package/src/toolbar/widgets/BaseWidget.ts +2 -0
- package/src/toolbar/widgets/HandToolWidget.ts +3 -3
- package/src/toolbar/widgets/PenWidget.ts +2 -0
- package/src/toolbar/widgets/SelectionWidget.ts +46 -41
- package/src/tools/Eraser.ts +2 -2
- package/src/tools/PanZoom.ts +28 -17
- package/src/tools/Pen.ts +11 -2
- package/src/tools/PipetteTool.ts +2 -0
- package/src/tools/SelectionTool.test.ts +2 -4
- package/src/tools/SelectionTool.ts +52 -24
- package/src/tools/TextTool.ts +2 -2
- package/src/tools/UndoRedoShortcut.test.ts +1 -1
- package/src/types.ts +23 -7
- package/tsconfig.json +4 -1
- package/typedoc.json +20 -0
- package/dist/src/geometry/Mat33.d.ts +0 -32
- 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 '../
|
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 *
|
74
|
+
cacheSize: 500 * 500 * 4 * 150,
|
55
75
|
maxScale: 1.5,
|
56
|
-
minComponentsPerCache:
|
57
|
-
minComponentsToUseCache:
|
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
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
130
|
-
|
152
|
+
this.textRerenderOutput = document.createElement('div');
|
153
|
+
this.textRerenderOutput.setAttribute('aria-live', 'polite');
|
131
154
|
rerenderButton.onclick = () => {
|
132
|
-
this.
|
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,
|
157
|
+
textRendererOutputContainer.replaceChildren(rerenderButton, this.textRerenderOutput);
|
137
158
|
this.editor.createHTMLOverlay(textRendererOutputContainer);
|
138
159
|
}
|
139
|
-
|
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
|
-
|
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 '../../
|
2
|
-
import Rect2 from '../../
|
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 '../../
|
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,
|
1
|
+
import { BeforeDeallocCallback, CacheProps, CacheState } from './types';
|
2
2
|
import CacheRecord from './CacheRecord';
|
3
|
-
import Rect2 from '../../
|
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
|
-
|
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(
|
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,
|
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
|
4
|
+
import { CacheProps } from './types';
|
5
5
|
export default class RenderingCache {
|
6
|
-
private
|
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 '../../
|
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.
|
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
|
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.
|
17
|
+
this.sharedState.currentRenderingCycle++;
|
19
18
|
// If we can't use the cache,
|
20
|
-
if (!this.
|
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.
|
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.
|
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.
|
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 '../../
|
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 '../../
|
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
|
-
|
120
|
-
|
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 <
|
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:
|
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 '../../
|
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
|
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 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 '../../
|
4
|
-
import { PathCommand } from '../../
|
5
|
-
import Rect2 from '../../
|
6
|
-
import { Point2, Vec2 } from '../../
|
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 '../../
|
2
|
-
import { Vec2 } from '../../
|
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 '../../
|
3
|
-
import Rect2 from '../../
|
4
|
-
import { Point2, Vec2 } from '../../
|
5
|
-
import Vec3 from '../../
|
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 '../../
|
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 =
|
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 '../../
|
3
|
-
import Rect2 from '../../
|
4
|
-
import { Point2, Vec2 } from '../../
|
5
|
-
import Vec3 from '../../
|
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 '../../
|
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 '../../
|
4
|
-
import Rect2 from '../../
|
5
|
-
import { Point2, Vec2 } from '../../
|
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
|
2
|
-
import {
|
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 '../../
|
3
|
-
import Rect2 from '../../
|
4
|
-
import Vec3 from '../../
|
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 '../../
|
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
|
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 <
|
46
|
+
return rect.maxDimension < 15 / this.getSizeOfCanvasPixelOnScreen();
|
37
47
|
}
|
38
48
|
drawPoints(..._points) {
|
39
49
|
}
|
@@ -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;
|