js-draw 0.0.9 → 0.1.1
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 +12 -0
- package/dist/bundle.js +1 -1
- package/dist/src/Editor.d.ts +2 -2
- package/dist/src/Editor.js +15 -7
- package/dist/src/EditorImage.d.ts +15 -7
- package/dist/src/EditorImage.js +43 -37
- package/dist/src/SVGLoader.d.ts +3 -2
- package/dist/src/SVGLoader.js +9 -7
- package/dist/src/Viewport.d.ts +4 -0
- package/dist/src/Viewport.js +41 -0
- package/dist/src/components/AbstractComponent.d.ts +3 -2
- package/dist/src/components/AbstractComponent.js +3 -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/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 +5 -3
- 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} +6 -2
- package/dist/src/{Display.js → rendering/Display.js} +37 -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} +19 -8
- package/dist/src/rendering/{AbstractRenderer.js → renderers/AbstractRenderer.js} +37 -2
- package/dist/src/rendering/{CanvasRenderer.d.ts → renderers/CanvasRenderer.d.ts} +14 -5
- package/dist/src/rendering/renderers/CanvasRenderer.js +164 -0
- 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} +4 -3
- package/dist/src/rendering/{SVGRenderer.js → renderers/SVGRenderer.js} +14 -11
- package/dist/src/testing/createEditor.js +1 -1
- package/dist/src/toolbar/HTMLToolbar.js +11 -2
- package/dist/src/toolbar/localization.d.ts +1 -0
- package/dist/src/toolbar/localization.js +1 -0
- package/dist/src/tools/PanZoom.js +3 -0
- package/dist/src/tools/SelectionTool.d.ts +3 -0
- package/dist/src/tools/SelectionTool.js +22 -24
- package/dist/src/types.d.ts +2 -1
- package/package.json +1 -1
- package/src/Editor.ts +17 -8
- package/src/EditorImage.test.ts +2 -2
- package/src/EditorImage.ts +54 -42
- package/src/SVGLoader.ts +11 -8
- package/src/Viewport.ts +56 -0
- package/src/components/AbstractComponent.ts +6 -2
- package/src/components/SVGGlobalAttributesObject.ts +2 -2
- package/src/components/Stroke.ts +1 -1
- 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.toString.test.ts +12 -2
- package/src/geometry/Path.ts +8 -4
- package/src/geometry/Rect2.test.ts +47 -8
- package/src/geometry/Rect2.ts +57 -9
- package/src/{Display.ts → rendering/Display.ts} +43 -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 -8
- package/src/rendering/renderers/CanvasRenderer.ts +219 -0
- 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} +17 -13
- package/src/testing/createEditor.ts +1 -1
- package/src/toolbar/HTMLToolbar.ts +13 -2
- package/src/toolbar/localization.ts +2 -0
- package/src/tools/PanZoom.ts +3 -0
- package/src/tools/SelectionTool.test.ts +1 -1
- package/src/tools/SelectionTool.ts +28 -33
- 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
- package/dist/src/rendering/CanvasRenderer.js +0 -108
- package/src/rendering/CanvasRenderer.ts +0 -141
@@ -38,20 +38,31 @@ export default class Rect2 {
|
|
38
38
|
&& this.bottomRight.y >= other.bottomRight.y;
|
39
39
|
}
|
40
40
|
intersects(other) {
|
41
|
-
|
41
|
+
// Project along x/y axes.
|
42
|
+
const thisMinX = this.x;
|
43
|
+
const thisMaxX = thisMinX + this.w;
|
44
|
+
const otherMinX = other.x;
|
45
|
+
const otherMaxX = other.x + other.w;
|
46
|
+
if (thisMaxX < otherMinX || thisMinX > otherMaxX) {
|
47
|
+
return false;
|
48
|
+
}
|
49
|
+
const thisMinY = this.y;
|
50
|
+
const thisMaxY = thisMinY + this.h;
|
51
|
+
const otherMinY = other.y;
|
52
|
+
const otherMaxY = other.y + other.h;
|
53
|
+
if (thisMaxY < otherMinY || thisMinY > otherMaxY) {
|
54
|
+
return false;
|
55
|
+
}
|
56
|
+
return true;
|
42
57
|
}
|
43
58
|
// Returns the overlap of this and [other], or null, if no such
|
44
|
-
//
|
59
|
+
// overlap exists
|
45
60
|
intersection(other) {
|
46
|
-
|
47
|
-
const bottomRight = this.bottomRight.zip(other.bottomRight, Math.min);
|
48
|
-
// The intersection can't be outside of this rectangle
|
49
|
-
if (!this.containsPoint(topLeft) || !this.containsPoint(bottomRight)) {
|
50
|
-
return null;
|
51
|
-
}
|
52
|
-
else if (!other.containsPoint(topLeft) || !other.containsPoint(bottomRight)) {
|
61
|
+
if (!this.intersects(other)) {
|
53
62
|
return null;
|
54
63
|
}
|
64
|
+
const topLeft = this.topLeft.zip(other.topLeft, Math.max);
|
65
|
+
const bottomRight = this.bottomRight.zip(other.bottomRight, Math.min);
|
55
66
|
return Rect2.fromCorners(topLeft, bottomRight);
|
56
67
|
}
|
57
68
|
// Returns a new rectangle containing both [this] and [other].
|
@@ -60,6 +71,33 @@ export default class Rect2 {
|
|
60
71
|
const bottomRight = this.bottomRight.zip(other.bottomRight, Math.max);
|
61
72
|
return Rect2.fromCorners(topLeft, bottomRight);
|
62
73
|
}
|
74
|
+
// Returns a the subdivision of this into [columns] columns
|
75
|
+
// and [rows] rows. For example,
|
76
|
+
// Rect2.unitSquare.divideIntoGrid(2, 2)
|
77
|
+
// -> [ Rect2(0, 0, 0.5, 0.5), Rect2(0.5, 0, 0.5, 0.5), Rect2(0, 0.5, 0.5, 0.5), Rect2(0.5, 0.5, 0.5, 0.5) ]
|
78
|
+
// The rectangles are ordered in row-major order.
|
79
|
+
divideIntoGrid(columns, rows) {
|
80
|
+
const result = [];
|
81
|
+
if (columns <= 0 || rows <= 0) {
|
82
|
+
return result;
|
83
|
+
}
|
84
|
+
const eachRectWidth = this.w / columns;
|
85
|
+
const eachRectHeight = this.h / rows;
|
86
|
+
if (eachRectWidth === 0) {
|
87
|
+
columns = 1;
|
88
|
+
}
|
89
|
+
if (eachRectHeight === 0) {
|
90
|
+
rows = 1;
|
91
|
+
}
|
92
|
+
for (let j = 0; j < rows; j++) {
|
93
|
+
for (let i = 0; i < columns; i++) {
|
94
|
+
const x = eachRectWidth * i + this.x;
|
95
|
+
const y = eachRectHeight * j + this.y;
|
96
|
+
result.push(new Rect2(x, y, eachRectWidth, eachRectHeight));
|
97
|
+
}
|
98
|
+
}
|
99
|
+
return result;
|
100
|
+
}
|
63
101
|
// Returns a rectangle containing this and [point].
|
64
102
|
// [margin] is the minimum distance between the new point and the edge
|
65
103
|
// of the resultant rectangle.
|
@@ -1,5 +1,6 @@
|
|
1
|
-
import AbstractRenderer from './
|
2
|
-
import { Editor } from '
|
1
|
+
import AbstractRenderer from './renderers/AbstractRenderer';
|
2
|
+
import { Editor } from '../Editor';
|
3
|
+
import RenderingCache from './caching/RenderingCache';
|
3
4
|
export declare enum RenderingMode {
|
4
5
|
DummyRenderer = 0,
|
5
6
|
CanvasRenderer = 1
|
@@ -9,13 +10,16 @@ export default class Display {
|
|
9
10
|
private parent;
|
10
11
|
private dryInkRenderer;
|
11
12
|
private wetInkRenderer;
|
13
|
+
private cache;
|
12
14
|
private resizeSurfacesCallback?;
|
13
15
|
private flattenCallback?;
|
14
16
|
constructor(editor: Editor, mode: RenderingMode, parent: HTMLElement | null);
|
15
17
|
get width(): number;
|
16
18
|
get height(): number;
|
19
|
+
getCache(): RenderingCache;
|
17
20
|
private initializeCanvasRendering;
|
18
21
|
startRerender(): AbstractRenderer;
|
22
|
+
setDraftMode(draftMode: boolean): void;
|
19
23
|
getDryInkRenderer(): AbstractRenderer;
|
20
24
|
getWetInkRenderer(): AbstractRenderer;
|
21
25
|
flatten(): void;
|
@@ -1,7 +1,8 @@
|
|
1
|
-
import CanvasRenderer from './
|
2
|
-
import { EditorEventType } from '
|
3
|
-
import DummyRenderer from './
|
4
|
-
import { Vec2 } from '
|
1
|
+
import CanvasRenderer from './renderers/CanvasRenderer';
|
2
|
+
import { EditorEventType } from '../types';
|
3
|
+
import DummyRenderer from './renderers/DummyRenderer';
|
4
|
+
import { Vec2 } from '../geometry/Vec2';
|
5
|
+
import RenderingCache from './caching/RenderingCache';
|
5
6
|
export var RenderingMode;
|
6
7
|
(function (RenderingMode) {
|
7
8
|
RenderingMode[RenderingMode["DummyRenderer"] = 0] = "DummyRenderer";
|
@@ -22,6 +23,32 @@ export default class Display {
|
|
22
23
|
else {
|
23
24
|
throw new Error(`Unknown rendering mode, ${mode}!`);
|
24
25
|
}
|
26
|
+
const cacheBlockResolution = Vec2.of(600, 600);
|
27
|
+
this.cache = new RenderingCache({
|
28
|
+
createRenderer: () => {
|
29
|
+
if (mode === RenderingMode.DummyRenderer) {
|
30
|
+
return new DummyRenderer(editor.viewport);
|
31
|
+
}
|
32
|
+
else if (mode !== RenderingMode.CanvasRenderer) {
|
33
|
+
throw new Error('Unspported rendering mode');
|
34
|
+
}
|
35
|
+
// Make the canvas slightly larger than each cache block to prevent
|
36
|
+
// seams.
|
37
|
+
const canvas = document.createElement('canvas');
|
38
|
+
canvas.width = cacheBlockResolution.x + 1;
|
39
|
+
canvas.height = cacheBlockResolution.y + 1;
|
40
|
+
const ctx = canvas.getContext('2d');
|
41
|
+
return new CanvasRenderer(ctx, editor.viewport);
|
42
|
+
},
|
43
|
+
isOfCorrectType: (renderer) => {
|
44
|
+
return this.dryInkRenderer.canRenderFromWithoutDataLoss(renderer);
|
45
|
+
},
|
46
|
+
blockResolution: cacheBlockResolution,
|
47
|
+
cacheSize: 500 * 500 * 4 * 200,
|
48
|
+
maxScale: 1.5,
|
49
|
+
minComponentsPerCache: 50,
|
50
|
+
minComponentsToUseCache: 120,
|
51
|
+
});
|
25
52
|
this.editor.notifier.on(EditorEventType.DisplayResized, event => {
|
26
53
|
var _a;
|
27
54
|
if (event.kind !== EditorEventType.DisplayResized) {
|
@@ -39,6 +66,9 @@ export default class Display {
|
|
39
66
|
get height() {
|
40
67
|
return this.dryInkRenderer.displaySize().y;
|
41
68
|
}
|
69
|
+
getCache() {
|
70
|
+
return this.cache;
|
71
|
+
}
|
42
72
|
initializeCanvasRendering() {
|
43
73
|
const dryInkCanvas = document.createElement('canvas');
|
44
74
|
const wetInkCanvas = document.createElement('canvas');
|
@@ -82,6 +112,9 @@ export default class Display {
|
|
82
112
|
this.dryInkRenderer.clear();
|
83
113
|
return this.dryInkRenderer;
|
84
114
|
}
|
115
|
+
setDraftMode(draftMode) {
|
116
|
+
this.dryInkRenderer.setDraftMode(draftMode);
|
117
|
+
}
|
85
118
|
getDryInkRenderer() {
|
86
119
|
return this.dryInkRenderer;
|
87
120
|
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import Mat33 from '../../geometry/Mat33';
|
2
|
+
import Rect2 from '../../geometry/Rect2';
|
3
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
4
|
+
import { BeforeDeallocCallback, CacheState } from './types';
|
5
|
+
export default class CacheRecord {
|
6
|
+
private onBeforeDeallocCallback;
|
7
|
+
private cacheState;
|
8
|
+
private renderer;
|
9
|
+
private lastUsedCycle;
|
10
|
+
private allocd;
|
11
|
+
constructor(onBeforeDeallocCallback: BeforeDeallocCallback | null, cacheState: CacheState);
|
12
|
+
startRender(): AbstractRenderer;
|
13
|
+
dealloc(): void;
|
14
|
+
isAllocd(): boolean;
|
15
|
+
realloc(newDeallocCallback: BeforeDeallocCallback): void;
|
16
|
+
getLastUsedCycle(): number;
|
17
|
+
getTransform(drawTo: Rect2): Mat33;
|
18
|
+
setRenderingRegion(drawTo: Rect2): void;
|
19
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import Mat33 from '../../geometry/Mat33';
|
2
|
+
// Represents a cached renderer/canvas
|
3
|
+
// This is not a [CacheNode] -- it handles cached renderers and does not have sub-renderers.
|
4
|
+
export default class CacheRecord {
|
5
|
+
constructor(onBeforeDeallocCallback, cacheState) {
|
6
|
+
this.onBeforeDeallocCallback = onBeforeDeallocCallback;
|
7
|
+
this.cacheState = cacheState;
|
8
|
+
this.allocd = false;
|
9
|
+
this.renderer = cacheState.props.createRenderer();
|
10
|
+
this.lastUsedCycle = -1;
|
11
|
+
this.allocd = true;
|
12
|
+
}
|
13
|
+
startRender() {
|
14
|
+
this.lastUsedCycle = this.cacheState.currentRenderingCycle;
|
15
|
+
if (!this.allocd) {
|
16
|
+
throw new Error('Only alloc\'d canvases can be rendered to');
|
17
|
+
}
|
18
|
+
return this.renderer;
|
19
|
+
}
|
20
|
+
dealloc() {
|
21
|
+
var _a;
|
22
|
+
(_a = this.onBeforeDeallocCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
23
|
+
this.allocd = false;
|
24
|
+
this.onBeforeDeallocCallback = null;
|
25
|
+
this.lastUsedCycle = 0;
|
26
|
+
}
|
27
|
+
isAllocd() {
|
28
|
+
return this.allocd;
|
29
|
+
}
|
30
|
+
realloc(newDeallocCallback) {
|
31
|
+
if (this.allocd) {
|
32
|
+
this.dealloc();
|
33
|
+
}
|
34
|
+
this.allocd = true;
|
35
|
+
this.onBeforeDeallocCallback = newDeallocCallback;
|
36
|
+
this.lastUsedCycle = this.cacheState.currentRenderingCycle;
|
37
|
+
}
|
38
|
+
getLastUsedCycle() {
|
39
|
+
return this.lastUsedCycle;
|
40
|
+
}
|
41
|
+
// Returns the transformation that maps [drawTo] to this' renderable region
|
42
|
+
// (i.e. a [cacheProps.blockResolution]-sized rectangle with top left at (0, 0))
|
43
|
+
getTransform(drawTo) {
|
44
|
+
const transform = Mat33.scaling2D(this.cacheState.props.blockResolution.x / drawTo.size.x).rightMul(Mat33.translation(drawTo.topLeft.times(-1)));
|
45
|
+
return transform;
|
46
|
+
}
|
47
|
+
setRenderingRegion(drawTo) {
|
48
|
+
this.renderer.setTransform(
|
49
|
+
// Invert to map objects instead of the viewport
|
50
|
+
this.getTransform(drawTo));
|
51
|
+
}
|
52
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { BeforeDeallocCallback, PartialCacheState } from './types';
|
2
|
+
import CacheRecord from './CacheRecord';
|
3
|
+
import Rect2 from '../../geometry/Rect2';
|
4
|
+
export declare class CacheRecordManager {
|
5
|
+
private readonly cacheState;
|
6
|
+
private cacheRecords;
|
7
|
+
private maxCanvases;
|
8
|
+
constructor(cacheState: PartialCacheState);
|
9
|
+
allocCanvas(drawTo: Rect2, onDealloc: BeforeDeallocCallback): CacheRecord;
|
10
|
+
private getLeastRecentlyUsedRecord;
|
11
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import CacheRecord from './CacheRecord';
|
2
|
+
export class CacheRecordManager {
|
3
|
+
constructor(cacheState) {
|
4
|
+
this.cacheState = cacheState;
|
5
|
+
// Fixed-size array: Cache blocks are assigned indicies into [cachedCanvases].
|
6
|
+
this.cacheRecords = [];
|
7
|
+
const cacheProps = cacheState.props;
|
8
|
+
this.maxCanvases = Math.ceil(
|
9
|
+
// Assuming four components per pixel:
|
10
|
+
cacheProps.cacheSize / 4 / cacheProps.blockResolution.x / cacheProps.blockResolution.y);
|
11
|
+
}
|
12
|
+
allocCanvas(drawTo, onDealloc) {
|
13
|
+
if (this.cacheRecords.length < this.maxCanvases) {
|
14
|
+
const record = new CacheRecord(onDealloc, Object.assign(Object.assign({}, this.cacheState), { recordManager: this }));
|
15
|
+
record.setRenderingRegion(drawTo);
|
16
|
+
this.cacheRecords.push(record);
|
17
|
+
return record;
|
18
|
+
}
|
19
|
+
else {
|
20
|
+
const lru = this.getLeastRecentlyUsedRecord();
|
21
|
+
lru.realloc(onDealloc);
|
22
|
+
lru.setRenderingRegion(drawTo);
|
23
|
+
return lru;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
// Returns null if there are no cache records. Returns an unalloc'd record if one exists.
|
27
|
+
getLeastRecentlyUsedRecord() {
|
28
|
+
this.cacheRecords.sort((a, b) => a.getLastUsedCycle() - b.getLastUsedCycle());
|
29
|
+
return this.cacheRecords[0];
|
30
|
+
}
|
31
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { ImageNode } from '../../EditorImage';
|
2
|
+
import Viewport from '../../Viewport';
|
3
|
+
import AbstractRenderer from '../renderers/AbstractRenderer';
|
4
|
+
import { CacheProps, CacheState } from './types';
|
5
|
+
export default class RenderingCache {
|
6
|
+
private partialSharedState;
|
7
|
+
private recordManager;
|
8
|
+
private rootNode;
|
9
|
+
constructor(cacheProps: CacheProps);
|
10
|
+
getSharedState(): CacheState;
|
11
|
+
render(screenRenderer: AbstractRenderer, image: ImageNode, viewport: Viewport): void;
|
12
|
+
}
|
@@ -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
|
+
}
|