js-draw 0.0.10 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/dist/bundle.js +1 -1
- package/dist/src/Editor.d.ts +2 -2
- package/dist/src/Editor.js +17 -7
- package/dist/src/EditorImage.d.ts +15 -7
- package/dist/src/EditorImage.js +46 -37
- package/dist/src/Pointer.d.ts +3 -2
- package/dist/src/Pointer.js +12 -3
- package/dist/src/SVGLoader.d.ts +6 -2
- package/dist/src/SVGLoader.js +20 -8
- package/dist/src/Viewport.d.ts +4 -0
- package/dist/src/Viewport.js +51 -0
- package/dist/src/components/AbstractComponent.d.ts +9 -2
- package/dist/src/components/AbstractComponent.js +14 -0
- package/dist/src/components/SVGGlobalAttributesObject.d.ts +1 -1
- package/dist/src/components/SVGGlobalAttributesObject.js +1 -1
- package/dist/src/components/Stroke.d.ts +1 -1
- package/dist/src/components/Stroke.js +1 -1
- package/dist/src/components/UnknownSVGObject.d.ts +1 -1
- package/dist/src/components/UnknownSVGObject.js +1 -1
- package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.d.ts +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.js +1 -1
- package/dist/src/components/builders/LineBuilder.d.ts +1 -1
- package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
- package/dist/src/components/builders/types.d.ts +1 -1
- package/dist/src/geometry/Mat33.js +3 -0
- package/dist/src/geometry/Path.d.ts +1 -1
- package/dist/src/geometry/Path.js +102 -69
- package/dist/src/geometry/Rect2.d.ts +1 -0
- package/dist/src/geometry/Rect2.js +47 -9
- package/dist/src/{Display.d.ts → rendering/Display.d.ts} +5 -2
- package/dist/src/{Display.js → rendering/Display.js} +34 -4
- package/dist/src/rendering/caching/CacheRecord.d.ts +19 -0
- package/dist/src/rendering/caching/CacheRecord.js +52 -0
- package/dist/src/rendering/caching/CacheRecordManager.d.ts +11 -0
- package/dist/src/rendering/caching/CacheRecordManager.js +31 -0
- package/dist/src/rendering/caching/RenderingCache.d.ts +12 -0
- package/dist/src/rendering/caching/RenderingCache.js +42 -0
- package/dist/src/rendering/caching/RenderingCacheNode.d.ts +28 -0
- package/dist/src/rendering/caching/RenderingCacheNode.js +301 -0
- package/dist/src/rendering/caching/testUtils.d.ts +9 -0
- package/dist/src/rendering/caching/testUtils.js +20 -0
- package/dist/src/rendering/caching/types.d.ts +21 -0
- package/dist/src/rendering/caching/types.js +1 -0
- package/dist/src/rendering/{AbstractRenderer.d.ts → renderers/AbstractRenderer.d.ts} +20 -9
- package/dist/src/rendering/{AbstractRenderer.js → renderers/AbstractRenderer.js} +37 -3
- package/dist/src/rendering/{CanvasRenderer.d.ts → renderers/CanvasRenderer.d.ts} +10 -5
- package/dist/src/rendering/{CanvasRenderer.js → renderers/CanvasRenderer.js} +60 -20
- package/dist/src/rendering/{DummyRenderer.d.ts → renderers/DummyRenderer.d.ts} +9 -5
- package/dist/src/rendering/{DummyRenderer.js → renderers/DummyRenderer.js} +35 -4
- package/dist/src/rendering/{SVGRenderer.d.ts → renderers/SVGRenderer.d.ts} +7 -5
- package/dist/src/rendering/{SVGRenderer.js → renderers/SVGRenderer.js} +35 -18
- package/dist/src/testing/createEditor.js +1 -1
- package/dist/src/toolbar/HTMLToolbar.d.ts +2 -1
- package/dist/src/toolbar/HTMLToolbar.js +165 -154
- package/dist/src/toolbar/icons.d.ts +10 -0
- package/dist/src/toolbar/icons.js +180 -0
- package/dist/src/toolbar/localization.d.ts +4 -1
- package/dist/src/toolbar/localization.js +4 -1
- package/dist/src/toolbar/types.d.ts +4 -0
- package/dist/src/tools/PanZoom.d.ts +9 -6
- package/dist/src/tools/PanZoom.js +30 -21
- package/dist/src/tools/Pen.js +8 -3
- package/dist/src/tools/SelectionTool.js +9 -24
- package/dist/src/tools/ToolController.d.ts +5 -6
- package/dist/src/tools/ToolController.js +8 -10
- package/dist/src/tools/localization.d.ts +1 -0
- package/dist/src/tools/localization.js +1 -0
- package/dist/src/types.d.ts +2 -1
- package/package.json +1 -1
- package/src/Editor.ts +19 -8
- package/src/EditorImage.test.ts +2 -2
- package/src/EditorImage.ts +58 -42
- package/src/Pointer.ts +13 -4
- package/src/SVGLoader.ts +36 -10
- package/src/Viewport.ts +68 -0
- package/src/components/AbstractComponent.ts +21 -2
- package/src/components/SVGGlobalAttributesObject.ts +2 -2
- package/src/components/Stroke.ts +2 -2
- package/src/components/UnknownSVGObject.ts +2 -2
- package/src/components/builders/ArrowBuilder.ts +1 -1
- package/src/components/builders/FreehandLineBuilder.ts +2 -2
- package/src/components/builders/LineBuilder.ts +1 -1
- package/src/components/builders/RectangleBuilder.ts +1 -1
- package/src/components/builders/types.ts +1 -1
- package/src/geometry/Mat33.ts +3 -0
- package/src/geometry/Path.fromString.test.ts +94 -4
- package/src/geometry/Path.toString.test.ts +12 -2
- package/src/geometry/Path.ts +107 -71
- package/src/geometry/Rect2.test.ts +47 -8
- package/src/geometry/Rect2.ts +57 -9
- package/src/{Display.ts → rendering/Display.ts} +39 -6
- package/src/rendering/caching/CacheRecord.test.ts +49 -0
- package/src/rendering/caching/CacheRecord.ts +73 -0
- package/src/rendering/caching/CacheRecordManager.ts +45 -0
- package/src/rendering/caching/RenderingCache.test.ts +44 -0
- package/src/rendering/caching/RenderingCache.ts +63 -0
- package/src/rendering/caching/RenderingCacheNode.ts +378 -0
- package/src/rendering/caching/testUtils.ts +35 -0
- package/src/rendering/caching/types.ts +39 -0
- package/src/rendering/{AbstractRenderer.ts → renderers/AbstractRenderer.ts} +57 -9
- package/src/rendering/{CanvasRenderer.ts → renderers/CanvasRenderer.ts} +74 -25
- package/src/rendering/renderers/DummyRenderer.test.ts +43 -0
- package/src/rendering/{DummyRenderer.ts → renderers/DummyRenderer.ts} +50 -7
- package/src/rendering/{SVGRenderer.ts → renderers/SVGRenderer.ts} +39 -23
- package/src/testing/createEditor.ts +1 -1
- package/src/toolbar/HTMLToolbar.ts +199 -170
- package/src/toolbar/icons.ts +203 -0
- package/src/toolbar/localization.ts +9 -2
- package/src/toolbar/toolbar.css +21 -8
- package/src/toolbar/types.ts +5 -0
- package/src/tools/PanZoom.ts +37 -27
- package/src/tools/Pen.ts +7 -3
- package/src/tools/SelectionTool.test.ts +1 -1
- package/src/tools/SelectionTool.ts +12 -33
- package/src/tools/ToolController.ts +3 -5
- package/src/tools/localization.ts +2 -0
- package/src/types.ts +10 -3
- package/tsconfig.json +1 -0
- package/dist/__mocks__/coloris.d.ts +0 -2
- package/dist/__mocks__/coloris.js +0 -5
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
|
|
@@ -367,6 +371,7 @@ export default class Path {
|
|
367
371
|
|
368
372
|
let lastPos: Point2 = Vec2.zero;
|
369
373
|
let firstPos: Point2|null = null;
|
374
|
+
let startPos: Point2|null = null;
|
370
375
|
let isFirstCommand: boolean = true;
|
371
376
|
const commands: PathCommand[] = [];
|
372
377
|
|
@@ -409,19 +414,67 @@ export default class Path {
|
|
409
414
|
endPoint,
|
410
415
|
});
|
411
416
|
};
|
417
|
+
const commandArgCounts: Record<string, number> = {
|
418
|
+
'm': 1,
|
419
|
+
'l': 1,
|
420
|
+
'c': 3,
|
421
|
+
'q': 2,
|
422
|
+
'z': 0,
|
423
|
+
'h': 1,
|
424
|
+
'v': 1,
|
425
|
+
};
|
412
426
|
|
413
427
|
// Each command: Command character followed by anything that isn't a command character
|
414
|
-
const commandExp = /([
|
428
|
+
const commandExp = /([MZLHVCSQTA])\s*([^MZLHVCSQTA]*)/ig;
|
415
429
|
let current;
|
416
430
|
while ((current = commandExp.exec(pathString)) !== null) {
|
417
|
-
const argParts = current[2].trim().split(/[^0-
|
431
|
+
const argParts = current[2].trim().split(/[^0-9Ee.-]/).filter(
|
418
432
|
part => part.length > 0
|
419
|
-
)
|
420
|
-
|
433
|
+
).reduce((accumualtor: string[], current: string): string[] => {
|
434
|
+
// As of 09/2022, iOS Safari doesn't support support lookbehind in regular
|
435
|
+
// expressions. As such, we need an alternative.
|
436
|
+
// Because '-' can be used as a path separator, unless preceeded by an 'e' (as in 1e-5),
|
437
|
+
// we need special cases:
|
438
|
+
current = current.replace(/([^eE])[-]/g, '$1 -');
|
439
|
+
const parts = current.split(' -');
|
440
|
+
if (parts[0] !== '') {
|
441
|
+
accumualtor.push(parts[0]);
|
442
|
+
}
|
443
|
+
accumualtor.push(...parts.slice(1).map(part => `-${part}`));
|
444
|
+
return accumualtor;
|
445
|
+
}, []);
|
446
|
+
|
447
|
+
let numericArgs = argParts.map(arg => parseFloat(arg));
|
448
|
+
|
449
|
+
let commandChar = current[1].toLowerCase();
|
450
|
+
let uppercaseCommand = current[1] !== commandChar;
|
451
|
+
|
452
|
+
// Convert commands that don't take points into commands that do.
|
453
|
+
if (commandChar === 'v' || commandChar === 'h') {
|
454
|
+
numericArgs = numericArgs.reduce((accumulator: number[], current: number): number[] => {
|
455
|
+
if (commandChar === 'v') {
|
456
|
+
return accumulator.concat(uppercaseCommand ? lastPos.x : 0, current);
|
457
|
+
} else {
|
458
|
+
return accumulator.concat(current, uppercaseCommand ? lastPos.y : 0);
|
459
|
+
}
|
460
|
+
}, []);
|
461
|
+
commandChar = 'l';
|
462
|
+
} else if (commandChar === 'z') {
|
463
|
+
if (firstPos) {
|
464
|
+
numericArgs = [ firstPos.x, firstPos.y ];
|
465
|
+
firstPos = lastPos;
|
466
|
+
} else {
|
467
|
+
continue;
|
468
|
+
}
|
421
469
|
|
422
|
-
|
423
|
-
|
424
|
-
|
470
|
+
// 'z' always acts like an uppercase lineTo(startPos)
|
471
|
+
uppercaseCommand = true;
|
472
|
+
commandChar = 'l';
|
473
|
+
}
|
474
|
+
|
475
|
+
|
476
|
+
const commandArgCount: number = commandArgCounts[commandChar] ?? 0;
|
477
|
+
const allArgs = numericArgs.reduce((
|
425
478
|
accumulator: Point2[], current, index, parts
|
426
479
|
): Point2[] => {
|
427
480
|
if (index % 2 !== 0) {
|
@@ -431,82 +484,65 @@ export default class Path {
|
|
431
484
|
} else {
|
432
485
|
return accumulator;
|
433
486
|
}
|
434
|
-
}, []).map((coordinate
|
487
|
+
}, []).map((coordinate, index): Point2 => {
|
435
488
|
// Lowercase commands are relative, uppercase commands use absolute
|
436
489
|
// positioning
|
490
|
+
let newPos;
|
437
491
|
if (uppercaseCommand) {
|
438
|
-
|
439
|
-
return coordinate;
|
492
|
+
newPos = coordinate;
|
440
493
|
} else {
|
441
|
-
|
442
|
-
return lastPos;
|
494
|
+
newPos = lastPos.plus(coordinate);
|
443
495
|
}
|
444
|
-
});
|
445
496
|
|
446
|
-
|
447
|
-
|
448
|
-
switch (commandChar.toLowerCase()) {
|
449
|
-
case 'm':
|
450
|
-
expectedPointArgCount = 1;
|
451
|
-
moveTo(args[0]);
|
452
|
-
break;
|
453
|
-
case 'l':
|
454
|
-
expectedPointArgCount = 1;
|
455
|
-
lineTo(args[0]);
|
456
|
-
break;
|
457
|
-
case 'z':
|
458
|
-
expectedPointArgCount = 0;
|
459
|
-
// firstPos can be null if the stroke data is just 'z'.
|
460
|
-
if (firstPos) {
|
461
|
-
lineTo(firstPos);
|
497
|
+
if ((index + 1) % commandArgCount === 0) {
|
498
|
+
lastPos = newPos;
|
462
499
|
}
|
463
|
-
break;
|
464
|
-
case 'c':
|
465
|
-
expectedPointArgCount = 3;
|
466
|
-
cubicBezierTo(args[0], args[1], args[2]);
|
467
|
-
break;
|
468
|
-
case 'q':
|
469
|
-
expectedPointArgCount = 2;
|
470
|
-
quadraticBeierTo(args[0], args[1]);
|
471
|
-
break;
|
472
500
|
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
501
|
+
return newPos;
|
502
|
+
});
|
503
|
+
|
504
|
+
if (allArgs.length % commandArgCount !== 0) {
|
505
|
+
throw new Error([
|
506
|
+
`Incorrect number of arguments: got ${JSON.stringify(allArgs)} with a length of ${allArgs.length} ≠ ${commandArgCount}k, k ∈ ℤ.`,
|
507
|
+
`The number of arguments to ${commandChar} must be a multiple of ${commandArgCount}!`,
|
508
|
+
`Command: ${current[0]}`,
|
509
|
+
].join('\n'));
|
510
|
+
}
|
483
511
|
|
484
|
-
|
485
|
-
|
486
|
-
expectedPointArgCount = 0;
|
512
|
+
for (let argPos = 0; argPos < allArgs.length; argPos += commandArgCount) {
|
513
|
+
const args = allArgs.slice(argPos, argPos + commandArgCount);
|
487
514
|
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
515
|
+
switch (commandChar.toLowerCase()) {
|
516
|
+
case 'm':
|
517
|
+
if (argPos === 0) {
|
518
|
+
moveTo(args[0]);
|
519
|
+
} else {
|
520
|
+
lineTo(args[0]);
|
521
|
+
}
|
522
|
+
break;
|
523
|
+
case 'l':
|
524
|
+
lineTo(args[0]);
|
525
|
+
break;
|
526
|
+
case 'c':
|
527
|
+
cubicBezierTo(args[0], args[1], args[2]);
|
528
|
+
break;
|
529
|
+
case 'q':
|
530
|
+
quadraticBeierTo(args[0], args[1]);
|
531
|
+
break;
|
532
|
+
default:
|
533
|
+
throw new Error(`Unknown path command ${commandChar}`);
|
492
534
|
}
|
493
|
-
break;
|
494
|
-
default:
|
495
|
-
throw new Error(`Unknown path command ${commandChar}`);
|
496
|
-
}
|
497
535
|
|
498
|
-
|
499
|
-
throw new Error(`
|
500
|
-
Incorrect number of arguments: got ${JSON.stringify(args)} with a length of ${args.length} ≠ ${expectedPointArgCount}.
|
501
|
-
`.trim());
|
536
|
+
isFirstCommand = false;
|
502
537
|
}
|
503
538
|
|
504
|
-
if (
|
505
|
-
firstPos ??=
|
539
|
+
if (allArgs.length > 0) {
|
540
|
+
firstPos ??= allArgs[0];
|
541
|
+
startPos ??= firstPos;
|
542
|
+
lastPos = allArgs[allArgs.length - 1];
|
506
543
|
}
|
507
|
-
isFirstCommand = false;
|
508
544
|
}
|
509
545
|
|
510
|
-
return new Path(
|
546
|
+
return new Path(startPos ?? Vec2.zero, commands);
|
511
547
|
}
|
512
548
|
}
|
@@ -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');
|
@@ -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
|
+
}
|