js-draw 0.0.10 → 0.1.2
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/CHANGELOG.md +11 -0
- package/dist/bundle.js +1 -1
- package/dist/src/Editor.d.ts +2 -2
- package/dist/src/Editor.js +17 -7
- package/dist/src/EditorImage.d.ts +15 -7
- package/dist/src/EditorImage.js +46 -37
- package/dist/src/Pointer.d.ts +3 -2
- package/dist/src/Pointer.js +12 -3
- package/dist/src/SVGLoader.d.ts +6 -2
- package/dist/src/SVGLoader.js +20 -8
- package/dist/src/Viewport.d.ts +4 -0
- package/dist/src/Viewport.js +51 -0
- package/dist/src/components/AbstractComponent.d.ts +9 -2
- package/dist/src/components/AbstractComponent.js +14 -0
- package/dist/src/components/SVGGlobalAttributesObject.d.ts +1 -1
- package/dist/src/components/SVGGlobalAttributesObject.js +1 -1
- package/dist/src/components/Stroke.d.ts +1 -1
- package/dist/src/components/Stroke.js +1 -1
- package/dist/src/components/UnknownSVGObject.d.ts +1 -1
- package/dist/src/components/UnknownSVGObject.js +1 -1
- package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.d.ts +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.js +1 -1
- package/dist/src/components/builders/LineBuilder.d.ts +1 -1
- package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
- package/dist/src/components/builders/types.d.ts +1 -1
- package/dist/src/geometry/Mat33.js +3 -0
- package/dist/src/geometry/Path.d.ts +1 -1
- package/dist/src/geometry/Path.js +102 -69
- package/dist/src/geometry/Rect2.d.ts +1 -0
- package/dist/src/geometry/Rect2.js +47 -9
- package/dist/src/{Display.d.ts → rendering/Display.d.ts} +5 -2
- package/dist/src/{Display.js → rendering/Display.js} +34 -4
- package/dist/src/rendering/caching/CacheRecord.d.ts +19 -0
- package/dist/src/rendering/caching/CacheRecord.js +52 -0
- package/dist/src/rendering/caching/CacheRecordManager.d.ts +11 -0
- package/dist/src/rendering/caching/CacheRecordManager.js +31 -0
- package/dist/src/rendering/caching/RenderingCache.d.ts +12 -0
- package/dist/src/rendering/caching/RenderingCache.js +42 -0
- package/dist/src/rendering/caching/RenderingCacheNode.d.ts +28 -0
- package/dist/src/rendering/caching/RenderingCacheNode.js +301 -0
- package/dist/src/rendering/caching/testUtils.d.ts +9 -0
- package/dist/src/rendering/caching/testUtils.js +20 -0
- package/dist/src/rendering/caching/types.d.ts +21 -0
- package/dist/src/rendering/caching/types.js +1 -0
- package/dist/src/rendering/{AbstractRenderer.d.ts → renderers/AbstractRenderer.d.ts} +20 -9
- package/dist/src/rendering/{AbstractRenderer.js → renderers/AbstractRenderer.js} +37 -3
- package/dist/src/rendering/{CanvasRenderer.d.ts → renderers/CanvasRenderer.d.ts} +10 -5
- package/dist/src/rendering/{CanvasRenderer.js → renderers/CanvasRenderer.js} +60 -20
- package/dist/src/rendering/{DummyRenderer.d.ts → renderers/DummyRenderer.d.ts} +9 -5
- package/dist/src/rendering/{DummyRenderer.js → renderers/DummyRenderer.js} +35 -4
- package/dist/src/rendering/{SVGRenderer.d.ts → renderers/SVGRenderer.d.ts} +7 -5
- package/dist/src/rendering/{SVGRenderer.js → renderers/SVGRenderer.js} +35 -18
- package/dist/src/testing/createEditor.js +1 -1
- package/dist/src/toolbar/HTMLToolbar.d.ts +2 -1
- package/dist/src/toolbar/HTMLToolbar.js +165 -154
- package/dist/src/toolbar/icons.d.ts +10 -0
- package/dist/src/toolbar/icons.js +180 -0
- package/dist/src/toolbar/localization.d.ts +4 -1
- package/dist/src/toolbar/localization.js +4 -1
- package/dist/src/toolbar/types.d.ts +4 -0
- package/dist/src/tools/PanZoom.d.ts +9 -6
- package/dist/src/tools/PanZoom.js +30 -21
- package/dist/src/tools/Pen.js +8 -3
- package/dist/src/tools/SelectionTool.js +9 -24
- package/dist/src/tools/ToolController.d.ts +5 -6
- package/dist/src/tools/ToolController.js +8 -10
- package/dist/src/tools/localization.d.ts +1 -0
- package/dist/src/tools/localization.js +1 -0
- package/dist/src/types.d.ts +2 -1
- package/package.json +1 -1
- package/src/Editor.ts +19 -8
- package/src/EditorImage.test.ts +2 -2
- package/src/EditorImage.ts +58 -42
- package/src/Pointer.ts +13 -4
- package/src/SVGLoader.ts +36 -10
- package/src/Viewport.ts +68 -0
- package/src/components/AbstractComponent.ts +21 -2
- package/src/components/SVGGlobalAttributesObject.ts +2 -2
- package/src/components/Stroke.ts +2 -2
- package/src/components/UnknownSVGObject.ts +2 -2
- package/src/components/builders/ArrowBuilder.ts +1 -1
- package/src/components/builders/FreehandLineBuilder.ts +2 -2
- package/src/components/builders/LineBuilder.ts +1 -1
- package/src/components/builders/RectangleBuilder.ts +1 -1
- package/src/components/builders/types.ts +1 -1
- package/src/geometry/Mat33.ts +3 -0
- package/src/geometry/Path.fromString.test.ts +94 -4
- package/src/geometry/Path.toString.test.ts +12 -2
- package/src/geometry/Path.ts +107 -71
- package/src/geometry/Rect2.test.ts +47 -8
- package/src/geometry/Rect2.ts +57 -9
- package/src/{Display.ts → rendering/Display.ts} +39 -6
- package/src/rendering/caching/CacheRecord.test.ts +49 -0
- package/src/rendering/caching/CacheRecord.ts +73 -0
- package/src/rendering/caching/CacheRecordManager.ts +45 -0
- package/src/rendering/caching/RenderingCache.test.ts +44 -0
- package/src/rendering/caching/RenderingCache.ts +63 -0
- package/src/rendering/caching/RenderingCacheNode.ts +378 -0
- package/src/rendering/caching/testUtils.ts +35 -0
- package/src/rendering/caching/types.ts +39 -0
- package/src/rendering/{AbstractRenderer.ts → renderers/AbstractRenderer.ts} +57 -9
- package/src/rendering/{CanvasRenderer.ts → renderers/CanvasRenderer.ts} +74 -25
- package/src/rendering/renderers/DummyRenderer.test.ts +43 -0
- package/src/rendering/{DummyRenderer.ts → renderers/DummyRenderer.ts} +50 -7
- package/src/rendering/{SVGRenderer.ts → renderers/SVGRenderer.ts} +39 -23
- package/src/testing/createEditor.ts +1 -1
- package/src/toolbar/HTMLToolbar.ts +199 -170
- package/src/toolbar/icons.ts +203 -0
- package/src/toolbar/localization.ts +9 -2
- package/src/toolbar/toolbar.css +21 -8
- package/src/toolbar/types.ts +5 -0
- package/src/tools/PanZoom.ts +37 -27
- package/src/tools/Pen.ts +7 -3
- package/src/tools/SelectionTool.test.ts +1 -1
- package/src/tools/SelectionTool.ts +12 -33
- package/src/tools/ToolController.ts +3 -5
- package/src/tools/localization.ts +2 -0
- package/src/types.ts +10 -3
- package/tsconfig.json +1 -0
- package/dist/__mocks__/coloris.d.ts +0 -2
- package/dist/__mocks__/coloris.js +0 -5
@@ -0,0 +1,44 @@
|
|
1
|
+
/* @jest-environment jsdom */
|
2
|
+
|
3
|
+
import DummyRenderer from '../renderers/DummyRenderer';
|
4
|
+
import { createCache } from './testUtils';
|
5
|
+
import Stroke from '../../components/Stroke';
|
6
|
+
import Path from '../../geometry/Path';
|
7
|
+
import Color4 from '../../Color4';
|
8
|
+
import EditorImage from '../../EditorImage';
|
9
|
+
import Viewport from '../../Viewport';
|
10
|
+
import Mat33 from '../../geometry/Mat33';
|
11
|
+
|
12
|
+
describe('RenderingCache', () => {
|
13
|
+
const testPath = Path.fromString('M0,0 l100,500 l-20,20 L-100,-100');
|
14
|
+
const testStroke = new Stroke([ testPath.toRenderable({ fill: Color4.purple }) ]);
|
15
|
+
|
16
|
+
it('should create a root node large enough to contain the viewport', () => {
|
17
|
+
let lastRenderer: DummyRenderer|null = null;
|
18
|
+
let allocdRenderers: number = 0;
|
19
|
+
|
20
|
+
const { editor, cache } = createCache((renderer) => {
|
21
|
+
allocdRenderers ++;
|
22
|
+
lastRenderer = renderer;
|
23
|
+
});
|
24
|
+
const screenRenderer = editor.display.getDryInkRenderer() as DummyRenderer;
|
25
|
+
|
26
|
+
// No objects: Should not create a renderer.
|
27
|
+
expect(lastRenderer).toBeNull();
|
28
|
+
editor.image.renderWithCache(screenRenderer, cache, editor.viewport);
|
29
|
+
expect(lastRenderer).toBeNull();
|
30
|
+
|
31
|
+
editor.dispatch(new EditorImage.AddElementCommand(testStroke));
|
32
|
+
editor.image.renderWithCache(screenRenderer, cache, editor.viewport);
|
33
|
+
|
34
|
+
expect(allocdRenderers).toBeGreaterThanOrEqual(1);
|
35
|
+
expect(lastRenderer).not.toBeNull();
|
36
|
+
expect(lastRenderer!.renderedPathCount).toBe(1);
|
37
|
+
|
38
|
+
editor.dispatch(new Viewport.ViewportTransform(Mat33.scaling2D(0.1)));
|
39
|
+
editor.image.renderWithCache(screenRenderer, cache, editor.viewport);
|
40
|
+
expect(allocdRenderers).toBe(1);
|
41
|
+
expect(lastRenderer!.renderedPathCount).toBe(1);
|
42
|
+
expect(screenRenderer.renderedPathCount).toBeGreaterThanOrEqual(1);
|
43
|
+
});
|
44
|
+
});
|
@@ -0,0 +1,63 @@
|
|
1
|
+
import { ImageNode } from '../../EditorImage';
|
2
|
+
import Rect2 from '../../geometry/Rect2';
|
3
|
+
import Viewport from '../../Viewport';
|
4
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
5
|
+
import RenderingCacheNode from './RenderingCacheNode';
|
6
|
+
import { CacheRecordManager } from './CacheRecordManager';
|
7
|
+
import { CacheProps, CacheState, PartialCacheState } from './types';
|
8
|
+
|
9
|
+
export default class RenderingCache {
|
10
|
+
private partialSharedState: PartialCacheState;
|
11
|
+
private recordManager: CacheRecordManager;
|
12
|
+
private rootNode: RenderingCacheNode|null;
|
13
|
+
|
14
|
+
public constructor(cacheProps: CacheProps) {
|
15
|
+
this.partialSharedState = {
|
16
|
+
props: cacheProps,
|
17
|
+
currentRenderingCycle: 0,
|
18
|
+
};
|
19
|
+
this.recordManager = new CacheRecordManager(this.partialSharedState);
|
20
|
+
}
|
21
|
+
|
22
|
+
public getSharedState(): CacheState {
|
23
|
+
return {
|
24
|
+
...this.partialSharedState,
|
25
|
+
recordManager: this.recordManager,
|
26
|
+
};
|
27
|
+
}
|
28
|
+
|
29
|
+
public render(screenRenderer: AbstractRenderer, image: ImageNode, viewport: Viewport) {
|
30
|
+
const visibleRect = viewport.visibleRect;
|
31
|
+
this.partialSharedState.currentRenderingCycle ++;
|
32
|
+
|
33
|
+
// If we can't use the cache,
|
34
|
+
if (!this.partialSharedState.props.isOfCorrectType(screenRenderer)) {
|
35
|
+
image.render(screenRenderer, visibleRect);
|
36
|
+
return;
|
37
|
+
}
|
38
|
+
|
39
|
+
if (!this.rootNode) {
|
40
|
+
// Adjust the node so that it has the correct aspect ratio
|
41
|
+
const res = this.partialSharedState.props.blockResolution;
|
42
|
+
|
43
|
+
const topLeft = visibleRect.topLeft;
|
44
|
+
this.rootNode = new RenderingCacheNode(
|
45
|
+
new Rect2(topLeft.x, topLeft.y, res.x, res.y),
|
46
|
+
this.getSharedState()
|
47
|
+
);
|
48
|
+
}
|
49
|
+
|
50
|
+
while (!this.rootNode!.region.containsRect(visibleRect)) {
|
51
|
+
this.rootNode = this.rootNode!.generateParent();
|
52
|
+
}
|
53
|
+
|
54
|
+
this.rootNode = this.rootNode!.smallestChildContaining(visibleRect) ?? this.rootNode;
|
55
|
+
|
56
|
+
const visibleLeaves = image.getLeavesIntersectingRegion(viewport.visibleRect, rect => screenRenderer.isTooSmallToRender(rect));
|
57
|
+
if (visibleLeaves.length > this.partialSharedState.props.minComponentsToUseCache) {
|
58
|
+
this.rootNode!.renderItems(screenRenderer, [ image ], viewport);
|
59
|
+
} else {
|
60
|
+
image.render(screenRenderer, visibleRect);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
@@ -0,0 +1,378 @@
|
|
1
|
+
|
2
|
+
// A cache record with sub-nodes.
|
3
|
+
|
4
|
+
import Color4 from '../../Color4';
|
5
|
+
import { ImageNode, sortLeavesByZIndex } from '../../EditorImage';
|
6
|
+
import Rect2 from '../../geometry/Rect2';
|
7
|
+
import Viewport from '../../Viewport';
|
8
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
9
|
+
import CacheRecord from './CacheRecord';
|
10
|
+
import { CacheState } from './types';
|
11
|
+
|
12
|
+
// 3x3 divisions for each node.
|
13
|
+
const cacheDivisionSize = 3;
|
14
|
+
|
15
|
+
// True: Show rendering updates.
|
16
|
+
const debugMode = false;
|
17
|
+
|
18
|
+
export default class RenderingCacheNode {
|
19
|
+
// invariant: instantiatedChildren.length === 9
|
20
|
+
private instantiatedChildren: RenderingCacheNode[] = [];
|
21
|
+
private parent: RenderingCacheNode|null = null;
|
22
|
+
|
23
|
+
private cachedRenderer: CacheRecord|null = null;
|
24
|
+
// invariant: sortedInAscendingOrder(renderedIds)
|
25
|
+
private renderedIds: Array<number> = [];
|
26
|
+
private renderedMaxZIndex: number|null = null;
|
27
|
+
|
28
|
+
public constructor(
|
29
|
+
public readonly region: Rect2, private readonly cacheState: CacheState
|
30
|
+
) {
|
31
|
+
}
|
32
|
+
|
33
|
+
// Creates a previous layer of the cache tree and adds this as a child near the
|
34
|
+
// center of the previous layer's children.
|
35
|
+
// Returns this' parent if it already exists.
|
36
|
+
public generateParent(): RenderingCacheNode {
|
37
|
+
if (this.parent) {
|
38
|
+
return this.parent;
|
39
|
+
}
|
40
|
+
|
41
|
+
const parentRegion = Rect2.fromCorners(
|
42
|
+
this.region.topLeft.minus(this.region.size),
|
43
|
+
this.region.bottomRight.plus(this.region.size)
|
44
|
+
);
|
45
|
+
const parent = new RenderingCacheNode(parentRegion, this.cacheState);
|
46
|
+
parent.generateChildren();
|
47
|
+
|
48
|
+
// Ensure the new node is matches the middle child's region.
|
49
|
+
const checkTolerance = this.region.maxDimension / 100;
|
50
|
+
const middleChildIdx = (parent.instantiatedChildren.length - 1) / 2;
|
51
|
+
if (!parent.instantiatedChildren[middleChildIdx].region.eq(this.region, checkTolerance)) {
|
52
|
+
console.error(parent.instantiatedChildren[middleChildIdx].region, '≠', this.region);
|
53
|
+
throw new Error('Logic error: [this] is not contained within its parent\'s center child');
|
54
|
+
}
|
55
|
+
|
56
|
+
// Replace the middle child
|
57
|
+
parent.instantiatedChildren[middleChildIdx] = this;
|
58
|
+
this.parent = parent;
|
59
|
+
|
60
|
+
return parent;
|
61
|
+
}
|
62
|
+
|
63
|
+
// Generates children, if missing.
|
64
|
+
private generateChildren() {
|
65
|
+
if (this.instantiatedChildren.length === 0) {
|
66
|
+
const childRects = this.region.divideIntoGrid(cacheDivisionSize, cacheDivisionSize);
|
67
|
+
|
68
|
+
for (const rect of childRects) {
|
69
|
+
const child = new RenderingCacheNode(rect, this.cacheState);
|
70
|
+
child.parent = this;
|
71
|
+
this.instantiatedChildren.push(child);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
this.checkRep();
|
75
|
+
}
|
76
|
+
|
77
|
+
// Returns CacheNodes directly contained within this.
|
78
|
+
private getChildren(): RenderingCacheNode[] {
|
79
|
+
this.checkRep();
|
80
|
+
this.generateChildren();
|
81
|
+
|
82
|
+
return this.instantiatedChildren;
|
83
|
+
}
|
84
|
+
|
85
|
+
public smallestChildContaining(rect: Rect2): RenderingCacheNode|null {
|
86
|
+
const largerThanChildren = rect.maxDimension > this.region.maxDimension / cacheDivisionSize;
|
87
|
+
if (!this.region.containsRect(rect) || largerThanChildren) {
|
88
|
+
return null;
|
89
|
+
}
|
90
|
+
|
91
|
+
for (const child of this.getChildren()) {
|
92
|
+
if (child.region.containsRect(rect)) {
|
93
|
+
return child.smallestChildContaining(rect) ?? child;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
return null;
|
98
|
+
}
|
99
|
+
|
100
|
+
// => [true] iff [this] can be rendered without too much scaling
|
101
|
+
private renderingWouldBeHighEnoughResolution(viewport: Viewport) {
|
102
|
+
// Determine how 1px in this corresponds to 1px on the canvas.
|
103
|
+
// this.region.w is in canvas units. Thus,
|
104
|
+
const sizeOfThisPixelOnCanvas = this.region.w / this.cacheState.props.blockResolution.x;
|
105
|
+
const sizeOfThisPixelOnScreen = viewport.getScaleFactor() * sizeOfThisPixelOnCanvas;
|
106
|
+
|
107
|
+
if (sizeOfThisPixelOnScreen > this.cacheState.props.maxScale) {
|
108
|
+
return false;
|
109
|
+
}
|
110
|
+
return true;
|
111
|
+
}
|
112
|
+
|
113
|
+
// => [true] if all children of this can be rendered from their caches.
|
114
|
+
private allChildrenCanRender(viewport: Viewport, leavesSortedById: ImageNode[]) {
|
115
|
+
if (this.instantiatedChildren.length === 0) {
|
116
|
+
return false;
|
117
|
+
}
|
118
|
+
|
119
|
+
for (const child of this.instantiatedChildren) {
|
120
|
+
if (!child.region.intersects(viewport.visibleRect)) {
|
121
|
+
continue;
|
122
|
+
}
|
123
|
+
|
124
|
+
if (!child.renderingIsUpToDate(this.idsOfIntersecting(leavesSortedById))) {
|
125
|
+
return false;
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
return true;
|
130
|
+
}
|
131
|
+
|
132
|
+
private computeSortedByLeafIds(leaves: ImageNode[]) {
|
133
|
+
const ids = leaves.slice();
|
134
|
+
ids.sort((a, b) => a.getId() - b.getId());
|
135
|
+
return ids;
|
136
|
+
}
|
137
|
+
|
138
|
+
// Returns a list of the ids of the nodes intersecting this
|
139
|
+
private idsOfIntersecting(nodes: ImageNode[]) {
|
140
|
+
const result = [];
|
141
|
+
for (const node of nodes) {
|
142
|
+
if (node.getBBox().intersects(this.region)) {
|
143
|
+
result.push(node.getId());
|
144
|
+
}
|
145
|
+
}
|
146
|
+
return result;
|
147
|
+
}
|
148
|
+
|
149
|
+
private renderingIsUpToDate(sortedIds: number[]) {
|
150
|
+
if (this.cachedRenderer === null || sortedIds.length !== this.renderedIds.length) {
|
151
|
+
return false;
|
152
|
+
}
|
153
|
+
|
154
|
+
for (let i = 0; i < sortedIds.length; i++) {
|
155
|
+
if (sortedIds[i] !== this.renderedIds[i]) {
|
156
|
+
return false;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
return true;
|
161
|
+
}
|
162
|
+
|
163
|
+
// Render all [items] within [viewport]
|
164
|
+
public renderItems(screenRenderer: AbstractRenderer, items: ImageNode[], viewport: Viewport) {
|
165
|
+
if (
|
166
|
+
!viewport.visibleRect.intersects(this.region)
|
167
|
+
|| items.length === 0
|
168
|
+
) {
|
169
|
+
return;
|
170
|
+
}
|
171
|
+
|
172
|
+
const newItems = [];
|
173
|
+
// Divide [items] until nodes are leaves or smaller than this
|
174
|
+
for (const item of items) {
|
175
|
+
const bbox = item.getBBox();
|
176
|
+
if (!bbox.intersects(this.region)) {
|
177
|
+
continue;
|
178
|
+
}
|
179
|
+
|
180
|
+
if (bbox.maxDimension >= this.region.maxDimension) {
|
181
|
+
newItems.push(...item.getChildrenOrSelfIntersectingRegion(this.region));
|
182
|
+
} else {
|
183
|
+
newItems.push(item);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
items = newItems;
|
187
|
+
|
188
|
+
// Can we cache at all?
|
189
|
+
if (!this.cacheState.props.isOfCorrectType(screenRenderer)) {
|
190
|
+
items.forEach(item => item.render(screenRenderer, viewport.visibleRect));
|
191
|
+
return;
|
192
|
+
}
|
193
|
+
|
194
|
+
if (debugMode) {
|
195
|
+
screenRenderer.drawRect(this.region, 0.5 * viewport.getSizeOfPixelOnCanvas(), { fill: Color4.yellow });
|
196
|
+
}
|
197
|
+
|
198
|
+
// Could we render direclty from [this] or do we need to recurse?
|
199
|
+
const couldRender = this.renderingWouldBeHighEnoughResolution(viewport);
|
200
|
+
if (!couldRender) {
|
201
|
+
for (const child of this.getChildren()) {
|
202
|
+
child.renderItems(screenRenderer, items.filter(item => {
|
203
|
+
return item.getBBox().intersects(child.region);
|
204
|
+
}), viewport);
|
205
|
+
}
|
206
|
+
} else {
|
207
|
+
// Determine whether we already have rendered the items
|
208
|
+
const leaves = [];
|
209
|
+
for (const item of items) {
|
210
|
+
leaves.push(
|
211
|
+
...item.getLeavesIntersectingRegion(
|
212
|
+
this.region, rect => rect.w / this.region.w < 2 / this.cacheState.props.blockResolution.x,
|
213
|
+
)
|
214
|
+
);
|
215
|
+
}
|
216
|
+
sortLeavesByZIndex(leaves);
|
217
|
+
const leavesByIds = this.computeSortedByLeafIds(leaves);
|
218
|
+
|
219
|
+
// No intersecting leaves? No need to render
|
220
|
+
if (leavesByIds.length === 0) {
|
221
|
+
return;
|
222
|
+
}
|
223
|
+
|
224
|
+
const leafIds = leavesByIds.map(leaf => leaf.getId());
|
225
|
+
|
226
|
+
let thisRenderer;
|
227
|
+
if (!this.renderingIsUpToDate(leafIds)) {
|
228
|
+
if (this.allChildrenCanRender(viewport, leavesByIds)) {
|
229
|
+
for (const child of this.getChildren()) {
|
230
|
+
child.renderItems(screenRenderer, items, viewport);
|
231
|
+
}
|
232
|
+
return;
|
233
|
+
}
|
234
|
+
|
235
|
+
// Is it worth it to render the items?
|
236
|
+
// TODO: Replace this with something performace based.
|
237
|
+
// TODO: Determine whether it is 'worth it' to cache this depending on rendering time.
|
238
|
+
if (leavesByIds.length > this.cacheState.props.minComponentsPerCache) {
|
239
|
+
let fullRerenderNeeded = true;
|
240
|
+
if (!this.cachedRenderer) {
|
241
|
+
this.cachedRenderer = this.cacheState.recordManager.allocCanvas(
|
242
|
+
this.region,
|
243
|
+
() => this.onRegionDealloc()
|
244
|
+
);
|
245
|
+
} else if (leavesByIds.length > this.renderedIds.length && this.renderedMaxZIndex !== null) {
|
246
|
+
// We often don't need to do a full re-render even if something's changed.
|
247
|
+
// Check whether we can just draw on top of the existing cache.
|
248
|
+
const newLeaves = [];
|
249
|
+
|
250
|
+
let minNewZIndex: number|null = null;
|
251
|
+
|
252
|
+
for (let i = 0; i < leavesByIds.length; i++) {
|
253
|
+
const leaf = leavesByIds[i];
|
254
|
+
const content = leaf.getContent()!;
|
255
|
+
|
256
|
+
const zIndex = content.getZIndex();
|
257
|
+
if (i >= this.renderedIds.length || leaf.getId() !== this.renderedIds[i]) {
|
258
|
+
newLeaves.push(leaf);
|
259
|
+
|
260
|
+
if (minNewZIndex === null || zIndex < minNewZIndex) {
|
261
|
+
minNewZIndex = zIndex;
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
if (minNewZIndex !== null && minNewZIndex > this.renderedMaxZIndex!) {
|
267
|
+
fullRerenderNeeded = false;
|
268
|
+
thisRenderer = this.cachedRenderer.startRender();
|
269
|
+
|
270
|
+
// Looping is faster than re-sorting.
|
271
|
+
for (let i = 0; i < leaves.length; i++) {
|
272
|
+
const leaf = leaves[i];
|
273
|
+
const zIndex = leaf.getContent()!.getZIndex();
|
274
|
+
|
275
|
+
if (zIndex > this.renderedMaxZIndex) {
|
276
|
+
leaf.render(thisRenderer, this.region);
|
277
|
+
this.renderedMaxZIndex = zIndex;
|
278
|
+
}
|
279
|
+
}
|
280
|
+
|
281
|
+
if (debugMode) {
|
282
|
+
screenRenderer.drawRect(this.region, viewport.getSizeOfPixelOnCanvas(), { fill: Color4.clay });
|
283
|
+
}
|
284
|
+
}
|
285
|
+
}
|
286
|
+
|
287
|
+
if (fullRerenderNeeded) {
|
288
|
+
thisRenderer = this.cachedRenderer.startRender();
|
289
|
+
thisRenderer.clear();
|
290
|
+
|
291
|
+
this.renderedMaxZIndex = null;
|
292
|
+
for (const leaf of leaves) {
|
293
|
+
const content = leaf.getContent()!;
|
294
|
+
this.renderedMaxZIndex ??= content.getZIndex();
|
295
|
+
this.renderedMaxZIndex = Math.max(this.renderedMaxZIndex, content.getZIndex());
|
296
|
+
|
297
|
+
leaf.render(thisRenderer, this.region);
|
298
|
+
}
|
299
|
+
|
300
|
+
if (debugMode) {
|
301
|
+
screenRenderer.drawRect(this.region, viewport.getSizeOfPixelOnCanvas(), { fill: Color4.red });
|
302
|
+
}
|
303
|
+
}
|
304
|
+
this.renderedIds = leafIds;
|
305
|
+
} else {
|
306
|
+
this.cachedRenderer?.dealloc();
|
307
|
+
|
308
|
+
// Slightly increase the clip region to prevent seams.
|
309
|
+
// Divide by two because grownBy expands the rectangle on all sides.
|
310
|
+
const pixelSize = viewport.getSizeOfPixelOnCanvas();
|
311
|
+
const expandedRegion = new Rect2(
|
312
|
+
this.region.x, this.region.y,
|
313
|
+
this.region.w + pixelSize, this.region.h + pixelSize
|
314
|
+
);
|
315
|
+
|
316
|
+
const clip = true;
|
317
|
+
screenRenderer.startObject(expandedRegion, clip);
|
318
|
+
for (const leaf of leaves) {
|
319
|
+
leaf.render(screenRenderer, this.region.intersection(viewport.visibleRect)!);
|
320
|
+
}
|
321
|
+
|
322
|
+
screenRenderer.endObject();
|
323
|
+
}
|
324
|
+
} else {
|
325
|
+
thisRenderer = this.cachedRenderer!.startRender();
|
326
|
+
}
|
327
|
+
|
328
|
+
if (thisRenderer) {
|
329
|
+
const transformMat = this.cachedRenderer!.getTransform(this.region).inverse();
|
330
|
+
screenRenderer.renderFromOtherOfSameType(transformMat, thisRenderer);
|
331
|
+
}
|
332
|
+
|
333
|
+
// Can we clean up this' children? (Are they unused?)
|
334
|
+
if (this.instantiatedChildren.every(child => child.isEmpty())) {
|
335
|
+
this.instantiatedChildren = [];
|
336
|
+
}
|
337
|
+
}
|
338
|
+
|
339
|
+
this.checkRep();
|
340
|
+
}
|
341
|
+
|
342
|
+
// Returns true iff this/its children have no cached state.
|
343
|
+
private isEmpty(): boolean {
|
344
|
+
if (this.cachedRenderer !== null) {
|
345
|
+
return false;
|
346
|
+
}
|
347
|
+
|
348
|
+
return this.instantiatedChildren.every(child => child.isEmpty());
|
349
|
+
}
|
350
|
+
|
351
|
+
private onRegionDealloc() {
|
352
|
+
this.cachedRenderer = null;
|
353
|
+
if (this.isEmpty()) {
|
354
|
+
this.instantiatedChildren = [];
|
355
|
+
}
|
356
|
+
}
|
357
|
+
|
358
|
+
private checkRep() {
|
359
|
+
if (this.instantiatedChildren.length !== cacheDivisionSize * cacheDivisionSize && this.instantiatedChildren.length !== 0) {
|
360
|
+
throw new Error('Repcheck: Wrong number of children');
|
361
|
+
}
|
362
|
+
|
363
|
+
if (this.renderedIds[1] !== undefined && this.renderedIds[0] >= this.renderedIds[1]) {
|
364
|
+
console.error(this.renderedIds);
|
365
|
+
throw new Error('Repcheck: First two ids are not in ascending order!');
|
366
|
+
}
|
367
|
+
|
368
|
+
for (const child of this.instantiatedChildren) {
|
369
|
+
if (child.parent !== this) {
|
370
|
+
throw new Error('Children should be linked to their parents!');
|
371
|
+
}
|
372
|
+
}
|
373
|
+
|
374
|
+
if (this.cachedRenderer && !this.cachedRenderer.isAllocd()) {
|
375
|
+
throw new Error('this\' cachedRenderer != null, but is dealloc\'d');
|
376
|
+
}
|
377
|
+
}
|
378
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import { Vec2 } from '../../geometry/Vec2';
|
2
|
+
import DummyRenderer from '../renderers/DummyRenderer';
|
3
|
+
import createEditor from '../../testing/createEditor';
|
4
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
5
|
+
import RenderingCache from './RenderingCache';
|
6
|
+
import { CacheProps } from './types';
|
7
|
+
|
8
|
+
type RenderAllocCallback = (renderer: DummyRenderer)=> void;
|
9
|
+
|
10
|
+
// Override any default test options with [cacheOptions]
|
11
|
+
export const createCache = (onRenderAlloc?: RenderAllocCallback, cacheOptions?: Partial<CacheProps>) => {
|
12
|
+
const editor = createEditor();
|
13
|
+
|
14
|
+
const cache = new RenderingCache({
|
15
|
+
createRenderer() {
|
16
|
+
const renderer = new DummyRenderer(editor.viewport);
|
17
|
+
onRenderAlloc?.(renderer);
|
18
|
+
return renderer;
|
19
|
+
},
|
20
|
+
isOfCorrectType(renderer: AbstractRenderer) {
|
21
|
+
return renderer instanceof DummyRenderer;
|
22
|
+
},
|
23
|
+
blockResolution: Vec2.of(500, 500),
|
24
|
+
cacheSize: 500 * 10 * 4,
|
25
|
+
maxScale: 2,
|
26
|
+
minComponentsPerCache: 0,
|
27
|
+
minComponentsToUseCache: 0,
|
28
|
+
...cacheOptions
|
29
|
+
});
|
30
|
+
|
31
|
+
return {
|
32
|
+
cache,
|
33
|
+
editor
|
34
|
+
};
|
35
|
+
};
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { Vec2 } from '../../geometry/Vec2';
|
2
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
3
|
+
import { CacheRecordManager } from './CacheRecordManager';
|
4
|
+
|
5
|
+
|
6
|
+
export type CacheAddress = number;
|
7
|
+
export type BeforeDeallocCallback = ()=>void;
|
8
|
+
|
9
|
+
|
10
|
+
export interface CacheProps {
|
11
|
+
createRenderer(): AbstractRenderer;
|
12
|
+
// Returns whether the cache can be rendered onto [renderer].
|
13
|
+
isOfCorrectType(renderer: AbstractRenderer): boolean;
|
14
|
+
|
15
|
+
blockResolution: Vec2;
|
16
|
+
cacheSize: number;
|
17
|
+
|
18
|
+
// Maximum amount a cached image can be scaled without a re-render
|
19
|
+
// (larger numbers = blurrier, but faster)
|
20
|
+
maxScale: number;
|
21
|
+
|
22
|
+
// Minimum component count to cache, rather than just re-render each time.
|
23
|
+
minComponentsPerCache: number;
|
24
|
+
|
25
|
+
// Minimum number of strokes/etc. to use the cache to render, isntead of
|
26
|
+
// rendering directly.
|
27
|
+
minComponentsToUseCache: number;
|
28
|
+
}
|
29
|
+
|
30
|
+
// CacheRecordManager relies on a partial copy of the shared state. Thus,
|
31
|
+
// we need to separate partial/non-partial state.
|
32
|
+
export interface PartialCacheState {
|
33
|
+
currentRenderingCycle: number;
|
34
|
+
props: CacheProps;
|
35
|
+
}
|
36
|
+
|
37
|
+
export interface CacheState extends PartialCacheState {
|
38
|
+
recordManager: CacheRecordManager;
|
39
|
+
}
|
@@ -1,8 +1,10 @@
|
|
1
|
-
import Color4 from '
|
2
|
-
import
|
3
|
-
import
|
4
|
-
import {
|
5
|
-
import
|
1
|
+
import Color4 from '../../Color4';
|
2
|
+
import { LoadSaveDataTable } from '../../components/AbstractComponent';
|
3
|
+
import Mat33 from '../../geometry/Mat33';
|
4
|
+
import Path, { PathCommand, PathCommandType } from '../../geometry/Path';
|
5
|
+
import Rect2 from '../../geometry/Rect2';
|
6
|
+
import { Point2, Vec2 } from '../../geometry/Vec2';
|
7
|
+
import Viewport from '../../Viewport';
|
6
8
|
|
7
9
|
export interface RenderingStyle {
|
8
10
|
fill: Color4;
|
@@ -25,7 +27,14 @@ const stylesEqual = (a: RenderingStyle, b: RenderingStyle) => {
|
|
25
27
|
};
|
26
28
|
|
27
29
|
export default abstract class AbstractRenderer {
|
28
|
-
|
30
|
+
// If null, this' transformation is linked to the Viewport
|
31
|
+
private selfTransform: Mat33|null = null;
|
32
|
+
|
33
|
+
protected constructor(private viewport: Viewport) { }
|
34
|
+
|
35
|
+
// this.canvasToScreen, etc. should be used instead of the corresponding
|
36
|
+
// methods on Viewport.
|
37
|
+
protected getViewport(): Viewport { return this.viewport; }
|
29
38
|
|
30
39
|
// Returns the size of the rendered region of this on
|
31
40
|
// the display (in pixels).
|
@@ -43,9 +52,13 @@ export default abstract class AbstractRenderer {
|
|
43
52
|
controlPoint: Point2, endPoint: Point2,
|
44
53
|
): void;
|
45
54
|
|
55
|
+
// Returns true iff the given rectangle is so small, rendering anything within
|
56
|
+
// it has no effect on the image.
|
57
|
+
public abstract isTooSmallToRender(rect: Rect2): boolean;
|
58
|
+
|
46
59
|
public setDraftMode(_draftMode: boolean) { }
|
47
60
|
|
48
|
-
|
61
|
+
protected objectLevel: number = 0;
|
49
62
|
private currentPaths: RenderablePathSpec[]|null = null;
|
50
63
|
private flushPath() {
|
51
64
|
if (!this.currentPaths) {
|
@@ -110,12 +123,13 @@ export default abstract class AbstractRenderer {
|
|
110
123
|
}
|
111
124
|
|
112
125
|
// Note the start/end of an object with the given bounding box.
|
113
|
-
|
126
|
+
// Renderers are not required to support [clip]
|
127
|
+
public startObject(_boundingBox: Rect2, _clip?: boolean) {
|
114
128
|
this.currentPaths = [];
|
115
129
|
this.objectLevel ++;
|
116
130
|
}
|
117
131
|
|
118
|
-
public endObject() {
|
132
|
+
public endObject(_loaderData?: LoadSaveDataTable) {
|
119
133
|
// Render the paths all at once
|
120
134
|
this.flushPath();
|
121
135
|
this.currentPaths = null;
|
@@ -134,4 +148,38 @@ export default abstract class AbstractRenderer {
|
|
134
148
|
|
135
149
|
// Draw a representation of [points]. Intended for debugging.
|
136
150
|
public abstract drawPoints(...points: Point2[]): void;
|
151
|
+
|
152
|
+
|
153
|
+
// Returns true iff other can be rendered onto this without data loss.
|
154
|
+
public canRenderFromWithoutDataLoss(_other: AbstractRenderer): boolean {
|
155
|
+
return false;
|
156
|
+
}
|
157
|
+
|
158
|
+
// MUST throw if other and this are not of the same base class.
|
159
|
+
public renderFromOtherOfSameType(_renderTo: Mat33, other: AbstractRenderer) {
|
160
|
+
throw new Error(`Unable to render from ${other}: Not implemented`);
|
161
|
+
}
|
162
|
+
|
163
|
+
// Set a transformation to apply to things before rendering,
|
164
|
+
// replacing the viewport's transform.
|
165
|
+
public setTransform(transform: Mat33|null) {
|
166
|
+
this.selfTransform = transform;
|
167
|
+
}
|
168
|
+
|
169
|
+
// Get the matrix that transforms a vector on the canvas to a vector on this'
|
170
|
+
// rendering target.
|
171
|
+
public getCanvasToScreenTransform(): Mat33 {
|
172
|
+
if (this.selfTransform) {
|
173
|
+
return this.selfTransform;
|
174
|
+
}
|
175
|
+
return this.viewport.canvasToScreenTransform;
|
176
|
+
}
|
177
|
+
|
178
|
+
public canvasToScreen(vec: Vec2): Vec2 {
|
179
|
+
return this.getCanvasToScreenTransform().transformVec2(vec);
|
180
|
+
}
|
181
|
+
|
182
|
+
public getSizeOfCanvasPixelOnScreen(): number {
|
183
|
+
return this.getCanvasToScreenTransform().transformVec3(Vec2.unitX).length();
|
184
|
+
}
|
137
185
|
}
|