js-draw 0.0.8 → 0.1.0

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