@altnovatrail/canvas 0.3.41 → 0.3.43
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/lib/core/crop.d.ts +17 -2
- package/lib/core/crop.js +45 -17
- package/lib/core/cut.d.ts +46 -0
- package/lib/core/cut.js +297 -0
- package/lib/core/eraser.d.ts +27 -9
- package/lib/core/eraser.js +32 -10
- package/lib/core/hands.d.ts +7 -0
- package/lib/core/hands.js +12 -0
- package/lib/core/history.d.ts +21 -15
- package/lib/core/history.js +234 -28
- package/lib/core/index.d.ts +2 -1
- package/lib/core/index.js +3 -2
- package/lib/core/outline.d.ts +23 -5
- package/lib/core/outline.js +148 -14
- package/lib/core/png.d.ts +4 -3
- package/lib/core/png.js +15 -10
- package/lib/core/raster.d.ts +20 -9
- package/lib/core/raster.js +121 -35
- package/lib/core/recorder.d.ts +13 -2
- package/lib/core/recorder.js +20 -4
- package/lib/core/sheet.d.ts +3 -2
- package/lib/core/sheet.js +6 -4
- package/lib/core/style.d.ts +54 -0
- package/lib/core/style.js +146 -0
- package/lib/core/submission.js +4 -3
- package/lib/core/tracing.d.ts +6 -4
- package/lib/core/tracing.js +15 -7
- package/lib/core/work.d.ts +40 -11
- package/lib/core/work.js +106 -15
- package/lib/element/index.d.ts +37 -15
- package/lib/element/index.js +458 -78
- package/lib/element/labels.d.ts +42 -0
- package/lib/element/labels.js +73 -3
- package/lib/element/pens.d.ts +55 -0
- package/lib/element/pens.js +676 -0
- package/lib/element/tools.d.ts +9 -0
- package/lib/element/tools.js +77 -9
- package/package.json +3 -3
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { EraserMode, PenKind, PenSettings, StrokeStyle } from "@altnovatrail/spec/canvas";
|
|
2
|
+
import type { Stroke } from "./ink.js";
|
|
3
|
+
/**
|
|
4
|
+
* How a stroke looks, the learner's settings of the pen and the eraser, the kinds of pen and the
|
|
5
|
+
* modes of the eraser: spec's types, of the contract of canvas and app.
|
|
6
|
+
*/
|
|
7
|
+
export type { EraserMode, PenKind, PenSettings, StrokeStyle };
|
|
8
|
+
/** The kinds of pen, in the order the pen's popover offers them. */
|
|
9
|
+
export declare const PEN_KINDS: readonly PenKind[];
|
|
10
|
+
/** The eraser's modes: a stroke it touches whole, or only what it passes over. */
|
|
11
|
+
export declare const ERASER_MODES: readonly EraserMode[];
|
|
12
|
+
/** The five widths of the pen and the liner, in sheet units, the thinnest first. */
|
|
13
|
+
export declare const PEN_WIDTHS: readonly number[];
|
|
14
|
+
/**
|
|
15
|
+
* The five widths of the marker, in sheet units: each four times the pen's step, and its style's
|
|
16
|
+
* width is the very width it draws.
|
|
17
|
+
*/
|
|
18
|
+
export declare const MARKER_WIDTHS: readonly number[];
|
|
19
|
+
/** The eraser's five sizes, its diameters in sheet units, the smallest first. */
|
|
20
|
+
export declare const ERASER_SIZES: readonly number[];
|
|
21
|
+
/** The palette: black, blue, green, yellow and red. */
|
|
22
|
+
export declare const PALETTE: readonly string[];
|
|
23
|
+
/** The other colours, offered past the palette. */
|
|
24
|
+
export declare const OTHER_COLOURS: readonly string[];
|
|
25
|
+
/** The bounds of a style, in sheet units and in percent, and of the eraser's size, bounds in. */
|
|
26
|
+
export declare const STYLE_BOUNDS: Readonly<{
|
|
27
|
+
width: readonly [0.1, 50];
|
|
28
|
+
opacity: readonly [10, 100];
|
|
29
|
+
eraserSize: readonly [1, 200];
|
|
30
|
+
}>;
|
|
31
|
+
/** The settings an element starts with, frozen through. */
|
|
32
|
+
export declare const PEN_SETTINGS: PenSettings;
|
|
33
|
+
/**
|
|
34
|
+
* Settings of the pen and the eraser given by a consumer, checked field by field and copied into
|
|
35
|
+
* frozen objects of the element's own, with only the fields they have: others are passed over.
|
|
36
|
+
* Anything not of `PenSettings` is a TypeError that names the field — a style whose `pen` is not
|
|
37
|
+
* its kind's, a colour, a width, an opacity or a size out of its bounds.
|
|
38
|
+
*/
|
|
39
|
+
export declare function penSettingsOf(value: unknown, path?: string): PenSettings;
|
|
40
|
+
/**
|
|
41
|
+
* A stroke's style, checked field by field and copied, frozen: a kind of pen, a colour of sRGB as
|
|
42
|
+
* `#rrggbb` in lowercase digits, a width in sheet units to a tenth from 0.1 to 50 and an opacity
|
|
43
|
+
* in whole percent from 10 to 100. Anything else is a TypeError at `path`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function styleOf(value: unknown, path: string): StrokeStyle;
|
|
46
|
+
/** Whether two styles say the same, field by field; none is the same as none alone. */
|
|
47
|
+
export declare function sameStyle(a: StrokeStyle | undefined, b: StrokeStyle | undefined): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* How far a stroke's ink reaches from its points on the screen at most, in its own units: a stroke
|
|
50
|
+
* without a style as the pen without styles drew it, `INK_REACH`; one of the kind "pen" half its
|
|
51
|
+
* width at the widest a pen's pressure or its speed makes it; a liner's and a marker's half their
|
|
52
|
+
* width.
|
|
53
|
+
*/
|
|
54
|
+
export declare function reachOf(stroke: Stroke): number;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { record, shown } from "./fields.js";
|
|
2
|
+
import { INK_REACH, PEN_WIDEST } from "./outline.js";
|
|
3
|
+
/** The kinds of pen, in the order the pen's popover offers them. */
|
|
4
|
+
export const PEN_KINDS = Object.freeze(["pen", "liner", "marker"]);
|
|
5
|
+
/** The eraser's modes: a stroke it touches whole, or only what it passes over. */
|
|
6
|
+
export const ERASER_MODES = Object.freeze(["stroke", "part"]);
|
|
7
|
+
/** The five widths of the pen and the liner, in sheet units, the thinnest first. */
|
|
8
|
+
export const PEN_WIDTHS = Object.freeze([1.2, 2, 3, 4.5, 7]);
|
|
9
|
+
/**
|
|
10
|
+
* The five widths of the marker, in sheet units: each four times the pen's step, and its style's
|
|
11
|
+
* width is the very width it draws.
|
|
12
|
+
*/
|
|
13
|
+
export const MARKER_WIDTHS = Object.freeze([4.8, 8, 12, 18, 28]);
|
|
14
|
+
/** The eraser's five sizes, its diameters in sheet units, the smallest first. */
|
|
15
|
+
export const ERASER_SIZES = Object.freeze([8, 16, 28, 44, 64]);
|
|
16
|
+
/** The palette: black, blue, green, yellow and red. */
|
|
17
|
+
export const PALETTE = Object.freeze([
|
|
18
|
+
"#1c1c1e",
|
|
19
|
+
"#0a63d6",
|
|
20
|
+
"#1e9e4a",
|
|
21
|
+
"#ffcc00",
|
|
22
|
+
"#e5322d",
|
|
23
|
+
]);
|
|
24
|
+
/** The other colours, offered past the palette. */
|
|
25
|
+
export const OTHER_COLOURS = Object.freeze([
|
|
26
|
+
"#8e8e93",
|
|
27
|
+
"#7a4fd0",
|
|
28
|
+
"#f08a00",
|
|
29
|
+
"#00a6a6",
|
|
30
|
+
"#ff5fa2",
|
|
31
|
+
"#8b5a2b",
|
|
32
|
+
"#5ac8fa",
|
|
33
|
+
"#34c759",
|
|
34
|
+
"#ff9f0a",
|
|
35
|
+
"#bf5af2",
|
|
36
|
+
"#ff3b30",
|
|
37
|
+
"#ffffff",
|
|
38
|
+
]);
|
|
39
|
+
/** The bounds of a style, in sheet units and in percent, and of the eraser's size, bounds in. */
|
|
40
|
+
export const STYLE_BOUNDS = Object.freeze({
|
|
41
|
+
width: Object.freeze([0.1, 50]),
|
|
42
|
+
opacity: Object.freeze([10, 100]),
|
|
43
|
+
eraserSize: Object.freeze([1, 200]),
|
|
44
|
+
});
|
|
45
|
+
const COLOUR = /^#[0-9a-f]{6}$/;
|
|
46
|
+
/** The settings an element starts with, frozen through. */
|
|
47
|
+
export const PEN_SETTINGS = penSettingsOf({
|
|
48
|
+
pen: "pen",
|
|
49
|
+
styles: {
|
|
50
|
+
pen: { pen: "pen", color: "#1c1c1e", width: 3, opacity: 100 },
|
|
51
|
+
liner: { pen: "liner", color: "#0a63d6", width: 2, opacity: 100 },
|
|
52
|
+
marker: { pen: "marker", color: "#ffcc00", width: 18, opacity: 40 },
|
|
53
|
+
},
|
|
54
|
+
eraser: "stroke",
|
|
55
|
+
eraserSize: 16,
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Settings of the pen and the eraser given by a consumer, checked field by field and copied into
|
|
59
|
+
* frozen objects of the element's own, with only the fields they have: others are passed over.
|
|
60
|
+
* Anything not of `PenSettings` is a TypeError that names the field — a style whose `pen` is not
|
|
61
|
+
* its kind's, a colour, a width, an opacity or a size out of its bounds.
|
|
62
|
+
*/
|
|
63
|
+
export function penSettingsOf(value, path = "pens") {
|
|
64
|
+
const fields = record(value, path);
|
|
65
|
+
const pen = kindOf(fields.pen, `${path}.pen`);
|
|
66
|
+
const given = record(fields.styles, `${path}.styles`);
|
|
67
|
+
const style = (kind) => {
|
|
68
|
+
const at = `${path}.styles.${kind}`;
|
|
69
|
+
const one = styleOf(given[kind], at);
|
|
70
|
+
if (one.pen !== kind)
|
|
71
|
+
throw new TypeError(`${at}.pen: "${kind}", its kind's, not "${one.pen}"`);
|
|
72
|
+
return one;
|
|
73
|
+
};
|
|
74
|
+
const styles = Object.freeze({
|
|
75
|
+
pen: style("pen"),
|
|
76
|
+
liner: style("liner"),
|
|
77
|
+
marker: style("marker"),
|
|
78
|
+
});
|
|
79
|
+
if (!ERASER_MODES.includes(fields.eraser)) {
|
|
80
|
+
throw new TypeError(`${path}.eraser: "stroke" or "part", not ${shown(fields.eraser)}`);
|
|
81
|
+
}
|
|
82
|
+
const eraser = fields.eraser;
|
|
83
|
+
const [least, most] = STYLE_BOUNDS.eraserSize;
|
|
84
|
+
const eraserSize = fields.eraserSize;
|
|
85
|
+
if (typeof eraserSize !== "number" || !(eraserSize >= least && eraserSize <= most)) {
|
|
86
|
+
throw new TypeError(`${path}.eraserSize: the eraser's diameter in sheet units, from ${least} to ${most}, not ${shown(eraserSize)}`);
|
|
87
|
+
}
|
|
88
|
+
return Object.freeze({ pen, styles, eraser, eraserSize });
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A stroke's style, checked field by field and copied, frozen: a kind of pen, a colour of sRGB as
|
|
92
|
+
* `#rrggbb` in lowercase digits, a width in sheet units to a tenth from 0.1 to 50 and an opacity
|
|
93
|
+
* in whole percent from 10 to 100. Anything else is a TypeError at `path`.
|
|
94
|
+
*/
|
|
95
|
+
export function styleOf(value, path) {
|
|
96
|
+
const fields = record(value, path);
|
|
97
|
+
const pen = kindOf(fields.pen, `${path}.pen`);
|
|
98
|
+
const color = fields.color;
|
|
99
|
+
if (typeof color !== "string" || !COLOUR.test(color)) {
|
|
100
|
+
throw new TypeError(`${path}.color: a colour, # and six lowercase hexadecimal digits, not ${shown(color)}`);
|
|
101
|
+
}
|
|
102
|
+
const width = fields.width;
|
|
103
|
+
const [thinnest, widest] = STYLE_BOUNDS.width;
|
|
104
|
+
if (typeof width !== "number" ||
|
|
105
|
+
!Number.isFinite(width) ||
|
|
106
|
+
Math.round(width * 10) / 10 !== width ||
|
|
107
|
+
width < thinnest ||
|
|
108
|
+
width > widest) {
|
|
109
|
+
throw new TypeError(`${path}.width: sheet units to a tenth, from ${thinnest} to ${widest}, not ${shown(width)}`);
|
|
110
|
+
}
|
|
111
|
+
const opacity = fields.opacity;
|
|
112
|
+
const [faintest, fullest] = STYLE_BOUNDS.opacity;
|
|
113
|
+
if (typeof opacity !== "number" ||
|
|
114
|
+
!Number.isInteger(opacity) ||
|
|
115
|
+
opacity < faintest ||
|
|
116
|
+
opacity > fullest) {
|
|
117
|
+
throw new TypeError(`${path}.opacity: whole percent from ${faintest} to ${fullest}, not ${shown(opacity)}`);
|
|
118
|
+
}
|
|
119
|
+
return Object.freeze({ pen, color, width, opacity });
|
|
120
|
+
}
|
|
121
|
+
/** Whether two styles say the same, field by field; none is the same as none alone. */
|
|
122
|
+
export function sameStyle(a, b) {
|
|
123
|
+
if (a === b)
|
|
124
|
+
return true;
|
|
125
|
+
if (a === undefined || b === undefined)
|
|
126
|
+
return false;
|
|
127
|
+
return a.pen === b.pen && a.color === b.color && a.width === b.width && a.opacity === b.opacity;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* How far a stroke's ink reaches from its points on the screen at most, in its own units: a stroke
|
|
131
|
+
* without a style as the pen without styles drew it, `INK_REACH`; one of the kind "pen" half its
|
|
132
|
+
* width at the widest a pen's pressure or its speed makes it; a liner's and a marker's half their
|
|
133
|
+
* width.
|
|
134
|
+
*/
|
|
135
|
+
export function reachOf(stroke) {
|
|
136
|
+
const { style } = stroke;
|
|
137
|
+
if (style === undefined)
|
|
138
|
+
return INK_REACH;
|
|
139
|
+
return style.pen === "pen" ? (style.width * PEN_WIDEST) / 2 : style.width / 2;
|
|
140
|
+
}
|
|
141
|
+
function kindOf(value, path) {
|
|
142
|
+
if (!PEN_KINDS.includes(value)) {
|
|
143
|
+
throw new TypeError(`${path}: "pen", "liner" or "marker", not ${shown(value)}`);
|
|
144
|
+
}
|
|
145
|
+
return value;
|
|
146
|
+
}
|
package/lib/core/submission.js
CHANGED
|
@@ -4,7 +4,7 @@ import { tenths } from "./geometry.js";
|
|
|
4
4
|
import { groupsOf } from "./groups.js";
|
|
5
5
|
import { digestOf, workbookDigestOf, } from "./marks.js";
|
|
6
6
|
import { encodePNG } from "./png.js";
|
|
7
|
-
import { pictureOf, rasterOf } from "./raster.js";
|
|
7
|
+
import { channelsOf, pictureOf, rasterOf } from "./raster.js";
|
|
8
8
|
import { layOver, tenthsOf } from "./tracing.js";
|
|
9
9
|
/**
|
|
10
10
|
* The work of a sheet for checking, as `exportWork()` gives it: of the strokes the sheet
|
|
@@ -67,8 +67,9 @@ export function paperWorkForCheck(strokes, paper, tracing, rect, picture, digit)
|
|
|
67
67
|
if (crop === null)
|
|
68
68
|
return null;
|
|
69
69
|
const ink = rasterOf(strokes, crop);
|
|
70
|
+
const channels = channelsOf(strokes);
|
|
70
71
|
if (picture !== null)
|
|
71
|
-
layOver(ink, crop.width, picture, pixelsOf(rect, crop));
|
|
72
|
+
layOver(ink, crop.width, picture, pixelsOf(rect, crop), channels);
|
|
72
73
|
const { drawn, width, height, margins, sheetBox, frames, exact } = crop;
|
|
73
74
|
const numbers = { drawn, width, height, margins, sheetBox, frames, exact };
|
|
74
75
|
const submission = {
|
|
@@ -82,7 +83,7 @@ export function paperWorkForCheck(strokes, paper, tracing, rect, picture, digit)
|
|
|
82
83
|
digest: digestOf(strokes, numbers, groups, paper),
|
|
83
84
|
};
|
|
84
85
|
return Object.freeze({
|
|
85
|
-
png: encodePNG(ink, crop.width, crop.height),
|
|
86
|
+
png: encodePNG(ink, crop.width, crop.height, channels),
|
|
86
87
|
submission: frozen(submission),
|
|
87
88
|
});
|
|
88
89
|
}
|
package/lib/core/tracing.d.ts
CHANGED
|
@@ -105,7 +105,8 @@ export declare function toTracing(point: Point, at: readonly [number, number], s
|
|
|
105
105
|
/**
|
|
106
106
|
* A stroke of the tracing paper where it lies on the sheet: its points scaled by `scale` and moved
|
|
107
107
|
* by `at`, and kept to the sheet, should the paper lie in part beyond it — the rules of the groups
|
|
108
|
-
* and the crop hold on the sheet alone. The same ID, kind and times
|
|
108
|
+
* and the crop hold on the sheet alone. The same ID, kind and times, and its style with its width
|
|
109
|
+
* scaled too, to a tenth and within a style's bounds, as the screen draws it.
|
|
109
110
|
*/
|
|
110
111
|
export declare function onSheet(stroke: Stroke, at: readonly [number, number], scale?: number): Stroke;
|
|
111
112
|
/**
|
|
@@ -132,8 +133,9 @@ export interface Gray {
|
|
|
132
133
|
export declare function grayOf(rgba: Uint8ClampedArray | Uint8Array, width: number, height: number): Gray;
|
|
133
134
|
/**
|
|
134
135
|
* The crop's picture laid into the pixels of a crop of the work, multiplied, as the screen
|
|
135
|
-
* lays the tracing paper over the sheet: `ink` the crop's pixels, `width` px wide,
|
|
136
|
-
*
|
|
136
|
+
* lays the tracing paper over the sheet: `ink` the crop's pixels, `width` px wide, of `channels`
|
|
137
|
+
* bytes each — grey, or RGB, each channel multiplied alike —, and `rect` where the tracing paper
|
|
138
|
+
* lies on them, in px, which may reach beyond them. Each pixel of the crop
|
|
137
139
|
* under the paper takes the mean of the picture's pixels whose centres fall within it, or the one
|
|
138
140
|
* nearest its centre when none does, so the crop shrinks or grows by sums of whole numbers alone,
|
|
139
141
|
* the same in every engine. Changes `ink` in place.
|
|
@@ -143,4 +145,4 @@ export declare function layOver(ink: Uint8Array, width: number, picture: Gray, r
|
|
|
143
145
|
readonly top: number;
|
|
144
146
|
readonly right: number;
|
|
145
147
|
readonly bottom: number;
|
|
146
|
-
}): void;
|
|
148
|
+
}, channels?: 1 | 3): void;
|
package/lib/core/tracing.js
CHANGED
|
@@ -184,12 +184,18 @@ export function toTracing(point, at, size, scale = 1) {
|
|
|
184
184
|
/**
|
|
185
185
|
* A stroke of the tracing paper where it lies on the sheet: its points scaled by `scale` and moved
|
|
186
186
|
* by `at`, and kept to the sheet, should the paper lie in part beyond it — the rules of the groups
|
|
187
|
-
* and the crop hold on the sheet alone. The same ID, kind and times
|
|
187
|
+
* and the crop hold on the sheet alone. The same ID, kind and times, and its style with its width
|
|
188
|
+
* scaled too, to a tenth and within a style's bounds, as the screen draws it.
|
|
188
189
|
*/
|
|
189
190
|
export function onSheet(stroke, at, scale = 1) {
|
|
191
|
+
const { style } = stroke;
|
|
192
|
+
const width = (value) => Math.min(50, Math.max(0.1, Math.round(value * scale * 10) / 10));
|
|
190
193
|
return Object.freeze({
|
|
191
194
|
id: stroke.id,
|
|
192
195
|
kind: stroke.kind,
|
|
196
|
+
...(style === undefined
|
|
197
|
+
? {}
|
|
198
|
+
: { style: Object.freeze({ ...style, width: width(style.width) }) }),
|
|
193
199
|
points: Object.freeze(stroke.points.map(({ x, y, t, p }) => Object.freeze({
|
|
194
200
|
x: clamp(x * scale + at[0], 0, REACH.width),
|
|
195
201
|
y: clamp(y * scale + at[1], 0, REACH.height),
|
|
@@ -244,14 +250,15 @@ export function grayOf(rgba, width, height) {
|
|
|
244
250
|
}
|
|
245
251
|
/**
|
|
246
252
|
* The crop's picture laid into the pixels of a crop of the work, multiplied, as the screen
|
|
247
|
-
* lays the tracing paper over the sheet: `ink` the crop's pixels, `width` px wide,
|
|
248
|
-
*
|
|
253
|
+
* lays the tracing paper over the sheet: `ink` the crop's pixels, `width` px wide, of `channels`
|
|
254
|
+
* bytes each — grey, or RGB, each channel multiplied alike —, and `rect` where the tracing paper
|
|
255
|
+
* lies on them, in px, which may reach beyond them. Each pixel of the crop
|
|
249
256
|
* under the paper takes the mean of the picture's pixels whose centres fall within it, or the one
|
|
250
257
|
* nearest its centre when none does, so the crop shrinks or grows by sums of whole numbers alone,
|
|
251
258
|
* the same in every engine. Changes `ink` in place.
|
|
252
259
|
*/
|
|
253
|
-
export function layOver(ink, width, picture, rect) {
|
|
254
|
-
const height = ink.length / width;
|
|
260
|
+
export function layOver(ink, width, picture, rect, channels = 1) {
|
|
261
|
+
const height = ink.length / (width * channels);
|
|
255
262
|
const across = (rect.right - rect.left) / picture.width;
|
|
256
263
|
const down = (rect.bottom - rect.top) / picture.height;
|
|
257
264
|
if (!(across > 0 && down > 0))
|
|
@@ -287,9 +294,10 @@ export function layOver(ink, width, picture, rect) {
|
|
|
287
294
|
count += 1;
|
|
288
295
|
}
|
|
289
296
|
}
|
|
290
|
-
const at = row * width + firstColumn + c;
|
|
297
|
+
const at = (row * width + firstColumn + c) * channels;
|
|
291
298
|
const mean = Math.round(sum / count);
|
|
292
|
-
|
|
299
|
+
for (let k = at; k < at + channels; k++)
|
|
300
|
+
ink[k] = Math.round((ink[k] * mean) / 255);
|
|
293
301
|
}
|
|
294
302
|
}
|
|
295
303
|
}
|
package/lib/core/work.d.ts
CHANGED
|
@@ -2,6 +2,14 @@ import { Clock } from "./clock.js";
|
|
|
2
2
|
import { type HistoryEvent } from "./history.js";
|
|
3
3
|
import type { Stroke } from "./ink.js";
|
|
4
4
|
import type { Point } from "./screen.js";
|
|
5
|
+
/**
|
|
6
|
+
* What one press of the eraser that takes a part left of a stroke it crossed: the stroke's ID,
|
|
7
|
+
* and its pieces along it, new strokes with their own IDs — none when it covered the stroke whole.
|
|
8
|
+
*/
|
|
9
|
+
export interface Cutting {
|
|
10
|
+
readonly id: number;
|
|
11
|
+
readonly pieces: readonly Stroke[];
|
|
12
|
+
}
|
|
5
13
|
/**
|
|
6
14
|
* The work on the sheet: the strokes in sight, the stack of the actions that undo takes back, the
|
|
7
15
|
* last first, and the history of writing, every action as an event at the sheet's time.
|
|
@@ -18,7 +26,7 @@ export declare class Work {
|
|
|
18
26
|
* actions for undo and the history itself, in frozen objects of the work's own. The events are
|
|
19
27
|
* checked as `historyOf` checks them, then replayed by the work's own rules, each at its own
|
|
20
28
|
* time, and each must be what the work records for it: an erasing takes off strokes on the sheet,
|
|
21
|
-
* in the sheet's order; a clearing clears a sheet with strokes, all of them; an undo takes back
|
|
29
|
+
* in the sheet's order, and so does a cut, which leaves its pieces at their places; a clearing clears a sheet with strokes, all of them; an undo takes back
|
|
22
30
|
* the last action still in force and says what the stack says. A history the sheet could not have
|
|
23
31
|
* written is a TypeError that names the event. The clock is not asked: it goes on as the sheet
|
|
24
32
|
* that takes the work tells it (`Clock.resume`). On a task from paper, `paper`, a stroke may be
|
|
@@ -26,8 +34,9 @@ export declare class Work {
|
|
|
26
34
|
*/
|
|
27
35
|
static restore(value: unknown, clock: Clock, paper?: boolean): Work;
|
|
28
36
|
/**
|
|
29
|
-
* The strokes on the sheet, in the order they were written
|
|
30
|
-
* undoing a clearing
|
|
37
|
+
* The strokes on the sheet, in the order they were written, each piece of a cut stroke at the
|
|
38
|
+
* place the stroke had. Every action gives a new array, and undoing a clearing or a cut gives back
|
|
39
|
+
* the very one it took off.
|
|
31
40
|
*/
|
|
32
41
|
get strokes(): readonly Stroke[];
|
|
33
42
|
/**
|
|
@@ -42,6 +51,12 @@ export declare class Work {
|
|
|
42
51
|
onTracing(id: number): boolean;
|
|
43
52
|
/** Whether any stroke was written on the tracing paper, erased or not. */
|
|
44
53
|
get traced(): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* The strokes a stroke was cut of, by their IDs: the stroke it is a piece of, the stroke that one
|
|
56
|
+
* is a piece of, and on to a stroke written whole. None for a stroke written whole, or an ID the
|
|
57
|
+
* work does not know.
|
|
58
|
+
*/
|
|
59
|
+
cutFrom(id: number): readonly number[];
|
|
45
60
|
/**
|
|
46
61
|
* The history of writing: every action, in the order it landed, frozen through. An array once
|
|
47
62
|
* handed out never changes: after an action the history is a new one, made when it is next asked
|
|
@@ -61,23 +76,37 @@ export declare class Work {
|
|
|
61
76
|
* IDs no longer on the sheet are left out; if none is on it, nothing is done.
|
|
62
77
|
*/
|
|
63
78
|
erase(ids: Iterable<number>, time: number): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* One press of the eraser that takes a part, in one action: each stroke it crossed, by its ID,
|
|
81
|
+
* goes off the sheet, and its pieces come on at the place it had, so the strokes keep their order.
|
|
82
|
+
* The event names the strokes in the sheet's order, and their pieces in that order and along
|
|
83
|
+
* each. A stroke no longer on the sheet is left out, with its pieces; if none is on it, nothing is
|
|
84
|
+
* done. The pieces' IDs are the caller's, new ones the history never had, and the pieces are the
|
|
85
|
+
* cut stroke's layer's: a piece of the tracing paper's stroke is the tracing paper's.
|
|
86
|
+
*/
|
|
87
|
+
cut(cuts: readonly Cutting[], time: number): boolean;
|
|
64
88
|
/**
|
|
65
89
|
* Takes back the last action: a written stroke goes, erased strokes come back to their places, a
|
|
66
|
-
*
|
|
90
|
+
* cut takes its pieces off and puts its strokes back, a clearing brings its strokes back. On a
|
|
91
|
+
* sheet without actions nothing changes.
|
|
67
92
|
*/
|
|
68
93
|
undo(time: number): boolean;
|
|
69
94
|
/** Takes every stroke off the sheet, in one action. Clearing an empty sheet is no action. */
|
|
70
95
|
clear(time: number): boolean;
|
|
71
96
|
/**
|
|
72
|
-
* The strokes on the sheet whose
|
|
73
|
-
* sheet units: the
|
|
74
|
-
* of
|
|
75
|
-
*
|
|
76
|
-
*
|
|
97
|
+
* The strokes on the sheet whose ink comes within `radius` of the segment from `from` to `to`,
|
|
98
|
+
* in sheet units: whose line — the segments between its points — comes within `radius` and the
|
|
99
|
+
* reach of its ink (`reachOf`). The index finds those whose box, widened by that reach, is near
|
|
100
|
+
* enough, and the distance to each segment of their points decides. A segment whose ends are one
|
|
101
|
+
* point is that point. On a task from paper an erasing takes the strokes of one layer alone,
|
|
102
|
+
* `layer`: the tracing paper's, its points from its corner, or the sheet's.
|
|
77
103
|
*/
|
|
78
|
-
near(from: Point, to: Point,
|
|
104
|
+
near(from: Point, to: Point, radius: number, layer?: "tracing" | "sheet"): Stroke[];
|
|
79
105
|
}
|
|
80
|
-
/**
|
|
106
|
+
/**
|
|
107
|
+
* The ID the next stroke takes after a history: one past the highest written, a piece a cut left
|
|
108
|
+
* too, or 1.
|
|
109
|
+
*/
|
|
81
110
|
export declare function nextIdOf(history: readonly HistoryEvent[]): number;
|
|
82
111
|
/**
|
|
83
112
|
* Where the sheet's clock goes on after a history given to it: past its last event by more
|
package/lib/core/work.js
CHANGED
|
@@ -4,6 +4,7 @@ import { shown } from "./fields.js";
|
|
|
4
4
|
import { boxOf, segmentBox, segmentDistance } from "./geometry.js";
|
|
5
5
|
import { GROUP_RULE } from "./groups.js";
|
|
6
6
|
import { frozen, historyOf, sameEvent } from "./history.js";
|
|
7
|
+
import { reachOf } from "./style.js";
|
|
7
8
|
/**
|
|
8
9
|
* The work on the sheet: the strokes in sight, the stack of the actions that undo takes back, the
|
|
9
10
|
* last first, and the history of writing, every action as an event at the sheet's time.
|
|
@@ -28,8 +29,10 @@ export class Work {
|
|
|
28
29
|
#index = new RBush();
|
|
29
30
|
#indexed = this.#strokes;
|
|
30
31
|
#entries = new WeakMap();
|
|
31
|
-
/** The IDs of the strokes written on the tracing paper of a task from paper. */
|
|
32
|
+
/** The IDs of the strokes written on the tracing paper of a task from paper, and their pieces. */
|
|
32
33
|
#tracing = new Set();
|
|
34
|
+
/** The stroke each piece was cut of, by the piece's ID. */
|
|
35
|
+
#cutOf = new Map();
|
|
33
36
|
constructor(clock = new Clock()) {
|
|
34
37
|
this.#clock = clock;
|
|
35
38
|
}
|
|
@@ -38,7 +41,7 @@ export class Work {
|
|
|
38
41
|
* actions for undo and the history itself, in frozen objects of the work's own. The events are
|
|
39
42
|
* checked as `historyOf` checks them, then replayed by the work's own rules, each at its own
|
|
40
43
|
* time, and each must be what the work records for it: an erasing takes off strokes on the sheet,
|
|
41
|
-
* in the sheet's order; a clearing clears a sheet with strokes, all of them; an undo takes back
|
|
44
|
+
* in the sheet's order, and so does a cut, which leaves its pieces at their places; a clearing clears a sheet with strokes, all of them; an undo takes back
|
|
42
45
|
* the last action still in force and says what the stack says. A history the sheet could not have
|
|
43
46
|
* written is a TypeError that names the event. The clock is not asked: it goes on as the sheet
|
|
44
47
|
* that takes the work tells it (`Clock.resume`). On a task from paper, `paper`, a stroke may be
|
|
@@ -56,8 +59,9 @@ export class Work {
|
|
|
56
59
|
return work;
|
|
57
60
|
}
|
|
58
61
|
/**
|
|
59
|
-
* The strokes on the sheet, in the order they were written
|
|
60
|
-
* undoing a clearing
|
|
62
|
+
* The strokes on the sheet, in the order they were written, each piece of a cut stroke at the
|
|
63
|
+
* place the stroke had. Every action gives a new array, and undoing a clearing or a cut gives back
|
|
64
|
+
* the very one it took off.
|
|
61
65
|
*/
|
|
62
66
|
get strokes() {
|
|
63
67
|
return this.#strokes;
|
|
@@ -80,6 +84,17 @@ export class Work {
|
|
|
80
84
|
get traced() {
|
|
81
85
|
return this.#tracing.size > 0;
|
|
82
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* The strokes a stroke was cut of, by their IDs: the stroke it is a piece of, the stroke that one
|
|
89
|
+
* is a piece of, and on to a stroke written whole. None for a stroke written whole, or an ID the
|
|
90
|
+
* work does not know.
|
|
91
|
+
*/
|
|
92
|
+
cutFrom(id) {
|
|
93
|
+
const from = [];
|
|
94
|
+
for (let of = this.#cutOf.get(id); of !== undefined; of = this.#cutOf.get(of))
|
|
95
|
+
from.push(of);
|
|
96
|
+
return from;
|
|
97
|
+
}
|
|
83
98
|
/**
|
|
84
99
|
* The history of writing: every action, in the order it landed, frozen through. An array once
|
|
85
100
|
* handed out never changes: after an action the history is a new one, made when it is next asked
|
|
@@ -106,9 +121,21 @@ export class Work {
|
|
|
106
121
|
erase(ids, time) {
|
|
107
122
|
return this.#erase(ids, () => this.#clock.at(time));
|
|
108
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* One press of the eraser that takes a part, in one action: each stroke it crossed, by its ID,
|
|
126
|
+
* goes off the sheet, and its pieces come on at the place it had, so the strokes keep their order.
|
|
127
|
+
* The event names the strokes in the sheet's order, and their pieces in that order and along
|
|
128
|
+
* each. A stroke no longer on the sheet is left out, with its pieces; if none is on it, nothing is
|
|
129
|
+
* done. The pieces' IDs are the caller's, new ones the history never had, and the pieces are the
|
|
130
|
+
* cut stroke's layer's: a piece of the tracing paper's stroke is the tracing paper's.
|
|
131
|
+
*/
|
|
132
|
+
cut(cuts, time) {
|
|
133
|
+
return this.#cut(cuts, () => this.#clock.at(time));
|
|
134
|
+
}
|
|
109
135
|
/**
|
|
110
136
|
* Takes back the last action: a written stroke goes, erased strokes come back to their places, a
|
|
111
|
-
*
|
|
137
|
+
* cut takes its pieces off and puts its strokes back, a clearing brings its strokes back. On a
|
|
138
|
+
* sheet without actions nothing changes.
|
|
112
139
|
*/
|
|
113
140
|
undo(time) {
|
|
114
141
|
return this.#undo(() => this.#clock.at(time));
|
|
@@ -118,18 +145,19 @@ export class Work {
|
|
|
118
145
|
return this.#clear(() => this.#clock.at(time));
|
|
119
146
|
}
|
|
120
147
|
/**
|
|
121
|
-
* The strokes on the sheet whose
|
|
122
|
-
* sheet units: the
|
|
123
|
-
* of
|
|
124
|
-
*
|
|
125
|
-
*
|
|
148
|
+
* The strokes on the sheet whose ink comes within `radius` of the segment from `from` to `to`,
|
|
149
|
+
* in sheet units: whose line — the segments between its points — comes within `radius` and the
|
|
150
|
+
* reach of its ink (`reachOf`). The index finds those whose box, widened by that reach, is near
|
|
151
|
+
* enough, and the distance to each segment of their points decides. A segment whose ends are one
|
|
152
|
+
* point is that point. On a task from paper an erasing takes the strokes of one layer alone,
|
|
153
|
+
* `layer`: the tracing paper's, its points from its corner, or the sheet's.
|
|
126
154
|
*/
|
|
127
|
-
near(from, to,
|
|
155
|
+
near(from, to, radius, layer) {
|
|
128
156
|
const hits = [];
|
|
129
|
-
for (const { stroke } of this.#indexOf().search(segmentBox(from, to,
|
|
157
|
+
for (const { stroke } of this.#indexOf().search(segmentBox(from, to, radius))) {
|
|
130
158
|
if (layer !== undefined && this.#tracing.has(stroke.id) !== (layer === "tracing"))
|
|
131
159
|
continue;
|
|
132
|
-
if (lineDistance(stroke, from, to) <=
|
|
160
|
+
if (lineDistance(stroke, from, to) <= radius + reachOf(stroke))
|
|
133
161
|
hits.push(stroke);
|
|
134
162
|
}
|
|
135
163
|
return hits;
|
|
@@ -169,6 +197,35 @@ export class Work {
|
|
|
169
197
|
this.#record({ kind: "erase", t: at(), ids: erased.map(({ stroke }) => stroke.id) });
|
|
170
198
|
return true;
|
|
171
199
|
}
|
|
200
|
+
#cut(cuts, at) {
|
|
201
|
+
const byId = new Map(cuts.map((cut) => [cut.id, cut.pieces]));
|
|
202
|
+
const before = this.#strokes;
|
|
203
|
+
const ids = [];
|
|
204
|
+
const pieces = [];
|
|
205
|
+
const strokes = [];
|
|
206
|
+
for (const stroke of before) {
|
|
207
|
+
const left = byId.get(stroke.id);
|
|
208
|
+
if (left === undefined) {
|
|
209
|
+
strokes.push(stroke);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
ids.push(stroke.id);
|
|
213
|
+
const traced = this.#tracing.has(stroke.id);
|
|
214
|
+
for (const piece of left) {
|
|
215
|
+
strokes.push(piece);
|
|
216
|
+
pieces.push({ of: stroke.id, stroke: piece });
|
|
217
|
+
this.#cutOf.set(piece.id, stroke.id);
|
|
218
|
+
if (traced)
|
|
219
|
+
this.#tracing.add(piece.id);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (ids.length === 0)
|
|
223
|
+
return false;
|
|
224
|
+
this.#strokes = strokes;
|
|
225
|
+
this.#done.push({ kind: "cut", before, ids, pieces });
|
|
226
|
+
this.#record({ kind: "cut", t: at(), ids, pieces });
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
172
229
|
#undo(at) {
|
|
173
230
|
const action = this.#done.pop();
|
|
174
231
|
if (action === undefined)
|
|
@@ -189,6 +246,12 @@ export class Work {
|
|
|
189
246
|
const restored = action.erased.map(({ stroke }) => stroke.id);
|
|
190
247
|
this.#record({ kind: "undo", t, removed: [], restored });
|
|
191
248
|
}
|
|
249
|
+
else if (action.kind === "cut") {
|
|
250
|
+
// The sheet is as the cut left it, so it goes back to the very sheet before the cut.
|
|
251
|
+
this.#strokes = action.before;
|
|
252
|
+
const removed = action.pieces.map(({ stroke }) => stroke.id);
|
|
253
|
+
this.#record({ kind: "undo", t, removed, restored: [...action.ids] });
|
|
254
|
+
}
|
|
192
255
|
else {
|
|
193
256
|
this.#strokes = action.strokes;
|
|
194
257
|
const restored = action.strokes.map((stroke) => stroke.id);
|
|
@@ -216,6 +279,8 @@ export class Work {
|
|
|
216
279
|
this.#write(event.stroke, event.t, event.on);
|
|
217
280
|
else if (event.kind === "erase")
|
|
218
281
|
done = this.#erase(event.ids, at);
|
|
282
|
+
else if (event.kind === "cut")
|
|
283
|
+
done = this.#cut(cuttingsOf(event), at);
|
|
219
284
|
else if (event.kind === "clear")
|
|
220
285
|
done = this.#clear(at);
|
|
221
286
|
else
|
|
@@ -240,21 +305,43 @@ export class Work {
|
|
|
240
305
|
#entryOf(stroke) {
|
|
241
306
|
let entry = this.#entries.get(stroke);
|
|
242
307
|
if (entry === undefined) {
|
|
243
|
-
|
|
308
|
+
const reach = reachOf(stroke);
|
|
309
|
+
const { minX, minY, maxX, maxY } = boxOf(stroke.points);
|
|
310
|
+
entry = {
|
|
311
|
+
minX: minX - reach,
|
|
312
|
+
minY: minY - reach,
|
|
313
|
+
maxX: maxX + reach,
|
|
314
|
+
maxY: maxY + reach,
|
|
315
|
+
stroke,
|
|
316
|
+
};
|
|
244
317
|
this.#entries.set(stroke, entry);
|
|
245
318
|
}
|
|
246
319
|
return entry;
|
|
247
320
|
}
|
|
248
321
|
}
|
|
249
|
-
/**
|
|
322
|
+
/**
|
|
323
|
+
* The ID the next stroke takes after a history: one past the highest written, a piece a cut left
|
|
324
|
+
* too, or 1.
|
|
325
|
+
*/
|
|
250
326
|
export function nextIdOf(history) {
|
|
251
327
|
let highest = 0;
|
|
252
328
|
for (const event of history) {
|
|
253
329
|
if (event.kind === "write")
|
|
254
330
|
highest = Math.max(highest, event.stroke.id);
|
|
331
|
+
else if (event.kind === "cut") {
|
|
332
|
+
for (const { stroke } of event.pieces)
|
|
333
|
+
highest = Math.max(highest, stroke.id);
|
|
334
|
+
}
|
|
255
335
|
}
|
|
256
336
|
return highest + 1;
|
|
257
337
|
}
|
|
338
|
+
/** A cut of a history as the work takes one: each stroke it names, with its pieces. */
|
|
339
|
+
function cuttingsOf(event) {
|
|
340
|
+
return event.ids.map((id) => ({
|
|
341
|
+
id,
|
|
342
|
+
pieces: event.pieces.filter(({ of }) => of === id).map(({ stroke }) => stroke),
|
|
343
|
+
}));
|
|
344
|
+
}
|
|
258
345
|
/**
|
|
259
346
|
* Where the sheet's clock goes on after a history given to it: past its last event by more
|
|
260
347
|
* than the pause of the groups, so that the first stroke after it is no neighbour of its
|
|
@@ -271,6 +358,8 @@ function what(event) {
|
|
|
271
358
|
return `stroke ${event.stroke.id} written`;
|
|
272
359
|
case "erase":
|
|
273
360
|
return `strokes ${shown(event.ids)} erased`;
|
|
361
|
+
case "cut":
|
|
362
|
+
return `strokes ${shown(event.ids)} cut into ${shown(event.pieces.map(({ stroke }) => stroke.id))}`;
|
|
274
363
|
case "clear":
|
|
275
364
|
return `strokes ${shown(event.ids)} cleared`;
|
|
276
365
|
case "undo":
|
|
@@ -281,6 +370,7 @@ function what(event) {
|
|
|
281
370
|
function instead(recorded) {
|
|
282
371
|
switch (recorded?.kind) {
|
|
283
372
|
case "erase":
|
|
373
|
+
case "cut":
|
|
284
374
|
return `where the sheet takes off ${shown(recorded.ids)}, in its order`;
|
|
285
375
|
case "clear":
|
|
286
376
|
return `where the sheet clears ${shown(recorded.ids)}`;
|
|
@@ -294,6 +384,7 @@ function instead(recorded) {
|
|
|
294
384
|
function unable(event) {
|
|
295
385
|
switch (event.kind) {
|
|
296
386
|
case "erase":
|
|
387
|
+
case "cut":
|
|
297
388
|
return "but none of them is on the sheet";
|
|
298
389
|
case "clear":
|
|
299
390
|
return "but the sheet is empty";
|