@altnovatrail/canvas 0.3.42 → 0.3.44
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/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 +13 -3
- package/lib/core/outline.js +110 -11
- 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 +412 -70
- 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
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";
|
package/lib/element/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { type DemoState, type WorkedExample } from "../core/example.js";
|
|
|
4
4
|
import { type HistoryEvent } from "../core/history.js";
|
|
5
5
|
import { type Manifest } from "../core/manifest.js";
|
|
6
6
|
import { type Marks } from "../core/marks.js";
|
|
7
|
+
import { type PenSettings } from "../core/style.js";
|
|
7
8
|
import { type Task, type Tool } from "../core/task.js";
|
|
8
9
|
/**
|
|
9
10
|
* The types of the contract of canvas and app are spec's: the element as
|
|
@@ -13,13 +14,13 @@ import { type Task, type Tool } from "../core/task.js";
|
|
|
13
14
|
* media and the tutor's marks too. The crop and a task with its temporary tools are the element's
|
|
14
15
|
* own.
|
|
15
16
|
*/
|
|
16
|
-
export type { Box, CanvasElement, CanvasEventMap, DemoChangeEvent, ExportedCell, ExportedPaper, ExportedSheet, ExportedWork, ExportedWorkbook, GridChange, GridChangeEvent, HistoryChange, HistoryChangeEvent, Media, PaperGrid, PaperTask, ToolChange, ToolChangeEvent, TracingChangeEvent, TracingReadEvent, TracingReading, TracingState, } from "@altnovatrail/spec/canvas";
|
|
17
|
+
export type { Box, CanvasElement, CanvasEventMap, DemoChangeEvent, EraserMode, ExportedCell, ExportedPaper, ExportedSheet, ExportedWork, ExportedWorkbook, GridChange, GridChangeEvent, HistoryChange, HistoryChangeEvent, Media, PaperGrid, PaperTask, PenChangeEvent, PenKind, PenSettings, StrokeStyle, ToolChange, ToolChangeEvent, TracingChangeEvent, TracingReadEvent, TracingReading, TracingState, } from "@altnovatrail/spec/canvas";
|
|
17
18
|
export type { Filled, Filling } from "../core/boxes.js";
|
|
18
19
|
export type { Crop, Frame } from "../core/crop.js";
|
|
19
20
|
export type { DemoState, WorkedExample } from "../core/example.js";
|
|
20
21
|
export type { GridKind } from "../core/grid.js";
|
|
21
22
|
export type { Group } from "../core/groups.js";
|
|
22
|
-
export type { HistoryEvent } from "../core/history.js";
|
|
23
|
+
export type { HistoryEvent, Piece } from "../core/history.js";
|
|
23
24
|
export type { InkPoint, Stroke } from "../core/ink.js";
|
|
24
25
|
export type { Manifest } from "../core/manifest.js";
|
|
25
26
|
export type { Anchor, CellPlace, CropNumbers, Mark, Marks, PaperSubmission, SheetSubmission, Submission, WorkbookSubmission, } from "../core/marks.js";
|
|
@@ -33,8 +34,23 @@ export declare const TAG = "novatrail-canvas";
|
|
|
33
34
|
export declare class NovatrailCanvas extends HTMLElement implements CanvasElement {
|
|
34
35
|
#private;
|
|
35
36
|
constructor();
|
|
36
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* The element follows its own `lang`, its buttons labelled in it, `tools-hidden`, and
|
|
39
|
+
* `unstyled`, under which the strokes take no style.
|
|
40
|
+
*/
|
|
37
41
|
static readonly observedAttributes: string[];
|
|
42
|
+
/**
|
|
43
|
+
* The settings of the pen and the eraser, frozen: canvas's as the element comes — a pen, black
|
|
44
|
+
* and three units wide, a blue liner, a yellow marker, and an eraser of strokes whole, 16 units
|
|
45
|
+
* across. The learner changes them in the popovers of the pen and the eraser, which a `penchange`
|
|
46
|
+
* tells; the consumer keeps them and gives them back to the next element, which tells nothing.
|
|
47
|
+
* The next stroke takes the style of the kind in use, and the eraser its mode and its size: a
|
|
48
|
+
* stroke written keeps its own. Settings not of `PenSettings` are a TypeError that names the
|
|
49
|
+
* field, and leave the settings as they were; `undefined` changes nothing. Under the attribute
|
|
50
|
+
* `unstyled` they are kept and given all the same, and the strokes take none of them.
|
|
51
|
+
*/
|
|
52
|
+
get pens(): PenSettings;
|
|
53
|
+
set pens(settings: PenSettings | undefined);
|
|
38
54
|
/**
|
|
39
55
|
* What a press of the primary button does: "pen" writes, as the element starts, "eraser" erases,
|
|
40
56
|
* and "hand" moves the sheet, under any pointer. The learner switches it with the
|
|
@@ -224,7 +240,9 @@ export declare class NovatrailCanvas extends HTMLElement implements CanvasElemen
|
|
|
224
240
|
* `tools-hidden` did: while the element has it, the bar is out of sight and of the accessibility
|
|
225
241
|
* tree, as `app` hides it on a phone under its tutor's sheet. An attribute the
|
|
226
242
|
* element had before it was defined comes here just after the constructor, which reads no
|
|
227
|
-
* attribute.
|
|
243
|
+
* attribute. Or `unstyled` did: while the element has it, a stroke takes no style, 3 units wide
|
|
244
|
+
* in the theme's ink, no popover opens, and the eraser takes whole strokes as near as it
|
|
245
|
+
* always took them — the samples page writes so, while its texts say nothing of a style.
|
|
228
246
|
*/
|
|
229
247
|
attributeChangedCallback(name: string): void;
|
|
230
248
|
/**
|
|
@@ -259,17 +277,21 @@ export declare class NovatrailCanvas extends HTMLElement implements CanvasElemen
|
|
|
259
277
|
* The picture of the work, for the `picture` of a stored work that app puts on the server, a
|
|
260
278
|
* method of the agreement with app: the PNG of the strokes the surface shows,
|
|
261
279
|
* with no condition — the sheet's page, or the workbook's whole page with its draft, and the ink
|
|
262
|
-
* past the page, right and down, with the tracing paper where it lies
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
280
|
+
* past the page, right and down, with the tracing paper where it lies —, whatever is filled and
|
|
281
|
+
* whether the work was sent to be checked or not; an empty surface, and a worked example's, is
|
|
282
|
+
* white. The strokes are those `exportWork()` takes, as the history has them: not the stroke
|
|
283
|
+
* under way, but those under an eraser not yet lifted. Each is the outline the screen fills, on
|
|
284
|
+
* white with no theme and no template, in its style — its own colour, the palette's black too,
|
|
285
|
+
* at its opacity, a marker multiplied —, and a stroke without one black; on a task from paper
|
|
286
|
+
* the strokes of both layers, the tracing paper's where it lies, hidden or not, with the crop's
|
|
287
|
+
* picture laid in grey and multiplied. While every stroke is grey — black, and the palette's
|
|
288
|
+
* black, a hair off grey, among them —, the pixels
|
|
289
|
+
* are grey, a byte each, one pixel a unit, 1000 × 1400 for the page alone, and scaled down past
|
|
290
|
+
* the page to hold no more pixels than it; with a colour among them they are RGB, three bytes
|
|
291
|
+
* each, and scaled down to hold no more bytes. Its size is in its PNG's header. The pixels go
|
|
292
|
+
* through the crop's encoder: so the picture looks as the sheet does, and holds at most
|
|
293
|
+
* 1,576,640 bytes, for the encoder spends at most 9 bits on a byte of a row, under the 4 MiB of a
|
|
294
|
+
* stored work.
|
|
273
295
|
*/
|
|
274
296
|
exportPicture(): Promise<Blob>;
|
|
275
297
|
disconnectedCallback(): void;
|