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
@@ -1,5 +1,5 @@
|
|
1
1
|
import Rect2 from '../../geometry/Rect2';
|
2
|
-
import AbstractRenderer from '../../rendering/AbstractRenderer';
|
2
|
+
import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
|
3
3
|
import { StrokeDataPoint } from '../../types';
|
4
4
|
import Viewport from '../../Viewport';
|
5
5
|
import AbstractComponent from '../AbstractComponent';
|
package/src/geometry/Mat33.ts
CHANGED
@@ -19,13 +19,23 @@ describe('Path.toString', () => {
|
|
19
19
|
});
|
20
20
|
|
21
21
|
it('should fix rounding errors', () => {
|
22
|
-
const path = new Path(Vec2.of(0.
|
22
|
+
const path = new Path(Vec2.of(0.10000001, 0.19999999), [
|
23
23
|
{
|
24
24
|
kind: PathCommandType.QuadraticBezierTo,
|
25
25
|
controlPoint: Vec2.of(9999, -10.999999995),
|
26
|
-
endPoint: Vec2.of(0.000300001, 1.
|
26
|
+
endPoint: Vec2.of(0.000300001, 1.40000002),
|
27
27
|
},
|
28
28
|
]);
|
29
29
|
expect(path.toString()).toBe('M0.1,0.2Q9999,-11 0.0003,1.4');
|
30
30
|
});
|
31
|
+
|
32
|
+
it('should not remove trailing zeroes before decimal points', () => {
|
33
|
+
const path = new Path(Vec2.of(1000, 2_000_000), [
|
34
|
+
{
|
35
|
+
kind: PathCommandType.LineTo,
|
36
|
+
point: Vec2.of(30.0001, 40.000000001),
|
37
|
+
},
|
38
|
+
]);
|
39
|
+
expect(path.toString()).toBe('M1000,2000000L30.0001,40');
|
40
|
+
});
|
31
41
|
});
|
package/src/geometry/Path.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { Bezier } from 'bezier-js';
|
2
|
-
import { RenderingStyle, RenderablePathSpec } from '../rendering/AbstractRenderer';
|
2
|
+
import { RenderingStyle, RenderablePathSpec } from '../rendering/renderers/AbstractRenderer';
|
3
3
|
import LineSegment2 from './LineSegment2';
|
4
4
|
import Mat33 from './Mat33';
|
5
5
|
import Rect2 from './Rect2';
|
@@ -288,8 +288,8 @@ export default class Path {
|
|
288
288
|
const toRoundedString = (num: number): string => {
|
289
289
|
// Try to remove rounding errors. If the number ends in at least three/four zeroes
|
290
290
|
// (or nines) just one or two digits, it's probably a rounding error.
|
291
|
-
const fixRoundingUpExp = /^([-]?\d
|
292
|
-
const hasRoundingDownExp = /^([-]?)(\d*)\.(\d
|
291
|
+
const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d$/;
|
292
|
+
const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,}\d)$/;
|
293
293
|
|
294
294
|
let text = num.toString();
|
295
295
|
if (text.indexOf('.') === -1) {
|
@@ -314,7 +314,11 @@ export default class Path {
|
|
314
314
|
}
|
315
315
|
|
316
316
|
text = text.replace(fixRoundingUpExp, '$1');
|
317
|
-
|
317
|
+
|
318
|
+
// Remove trailing zeroes
|
319
|
+
text = text.replace(/([.][^0]*)0+$/, '$1');
|
320
|
+
|
321
|
+
// Remove trailing period
|
318
322
|
return text.replace(/[.]$/, '');
|
319
323
|
};
|
320
324
|
|
@@ -6,8 +6,8 @@ import Mat33 from './Mat33';
|
|
6
6
|
|
7
7
|
loadExpectExtensions();
|
8
8
|
|
9
|
-
describe('Rect2
|
10
|
-
it('
|
9
|
+
describe('Rect2', () => {
|
10
|
+
it('width, height should always be positive', () => {
|
11
11
|
expect(new Rect2(-1, -2, -3, 4)).objEq(new Rect2(-4, -2, 3, 4));
|
12
12
|
expect(new Rect2(0, 0, 0, 0).size).objEq(Vec2.zero);
|
13
13
|
expect(Rect2.fromCorners(
|
@@ -19,7 +19,7 @@ describe('Rect2 tests', () => {
|
|
19
19
|
));
|
20
20
|
});
|
21
21
|
|
22
|
-
it('
|
22
|
+
it('bounding boxes should be correctly computed', () => {
|
23
23
|
expect(Rect2.bboxOf([
|
24
24
|
Vec2.zero,
|
25
25
|
])).objEq(Rect2.empty);
|
@@ -42,14 +42,14 @@ describe('Rect2 tests', () => {
|
|
42
42
|
));
|
43
43
|
});
|
44
44
|
|
45
|
-
it('"union"
|
45
|
+
it('"union"s should contain both composite rectangles.', () => {
|
46
46
|
expect(new Rect2(0, 0, 1, 1).union(new Rect2(1, 1, 2, 2))).objEq(
|
47
47
|
new Rect2(0, 0, 3, 3)
|
48
48
|
);
|
49
49
|
expect(Rect2.empty.union(Rect2.empty)).objEq(Rect2.empty);
|
50
50
|
});
|
51
51
|
|
52
|
-
it('
|
52
|
+
it('should contain points that are within a rectangle', () => {
|
53
53
|
expect(new Rect2(-1, -1, 2, 2).containsPoint(Vec2.zero)).toBe(true);
|
54
54
|
expect(new Rect2(-1, -1, 0, 0).containsPoint(Vec2.zero)).toBe(false);
|
55
55
|
expect(new Rect2(1, 2, 3, 4).containsRect(Rect2.empty)).toBe(false);
|
@@ -67,14 +67,15 @@ describe('Rect2 tests', () => {
|
|
67
67
|
expect(Rect2.empty.containsRect(new Rect2(-1, -1, 3, 3))).toBe(false);
|
68
68
|
});
|
69
69
|
|
70
|
-
it('
|
70
|
+
it('intersecting rectangles should be identified as intersecting', () => {
|
71
71
|
expect(new Rect2(-1, -1, 2, 2).intersects(Rect2.empty)).toBe(true);
|
72
72
|
expect(new Rect2(-1, -1, 2, 2).intersects(new Rect2(0, 0, 1, 1))).toBe(true);
|
73
73
|
expect(new Rect2(-1, -1, 2, 2).intersects(new Rect2(0, 0, 10, 10))).toBe(true);
|
74
74
|
expect(new Rect2(-1, -1, 2, 2).intersects(new Rect2(3, 3, 10, 10))).toBe(false);
|
75
|
+
expect(new Rect2(-1, -1, 2, 2).intersects(new Rect2(0.2, 0.1, 0, 0))).toBe(true);
|
75
76
|
});
|
76
77
|
|
77
|
-
it('
|
78
|
+
it('intersecting rectangles should have their intersections correctly computed', () => {
|
78
79
|
expect(new Rect2(-1, -1, 2, 2).intersection(Rect2.empty)).objEq(Rect2.empty);
|
79
80
|
expect(new Rect2(-1, -1, 2, 2).intersection(new Rect2(0, 0, 3, 3))).objEq(
|
80
81
|
new Rect2(0, 0, 1, 1)
|
@@ -93,7 +94,7 @@ describe('Rect2 tests', () => {
|
|
93
94
|
expect(transformedBBox.containsRect(rect)).toBe(true);
|
94
95
|
});
|
95
96
|
|
96
|
-
describe('
|
97
|
+
describe('should correctly expand to include a given point', () => {
|
97
98
|
it('Growing an empty rectange to include (1, 0)', () => {
|
98
99
|
const originalRect = Rect2.empty;
|
99
100
|
const grownRect = originalRect.grownToPoint(Vec2.unitX);
|
@@ -118,4 +119,42 @@ describe('Rect2 tests', () => {
|
|
118
119
|
expect(grown).objEq(new Rect2(0, 0, 2, 2));
|
119
120
|
});
|
120
121
|
});
|
122
|
+
|
123
|
+
describe('divideIntoGrid', () => {
|
124
|
+
it('division of unit square', () => {
|
125
|
+
expect(Rect2.unitSquare.divideIntoGrid(2, 2)).toMatchObject(
|
126
|
+
[
|
127
|
+
new Rect2(0, 0, 0.5, 0.5), new Rect2(0.5, 0, 0.5, 0.5),
|
128
|
+
new Rect2(0, 0.5, 0.5, 0.5), new Rect2(0.5, 0.5, 0.5, 0.5),
|
129
|
+
]
|
130
|
+
);
|
131
|
+
expect(Rect2.unitSquare.divideIntoGrid(0, 0).length).toBe(0);
|
132
|
+
expect(Rect2.unitSquare.divideIntoGrid(100, 0).length).toBe(0);
|
133
|
+
expect(Rect2.unitSquare.divideIntoGrid(4, 1)).toMatchObject(
|
134
|
+
[
|
135
|
+
new Rect2(0, 0, 0.25, 1), new Rect2(0.25, 0, 0.25, 1),
|
136
|
+
new Rect2(0.5, 0, 0.25, 1), new Rect2(0.75, 0, 0.25, 1),
|
137
|
+
]
|
138
|
+
);
|
139
|
+
});
|
140
|
+
it('division of translated square', () => {
|
141
|
+
expect(new Rect2(3, -3, 4, 4).divideIntoGrid(2, 1)).toMatchObject(
|
142
|
+
[
|
143
|
+
new Rect2(3, -3, 2, 4), new Rect2(5, -3, 2, 4),
|
144
|
+
]
|
145
|
+
);
|
146
|
+
});
|
147
|
+
it('division of empty square', () => {
|
148
|
+
expect(Rect2.empty.divideIntoGrid(1000, 10000).length).toBe(1);
|
149
|
+
});
|
150
|
+
});
|
151
|
+
|
152
|
+
it('division of rectangle', () => {
|
153
|
+
expect(new Rect2(0, 0, 2, 1).divideIntoGrid(2, 2)).toMatchObject(
|
154
|
+
[
|
155
|
+
new Rect2(0, 0, 1, 0.5), new Rect2(1, 0, 1, 0.5),
|
156
|
+
new Rect2(0, 0.5, 1, 0.5), new Rect2(1, 0.5, 1, 0.5),
|
157
|
+
]
|
158
|
+
);
|
159
|
+
});
|
121
160
|
});
|
package/src/geometry/Rect2.ts
CHANGED
@@ -67,22 +67,39 @@ export default class Rect2 {
|
|
67
67
|
}
|
68
68
|
|
69
69
|
public intersects(other: Rect2): boolean {
|
70
|
-
|
70
|
+
// Project along x/y axes.
|
71
|
+
const thisMinX = this.x;
|
72
|
+
const thisMaxX = thisMinX + this.w;
|
73
|
+
const otherMinX = other.x;
|
74
|
+
const otherMaxX = other.x + other.w;
|
75
|
+
|
76
|
+
if (thisMaxX < otherMinX || thisMinX > otherMaxX) {
|
77
|
+
return false;
|
78
|
+
}
|
79
|
+
|
80
|
+
|
81
|
+
const thisMinY = this.y;
|
82
|
+
const thisMaxY = thisMinY + this.h;
|
83
|
+
const otherMinY = other.y;
|
84
|
+
const otherMaxY = other.y + other.h;
|
85
|
+
|
86
|
+
if (thisMaxY < otherMinY || thisMinY > otherMaxY) {
|
87
|
+
return false;
|
88
|
+
}
|
89
|
+
|
90
|
+
return true;
|
71
91
|
}
|
72
92
|
|
73
93
|
// Returns the overlap of this and [other], or null, if no such
|
74
|
-
//
|
94
|
+
// overlap exists
|
75
95
|
public intersection(other: Rect2): Rect2|null {
|
76
|
-
|
77
|
-
const bottomRight = this.bottomRight.zip(other.bottomRight, Math.min);
|
78
|
-
|
79
|
-
// The intersection can't be outside of this rectangle
|
80
|
-
if (!this.containsPoint(topLeft) || !this.containsPoint(bottomRight)) {
|
81
|
-
return null;
|
82
|
-
} else if (!other.containsPoint(topLeft) || !other.containsPoint(bottomRight)) {
|
96
|
+
if (!this.intersects(other)) {
|
83
97
|
return null;
|
84
98
|
}
|
85
99
|
|
100
|
+
const topLeft = this.topLeft.zip(other.topLeft, Math.max);
|
101
|
+
const bottomRight = this.bottomRight.zip(other.bottomRight, Math.min);
|
102
|
+
|
86
103
|
return Rect2.fromCorners(topLeft, bottomRight);
|
87
104
|
}
|
88
105
|
|
@@ -97,6 +114,37 @@ export default class Rect2 {
|
|
97
114
|
);
|
98
115
|
}
|
99
116
|
|
117
|
+
// Returns a the subdivision of this into [columns] columns
|
118
|
+
// and [rows] rows. For example,
|
119
|
+
// Rect2.unitSquare.divideIntoGrid(2, 2)
|
120
|
+
// -> [ 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) ]
|
121
|
+
// The rectangles are ordered in row-major order.
|
122
|
+
public divideIntoGrid(columns: number, rows: number): Rect2[] {
|
123
|
+
const result: Rect2[] = [];
|
124
|
+
if (columns <= 0 || rows <= 0) {
|
125
|
+
return result;
|
126
|
+
}
|
127
|
+
|
128
|
+
const eachRectWidth = this.w / columns;
|
129
|
+
const eachRectHeight = this.h / rows;
|
130
|
+
|
131
|
+
if (eachRectWidth === 0) {
|
132
|
+
columns = 1;
|
133
|
+
}
|
134
|
+
if (eachRectHeight === 0) {
|
135
|
+
rows = 1;
|
136
|
+
}
|
137
|
+
|
138
|
+
for (let j = 0; j < rows; j++) {
|
139
|
+
for (let i = 0; i < columns; i++) {
|
140
|
+
const x = eachRectWidth * i + this.x;
|
141
|
+
const y = eachRectHeight * j + this.y;
|
142
|
+
result.push(new Rect2(x, y, eachRectWidth, eachRectHeight));
|
143
|
+
}
|
144
|
+
}
|
145
|
+
return result;
|
146
|
+
}
|
147
|
+
|
100
148
|
// Returns a rectangle containing this and [point].
|
101
149
|
// [margin] is the minimum distance between the new point and the edge
|
102
150
|
// of the resultant rectangle.
|
@@ -1,9 +1,10 @@
|
|
1
|
-
import AbstractRenderer from './
|
2
|
-
import CanvasRenderer from './
|
3
|
-
import { Editor } from '
|
4
|
-
import { EditorEventType } from '
|
5
|
-
import DummyRenderer from './
|
6
|
-
import { Vec2 } from '
|
1
|
+
import AbstractRenderer from './renderers/AbstractRenderer';
|
2
|
+
import CanvasRenderer from './renderers/CanvasRenderer';
|
3
|
+
import { Editor } from '../Editor';
|
4
|
+
import { EditorEventType } from '../types';
|
5
|
+
import DummyRenderer from './renderers/DummyRenderer';
|
6
|
+
import { Vec2 } from '../geometry/Vec2';
|
7
|
+
import RenderingCache from './caching/RenderingCache';
|
7
8
|
|
8
9
|
export enum RenderingMode {
|
9
10
|
DummyRenderer,
|
@@ -14,6 +15,7 @@ export enum RenderingMode {
|
|
14
15
|
export default class Display {
|
15
16
|
private dryInkRenderer: AbstractRenderer;
|
16
17
|
private wetInkRenderer: AbstractRenderer;
|
18
|
+
private cache: RenderingCache;
|
17
19
|
private resizeSurfacesCallback?: ()=> void;
|
18
20
|
private flattenCallback?: ()=> void;
|
19
21
|
|
@@ -29,6 +31,33 @@ export default class Display {
|
|
29
31
|
throw new Error(`Unknown rendering mode, ${mode}!`);
|
30
32
|
}
|
31
33
|
|
34
|
+
const cacheBlockResolution = Vec2.of(600, 600);
|
35
|
+
this.cache = new RenderingCache({
|
36
|
+
createRenderer: () => {
|
37
|
+
if (mode === RenderingMode.DummyRenderer) {
|
38
|
+
return new DummyRenderer(editor.viewport);
|
39
|
+
} else if (mode !== RenderingMode.CanvasRenderer) {
|
40
|
+
throw new Error('Unspported rendering mode');
|
41
|
+
}
|
42
|
+
|
43
|
+
// Make the canvas slightly larger than each cache block to prevent
|
44
|
+
// seams.
|
45
|
+
const canvas = document.createElement('canvas');
|
46
|
+
canvas.width = cacheBlockResolution.x + 1;
|
47
|
+
canvas.height = cacheBlockResolution.y + 1;
|
48
|
+
const ctx = canvas.getContext('2d');
|
49
|
+
|
50
|
+
return new CanvasRenderer(ctx!, editor.viewport);
|
51
|
+
},
|
52
|
+
isOfCorrectType: (renderer) => {
|
53
|
+
return this.dryInkRenderer.canRenderFromWithoutDataLoss(renderer);
|
54
|
+
},
|
55
|
+
blockResolution: cacheBlockResolution,
|
56
|
+
cacheSize: 500 * 500 * 4 * 200,
|
57
|
+
maxScale: 1.5,
|
58
|
+
minComponentsPerCache: 50,
|
59
|
+
minComponentsToUseCache: 120,
|
60
|
+
});
|
32
61
|
|
33
62
|
this.editor.notifier.on(EditorEventType.DisplayResized, event => {
|
34
63
|
if (event.kind !== EditorEventType.DisplayResized) {
|
@@ -50,6 +79,10 @@ export default class Display {
|
|
50
79
|
return this.dryInkRenderer.displaySize().y;
|
51
80
|
}
|
52
81
|
|
82
|
+
public getCache() {
|
83
|
+
return this.cache;
|
84
|
+
}
|
85
|
+
|
53
86
|
private initializeCanvasRendering() {
|
54
87
|
const dryInkCanvas = document.createElement('canvas');
|
55
88
|
const wetInkCanvas = document.createElement('canvas');
|
@@ -105,6 +138,10 @@ export default class Display {
|
|
105
138
|
return this.dryInkRenderer;
|
106
139
|
}
|
107
140
|
|
141
|
+
public setDraftMode(draftMode: boolean) {
|
142
|
+
this.dryInkRenderer.setDraftMode(draftMode);
|
143
|
+
}
|
144
|
+
|
108
145
|
public getDryInkRenderer(): AbstractRenderer {
|
109
146
|
return this.dryInkRenderer;
|
110
147
|
}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
/* @jest-environment jsdom */
|
2
|
+
|
3
|
+
import Rect2 from '../../geometry/Rect2';
|
4
|
+
import { Vec2 } from '../../geometry/Vec2';
|
5
|
+
import CacheRecord from './CacheRecord';
|
6
|
+
import { createCache } from './testUtils';
|
7
|
+
|
8
|
+
describe('CacheRecord', () => {
|
9
|
+
describe('should map points in the cache renderer\'s region to the cache renderer', () => {
|
10
|
+
const blockResolution = Vec2.of(500, 500);
|
11
|
+
const { cache } = createCache(undefined, {
|
12
|
+
blockResolution,
|
13
|
+
});
|
14
|
+
const state = cache.getSharedState();
|
15
|
+
|
16
|
+
const record = new CacheRecord(() => {}, state);
|
17
|
+
|
18
|
+
describe('region has top left at (0, 0)', () => {
|
19
|
+
it('region as big as the cache block', () => {
|
20
|
+
const transform = record.getTransform(
|
21
|
+
new Rect2(0, 0, 500, 500)
|
22
|
+
);
|
23
|
+
expect(transform.transformVec2(Vec2.unitX)).toMatchObject(Vec2.unitX);
|
24
|
+
expect(transform.transformVec2(Vec2.unitY)).toMatchObject(Vec2.unitY);
|
25
|
+
});
|
26
|
+
|
27
|
+
it('region twice as big as cache block', () => {
|
28
|
+
const transform = record.getTransform(
|
29
|
+
new Rect2(0, 0, 1000, 1000)
|
30
|
+
);
|
31
|
+
|
32
|
+
// A size-one vector on the screen corresponds to a size 1/2 vector on the
|
33
|
+
// cached surface.
|
34
|
+
expect(transform.transformVec2(Vec2.unitX)).toMatchObject(Vec2.of(0.5, 0));
|
35
|
+
expect(transform.transformVec2(Vec2.unitY)).toMatchObject(Vec2.of(0, 0.5));
|
36
|
+
});
|
37
|
+
});
|
38
|
+
|
39
|
+
describe('region has top left at (100, 100)', () => {
|
40
|
+
it('region as big as the cache block', () => {
|
41
|
+
const transform = record.getTransform(
|
42
|
+
new Rect2(100, 100, 500, 500)
|
43
|
+
);
|
44
|
+
expect(transform.transformVec2(Vec2.of(100, 100))).toMatchObject(Vec2.zero);
|
45
|
+
expect(transform.transformVec2(Vec2.of(500, 500))).toMatchObject(Vec2.of(400, 400));
|
46
|
+
});
|
47
|
+
});
|
48
|
+
});
|
49
|
+
});
|
@@ -0,0 +1,73 @@
|
|
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
|
+
|
6
|
+
// Represents a cached renderer/canvas
|
7
|
+
// This is not a [CacheNode] -- it handles cached renderers and does not have sub-renderers.
|
8
|
+
|
9
|
+
export default class CacheRecord {
|
10
|
+
private renderer: AbstractRenderer;
|
11
|
+
private lastUsedCycle: number;
|
12
|
+
private allocd: boolean = false;
|
13
|
+
|
14
|
+
public constructor(
|
15
|
+
private onBeforeDeallocCallback: BeforeDeallocCallback|null,
|
16
|
+
private cacheState: CacheState,
|
17
|
+
) {
|
18
|
+
this.renderer = cacheState.props.createRenderer();
|
19
|
+
this.lastUsedCycle = -1;
|
20
|
+
this.allocd = true;
|
21
|
+
}
|
22
|
+
|
23
|
+
public startRender(): AbstractRenderer {
|
24
|
+
this.lastUsedCycle = this.cacheState.currentRenderingCycle;
|
25
|
+
if (!this.allocd) {
|
26
|
+
throw new Error('Only alloc\'d canvases can be rendered to');
|
27
|
+
}
|
28
|
+
return this.renderer;
|
29
|
+
}
|
30
|
+
|
31
|
+
public dealloc() {
|
32
|
+
this.onBeforeDeallocCallback?.();
|
33
|
+
this.allocd = false;
|
34
|
+
this.onBeforeDeallocCallback = null;
|
35
|
+
this.lastUsedCycle = 0;
|
36
|
+
}
|
37
|
+
|
38
|
+
public isAllocd() {
|
39
|
+
return this.allocd;
|
40
|
+
}
|
41
|
+
|
42
|
+
public realloc(newDeallocCallback: BeforeDeallocCallback) {
|
43
|
+
if (this.allocd) {
|
44
|
+
this.dealloc();
|
45
|
+
}
|
46
|
+
this.allocd = true;
|
47
|
+
this.onBeforeDeallocCallback = newDeallocCallback;
|
48
|
+
this.lastUsedCycle = this.cacheState.currentRenderingCycle;
|
49
|
+
}
|
50
|
+
|
51
|
+
public getLastUsedCycle(): number {
|
52
|
+
return this.lastUsedCycle;
|
53
|
+
}
|
54
|
+
|
55
|
+
// Returns the transformation that maps [drawTo] to this' renderable region
|
56
|
+
// (i.e. a [cacheProps.blockResolution]-sized rectangle with top left at (0, 0))
|
57
|
+
public getTransform(drawTo: Rect2): Mat33 {
|
58
|
+
const transform = Mat33.scaling2D(
|
59
|
+
this.cacheState.props.blockResolution.x / drawTo.size.x
|
60
|
+
).rightMul(
|
61
|
+
Mat33.translation(drawTo.topLeft.times(-1))
|
62
|
+
);
|
63
|
+
|
64
|
+
return transform;
|
65
|
+
}
|
66
|
+
|
67
|
+
public setRenderingRegion(drawTo: Rect2) {
|
68
|
+
this.renderer.setTransform(
|
69
|
+
// Invert to map objects instead of the viewport
|
70
|
+
this.getTransform(drawTo)
|
71
|
+
);
|
72
|
+
}
|
73
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import { BeforeDeallocCallback, PartialCacheState } from './types';
|
2
|
+
import CacheRecord from './CacheRecord';
|
3
|
+
import Rect2 from '../../geometry/Rect2';
|
4
|
+
|
5
|
+
|
6
|
+
export class CacheRecordManager {
|
7
|
+
// Fixed-size array: Cache blocks are assigned indicies into [cachedCanvases].
|
8
|
+
private cacheRecords: CacheRecord[] = [];
|
9
|
+
private maxCanvases: number;
|
10
|
+
|
11
|
+
public constructor(private readonly cacheState: PartialCacheState) {
|
12
|
+
const cacheProps = cacheState.props;
|
13
|
+
this.maxCanvases = Math.ceil(
|
14
|
+
// Assuming four components per pixel:
|
15
|
+
cacheProps.cacheSize / 4 / cacheProps.blockResolution.x / cacheProps.blockResolution.y
|
16
|
+
);
|
17
|
+
}
|
18
|
+
|
19
|
+
public allocCanvas(drawTo: Rect2, onDealloc: BeforeDeallocCallback): CacheRecord {
|
20
|
+
if (this.cacheRecords.length < this.maxCanvases) {
|
21
|
+
const record: CacheRecord = new CacheRecord(
|
22
|
+
onDealloc,
|
23
|
+
{
|
24
|
+
...this.cacheState,
|
25
|
+
recordManager: this,
|
26
|
+
},
|
27
|
+
);
|
28
|
+
record.setRenderingRegion(drawTo);
|
29
|
+
this.cacheRecords.push(record);
|
30
|
+
|
31
|
+
return record;
|
32
|
+
} else {
|
33
|
+
const lru = this.getLeastRecentlyUsedRecord()!;
|
34
|
+
lru.realloc(onDealloc);
|
35
|
+
lru.setRenderingRegion(drawTo);
|
36
|
+
return lru;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
// Returns null if there are no cache records. Returns an unalloc'd record if one exists.
|
41
|
+
private getLeastRecentlyUsedRecord(): CacheRecord|null {
|
42
|
+
this.cacheRecords.sort((a, b) => a.getLastUsedCycle() - b.getLastUsedCycle());
|
43
|
+
return this.cacheRecords[0];
|
44
|
+
}
|
45
|
+
}
|
@@ -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
|
+
}
|