js-draw 0.1.11 → 0.1.12
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 +7 -0
- package/dist/bundle.js +1 -1
- package/dist/src/Editor.d.ts +4 -2
- package/dist/src/Editor.js +30 -10
- package/dist/src/EditorImage.d.ts +1 -1
- package/dist/src/EditorImage.js +2 -2
- package/dist/src/Pointer.d.ts +1 -1
- package/dist/src/Pointer.js +1 -1
- package/dist/src/SVGLoader.d.ts +1 -1
- package/dist/src/SVGLoader.js +14 -6
- package/dist/src/Viewport.d.ts +8 -25
- package/dist/src/Viewport.js +15 -10
- package/dist/src/commands/Command.d.ts +2 -2
- package/dist/src/commands/Command.js +4 -4
- package/dist/src/commands/Duplicate.d.ts +1 -1
- package/dist/src/commands/Duplicate.js +1 -1
- package/dist/src/commands/Erase.d.ts +1 -1
- package/dist/src/commands/Erase.js +1 -1
- package/dist/src/commands/localization.d.ts +1 -1
- package/dist/src/components/AbstractComponent.d.ts +3 -3
- package/dist/src/components/AbstractComponent.js +2 -2
- package/dist/src/components/SVGGlobalAttributesObject.d.ts +3 -3
- package/dist/src/components/SVGGlobalAttributesObject.js +1 -1
- package/dist/src/components/Stroke.d.ts +4 -4
- package/dist/src/components/Stroke.js +2 -2
- package/dist/src/components/Text.d.ts +3 -3
- package/dist/src/components/Text.js +3 -3
- package/dist/src/components/UnknownSVGObject.d.ts +3 -3
- package/dist/src/components/UnknownSVGObject.js +1 -1
- package/dist/src/components/builders/ArrowBuilder.d.ts +1 -1
- package/dist/src/components/builders/ArrowBuilder.js +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.d.ts +8 -3
- package/dist/src/components/builders/FreehandLineBuilder.js +142 -71
- package/dist/src/components/builders/LineBuilder.d.ts +1 -1
- package/dist/src/components/builders/LineBuilder.js +1 -1
- package/dist/src/components/builders/RectangleBuilder.d.ts +1 -1
- package/dist/src/components/builders/RectangleBuilder.js +3 -3
- package/dist/src/components/builders/types.d.ts +1 -1
- package/dist/src/localization.d.ts +1 -0
- package/dist/src/localization.js +5 -1
- package/dist/src/localizations/es.js +1 -1
- package/dist/src/{geometry → math}/LineSegment2.d.ts +0 -0
- package/dist/src/{geometry → math}/LineSegment2.js +0 -0
- package/dist/src/{geometry → math}/Mat33.d.ts +0 -0
- package/dist/src/{geometry → math}/Mat33.js +0 -0
- package/dist/src/{geometry → math}/Path.d.ts +2 -1
- package/dist/src/{geometry → math}/Path.js +58 -51
- package/dist/src/{geometry → math}/Rect2.d.ts +0 -0
- package/dist/src/{geometry → math}/Rect2.js +0 -0
- package/dist/src/{geometry → math}/Vec2.d.ts +0 -0
- package/dist/src/{geometry → math}/Vec2.js +0 -0
- package/dist/src/{geometry → math}/Vec3.d.ts +1 -1
- package/dist/src/{geometry → math}/Vec3.js +1 -1
- package/dist/src/math/rounding.d.ts +3 -0
- package/dist/src/math/rounding.js +120 -0
- package/dist/src/rendering/Display.d.ts +3 -1
- package/dist/src/rendering/Display.js +16 -10
- package/dist/src/rendering/caching/CacheRecord.d.ts +2 -2
- package/dist/src/rendering/caching/CacheRecord.js +1 -1
- package/dist/src/rendering/caching/CacheRecordManager.d.ts +1 -1
- package/dist/src/rendering/caching/RenderingCache.js +1 -1
- package/dist/src/rendering/caching/RenderingCacheNode.d.ts +2 -1
- package/dist/src/rendering/caching/RenderingCacheNode.js +18 -7
- package/dist/src/rendering/caching/testUtils.js +1 -1
- package/dist/src/rendering/caching/types.d.ts +1 -1
- package/dist/src/rendering/localization.d.ts +2 -0
- package/dist/src/rendering/localization.js +2 -0
- package/dist/src/rendering/renderers/AbstractRenderer.d.ts +4 -4
- package/dist/src/rendering/renderers/AbstractRenderer.js +2 -2
- package/dist/src/rendering/renderers/CanvasRenderer.d.ts +4 -4
- package/dist/src/rendering/renderers/CanvasRenderer.js +1 -1
- package/dist/src/rendering/renderers/DummyRenderer.d.ts +4 -4
- package/dist/src/rendering/renderers/DummyRenderer.js +1 -1
- package/dist/src/rendering/renderers/SVGRenderer.d.ts +3 -3
- package/dist/src/rendering/renderers/SVGRenderer.js +8 -2
- package/dist/src/rendering/renderers/TextOnlyRenderer.d.ts +5 -3
- package/dist/src/rendering/renderers/TextOnlyRenderer.js +13 -3
- package/dist/src/toolbar/icons.d.ts +3 -0
- package/dist/src/toolbar/icons.js +142 -132
- package/dist/src/toolbar/localization.d.ts +2 -1
- package/dist/src/toolbar/localization.js +2 -1
- package/dist/src/toolbar/makeColorInput.js +2 -1
- package/dist/src/toolbar/widgets/ActionButtonWidget.d.ts +13 -0
- package/dist/src/toolbar/widgets/ActionButtonWidget.js +21 -0
- package/dist/src/toolbar/widgets/BaseWidget.js +2 -0
- package/dist/src/toolbar/widgets/HandToolWidget.js +3 -3
- package/dist/src/toolbar/widgets/SelectionWidget.d.ts +0 -1
- package/dist/src/toolbar/widgets/SelectionWidget.js +23 -30
- package/dist/src/tools/Eraser.js +1 -1
- package/dist/src/tools/PanZoom.d.ts +1 -1
- package/dist/src/tools/PanZoom.js +24 -14
- package/dist/src/tools/SelectionTool.d.ts +3 -3
- package/dist/src/tools/SelectionTool.js +6 -6
- package/dist/src/tools/TextTool.js +1 -1
- package/dist/src/types.d.ts +4 -4
- package/package.json +1 -1
- package/src/Editor.ts +34 -12
- package/src/EditorImage.test.ts +2 -4
- package/src/EditorImage.ts +2 -2
- package/src/Pointer.ts +1 -1
- package/src/SVGLoader.ts +14 -6
- package/src/Viewport.ts +19 -17
- package/src/commands/Command.ts +5 -5
- package/src/commands/Duplicate.ts +1 -1
- package/src/commands/Erase.ts +1 -1
- package/src/commands/localization.ts +1 -1
- package/src/components/AbstractComponent.ts +4 -4
- package/src/components/SVGGlobalAttributesObject.ts +3 -3
- package/src/components/Stroke.test.ts +3 -5
- package/src/components/Stroke.ts +4 -4
- package/src/components/Text.test.ts +2 -2
- package/src/components/Text.ts +3 -3
- package/src/components/UnknownSVGObject.ts +3 -3
- package/src/components/builders/ArrowBuilder.ts +2 -2
- package/src/components/builders/FreehandLineBuilder.ts +190 -80
- package/src/components/builders/LineBuilder.ts +2 -2
- package/src/components/builders/RectangleBuilder.ts +3 -3
- package/src/components/builders/types.ts +1 -1
- package/src/localization.ts +6 -0
- package/src/localizations/es.ts +2 -1
- package/src/{geometry → math}/LineSegment2.test.ts +0 -0
- package/src/{geometry → math}/LineSegment2.ts +0 -0
- package/src/{geometry → math}/Mat33.test.ts +0 -0
- package/src/{geometry → math}/Mat33.ts +0 -0
- package/src/{geometry → math}/Path.fromString.test.ts +0 -0
- package/src/{geometry → math}/Path.test.ts +0 -0
- package/src/{geometry → math}/Path.toString.test.ts +11 -2
- package/src/{geometry → math}/Path.ts +60 -57
- package/src/{geometry → math}/Rect2.test.ts +0 -0
- package/src/{geometry → math}/Rect2.ts +0 -0
- package/src/{geometry → math}/Vec2.test.ts +0 -0
- package/src/{geometry → math}/Vec2.ts +0 -0
- package/src/{geometry → math}/Vec3.test.ts +0 -0
- package/src/{geometry → math}/Vec3.ts +2 -2
- package/src/math/rounding.test.ts +40 -0
- package/src/math/rounding.ts +145 -0
- package/src/rendering/Display.ts +18 -10
- package/src/rendering/caching/CacheRecord.test.ts +2 -2
- package/src/rendering/caching/CacheRecord.ts +2 -2
- package/src/rendering/caching/CacheRecordManager.ts +1 -1
- package/src/rendering/caching/RenderingCache.test.ts +3 -3
- package/src/rendering/caching/RenderingCache.ts +1 -1
- package/src/rendering/caching/RenderingCacheNode.ts +23 -7
- package/src/rendering/caching/testUtils.ts +1 -1
- package/src/rendering/caching/types.ts +1 -1
- package/src/rendering/localization.ts +4 -0
- package/src/rendering/renderers/AbstractRenderer.ts +4 -4
- package/src/rendering/renderers/CanvasRenderer.ts +4 -4
- package/src/rendering/renderers/DummyRenderer.test.ts +2 -2
- package/src/rendering/renderers/DummyRenderer.ts +4 -4
- package/src/rendering/renderers/SVGRenderer.ts +10 -4
- package/src/rendering/renderers/TextOnlyRenderer.ts +17 -6
- package/src/toolbar/icons.ts +157 -137
- package/src/toolbar/localization.ts +4 -2
- package/src/toolbar/makeColorInput.ts +2 -1
- package/src/toolbar/toolbar.css +1 -1
- package/src/toolbar/widgets/ActionButtonWidget.ts +31 -0
- package/src/toolbar/widgets/BaseWidget.ts +2 -0
- package/src/toolbar/widgets/HandToolWidget.ts +3 -3
- package/src/toolbar/widgets/SelectionWidget.ts +46 -41
- package/src/tools/Eraser.ts +2 -2
- package/src/tools/PanZoom.ts +28 -16
- package/src/tools/SelectionTool.test.ts +2 -4
- package/src/tools/SelectionTool.ts +6 -6
- package/src/tools/TextTool.ts +2 -2
- package/src/tools/UndoRedoShortcut.test.ts +1 -1
- package/src/types.ts +4 -4
@@ -0,0 +1,120 @@
|
|
1
|
+
// Clean up stringified numbers
|
2
|
+
const cleanUpNumber = (text) => {
|
3
|
+
// Regular expression substitions can be somewhat expensive. Only do them
|
4
|
+
// if necessary.
|
5
|
+
const lastChar = text.charAt(text.length - 1);
|
6
|
+
if (lastChar === '0' || lastChar === '.') {
|
7
|
+
// Remove trailing zeroes
|
8
|
+
text = text.replace(/([.]\d*[^0]+)0+$/, '$1');
|
9
|
+
text = text.replace(/[.]0+$/, '.');
|
10
|
+
// Remove trailing period
|
11
|
+
text = text.replace(/[.]$/, '');
|
12
|
+
if (text === '-0') {
|
13
|
+
return '0';
|
14
|
+
}
|
15
|
+
}
|
16
|
+
const firstChar = text.charAt(0);
|
17
|
+
if (firstChar === '0' || firstChar === '-') {
|
18
|
+
// Remove unnecessary leading zeroes.
|
19
|
+
text = text.replace(/^(0+)[.]/, '.');
|
20
|
+
text = text.replace(/^-(0+)[.]/, '-.');
|
21
|
+
}
|
22
|
+
return text;
|
23
|
+
};
|
24
|
+
export const toRoundedString = (num) => {
|
25
|
+
// Try to remove rounding errors. If the number ends in at least three/four zeroes
|
26
|
+
// (or nines) just one or two digits, it's probably a rounding error.
|
27
|
+
const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d{1,2}$/;
|
28
|
+
const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,})\d{1,2}$/;
|
29
|
+
let text = num.toString(10);
|
30
|
+
if (text.indexOf('.') === -1) {
|
31
|
+
return text;
|
32
|
+
}
|
33
|
+
const roundingDownMatch = hasRoundingDownExp.exec(text);
|
34
|
+
if (roundingDownMatch) {
|
35
|
+
const negativeSign = roundingDownMatch[1];
|
36
|
+
const postDecimalString = roundingDownMatch[3];
|
37
|
+
const lastDigit = parseInt(postDecimalString.charAt(postDecimalString.length - 1), 10);
|
38
|
+
const postDecimal = parseInt(postDecimalString, 10);
|
39
|
+
const preDecimal = parseInt(roundingDownMatch[2], 10);
|
40
|
+
const origPostDecimalString = roundingDownMatch[3];
|
41
|
+
let newPostDecimal = (postDecimal + 10 - lastDigit).toString();
|
42
|
+
let carry = 0;
|
43
|
+
if (newPostDecimal.length > postDecimal.toString().length) {
|
44
|
+
// Left-shift
|
45
|
+
newPostDecimal = newPostDecimal.substring(1);
|
46
|
+
carry = 1;
|
47
|
+
}
|
48
|
+
// parseInt(...).toString() removes leading zeroes. Add them back.
|
49
|
+
while (newPostDecimal.length < origPostDecimalString.length) {
|
50
|
+
newPostDecimal = carry.toString(10) + newPostDecimal;
|
51
|
+
carry = 0;
|
52
|
+
}
|
53
|
+
text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
|
54
|
+
}
|
55
|
+
text = text.replace(fixRoundingUpExp, '$1');
|
56
|
+
return cleanUpNumber(text);
|
57
|
+
};
|
58
|
+
const numberExp = /^([-]?)(\d*)[.](\d+)$/;
|
59
|
+
export const getLenAfterDecimal = (numberAsString) => {
|
60
|
+
const numberMatch = numberExp.exec(numberAsString);
|
61
|
+
if (!numberMatch) {
|
62
|
+
// If not a match, either the number is exponential notation (or is something
|
63
|
+
// like NaN or Infinity)
|
64
|
+
if (numberAsString.search(/[eE]/) !== -1 || /^[a-zA-Z]+$/.exec(numberAsString)) {
|
65
|
+
return -1;
|
66
|
+
// Or it has no decimal point
|
67
|
+
}
|
68
|
+
else {
|
69
|
+
return 0;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
const afterDecimalLen = numberMatch[3].length;
|
73
|
+
return afterDecimalLen;
|
74
|
+
};
|
75
|
+
// [reference] should be a string representation of a base-10 number (no exponential (e.g. 10e10))
|
76
|
+
export const toStringOfSamePrecision = (num, ...references) => {
|
77
|
+
const text = num.toString(10);
|
78
|
+
const textMatch = numberExp.exec(text);
|
79
|
+
if (!textMatch) {
|
80
|
+
return text;
|
81
|
+
}
|
82
|
+
let decimalPlaces = -1;
|
83
|
+
for (const reference of references) {
|
84
|
+
decimalPlaces = Math.max(getLenAfterDecimal(reference), decimalPlaces);
|
85
|
+
}
|
86
|
+
if (decimalPlaces === -1) {
|
87
|
+
return toRoundedString(num);
|
88
|
+
}
|
89
|
+
// Make text's after decimal length match [afterDecimalLen].
|
90
|
+
let postDecimal = textMatch[3].substring(0, decimalPlaces);
|
91
|
+
let preDecimal = textMatch[2];
|
92
|
+
const nextDigit = textMatch[3].charAt(decimalPlaces);
|
93
|
+
if (nextDigit !== '') {
|
94
|
+
const asNumber = parseInt(nextDigit, 10);
|
95
|
+
if (asNumber >= 5) {
|
96
|
+
// Don't attempt to parseInt() an empty string.
|
97
|
+
if (postDecimal.length > 0) {
|
98
|
+
const leadingZeroMatch = /^(0+)(\d*)$/.exec(postDecimal);
|
99
|
+
let leadingZeroes = '';
|
100
|
+
let postLeading = postDecimal;
|
101
|
+
if (leadingZeroMatch) {
|
102
|
+
leadingZeroes = leadingZeroMatch[1];
|
103
|
+
postLeading = leadingZeroMatch[2];
|
104
|
+
}
|
105
|
+
postDecimal = (parseInt(postDecimal) + 1).toString();
|
106
|
+
// If postDecimal got longer, remove leading zeroes if possible
|
107
|
+
if (postDecimal.length > postLeading.length && leadingZeroes.length > 0) {
|
108
|
+
leadingZeroes = leadingZeroes.substring(1);
|
109
|
+
}
|
110
|
+
postDecimal = leadingZeroes + postDecimal;
|
111
|
+
}
|
112
|
+
if (postDecimal.length === 0 || postDecimal.length > decimalPlaces) {
|
113
|
+
preDecimal = (parseInt(preDecimal) + 1).toString();
|
114
|
+
postDecimal = postDecimal.substring(1);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}
|
118
|
+
const negativeSign = textMatch[1];
|
119
|
+
return cleanUpNumber(`${negativeSign}${preDecimal}.${postDecimal}`);
|
120
|
+
};
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import AbstractRenderer from './renderers/AbstractRenderer';
|
2
2
|
import { Editor } from '../Editor';
|
3
|
-
import { Point2 } from '../
|
3
|
+
import { Point2 } from '../math/Vec2';
|
4
4
|
import RenderingCache from './caching/RenderingCache';
|
5
5
|
import Color4 from '../Color4';
|
6
6
|
export declare enum RenderingMode {
|
@@ -13,6 +13,7 @@ export default class Display {
|
|
13
13
|
private dryInkRenderer;
|
14
14
|
private wetInkRenderer;
|
15
15
|
private textRenderer;
|
16
|
+
private textRerenderOutput;
|
16
17
|
private cache;
|
17
18
|
private resizeSurfacesCallback?;
|
18
19
|
private flattenCallback?;
|
@@ -23,6 +24,7 @@ export default class Display {
|
|
23
24
|
getColorAt: (_screenPos: Point2) => Color4 | null;
|
24
25
|
private initializeCanvasRendering;
|
25
26
|
private initializeTextRendering;
|
27
|
+
rerenderAsText(): void;
|
26
28
|
startRerender(): AbstractRenderer;
|
27
29
|
setDraftMode(draftMode: boolean): void;
|
28
30
|
getDryInkRenderer(): AbstractRenderer;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import CanvasRenderer from './renderers/CanvasRenderer';
|
2
2
|
import { EditorEventType } from '../types';
|
3
3
|
import DummyRenderer from './renderers/DummyRenderer';
|
4
|
-
import { Vec2 } from '../
|
4
|
+
import { Vec2 } from '../math/Vec2';
|
5
5
|
import RenderingCache from './caching/RenderingCache';
|
6
6
|
import TextOnlyRenderer from './renderers/TextOnlyRenderer';
|
7
7
|
import Color4 from '../Color4';
|
@@ -15,6 +15,7 @@ export default class Display {
|
|
15
15
|
constructor(editor, mode, parent) {
|
16
16
|
this.editor = editor;
|
17
17
|
this.parent = parent;
|
18
|
+
this.textRerenderOutput = null;
|
18
19
|
this.getColorAt = (_screenPos) => {
|
19
20
|
return null;
|
20
21
|
};
|
@@ -51,10 +52,10 @@ export default class Display {
|
|
51
52
|
return this.dryInkRenderer.canRenderFromWithoutDataLoss(renderer);
|
52
53
|
},
|
53
54
|
blockResolution: cacheBlockResolution,
|
54
|
-
cacheSize: 500 * 500 * 4 *
|
55
|
+
cacheSize: 500 * 500 * 4 * 220,
|
55
56
|
maxScale: 1.5,
|
56
|
-
minComponentsPerCache:
|
57
|
-
minComponentsToUseCache:
|
57
|
+
minComponentsPerCache: 45,
|
58
|
+
minComponentsToUseCache: 105,
|
58
59
|
});
|
59
60
|
this.editor.notifier.on(EditorEventType.DisplayResized, event => {
|
60
61
|
var _a;
|
@@ -126,16 +127,21 @@ export default class Display {
|
|
126
127
|
const rerenderButton = document.createElement('button');
|
127
128
|
rerenderButton.classList.add('rerenderButton');
|
128
129
|
rerenderButton.innerText = this.editor.localization.rerenderAsText;
|
129
|
-
|
130
|
-
|
130
|
+
this.textRerenderOutput = document.createElement('div');
|
131
|
+
this.textRerenderOutput.setAttribute('aria-live', 'polite');
|
131
132
|
rerenderButton.onclick = () => {
|
132
|
-
this.
|
133
|
-
this.editor.image.render(this.textRenderer, this.editor.viewport);
|
134
|
-
rerenderOutput.innerText = this.textRenderer.getDescription();
|
133
|
+
this.rerenderAsText();
|
135
134
|
};
|
136
|
-
textRendererOutputContainer.replaceChildren(rerenderButton,
|
135
|
+
textRendererOutputContainer.replaceChildren(rerenderButton, this.textRerenderOutput);
|
137
136
|
this.editor.createHTMLOverlay(textRendererOutputContainer);
|
138
137
|
}
|
138
|
+
rerenderAsText() {
|
139
|
+
this.textRenderer.clear();
|
140
|
+
this.editor.image.render(this.textRenderer, this.editor.viewport);
|
141
|
+
if (this.textRerenderOutput) {
|
142
|
+
this.textRerenderOutput.innerText = this.textRenderer.getDescription();
|
143
|
+
}
|
144
|
+
}
|
139
145
|
// Clears the drawing surfaces and otherwise prepares for a rerender.
|
140
146
|
startRerender() {
|
141
147
|
var _a;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import Mat33 from '../../
|
2
|
-
import Rect2 from '../../
|
1
|
+
import Mat33 from '../../math/Mat33';
|
2
|
+
import Rect2 from '../../math/Rect2';
|
3
3
|
import AbstractRenderer from '../renderers/AbstractRenderer';
|
4
4
|
import { BeforeDeallocCallback, CacheState } from './types';
|
5
5
|
export default class CacheRecord {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { BeforeDeallocCallback, PartialCacheState } from './types';
|
2
2
|
import CacheRecord from './CacheRecord';
|
3
|
-
import Rect2 from '../../
|
3
|
+
import Rect2 from '../../math/Rect2';
|
4
4
|
export declare class CacheRecordManager {
|
5
5
|
private readonly cacheState;
|
6
6
|
private cacheRecords;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { ImageNode } from '../../EditorImage';
|
2
|
-
import Rect2 from '../../
|
2
|
+
import Rect2 from '../../math/Rect2';
|
3
3
|
import Viewport from '../../Viewport';
|
4
4
|
import AbstractRenderer from '../renderers/AbstractRenderer';
|
5
5
|
import { CacheState } from './types';
|
@@ -20,6 +20,7 @@ export default class RenderingCacheNode {
|
|
20
20
|
private allChildrenCanRender;
|
21
21
|
private computeSortedByLeafIds;
|
22
22
|
private idsOfIntersecting;
|
23
|
+
private allRenderedIdsIn;
|
23
24
|
private renderingIsUpToDate;
|
24
25
|
renderItems(screenRenderer: AbstractRenderer, items: ImageNode[], viewport: Viewport): void;
|
25
26
|
private isEmpty;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
// A cache record with sub-nodes.
|
2
2
|
import Color4 from '../../Color4';
|
3
3
|
import { sortLeavesByZIndex } from '../../EditorImage';
|
4
|
-
import Rect2 from '../../
|
4
|
+
import Rect2 from '../../math/Rect2';
|
5
5
|
// 3x3 divisions for each node.
|
6
6
|
const cacheDivisionSize = 3;
|
7
7
|
// True: Show rendering updates.
|
@@ -116,17 +116,26 @@ export default class RenderingCacheNode {
|
|
116
116
|
}
|
117
117
|
return result;
|
118
118
|
}
|
119
|
-
|
120
|
-
|
119
|
+
// Returns true iff all elems of this.renderedIds are in sortedIds.
|
120
|
+
// sortedIds should be sorted by z-index (or some other order, so long as they are
|
121
|
+
// sorted by the same thing as this.renderedIds.)
|
122
|
+
allRenderedIdsIn(sortedIds) {
|
123
|
+
if (this.renderedIds.length > sortedIds.length) {
|
121
124
|
return false;
|
122
125
|
}
|
123
|
-
for (let i = 0; i <
|
126
|
+
for (let i = 0; i < this.renderedIds.length; i++) {
|
124
127
|
if (sortedIds[i] !== this.renderedIds[i]) {
|
125
128
|
return false;
|
126
129
|
}
|
127
130
|
}
|
128
131
|
return true;
|
129
132
|
}
|
133
|
+
renderingIsUpToDate(sortedIds) {
|
134
|
+
if (this.cachedRenderer === null || sortedIds.length !== this.renderedIds.length) {
|
135
|
+
return false;
|
136
|
+
}
|
137
|
+
return this.allRenderedIdsIn(sortedIds);
|
138
|
+
}
|
130
139
|
// Render all [items] within [viewport]
|
131
140
|
renderItems(screenRenderer, items, viewport) {
|
132
141
|
var _a, _b;
|
@@ -188,14 +197,13 @@ export default class RenderingCacheNode {
|
|
188
197
|
return;
|
189
198
|
}
|
190
199
|
// Is it worth it to render the items?
|
191
|
-
// TODO:
|
192
|
-
// TODO: Determine whether it is 'worth it' to cache this depending on rendering time.
|
200
|
+
// TODO: Consider replacing this with something performace based.
|
193
201
|
if (leavesByIds.length > this.cacheState.props.minComponentsPerCache) {
|
194
202
|
let fullRerenderNeeded = true;
|
195
203
|
if (!this.cachedRenderer) {
|
196
204
|
this.cachedRenderer = this.cacheState.recordManager.allocCanvas(this.region, () => this.onRegionDealloc());
|
197
205
|
}
|
198
|
-
else if (leavesByIds.length > this.renderedIds.length && this.renderedMaxZIndex !== null) {
|
206
|
+
else if (leavesByIds.length > this.renderedIds.length && this.allRenderedIdsIn(leafIds) && this.renderedMaxZIndex !== null) {
|
199
207
|
// We often don't need to do a full re-render even if something's changed.
|
200
208
|
// Check whether we can just draw on top of the existing cache.
|
201
209
|
const newLeaves = [];
|
@@ -228,6 +236,9 @@ export default class RenderingCacheNode {
|
|
228
236
|
}
|
229
237
|
}
|
230
238
|
}
|
239
|
+
else if (debugMode) {
|
240
|
+
console.log('Decided on a full re-render. Reason: At least one of the following is false:', '\n leafIds.length > this.renderedIds.length: ', leafIds.length > this.renderedIds.length, '\n this.allRenderedIdsIn(leafIds): ', this.allRenderedIdsIn(leafIds), '\n this.renderedMaxZIndex !== null: ', this.renderedMaxZIndex !== null, '\n\nthis.rerenderedIds: ', this.renderedIds, ', leafIds: ', leafIds);
|
241
|
+
}
|
231
242
|
if (fullRerenderNeeded) {
|
232
243
|
thisRenderer = this.cachedRenderer.startRender();
|
233
244
|
thisRenderer.clear();
|
@@ -1,4 +1,6 @@
|
|
1
1
|
export const defaultTextRendererLocalization = {
|
2
|
+
pathNodeCount: (count) => `There are ${count} visible path objects.`,
|
3
|
+
textNodeCount: (count) => `There are ${count} visible text nodes.`,
|
2
4
|
textNode: (content) => `Text: ${content}`,
|
3
5
|
rerenderAsText: 'Re-render as text',
|
4
6
|
};
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { LoadSaveDataTable } from '../../components/AbstractComponent';
|
2
2
|
import { TextStyle } from '../../components/Text';
|
3
|
-
import Mat33 from '../../
|
4
|
-
import { PathCommand } from '../../
|
5
|
-
import Rect2 from '../../
|
6
|
-
import { Point2, Vec2 } from '../../
|
3
|
+
import Mat33 from '../../math/Mat33';
|
4
|
+
import { PathCommand } from '../../math/Path';
|
5
|
+
import Rect2 from '../../math/Rect2';
|
6
|
+
import { Point2, Vec2 } from '../../math/Vec2';
|
7
7
|
import Viewport from '../../Viewport';
|
8
8
|
import RenderingStyle from '../RenderingStyle';
|
9
9
|
export interface RenderablePathSpec {
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import Path, { PathCommandType } from '../../
|
2
|
-
import { Vec2 } from '../../
|
1
|
+
import Path, { PathCommandType } from '../../math/Path';
|
2
|
+
import { Vec2 } from '../../math/Vec2';
|
3
3
|
import { stylesEqual } from '../RenderingStyle';
|
4
4
|
export default class AbstractRenderer {
|
5
5
|
constructor(viewport) {
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { TextStyle } from '../../components/Text';
|
2
|
-
import Mat33 from '../../
|
3
|
-
import Rect2 from '../../
|
4
|
-
import { Point2, Vec2 } from '../../
|
5
|
-
import Vec3 from '../../
|
2
|
+
import Mat33 from '../../math/Mat33';
|
3
|
+
import Rect2 from '../../math/Rect2';
|
4
|
+
import { Point2, Vec2 } from '../../math/Vec2';
|
5
|
+
import Vec3 from '../../math/Vec3';
|
6
6
|
import Viewport from '../../Viewport';
|
7
7
|
import RenderingStyle from '../RenderingStyle';
|
8
8
|
import AbstractRenderer, { RenderablePathSpec } from './AbstractRenderer';
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import Color4 from '../../Color4';
|
2
2
|
import Text from '../../components/Text';
|
3
|
-
import { Vec2 } from '../../
|
3
|
+
import { Vec2 } from '../../math/Vec2';
|
4
4
|
import AbstractRenderer from './AbstractRenderer';
|
5
5
|
export default class CanvasRenderer extends AbstractRenderer {
|
6
6
|
constructor(ctx, viewport) {
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { TextStyle } from '../../components/Text';
|
2
|
-
import Mat33 from '../../
|
3
|
-
import Rect2 from '../../
|
4
|
-
import { Point2, Vec2 } from '../../
|
5
|
-
import Vec3 from '../../
|
2
|
+
import Mat33 from '../../math/Mat33';
|
3
|
+
import Rect2 from '../../math/Rect2';
|
4
|
+
import { Point2, Vec2 } from '../../math/Vec2';
|
5
|
+
import Vec3 from '../../math/Vec3';
|
6
6
|
import Viewport from '../../Viewport';
|
7
7
|
import RenderingStyle from '../RenderingStyle';
|
8
8
|
import AbstractRenderer from './AbstractRenderer';
|
@@ -1,5 +1,5 @@
|
|
1
1
|
// Renderer that outputs nothing. Useful for automated tests.
|
2
|
-
import { Vec2 } from '../../
|
2
|
+
import { Vec2 } from '../../math/Vec2';
|
3
3
|
import AbstractRenderer from './AbstractRenderer';
|
4
4
|
export default class DummyRenderer extends AbstractRenderer {
|
5
5
|
constructor(viewport) {
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { LoadSaveDataTable } from '../../components/AbstractComponent';
|
2
2
|
import { TextStyle } from '../../components/Text';
|
3
|
-
import Mat33 from '../../
|
4
|
-
import Rect2 from '../../
|
5
|
-
import { Point2, Vec2 } from '../../
|
3
|
+
import Mat33 from '../../math/Mat33';
|
4
|
+
import Rect2 from '../../math/Rect2';
|
5
|
+
import { Point2, Vec2 } from '../../math/Vec2';
|
6
6
|
import Viewport from '../../Viewport';
|
7
7
|
import RenderingStyle from '../RenderingStyle';
|
8
8
|
import AbstractRenderer from './AbstractRenderer';
|
@@ -1,5 +1,7 @@
|
|
1
|
-
import
|
2
|
-
import {
|
1
|
+
import Mat33 from '../../math/Mat33';
|
2
|
+
import Path, { PathCommandType } from '../../math/Path';
|
3
|
+
import { toRoundedString } from '../../math/rounding';
|
4
|
+
import { Vec2 } from '../../math/Vec2';
|
3
5
|
import { svgAttributesDataKey, svgStyleAttributesDataKey } from '../../SVGLoader';
|
4
6
|
import AbstractRenderer from './AbstractRenderer';
|
5
7
|
const svgNameSpace = 'http://www.w3.org/2000/svg';
|
@@ -89,6 +91,8 @@ export default class SVGRenderer extends AbstractRenderer {
|
|
89
91
|
drawText(text, transform, style) {
|
90
92
|
var _a, _b, _c;
|
91
93
|
transform = this.getCanvasToScreenTransform().rightMul(transform);
|
94
|
+
const translation = transform.transformVec2(Vec2.zero);
|
95
|
+
transform = transform.rightMul(Mat33.translation(translation.times(-1)));
|
92
96
|
const textElem = document.createElementNS(svgNameSpace, 'text');
|
93
97
|
textElem.appendChild(document.createTextNode(text));
|
94
98
|
textElem.style.transform = `matrix(
|
@@ -101,6 +105,8 @@ export default class SVGRenderer extends AbstractRenderer {
|
|
101
105
|
textElem.style.fontWeight = (_b = style.fontWeight) !== null && _b !== void 0 ? _b : '';
|
102
106
|
textElem.style.fontSize = style.size + 'px';
|
103
107
|
textElem.style.fill = style.renderingStyle.fill.toHexString();
|
108
|
+
textElem.setAttribute('x', `${toRoundedString(translation.x)}`);
|
109
|
+
textElem.setAttribute('y', `${toRoundedString(translation.y)}`);
|
104
110
|
if (style.renderingStyle.stroke) {
|
105
111
|
const strokeStyle = style.renderingStyle.stroke;
|
106
112
|
textElem.style.stroke = strokeStyle.color.toHexString();
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { TextStyle } from '../../components/Text';
|
2
|
-
import Mat33 from '../../
|
3
|
-
import Rect2 from '../../
|
4
|
-
import Vec3 from '../../
|
2
|
+
import Mat33 from '../../math/Mat33';
|
3
|
+
import Rect2 from '../../math/Rect2';
|
4
|
+
import Vec3 from '../../math/Vec3';
|
5
5
|
import Viewport from '../../Viewport';
|
6
6
|
import { TextRendererLocalization } from '../localization';
|
7
7
|
import RenderingStyle from '../RenderingStyle';
|
@@ -9,6 +9,8 @@ import AbstractRenderer from './AbstractRenderer';
|
|
9
9
|
export default class TextOnlyRenderer extends AbstractRenderer {
|
10
10
|
private localizationTable;
|
11
11
|
private descriptionBuilder;
|
12
|
+
private pathCount;
|
13
|
+
private textNodeCount;
|
12
14
|
constructor(viewport: Viewport, localizationTable: TextRendererLocalization);
|
13
15
|
displaySize(): Vec3;
|
14
16
|
clear(): void;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Vec2 } from '../../
|
1
|
+
import { Vec2 } from '../../math/Vec2';
|
2
2
|
import AbstractRenderer from './AbstractRenderer';
|
3
3
|
// Outputs a description of what was rendered.
|
4
4
|
export default class TextOnlyRenderer extends AbstractRenderer {
|
@@ -6,6 +6,8 @@ export default class TextOnlyRenderer extends AbstractRenderer {
|
|
6
6
|
super(viewport);
|
7
7
|
this.localizationTable = localizationTable;
|
8
8
|
this.descriptionBuilder = [];
|
9
|
+
this.pathCount = 0;
|
10
|
+
this.textNodeCount = 0;
|
9
11
|
}
|
10
12
|
displaySize() {
|
11
13
|
// We don't have a graphical display, export a reasonable size.
|
@@ -13,13 +15,20 @@ export default class TextOnlyRenderer extends AbstractRenderer {
|
|
13
15
|
}
|
14
16
|
clear() {
|
15
17
|
this.descriptionBuilder = [];
|
18
|
+
this.pathCount = 0;
|
19
|
+
this.textNodeCount = 0;
|
16
20
|
}
|
17
21
|
getDescription() {
|
18
|
-
return
|
22
|
+
return [
|
23
|
+
this.localizationTable.pathNodeCount(this.pathCount),
|
24
|
+
this.localizationTable.textNodeCount(this.textNodeCount),
|
25
|
+
...this.descriptionBuilder
|
26
|
+
].join('\n');
|
19
27
|
}
|
20
28
|
beginPath(_startPoint) {
|
21
29
|
}
|
22
30
|
endPath(_style) {
|
31
|
+
this.pathCount++;
|
23
32
|
}
|
24
33
|
lineTo(_point) {
|
25
34
|
}
|
@@ -31,9 +40,10 @@ export default class TextOnlyRenderer extends AbstractRenderer {
|
|
31
40
|
}
|
32
41
|
drawText(text, _transform, _style) {
|
33
42
|
this.descriptionBuilder.push(this.localizationTable.textNode(text));
|
43
|
+
this.textNodeCount++;
|
34
44
|
}
|
35
45
|
isTooSmallToRender(rect) {
|
36
|
-
return rect.maxDimension <
|
46
|
+
return rect.maxDimension < 15 / this.getSizeOfCanvasPixelOnScreen();
|
37
47
|
}
|
38
48
|
drawPoints(..._points) {
|
39
49
|
}
|
@@ -15,3 +15,6 @@ export declare const makeTextIcon: (textStyle: TextStyle) => SVGSVGElement;
|
|
15
15
|
export declare const makePenIcon: (tipThickness: number, color: string) => SVGSVGElement;
|
16
16
|
export declare const makeIconFromFactory: (pen: Pen, factory: ComponentBuilderFactory) => SVGSVGElement;
|
17
17
|
export declare const makePipetteIcon: (color?: Color4) => SVGSVGElement;
|
18
|
+
export declare const makeResizeViewportIcon: () => SVGSVGElement;
|
19
|
+
export declare const makeDuplicateSelectionIcon: () => SVGSVGElement;
|
20
|
+
export declare const makeDeleteSelectionIcon: () => SVGSVGElement;
|