js-draw 0.1.9 → 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 +18 -0
- package/README.md +15 -3
- package/dist/bundle.js +1 -1
- package/dist/src/Editor.d.ts +7 -2
- package/dist/src/Editor.js +74 -25
- 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/UndoRedoHistory.js +3 -0
- package/dist/src/Viewport.d.ts +8 -25
- package/dist/src/Viewport.js +17 -10
- package/dist/src/bundle/bundled.d.ts +2 -1
- package/dist/src/bundle/bundled.js +2 -1
- 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/en.d.ts +3 -0
- package/dist/src/localizations/en.js +4 -0
- package/dist/src/localizations/es.d.ts +3 -0
- package/dist/src/localizations/es.js +18 -0
- package/dist/src/localizations/getLocalizationTable.d.ts +3 -0
- package/dist/src/localizations/getLocalizationTable.js +43 -0
- package/dist/src/{geometry → math}/LineSegment2.d.ts +1 -0
- package/dist/src/{geometry → math}/LineSegment2.js +16 -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 +1 -0
- package/dist/src/{geometry → math}/Rect2.js +16 -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/HTMLToolbar.js +5 -7
- package/dist/src/toolbar/icons.d.ts +3 -0
- package/dist/src/toolbar/icons.js +152 -142
- package/dist/src/toolbar/localization.d.ts +4 -1
- package/dist/src/toolbar/localization.js +4 -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 +31 -0
- package/dist/src/toolbar/widgets/HandToolWidget.js +10 -3
- package/dist/src/toolbar/widgets/SelectionWidget.d.ts +0 -1
- package/dist/src/toolbar/widgets/SelectionWidget.js +23 -30
- package/dist/src/tools/BaseTool.d.ts +2 -1
- package/dist/src/tools/BaseTool.js +3 -0
- package/dist/src/tools/Eraser.js +1 -1
- package/dist/src/tools/PanZoom.d.ts +3 -2
- package/dist/src/tools/PanZoom.js +32 -16
- package/dist/src/tools/SelectionTool.d.ts +11 -4
- package/dist/src/tools/SelectionTool.js +135 -23
- package/dist/src/tools/TextTool.js +1 -1
- package/dist/src/tools/ToolController.js +6 -2
- package/dist/src/tools/localization.d.ts +1 -0
- package/dist/src/tools/localization.js +1 -0
- package/dist/src/types.d.ts +13 -6
- package/dist/src/types.js +1 -0
- package/package.json +9 -1
- package/src/Editor.ts +86 -24
- 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/UndoRedoHistory.ts +4 -0
- package/src/Viewport.ts +21 -17
- package/src/bundle/bundled.ts +2 -1
- 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/en.ts +8 -0
- package/src/localizations/es.ts +63 -0
- package/src/localizations/getLocalizationTable.test.ts +27 -0
- package/src/localizations/getLocalizationTable.ts +53 -0
- package/src/{geometry → math}/LineSegment2.test.ts +15 -0
- package/src/{geometry → math}/LineSegment2.ts +20 -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 +20 -7
- package/src/{geometry → math}/Rect2.ts +19 -1
- 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/HTMLToolbar.ts +5 -8
- package/src/toolbar/icons.ts +167 -147
- package/src/toolbar/localization.ts +8 -2
- package/src/toolbar/makeColorInput.ts +2 -1
- package/src/toolbar/toolbar.css +7 -3
- package/src/toolbar/widgets/ActionButtonWidget.ts +31 -0
- package/src/toolbar/widgets/BaseWidget.ts +36 -0
- package/src/toolbar/widgets/HandToolWidget.ts +14 -3
- package/src/toolbar/widgets/SelectionWidget.ts +46 -41
- package/src/tools/BaseTool.ts +5 -1
- package/src/tools/Eraser.ts +2 -2
- package/src/tools/PanZoom.ts +39 -18
- package/src/tools/SelectionTool.test.ts +26 -5
- package/src/tools/SelectionTool.ts +162 -27
- package/src/tools/TextTool.ts +2 -2
- package/src/tools/ToolController.ts +6 -2
- package/src/tools/UndoRedoShortcut.test.ts +1 -1
- package/src/tools/localization.ts +2 -0
- package/src/types.ts +14 -5
- package/dist-test/test-dist-bundle.html +0 -42
@@ -1,4 +1,5 @@
|
|
1
1
|
import { Bezier } from 'bezier-js';
|
2
|
+
import { toRoundedString, toStringOfSamePrecision } from './rounding';
|
2
3
|
import LineSegment2 from './LineSegment2';
|
3
4
|
import Rect2 from './Rect2';
|
4
5
|
import { Vec2 } from './Vec2';
|
@@ -109,8 +110,8 @@ export default class Path {
|
|
109
110
|
}
|
110
111
|
return result;
|
111
112
|
}
|
112
|
-
|
113
|
-
const startPoint =
|
113
|
+
mapPoints(mapping) {
|
114
|
+
const startPoint = mapping(this.startPoint);
|
114
115
|
const newParts = [];
|
115
116
|
let exhaustivenessCheck;
|
116
117
|
for (const part of this.parts) {
|
@@ -119,22 +120,22 @@ export default class Path {
|
|
119
120
|
case PathCommandType.LineTo:
|
120
121
|
newParts.push({
|
121
122
|
kind: part.kind,
|
122
|
-
point:
|
123
|
+
point: mapping(part.point),
|
123
124
|
});
|
124
125
|
break;
|
125
126
|
case PathCommandType.CubicBezierTo:
|
126
127
|
newParts.push({
|
127
128
|
kind: part.kind,
|
128
|
-
controlPoint1:
|
129
|
-
controlPoint2:
|
130
|
-
endPoint:
|
129
|
+
controlPoint1: mapping(part.controlPoint1),
|
130
|
+
controlPoint2: mapping(part.controlPoint2),
|
131
|
+
endPoint: mapping(part.endPoint),
|
131
132
|
});
|
132
133
|
break;
|
133
134
|
case PathCommandType.QuadraticBezierTo:
|
134
135
|
newParts.push({
|
135
136
|
kind: part.kind,
|
136
|
-
controlPoint:
|
137
|
-
endPoint:
|
137
|
+
controlPoint: mapping(part.controlPoint),
|
138
|
+
endPoint: mapping(part.endPoint),
|
138
139
|
});
|
139
140
|
break;
|
140
141
|
default:
|
@@ -144,6 +145,9 @@ export default class Path {
|
|
144
145
|
}
|
145
146
|
return new Path(startPoint, newParts);
|
146
147
|
}
|
148
|
+
transformedBy(affineTransfm) {
|
149
|
+
return this.mapPoints(point => affineTransfm.transformVec2(point));
|
150
|
+
}
|
147
151
|
// Creates a new path by joining [other] to the end of this path
|
148
152
|
union(other) {
|
149
153
|
if (!other) {
|
@@ -201,58 +205,61 @@ export default class Path {
|
|
201
205
|
};
|
202
206
|
}
|
203
207
|
toString() {
|
204
|
-
|
208
|
+
// Hueristic: Try to determine whether converting absolute to relative commands is worth it.
|
209
|
+
// If we're near (0, 0), it probably isn't worth it and if bounding boxes are large,
|
210
|
+
// it also probably isn't worth it.
|
211
|
+
const makeRelativeCommands = Math.abs(this.bbox.topLeft.x) > 10 && Math.abs(this.bbox.size.x) < 2
|
212
|
+
&& Math.abs(this.bbox.topLeft.y) > 10 && Math.abs(this.bbox.size.y) < 2;
|
213
|
+
return Path.toString(this.startPoint, this.parts, !makeRelativeCommands);
|
205
214
|
}
|
206
215
|
serialize() {
|
207
216
|
return this.toString();
|
208
217
|
}
|
209
|
-
|
218
|
+
// [onlyAbsCommands]: True if we should avoid converting absolute coordinates to relative offsets -- such
|
219
|
+
// conversions can lead to smaller output strings, but also take time.
|
220
|
+
static toString(startPoint, parts, onlyAbsCommands = true) {
|
210
221
|
const result = [];
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
const
|
215
|
-
const
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
// Left-shift
|
231
|
-
newPostDecimal = newPostDecimal.substring(1);
|
232
|
-
carry = 1;
|
222
|
+
let prevPoint;
|
223
|
+
const addCommand = (command, ...points) => {
|
224
|
+
const absoluteCommandParts = [];
|
225
|
+
const relativeCommandParts = [];
|
226
|
+
const makeAbsCommand = !prevPoint || onlyAbsCommands;
|
227
|
+
const roundedPrevX = prevPoint ? toRoundedString(prevPoint.x) : '';
|
228
|
+
const roundedPrevY = prevPoint ? toRoundedString(prevPoint.y) : '';
|
229
|
+
for (const point of points) {
|
230
|
+
// Relative commands are often shorter as strings than absolute commands.
|
231
|
+
if (!makeAbsCommand) {
|
232
|
+
const xComponentRelative = toStringOfSamePrecision(point.x - prevPoint.x, roundedPrevX, roundedPrevY);
|
233
|
+
const yComponentRelative = toStringOfSamePrecision(point.y - prevPoint.y, roundedPrevX, roundedPrevY);
|
234
|
+
// No need for an additional separator if it starts with a '-'
|
235
|
+
if (yComponentRelative.charAt(0) === '-') {
|
236
|
+
relativeCommandParts.push(`${xComponentRelative}${yComponentRelative}`);
|
237
|
+
}
|
238
|
+
else {
|
239
|
+
relativeCommandParts.push(`${xComponentRelative},${yComponentRelative}`);
|
240
|
+
}
|
233
241
|
}
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
242
|
+
else {
|
243
|
+
const xComponent = toRoundedString(point.x);
|
244
|
+
const yComponent = toRoundedString(point.y);
|
245
|
+
absoluteCommandParts.push(`${xComponent},${yComponent}`);
|
238
246
|
}
|
239
|
-
text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
|
240
247
|
}
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
248
|
+
let commandString;
|
249
|
+
if (makeAbsCommand) {
|
250
|
+
commandString = `${command}${absoluteCommandParts.join(' ')}`;
|
251
|
+
}
|
252
|
+
else {
|
253
|
+
commandString = `${command.toLowerCase()}${relativeCommandParts.join(' ')}`;
|
254
|
+
}
|
255
|
+
// Don't add no-ops.
|
256
|
+
if (commandString === 'l0,0') {
|
257
|
+
return;
|
258
|
+
}
|
259
|
+
result.push(commandString);
|
260
|
+
if (points.length > 0) {
|
261
|
+
prevPoint = points[points.length - 1];
|
254
262
|
}
|
255
|
-
result.push(`${command}${parts.join(' ')}`);
|
256
263
|
};
|
257
264
|
addCommand('M', startPoint);
|
258
265
|
let exhaustivenessCheck;
|
@@ -30,6 +30,7 @@ export default class Rect2 {
|
|
30
30
|
divideIntoGrid(columns: number, rows: number): Rect2[];
|
31
31
|
grownToPoint(point: Point2, margin?: number): Rect2;
|
32
32
|
grownBy(margin: number): Rect2;
|
33
|
+
getClosestPointOnBoundaryTo(target: Point2): import("./Vec3").default;
|
33
34
|
get corners(): Point2[];
|
34
35
|
get maxDimension(): number;
|
35
36
|
get topRight(): import("./Vec3").default;
|
@@ -25,6 +25,7 @@ export default class Rect2 {
|
|
25
25
|
translatedBy(vec) {
|
26
26
|
return new Rect2(vec.x + this.x, vec.y + this.y, this.w, this.h);
|
27
27
|
}
|
28
|
+
// Returns a copy of this with the given size (but same top-left).
|
28
29
|
resizedTo(size) {
|
29
30
|
return new Rect2(this.x, this.y, size.x, size.y);
|
30
31
|
}
|
@@ -109,6 +110,21 @@ export default class Rect2 {
|
|
109
110
|
grownBy(margin) {
|
110
111
|
return new Rect2(this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2);
|
111
112
|
}
|
113
|
+
getClosestPointOnBoundaryTo(target) {
|
114
|
+
const closestEdgePoints = this.getEdges().map(edge => {
|
115
|
+
return edge.closestPointTo(target);
|
116
|
+
});
|
117
|
+
let closest = null;
|
118
|
+
let closestDist = null;
|
119
|
+
for (const point of closestEdgePoints) {
|
120
|
+
const dist = point.minus(target).length();
|
121
|
+
if (closestDist === null || dist < closestDist) {
|
122
|
+
closest = point;
|
123
|
+
closestDist = dist;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
return closest;
|
127
|
+
}
|
112
128
|
get corners() {
|
113
129
|
return [
|
114
130
|
this.bottomRight,
|
File without changes
|
File without changes
|
@@ -23,7 +23,7 @@ export default class Vec3 {
|
|
23
23
|
extend(distance: number, direction: Vec3): Vec3;
|
24
24
|
lerp(target: Vec3, fractionTo: number): Vec3;
|
25
25
|
zip(other: Vec3, zip: (componentInThis: number, componentInOther: number) => number): Vec3;
|
26
|
-
map(fn: (component: number) => number): Vec3;
|
26
|
+
map(fn: (component: number, index: number) => number): Vec3;
|
27
27
|
asArray(): number[];
|
28
28
|
eq(other: Vec3, fuzz: number): boolean;
|
29
29
|
toString(): string;
|
@@ -86,7 +86,7 @@ export default class Vec3 {
|
|
86
86
|
}
|
87
87
|
// Returns a vector with each component acted on by [fn]
|
88
88
|
map(fn) {
|
89
|
-
return Vec3.of(fn(this.x), fn(this.y), fn(this.z));
|
89
|
+
return Vec3.of(fn(this.x, 0), fn(this.y, 1), fn(this.z, 2));
|
90
90
|
}
|
91
91
|
asArray() {
|
92
92
|
return [this.x, this.y, this.z];
|
@@ -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';
|