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,42 @@
|
|
1
|
+
import Rect2 from '../../geometry/Rect2';
|
2
|
+
import RenderingCacheNode from './RenderingCacheNode';
|
3
|
+
import { CacheRecordManager } from './CacheRecordManager';
|
4
|
+
export default class RenderingCache {
|
5
|
+
constructor(cacheProps) {
|
6
|
+
this.partialSharedState = {
|
7
|
+
props: cacheProps,
|
8
|
+
currentRenderingCycle: 0,
|
9
|
+
};
|
10
|
+
this.recordManager = new CacheRecordManager(this.partialSharedState);
|
11
|
+
}
|
12
|
+
getSharedState() {
|
13
|
+
return Object.assign(Object.assign({}, this.partialSharedState), { recordManager: this.recordManager });
|
14
|
+
}
|
15
|
+
render(screenRenderer, image, viewport) {
|
16
|
+
var _a;
|
17
|
+
const visibleRect = viewport.visibleRect;
|
18
|
+
this.partialSharedState.currentRenderingCycle++;
|
19
|
+
// If we can't use the cache,
|
20
|
+
if (!this.partialSharedState.props.isOfCorrectType(screenRenderer)) {
|
21
|
+
image.render(screenRenderer, visibleRect);
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
if (!this.rootNode) {
|
25
|
+
// Adjust the node so that it has the correct aspect ratio
|
26
|
+
const res = this.partialSharedState.props.blockResolution;
|
27
|
+
const topLeft = visibleRect.topLeft;
|
28
|
+
this.rootNode = new RenderingCacheNode(new Rect2(topLeft.x, topLeft.y, res.x, res.y), this.getSharedState());
|
29
|
+
}
|
30
|
+
while (!this.rootNode.region.containsRect(visibleRect)) {
|
31
|
+
this.rootNode = this.rootNode.generateParent();
|
32
|
+
}
|
33
|
+
this.rootNode = (_a = this.rootNode.smallestChildContaining(visibleRect)) !== null && _a !== void 0 ? _a : this.rootNode;
|
34
|
+
const visibleLeaves = image.getLeavesIntersectingRegion(viewport.visibleRect, rect => screenRenderer.isTooSmallToRender(rect));
|
35
|
+
if (visibleLeaves.length > this.partialSharedState.props.minComponentsToUseCache) {
|
36
|
+
this.rootNode.renderItems(screenRenderer, [image], viewport);
|
37
|
+
}
|
38
|
+
else {
|
39
|
+
image.render(screenRenderer, visibleRect);
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { ImageNode } from '../../EditorImage';
|
2
|
+
import Rect2 from '../../geometry/Rect2';
|
3
|
+
import Viewport from '../../Viewport';
|
4
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
5
|
+
import { CacheState } from './types';
|
6
|
+
export default class RenderingCacheNode {
|
7
|
+
readonly region: Rect2;
|
8
|
+
private readonly cacheState;
|
9
|
+
private instantiatedChildren;
|
10
|
+
private parent;
|
11
|
+
private cachedRenderer;
|
12
|
+
private renderedIds;
|
13
|
+
private renderedMaxZIndex;
|
14
|
+
constructor(region: Rect2, cacheState: CacheState);
|
15
|
+
generateParent(): RenderingCacheNode;
|
16
|
+
private generateChildren;
|
17
|
+
private getChildren;
|
18
|
+
smallestChildContaining(rect: Rect2): RenderingCacheNode | null;
|
19
|
+
private renderingWouldBeHighEnoughResolution;
|
20
|
+
private allChildrenCanRender;
|
21
|
+
private computeSortedByLeafIds;
|
22
|
+
private idsOfIntersecting;
|
23
|
+
private renderingIsUpToDate;
|
24
|
+
renderItems(screenRenderer: AbstractRenderer, items: ImageNode[], viewport: Viewport): void;
|
25
|
+
private isEmpty;
|
26
|
+
private onRegionDealloc;
|
27
|
+
private checkRep;
|
28
|
+
}
|
@@ -0,0 +1,301 @@
|
|
1
|
+
// A cache record with sub-nodes.
|
2
|
+
import Color4 from '../../Color4';
|
3
|
+
import { sortLeavesByZIndex } from '../../EditorImage';
|
4
|
+
import Rect2 from '../../geometry/Rect2';
|
5
|
+
// 3x3 divisions for each node.
|
6
|
+
const cacheDivisionSize = 3;
|
7
|
+
// True: Show rendering updates.
|
8
|
+
const debugMode = false;
|
9
|
+
export default class RenderingCacheNode {
|
10
|
+
constructor(region, cacheState) {
|
11
|
+
this.region = region;
|
12
|
+
this.cacheState = cacheState;
|
13
|
+
// invariant: instantiatedChildren.length === 9
|
14
|
+
this.instantiatedChildren = [];
|
15
|
+
this.parent = null;
|
16
|
+
this.cachedRenderer = null;
|
17
|
+
// invariant: sortedInAscendingOrder(renderedIds)
|
18
|
+
this.renderedIds = [];
|
19
|
+
this.renderedMaxZIndex = null;
|
20
|
+
}
|
21
|
+
// Creates a previous layer of the cache tree and adds this as a child near the
|
22
|
+
// center of the previous layer's children.
|
23
|
+
// Returns this' parent if it already exists.
|
24
|
+
generateParent() {
|
25
|
+
if (this.parent) {
|
26
|
+
return this.parent;
|
27
|
+
}
|
28
|
+
const parentRegion = Rect2.fromCorners(this.region.topLeft.minus(this.region.size), this.region.bottomRight.plus(this.region.size));
|
29
|
+
const parent = new RenderingCacheNode(parentRegion, this.cacheState);
|
30
|
+
parent.generateChildren();
|
31
|
+
// Ensure the new node is matches the middle child's region.
|
32
|
+
const checkTolerance = this.region.maxDimension / 100;
|
33
|
+
const middleChildIdx = (parent.instantiatedChildren.length - 1) / 2;
|
34
|
+
if (!parent.instantiatedChildren[middleChildIdx].region.eq(this.region, checkTolerance)) {
|
35
|
+
console.error(parent.instantiatedChildren[middleChildIdx].region, '≠', this.region);
|
36
|
+
throw new Error('Logic error: [this] is not contained within its parent\'s center child');
|
37
|
+
}
|
38
|
+
// Replace the middle child
|
39
|
+
parent.instantiatedChildren[middleChildIdx] = this;
|
40
|
+
this.parent = parent;
|
41
|
+
return parent;
|
42
|
+
}
|
43
|
+
// Generates children, if missing.
|
44
|
+
generateChildren() {
|
45
|
+
if (this.instantiatedChildren.length === 0) {
|
46
|
+
const childRects = this.region.divideIntoGrid(cacheDivisionSize, cacheDivisionSize);
|
47
|
+
for (const rect of childRects) {
|
48
|
+
const child = new RenderingCacheNode(rect, this.cacheState);
|
49
|
+
child.parent = this;
|
50
|
+
this.instantiatedChildren.push(child);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
this.checkRep();
|
54
|
+
}
|
55
|
+
// Returns CacheNodes directly contained within this.
|
56
|
+
getChildren() {
|
57
|
+
this.checkRep();
|
58
|
+
this.generateChildren();
|
59
|
+
return this.instantiatedChildren;
|
60
|
+
}
|
61
|
+
smallestChildContaining(rect) {
|
62
|
+
var _a;
|
63
|
+
const largerThanChildren = rect.maxDimension > this.region.maxDimension / cacheDivisionSize;
|
64
|
+
if (!this.region.containsRect(rect) || largerThanChildren) {
|
65
|
+
return null;
|
66
|
+
}
|
67
|
+
for (const child of this.getChildren()) {
|
68
|
+
if (child.region.containsRect(rect)) {
|
69
|
+
return (_a = child.smallestChildContaining(rect)) !== null && _a !== void 0 ? _a : child;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
return null;
|
73
|
+
}
|
74
|
+
// => [true] iff [this] can be rendered without too much scaling
|
75
|
+
renderingWouldBeHighEnoughResolution(viewport) {
|
76
|
+
// Determine how 1px in this corresponds to 1px on the canvas.
|
77
|
+
// this.region.w is in canvas units. Thus,
|
78
|
+
const sizeOfThisPixelOnCanvas = this.region.w / this.cacheState.props.blockResolution.x;
|
79
|
+
const sizeOfThisPixelOnScreen = viewport.getScaleFactor() * sizeOfThisPixelOnCanvas;
|
80
|
+
if (sizeOfThisPixelOnScreen > this.cacheState.props.maxScale) {
|
81
|
+
return false;
|
82
|
+
}
|
83
|
+
return true;
|
84
|
+
}
|
85
|
+
// => [true] if all children of this can be rendered from their caches.
|
86
|
+
allChildrenCanRender(viewport, leavesSortedById) {
|
87
|
+
if (this.instantiatedChildren.length === 0) {
|
88
|
+
return false;
|
89
|
+
}
|
90
|
+
for (const child of this.instantiatedChildren) {
|
91
|
+
if (!child.region.intersects(viewport.visibleRect)) {
|
92
|
+
continue;
|
93
|
+
}
|
94
|
+
if (!child.renderingIsUpToDate(this.idsOfIntersecting(leavesSortedById))) {
|
95
|
+
return false;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
return true;
|
99
|
+
}
|
100
|
+
computeSortedByLeafIds(leaves) {
|
101
|
+
const ids = leaves.slice();
|
102
|
+
ids.sort((a, b) => a.getId() - b.getId());
|
103
|
+
return ids;
|
104
|
+
}
|
105
|
+
// Returns a list of the ids of the nodes intersecting this
|
106
|
+
idsOfIntersecting(nodes) {
|
107
|
+
const result = [];
|
108
|
+
for (const node of nodes) {
|
109
|
+
if (node.getBBox().intersects(this.region)) {
|
110
|
+
result.push(node.getId());
|
111
|
+
}
|
112
|
+
}
|
113
|
+
return result;
|
114
|
+
}
|
115
|
+
renderingIsUpToDate(sortedIds) {
|
116
|
+
if (this.cachedRenderer === null || sortedIds.length !== this.renderedIds.length) {
|
117
|
+
return false;
|
118
|
+
}
|
119
|
+
for (let i = 0; i < sortedIds.length; i++) {
|
120
|
+
if (sortedIds[i] !== this.renderedIds[i]) {
|
121
|
+
return false;
|
122
|
+
}
|
123
|
+
}
|
124
|
+
return true;
|
125
|
+
}
|
126
|
+
// Render all [items] within [viewport]
|
127
|
+
renderItems(screenRenderer, items, viewport) {
|
128
|
+
var _a, _b;
|
129
|
+
if (!viewport.visibleRect.intersects(this.region)
|
130
|
+
|| items.length === 0) {
|
131
|
+
return;
|
132
|
+
}
|
133
|
+
const newItems = [];
|
134
|
+
// Divide [items] until nodes are leaves or smaller than this
|
135
|
+
for (const item of items) {
|
136
|
+
const bbox = item.getBBox();
|
137
|
+
if (!bbox.intersects(this.region)) {
|
138
|
+
continue;
|
139
|
+
}
|
140
|
+
if (bbox.maxDimension >= this.region.maxDimension) {
|
141
|
+
newItems.push(...item.getChildrenOrSelfIntersectingRegion(this.region));
|
142
|
+
}
|
143
|
+
else {
|
144
|
+
newItems.push(item);
|
145
|
+
}
|
146
|
+
}
|
147
|
+
items = newItems;
|
148
|
+
// Can we cache at all?
|
149
|
+
if (!this.cacheState.props.isOfCorrectType(screenRenderer)) {
|
150
|
+
items.forEach(item => item.render(screenRenderer, viewport.visibleRect));
|
151
|
+
return;
|
152
|
+
}
|
153
|
+
if (debugMode) {
|
154
|
+
screenRenderer.drawRect(this.region, 0.5 * viewport.getSizeOfPixelOnCanvas(), { fill: Color4.yellow });
|
155
|
+
}
|
156
|
+
// Could we render direclty from [this] or do we need to recurse?
|
157
|
+
const couldRender = this.renderingWouldBeHighEnoughResolution(viewport);
|
158
|
+
if (!couldRender) {
|
159
|
+
for (const child of this.getChildren()) {
|
160
|
+
child.renderItems(screenRenderer, items.filter(item => {
|
161
|
+
return item.getBBox().intersects(child.region);
|
162
|
+
}), viewport);
|
163
|
+
}
|
164
|
+
}
|
165
|
+
else {
|
166
|
+
// Determine whether we already have rendered the items
|
167
|
+
const leaves = [];
|
168
|
+
for (const item of items) {
|
169
|
+
leaves.push(...item.getLeavesIntersectingRegion(this.region, rect => rect.w / this.region.w < 2 / this.cacheState.props.blockResolution.x));
|
170
|
+
}
|
171
|
+
sortLeavesByZIndex(leaves);
|
172
|
+
const leavesByIds = this.computeSortedByLeafIds(leaves);
|
173
|
+
// No intersecting leaves? No need to render
|
174
|
+
if (leavesByIds.length === 0) {
|
175
|
+
return;
|
176
|
+
}
|
177
|
+
const leafIds = leavesByIds.map(leaf => leaf.getId());
|
178
|
+
let thisRenderer;
|
179
|
+
if (!this.renderingIsUpToDate(leafIds)) {
|
180
|
+
if (this.allChildrenCanRender(viewport, leavesByIds)) {
|
181
|
+
for (const child of this.getChildren()) {
|
182
|
+
child.renderItems(screenRenderer, items, viewport);
|
183
|
+
}
|
184
|
+
return;
|
185
|
+
}
|
186
|
+
// Is it worth it to render the items?
|
187
|
+
// TODO: Replace this with something performace based.
|
188
|
+
// TODO: Determine whether it is 'worth it' to cache this depending on rendering time.
|
189
|
+
if (leavesByIds.length > this.cacheState.props.minComponentsPerCache) {
|
190
|
+
let fullRerenderNeeded = true;
|
191
|
+
if (!this.cachedRenderer) {
|
192
|
+
this.cachedRenderer = this.cacheState.recordManager.allocCanvas(this.region, () => this.onRegionDealloc());
|
193
|
+
}
|
194
|
+
else if (leavesByIds.length > this.renderedIds.length && this.renderedMaxZIndex !== null) {
|
195
|
+
// We often don't need to do a full re-render even if something's changed.
|
196
|
+
// Check whether we can just draw on top of the existing cache.
|
197
|
+
const newLeaves = [];
|
198
|
+
let minNewZIndex = null;
|
199
|
+
for (let i = 0; i < leavesByIds.length; i++) {
|
200
|
+
const leaf = leavesByIds[i];
|
201
|
+
const content = leaf.getContent();
|
202
|
+
const zIndex = content.getZIndex();
|
203
|
+
if (i >= this.renderedIds.length || leaf.getId() !== this.renderedIds[i]) {
|
204
|
+
newLeaves.push(leaf);
|
205
|
+
if (minNewZIndex === null || zIndex < minNewZIndex) {
|
206
|
+
minNewZIndex = zIndex;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
if (minNewZIndex !== null && minNewZIndex > this.renderedMaxZIndex) {
|
211
|
+
fullRerenderNeeded = false;
|
212
|
+
thisRenderer = this.cachedRenderer.startRender();
|
213
|
+
// Looping is faster than re-sorting.
|
214
|
+
for (let i = 0; i < leaves.length; i++) {
|
215
|
+
const leaf = leaves[i];
|
216
|
+
const zIndex = leaf.getContent().getZIndex();
|
217
|
+
if (zIndex > this.renderedMaxZIndex) {
|
218
|
+
leaf.render(thisRenderer, this.region);
|
219
|
+
this.renderedMaxZIndex = zIndex;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
if (debugMode) {
|
223
|
+
screenRenderer.drawRect(this.region, viewport.getSizeOfPixelOnCanvas(), { fill: Color4.clay });
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
227
|
+
if (fullRerenderNeeded) {
|
228
|
+
thisRenderer = this.cachedRenderer.startRender();
|
229
|
+
thisRenderer.clear();
|
230
|
+
this.renderedMaxZIndex = null;
|
231
|
+
for (const leaf of leaves) {
|
232
|
+
const content = leaf.getContent();
|
233
|
+
(_a = this.renderedMaxZIndex) !== null && _a !== void 0 ? _a : (this.renderedMaxZIndex = content.getZIndex());
|
234
|
+
this.renderedMaxZIndex = Math.max(this.renderedMaxZIndex, content.getZIndex());
|
235
|
+
leaf.render(thisRenderer, this.region);
|
236
|
+
}
|
237
|
+
if (debugMode) {
|
238
|
+
screenRenderer.drawRect(this.region, viewport.getSizeOfPixelOnCanvas(), { fill: Color4.red });
|
239
|
+
}
|
240
|
+
}
|
241
|
+
this.renderedIds = leafIds;
|
242
|
+
}
|
243
|
+
else {
|
244
|
+
(_b = this.cachedRenderer) === null || _b === void 0 ? void 0 : _b.dealloc();
|
245
|
+
// Slightly increase the clip region to prevent seams.
|
246
|
+
// Divide by two because grownBy expands the rectangle on all sides.
|
247
|
+
const pixelSize = viewport.getSizeOfPixelOnCanvas();
|
248
|
+
const expandedRegion = new Rect2(this.region.x, this.region.y, this.region.w + pixelSize, this.region.h + pixelSize);
|
249
|
+
const clip = true;
|
250
|
+
screenRenderer.startObject(expandedRegion, clip);
|
251
|
+
for (const leaf of leaves) {
|
252
|
+
leaf.render(screenRenderer, this.region.intersection(viewport.visibleRect));
|
253
|
+
}
|
254
|
+
screenRenderer.endObject();
|
255
|
+
}
|
256
|
+
}
|
257
|
+
else {
|
258
|
+
thisRenderer = this.cachedRenderer.startRender();
|
259
|
+
}
|
260
|
+
if (thisRenderer) {
|
261
|
+
const transformMat = this.cachedRenderer.getTransform(this.region).inverse();
|
262
|
+
screenRenderer.renderFromOtherOfSameType(transformMat, thisRenderer);
|
263
|
+
}
|
264
|
+
// Can we clean up this' children? (Are they unused?)
|
265
|
+
if (this.instantiatedChildren.every(child => child.isEmpty())) {
|
266
|
+
this.instantiatedChildren = [];
|
267
|
+
}
|
268
|
+
}
|
269
|
+
this.checkRep();
|
270
|
+
}
|
271
|
+
// Returns true iff this/its children have no cached state.
|
272
|
+
isEmpty() {
|
273
|
+
if (this.cachedRenderer !== null) {
|
274
|
+
return false;
|
275
|
+
}
|
276
|
+
return this.instantiatedChildren.every(child => child.isEmpty());
|
277
|
+
}
|
278
|
+
onRegionDealloc() {
|
279
|
+
this.cachedRenderer = null;
|
280
|
+
if (this.isEmpty()) {
|
281
|
+
this.instantiatedChildren = [];
|
282
|
+
}
|
283
|
+
}
|
284
|
+
checkRep() {
|
285
|
+
if (this.instantiatedChildren.length !== cacheDivisionSize * cacheDivisionSize && this.instantiatedChildren.length !== 0) {
|
286
|
+
throw new Error('Repcheck: Wrong number of children');
|
287
|
+
}
|
288
|
+
if (this.renderedIds[1] !== undefined && this.renderedIds[0] >= this.renderedIds[1]) {
|
289
|
+
console.error(this.renderedIds);
|
290
|
+
throw new Error('Repcheck: First two ids are not in ascending order!');
|
291
|
+
}
|
292
|
+
for (const child of this.instantiatedChildren) {
|
293
|
+
if (child.parent !== this) {
|
294
|
+
throw new Error('Children should be linked to their parents!');
|
295
|
+
}
|
296
|
+
}
|
297
|
+
if (this.cachedRenderer && !this.cachedRenderer.isAllocd()) {
|
298
|
+
throw new Error('this\' cachedRenderer != null, but is dealloc\'d');
|
299
|
+
}
|
300
|
+
}
|
301
|
+
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import DummyRenderer from '../renderers/DummyRenderer';
|
2
|
+
import RenderingCache from './RenderingCache';
|
3
|
+
import { CacheProps } from './types';
|
4
|
+
declare type RenderAllocCallback = (renderer: DummyRenderer) => void;
|
5
|
+
export declare const createCache: (onRenderAlloc?: RenderAllocCallback, cacheOptions?: Partial<CacheProps>) => {
|
6
|
+
cache: RenderingCache;
|
7
|
+
editor: import("../../Editor").Editor;
|
8
|
+
};
|
9
|
+
export {};
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { Vec2 } from '../../geometry/Vec2';
|
2
|
+
import DummyRenderer from '../renderers/DummyRenderer';
|
3
|
+
import createEditor from '../../testing/createEditor';
|
4
|
+
import RenderingCache from './RenderingCache';
|
5
|
+
// Override any default test options with [cacheOptions]
|
6
|
+
export const createCache = (onRenderAlloc, cacheOptions) => {
|
7
|
+
const editor = createEditor();
|
8
|
+
const cache = new RenderingCache(Object.assign({ createRenderer() {
|
9
|
+
const renderer = new DummyRenderer(editor.viewport);
|
10
|
+
onRenderAlloc === null || onRenderAlloc === void 0 ? void 0 : onRenderAlloc(renderer);
|
11
|
+
return renderer;
|
12
|
+
},
|
13
|
+
isOfCorrectType(renderer) {
|
14
|
+
return renderer instanceof DummyRenderer;
|
15
|
+
}, blockResolution: Vec2.of(500, 500), cacheSize: 500 * 10 * 4, maxScale: 2, minComponentsPerCache: 0, minComponentsToUseCache: 0 }, cacheOptions));
|
16
|
+
return {
|
17
|
+
cache,
|
18
|
+
editor
|
19
|
+
};
|
20
|
+
};
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { Vec2 } from '../../geometry/Vec2';
|
2
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
3
|
+
import { CacheRecordManager } from './CacheRecordManager';
|
4
|
+
export declare type CacheAddress = number;
|
5
|
+
export declare type BeforeDeallocCallback = () => void;
|
6
|
+
export interface CacheProps {
|
7
|
+
createRenderer(): AbstractRenderer;
|
8
|
+
isOfCorrectType(renderer: AbstractRenderer): boolean;
|
9
|
+
blockResolution: Vec2;
|
10
|
+
cacheSize: number;
|
11
|
+
maxScale: number;
|
12
|
+
minComponentsPerCache: number;
|
13
|
+
minComponentsToUseCache: number;
|
14
|
+
}
|
15
|
+
export interface PartialCacheState {
|
16
|
+
currentRenderingCycle: number;
|
17
|
+
props: CacheProps;
|
18
|
+
}
|
19
|
+
export interface CacheState extends PartialCacheState {
|
20
|
+
recordManager: CacheRecordManager;
|
21
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -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 { PathCommand } from '../../geometry/Path';
|
5
|
+
import Rect2 from '../../geometry/Rect2';
|
6
|
+
import { Point2, Vec2 } from '../../geometry/Vec2';
|
7
|
+
import Viewport from '../../Viewport';
|
6
8
|
export interface RenderingStyle {
|
7
9
|
fill: Color4;
|
8
10
|
stroke?: {
|
@@ -16,8 +18,10 @@ export interface RenderablePathSpec {
|
|
16
18
|
style: RenderingStyle;
|
17
19
|
}
|
18
20
|
export default abstract class AbstractRenderer {
|
19
|
-
|
21
|
+
private viewport;
|
22
|
+
private selfTransform;
|
20
23
|
protected constructor(viewport: Viewport);
|
24
|
+
protected getViewport(): Viewport;
|
21
25
|
abstract displaySize(): Vec2;
|
22
26
|
abstract clear(): void;
|
23
27
|
protected abstract beginPath(startPoint: Point2): void;
|
@@ -26,14 +30,21 @@ export default abstract class AbstractRenderer {
|
|
26
30
|
protected abstract moveTo(point: Point2): void;
|
27
31
|
protected abstract traceCubicBezierCurve(p1: Point2, p2: Point2, p3: Point2): void;
|
28
32
|
protected abstract traceQuadraticBezierCurve(controlPoint: Point2, endPoint: Point2): void;
|
33
|
+
abstract isTooSmallToRender(rect: Rect2): boolean;
|
29
34
|
setDraftMode(_draftMode: boolean): void;
|
30
|
-
|
35
|
+
protected objectLevel: number;
|
31
36
|
private currentPaths;
|
32
37
|
private flushPath;
|
33
38
|
drawPath(path: RenderablePathSpec): void;
|
34
39
|
drawRect(rect: Rect2, lineWidth: number, lineFill: RenderingStyle): void;
|
35
|
-
startObject(_boundingBox: Rect2): void;
|
36
|
-
endObject(): void;
|
40
|
+
startObject(_boundingBox: Rect2, _clip?: boolean): void;
|
41
|
+
endObject(_loaderData?: LoadSaveDataTable): void;
|
37
42
|
protected getNestingLevel(): number;
|
38
43
|
abstract drawPoints(...points: Point2[]): void;
|
44
|
+
canRenderFromWithoutDataLoss(_other: AbstractRenderer): boolean;
|
45
|
+
renderFromOtherOfSameType(_renderTo: Mat33, other: AbstractRenderer): void;
|
46
|
+
setTransform(transform: Mat33 | null): void;
|
47
|
+
getCanvasToScreenTransform(): Mat33;
|
48
|
+
canvasToScreen(vec: Vec2): Vec2;
|
49
|
+
getSizeOfCanvasPixelOnScreen(): number;
|
39
50
|
}
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import Path, { PathCommandType } from '
|
1
|
+
import Path, { PathCommandType } from '../../geometry/Path';
|
2
|
+
import { Vec2 } from '../../geometry/Vec2';
|
2
3
|
const stylesEqual = (a, b) => {
|
3
4
|
var _a, _b, _c, _d, _e;
|
4
5
|
return a === b || (a.fill.eq(b.fill)
|
@@ -8,9 +9,14 @@ const stylesEqual = (a, b) => {
|
|
8
9
|
export default class AbstractRenderer {
|
9
10
|
constructor(viewport) {
|
10
11
|
this.viewport = viewport;
|
12
|
+
// If null, this' transformation is linked to the Viewport
|
13
|
+
this.selfTransform = null;
|
11
14
|
this.objectLevel = 0;
|
12
15
|
this.currentPaths = null;
|
13
16
|
}
|
17
|
+
// this.canvasToScreen, etc. should be used instead of the corresponding
|
18
|
+
// methods on Viewport.
|
19
|
+
getViewport() { return this.viewport; }
|
14
20
|
setDraftMode(_draftMode) { }
|
15
21
|
flushPath() {
|
16
22
|
if (!this.currentPaths) {
|
@@ -68,11 +74,12 @@ export default class AbstractRenderer {
|
|
68
74
|
this.drawPath(path.toRenderable(lineFill));
|
69
75
|
}
|
70
76
|
// Note the start/end of an object with the given bounding box.
|
71
|
-
|
77
|
+
// Renderers are not required to support [clip]
|
78
|
+
startObject(_boundingBox, _clip) {
|
72
79
|
this.currentPaths = [];
|
73
80
|
this.objectLevel++;
|
74
81
|
}
|
75
|
-
endObject() {
|
82
|
+
endObject(_loaderData) {
|
76
83
|
// Render the paths all at once
|
77
84
|
this.flushPath();
|
78
85
|
this.currentPaths = null;
|
@@ -84,4 +91,31 @@ export default class AbstractRenderer {
|
|
84
91
|
getNestingLevel() {
|
85
92
|
return this.objectLevel;
|
86
93
|
}
|
94
|
+
// Returns true iff other can be rendered onto this without data loss.
|
95
|
+
canRenderFromWithoutDataLoss(_other) {
|
96
|
+
return false;
|
97
|
+
}
|
98
|
+
// MUST throw if other and this are not of the same base class.
|
99
|
+
renderFromOtherOfSameType(_renderTo, other) {
|
100
|
+
throw new Error(`Unable to render from ${other}: Not implemented`);
|
101
|
+
}
|
102
|
+
// Set a transformation to apply to things before rendering,
|
103
|
+
// replacing the viewport's transform.
|
104
|
+
setTransform(transform) {
|
105
|
+
this.selfTransform = transform;
|
106
|
+
}
|
107
|
+
// Get the matrix that transforms a vector on the canvas to a vector on this'
|
108
|
+
// rendering target.
|
109
|
+
getCanvasToScreenTransform() {
|
110
|
+
if (this.selfTransform) {
|
111
|
+
return this.selfTransform;
|
112
|
+
}
|
113
|
+
return this.viewport.canvasToScreenTransform;
|
114
|
+
}
|
115
|
+
canvasToScreen(vec) {
|
116
|
+
return this.getCanvasToScreenTransform().transformVec2(vec);
|
117
|
+
}
|
118
|
+
getSizeOfCanvasPixelOnScreen() {
|
119
|
+
return this.getCanvasToScreenTransform().transformVec3(Vec2.unitX).length();
|
120
|
+
}
|
87
121
|
}
|
@@ -1,7 +1,8 @@
|
|
1
|
-
import
|
2
|
-
import
|
3
|
-
import
|
4
|
-
import
|
1
|
+
import Mat33 from '../../geometry/Mat33';
|
2
|
+
import Rect2 from '../../geometry/Rect2';
|
3
|
+
import { Point2, Vec2 } from '../../geometry/Vec2';
|
4
|
+
import Vec3 from '../../geometry/Vec3';
|
5
|
+
import Viewport from '../../Viewport';
|
5
6
|
import AbstractRenderer, { RenderablePathSpec, RenderingStyle } from './AbstractRenderer';
|
6
7
|
export default class CanvasRenderer extends AbstractRenderer {
|
7
8
|
private ctx;
|
@@ -11,6 +12,8 @@ export default class CanvasRenderer extends AbstractRenderer {
|
|
11
12
|
private minRenderSizeAnyDimen;
|
12
13
|
private minRenderSizeBothDimens;
|
13
14
|
constructor(ctx: CanvasRenderingContext2D, viewport: Viewport);
|
15
|
+
canRenderFromWithoutDataLoss(other: AbstractRenderer): boolean;
|
16
|
+
renderFromOtherOfSameType(transformBy: Mat33, other: AbstractRenderer): void;
|
14
17
|
setDraftMode(draftMode: boolean): void;
|
15
18
|
displaySize(): Vec2;
|
16
19
|
clear(): void;
|
@@ -21,7 +24,9 @@ export default class CanvasRenderer extends AbstractRenderer {
|
|
21
24
|
protected traceCubicBezierCurve(p1: Point2, p2: Point2, p3: Point2): void;
|
22
25
|
protected traceQuadraticBezierCurve(controlPoint: Vec3, endPoint: Vec3): void;
|
23
26
|
drawPath(path: RenderablePathSpec): void;
|
24
|
-
|
27
|
+
private clipLevels;
|
28
|
+
startObject(boundingBox: Rect2, clip: boolean): void;
|
25
29
|
endObject(): void;
|
26
30
|
drawPoints(...points: Point2[]): void;
|
31
|
+
isTooSmallToRender(rect: Rect2): boolean;
|
27
32
|
}
|