js-draw 0.0.10 → 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 (95) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/bundle.js +1 -1
  3. package/dist/src/Editor.d.ts +2 -2
  4. package/dist/src/Editor.js +13 -7
  5. package/dist/src/EditorImage.d.ts +15 -7
  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/Viewport.d.ts +4 -0
  10. package/dist/src/Viewport.js +41 -0
  11. package/dist/src/components/AbstractComponent.d.ts +3 -2
  12. package/dist/src/components/AbstractComponent.js +3 -0
  13. package/dist/src/components/SVGGlobalAttributesObject.d.ts +1 -1
  14. package/dist/src/components/SVGGlobalAttributesObject.js +1 -1
  15. package/dist/src/components/Stroke.d.ts +1 -1
  16. package/dist/src/components/UnknownSVGObject.d.ts +1 -1
  17. package/dist/src/components/UnknownSVGObject.js +1 -1
  18. package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
  19. package/dist/src/components/builders/FreehandLineBuilder.d.ts +1 -1
  20. package/dist/src/components/builders/LineBuilder.d.ts +1 -1
  21. package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
  22. package/dist/src/components/builders/types.d.ts +1 -1
  23. package/dist/src/geometry/Mat33.js +3 -0
  24. package/dist/src/geometry/Path.d.ts +1 -1
  25. package/dist/src/geometry/Path.js +5 -3
  26. package/dist/src/geometry/Rect2.d.ts +1 -0
  27. package/dist/src/geometry/Rect2.js +28 -1
  28. package/dist/src/{Display.d.ts → rendering/Display.d.ts} +5 -2
  29. package/dist/src/{Display.js → rendering/Display.js} +33 -4
  30. package/dist/src/rendering/caching/CacheRecord.d.ts +19 -0
  31. package/dist/src/rendering/caching/CacheRecord.js +51 -0
  32. package/dist/src/rendering/caching/CacheRecordManager.d.ts +11 -0
  33. package/dist/src/rendering/caching/CacheRecordManager.js +39 -0
  34. package/dist/src/rendering/caching/RenderingCache.d.ts +12 -0
  35. package/dist/src/rendering/caching/RenderingCache.js +36 -0
  36. package/dist/src/rendering/caching/RenderingCacheNode.d.ts +28 -0
  37. package/dist/src/rendering/caching/RenderingCacheNode.js +294 -0
  38. package/dist/src/rendering/caching/testUtils.d.ts +9 -0
  39. package/dist/src/rendering/caching/testUtils.js +20 -0
  40. package/dist/src/rendering/caching/types.d.ts +20 -0
  41. package/dist/src/rendering/caching/types.js +1 -0
  42. package/dist/src/rendering/{AbstractRenderer.d.ts → renderers/AbstractRenderer.d.ts} +18 -8
  43. package/dist/src/rendering/{AbstractRenderer.js → renderers/AbstractRenderer.js} +36 -2
  44. package/dist/src/rendering/{CanvasRenderer.d.ts → renderers/CanvasRenderer.d.ts} +10 -5
  45. package/dist/src/rendering/{CanvasRenderer.js → renderers/CanvasRenderer.js} +60 -20
  46. package/dist/src/rendering/{DummyRenderer.d.ts → renderers/DummyRenderer.d.ts} +9 -5
  47. package/dist/src/rendering/{DummyRenderer.js → renderers/DummyRenderer.js} +35 -4
  48. package/dist/src/rendering/{SVGRenderer.d.ts → renderers/SVGRenderer.d.ts} +4 -3
  49. package/dist/src/rendering/{SVGRenderer.js → renderers/SVGRenderer.js} +14 -11
  50. package/dist/src/testing/createEditor.js +1 -1
  51. package/dist/src/toolbar/HTMLToolbar.js +1 -1
  52. package/dist/src/tools/SelectionTool.js +9 -24
  53. package/dist/src/types.d.ts +2 -1
  54. package/package.json +1 -1
  55. package/src/Editor.ts +15 -8
  56. package/src/EditorImage.test.ts +2 -2
  57. package/src/EditorImage.ts +53 -41
  58. package/src/SVGLoader.ts +11 -8
  59. package/src/Viewport.ts +56 -0
  60. package/src/components/AbstractComponent.ts +6 -2
  61. package/src/components/SVGGlobalAttributesObject.ts +2 -2
  62. package/src/components/Stroke.ts +1 -1
  63. package/src/components/UnknownSVGObject.ts +2 -2
  64. package/src/components/builders/ArrowBuilder.ts +1 -1
  65. package/src/components/builders/FreehandLineBuilder.ts +1 -1
  66. package/src/components/builders/LineBuilder.ts +1 -1
  67. package/src/components/builders/RectangleBuilder.ts +1 -1
  68. package/src/components/builders/types.ts +1 -1
  69. package/src/geometry/Mat33.ts +3 -0
  70. package/src/geometry/Path.toString.test.ts +12 -2
  71. package/src/geometry/Path.ts +8 -4
  72. package/src/geometry/Rect2.test.ts +38 -8
  73. package/src/geometry/Rect2.ts +32 -1
  74. package/src/{Display.ts → rendering/Display.ts} +38 -6
  75. package/src/rendering/caching/CacheRecord.test.ts +49 -0
  76. package/src/rendering/caching/CacheRecord.ts +72 -0
  77. package/src/rendering/caching/CacheRecordManager.ts +55 -0
  78. package/src/rendering/caching/RenderingCache.test.ts +44 -0
  79. package/src/rendering/caching/RenderingCache.ts +56 -0
  80. package/src/rendering/caching/RenderingCacheNode.ts +365 -0
  81. package/src/rendering/caching/testUtils.ts +34 -0
  82. package/src/rendering/caching/types.ts +35 -0
  83. package/src/rendering/{AbstractRenderer.ts → renderers/AbstractRenderer.ts} +55 -8
  84. package/src/rendering/{CanvasRenderer.ts → renderers/CanvasRenderer.ts} +74 -25
  85. package/src/rendering/renderers/DummyRenderer.test.ts +43 -0
  86. package/src/rendering/{DummyRenderer.ts → renderers/DummyRenderer.ts} +50 -7
  87. package/src/rendering/{SVGRenderer.ts → renderers/SVGRenderer.ts} +17 -13
  88. package/src/testing/createEditor.ts +1 -1
  89. package/src/toolbar/HTMLToolbar.ts +1 -1
  90. package/src/tools/SelectionTool.test.ts +1 -1
  91. package/src/tools/SelectionTool.ts +12 -33
  92. package/src/types.ts +10 -3
  93. package/tsconfig.json +1 -0
  94. package/dist/__mocks__/coloris.d.ts +0 -2
  95. package/dist/__mocks__/coloris.js +0 -5
@@ -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,9 +51,13 @@ export default abstract class AbstractRenderer {
43
51
  controlPoint: Point2, endPoint: Point2,
44
52
  ): void;
45
53
 
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
+
46
58
  public setDraftMode(_draftMode: boolean) { }
47
59
 
48
- private objectLevel: number = 0;
60
+ protected objectLevel: number = 0;
49
61
  private currentPaths: RenderablePathSpec[]|null = null;
50
62
  private flushPath() {
51
63
  if (!this.currentPaths) {
@@ -110,7 +122,8 @@ export default abstract class AbstractRenderer {
110
122
  }
111
123
 
112
124
  // Note the start/end of an object with the given bounding box.
113
- public startObject(_boundingBox: Rect2) {
125
+ // Renderers are not required to support [clip]
126
+ public startObject(_boundingBox: Rect2, _clip?: boolean) {
114
127
  this.currentPaths = [];
115
128
  this.objectLevel ++;
116
129
  }
@@ -134,4 +147,38 @@ export default abstract class AbstractRenderer {
134
147
 
135
148
  // Draw a representation of [points]. Intended for debugging.
136
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
+ }
137
184
  }
@@ -1,8 +1,9 @@
1
- import Color4 from '../Color4';
2
- import Rect2 from '../geometry/Rect2';
3
- import { Point2, Vec2 } from '../geometry/Vec2';
4
- import Vec3 from '../geometry/Vec3';
5
- import Viewport from '../Viewport';
1
+ import Color4 from '../../Color4';
2
+ import Mat33 from '../../geometry/Mat33';
3
+ import Rect2 from '../../geometry/Rect2';
4
+ import { Point2, Vec2 } from '../../geometry/Vec2';
5
+ import Vec3 from '../../geometry/Vec3';
6
+ import Viewport from '../../Viewport';
6
7
  import AbstractRenderer, { RenderablePathSpec, RenderingStyle } from './AbstractRenderer';
7
8
 
8
9
  export default class CanvasRenderer extends AbstractRenderer {
@@ -25,6 +26,30 @@ export default class CanvasRenderer extends AbstractRenderer {
25
26
  this.setDraftMode(false);
26
27
  }
27
28
 
29
+ public canRenderFromWithoutDataLoss(other: AbstractRenderer) {
30
+ return other instanceof CanvasRenderer;
31
+ }
32
+
33
+ public renderFromOtherOfSameType(transformBy: Mat33, other: AbstractRenderer): void {
34
+ if (!(other instanceof CanvasRenderer)) {
35
+ throw new Error(`${other} cannot be rendered onto ${this}`);
36
+ }
37
+ transformBy = this.getCanvasToScreenTransform().rightMul(transformBy);
38
+ this.ctx.save();
39
+ // From MDN, transform(a,b,c,d,e,f)
40
+ // takes input such that
41
+ // ⎡ a c e ⎤
42
+ // ⎢ b d f ⎥ transforms content drawn to [ctx].
43
+ // ⎣ 0 0 1 ⎦
44
+ this.ctx.transform(
45
+ transformBy.a1, transformBy.b1, // a, b
46
+ transformBy.a2, transformBy.b2, // c, d
47
+ transformBy.a3, transformBy.b3, // e, f
48
+ );
49
+ this.ctx.drawImage(other.ctx.canvas, 0, 0);
50
+ this.ctx.restore();
51
+ }
52
+
28
53
  // Set parameters for lower/higher quality rendering
29
54
  public setDraftMode(draftMode: boolean) {
30
55
  if (draftMode) {
@@ -50,7 +75,7 @@ export default class CanvasRenderer extends AbstractRenderer {
50
75
  }
51
76
 
52
77
  protected beginPath(startPoint: Point2) {
53
- startPoint = this.viewport.canvasToScreen(startPoint);
78
+ startPoint = this.canvasToScreen(startPoint);
54
79
 
55
80
  this.ctx.beginPath();
56
81
  this.ctx.moveTo(startPoint.x, startPoint.y);
@@ -62,7 +87,7 @@ export default class CanvasRenderer extends AbstractRenderer {
62
87
 
63
88
  if (style.stroke) {
64
89
  this.ctx.strokeStyle = style.stroke.color.toHexString();
65
- this.ctx.lineWidth = this.viewport.getScaleFactor() * style.stroke.width;
90
+ this.ctx.lineWidth = this.getSizeOfCanvasPixelOnScreen() * style.stroke.width;
66
91
  this.ctx.stroke();
67
92
  }
68
93
 
@@ -70,19 +95,19 @@ export default class CanvasRenderer extends AbstractRenderer {
70
95
  }
71
96
 
72
97
  protected lineTo(point: Point2) {
73
- point = this.viewport.canvasToScreen(point);
98
+ point = this.canvasToScreen(point);
74
99
  this.ctx.lineTo(point.x, point.y);
75
100
  }
76
101
 
77
102
  protected moveTo(point: Point2) {
78
- point = this.viewport.canvasToScreen(point);
103
+ point = this.canvasToScreen(point);
79
104
  this.ctx.moveTo(point.x, point.y);
80
105
  }
81
106
 
82
107
  protected traceCubicBezierCurve(p1: Point2, p2: Point2, p3: Point2) {
83
- p1 = this.viewport.canvasToScreen(p1);
84
- p2 = this.viewport.canvasToScreen(p2);
85
- p3 = this.viewport.canvasToScreen(p3);
108
+ p1 = this.canvasToScreen(p1);
109
+ p2 = this.canvasToScreen(p2);
110
+ p3 = this.canvasToScreen(p3);
86
111
 
87
112
  // Approximate the curve if small enough.
88
113
  const delta1 = p2.minus(p1);
@@ -96,8 +121,8 @@ export default class CanvasRenderer extends AbstractRenderer {
96
121
  }
97
122
 
98
123
  protected traceQuadraticBezierCurve(controlPoint: Vec3, endPoint: Vec3) {
99
- controlPoint = this.viewport.canvasToScreen(controlPoint);
100
- endPoint = this.viewport.canvasToScreen(endPoint);
124
+ controlPoint = this.canvasToScreen(controlPoint);
125
+ endPoint = this.canvasToScreen(endPoint);
101
126
 
102
127
  // Approximate the curve with a line if small enough
103
128
  const delta = controlPoint.minus(endPoint);
@@ -118,23 +143,35 @@ export default class CanvasRenderer extends AbstractRenderer {
118
143
  super.drawPath(path);
119
144
  }
120
145
 
121
- public startObject(boundingBox: Rect2) {
122
- // Should we ignore all objects within this object's bbox?
123
- const diagonal = this.viewport.canvasToScreenTransform.transformVec3(boundingBox.size);
124
-
125
- const bothDimenMinSize = this.minRenderSizeBothDimens;
126
- const bothTooSmall = Math.abs(diagonal.x) < bothDimenMinSize && Math.abs(diagonal.y) < bothDimenMinSize;
127
- const anyDimenMinSize = this.minRenderSizeAnyDimen;
128
- const anyTooSmall = Math.abs(diagonal.x) < anyDimenMinSize || Math.abs(diagonal.y) < anyDimenMinSize;
129
-
130
- if (bothTooSmall || anyTooSmall) {
146
+ private clipLevels: number[] = [];
147
+ public startObject(boundingBox: Rect2, clip: boolean) {
148
+ if (this.isTooSmallToRender(boundingBox)) {
131
149
  this.ignoreObjectsAboveLevel = this.getNestingLevel();
132
150
  this.ignoringObject = true;
133
151
  }
134
152
 
135
153
  super.startObject(boundingBox);
154
+
155
+ if (!this.ignoringObject && clip) {
156
+ this.clipLevels.push(this.objectLevel);
157
+ this.ctx.save();
158
+ this.ctx.beginPath();
159
+ for (const corner of boundingBox.corners) {
160
+ const screenCorner = this.canvasToScreen(corner);
161
+ this.ctx.lineTo(screenCorner.x, screenCorner.y);
162
+ }
163
+ this.ctx.clip();
164
+ }
136
165
  }
166
+
137
167
  public endObject() {
168
+ if (!this.ignoringObject && this.clipLevels.length > 0) {
169
+ if (this.clipLevels[this.clipLevels.length - 1] === this.objectLevel) {
170
+ this.ctx.restore();
171
+ this.clipLevels.pop();
172
+ }
173
+ }
174
+
138
175
  super.endObject();
139
176
 
140
177
  // If exiting an object with a too-small-to-draw bounding box,
@@ -148,7 +185,7 @@ export default class CanvasRenderer extends AbstractRenderer {
148
185
  const pointRadius = 10;
149
186
 
150
187
  for (let i = 0; i < points.length; i++) {
151
- const point = this.viewport.canvasToScreen(points[i]);
188
+ const point = this.canvasToScreen(points[i]);
152
189
 
153
190
  this.ctx.beginPath();
154
191
  this.ctx.arc(point.x, point.y, pointRadius, 0, Math.PI * 2);
@@ -167,4 +204,16 @@ export default class CanvasRenderer extends AbstractRenderer {
167
204
  this.ctx.fillText(`${i}`, point.x, point.y, pointRadius * 2);
168
205
  }
169
206
  }
207
+
208
+ public isTooSmallToRender(rect: Rect2): boolean {
209
+ // Should we ignore all objects within this object's bbox?
210
+ const diagonal = this.getCanvasToScreenTransform().transformVec3(rect.size);
211
+
212
+ const bothDimenMinSize = this.minRenderSizeBothDimens;
213
+ const bothTooSmall = Math.abs(diagonal.x) < bothDimenMinSize && Math.abs(diagonal.y) < bothDimenMinSize;
214
+ const anyDimenMinSize = this.minRenderSizeAnyDimen;
215
+ const anyTooSmall = Math.abs(diagonal.x) < anyDimenMinSize || Math.abs(diagonal.y) < anyDimenMinSize;
216
+
217
+ return bothTooSmall || anyTooSmall;
218
+ }
170
219
  }