@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.
@@ -9,7 +9,7 @@ import type { Stroke } from "./ink.js";
9
9
  * runner, which crops by the same rule, gets the same numbers to the bit.
10
10
  */
11
11
  export declare const CROP: Readonly<{
12
- /** The margin around the work, px. */
12
+ /** The least margin around the work, px: wider where half the widest line and a pixel are. */
13
13
  margin: 12;
14
14
  /**
15
15
  * A stroke lower than this, in units, is a line, not writing — a dot, a minus, a bar, an
@@ -54,9 +54,24 @@ export interface Crop extends CropNumbers {
54
54
  readonly median: number;
55
55
  /** s, the crop's px to a sheet unit: D′ over h_med, or over the ceiling if h_med is above it. */
56
56
  readonly scale: number;
57
- /** The width of the line, px: D′ / 12, and 1 at least. */
57
+ /**
58
+ * The width of its widest line, px: of a stroke without a style D′ / 12, and 1 at least; of one
59
+ * with a style its width at the crop's scale, and 1 at least.
60
+ */
58
61
  readonly line: number;
59
62
  }
63
+ /**
64
+ * A stroke's width for the rule of the crop: its style's, in whole tenths of a unit, or null for
65
+ * a stroke without a style.
66
+ */
67
+ export declare function widthOf(stroke: Stroke): number | null;
68
+ /**
69
+ * N, the ink of a stroke's line on a crop with its digits `drawn` px high, of a set whose scale
70
+ * takes `unit`, H, in tenths: the line is N / (12 × H) px wide. Without a style, `width` null,
71
+ * N = H × max(D′, 12), the line max(D′, 12) / 12 px; with one of `width` W tenths,
72
+ * N = 12 × max(W × D′, H), its width at the crop's scale and 1 px at least. A whole number.
73
+ */
74
+ export declare function inkOf(width: number | null, drawn: number, unit: number): number;
60
75
  /**
61
76
  * The bundles of the strokes on a sheet: strokes no lower than `CROP.flat`, joined by a chain
62
77
  * of boxes that touch or overlap, whenever each was written. Each bundle by its IDs ascending, the
package/lib/core/crop.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import RBush from "rbush";
2
- import { around, extentOf } from "./geometry.js";
2
+ import { around, extentOf, tenths } from "./geometry.js";
3
3
  import { Sets } from "./sets.js";
4
4
  /**
5
5
  * The crop's rule in its numbers: sizes on the crop in pixels, heights on the sheet in units.
@@ -8,7 +8,7 @@ import { Sets } from "./sets.js";
8
8
  * runner, which crops by the same rule, gets the same numbers to the bit.
9
9
  */
10
10
  export const CROP = Object.freeze({
11
- /** The margin around the work, px. */
11
+ /** The least margin around the work, px: wider where half the widest line and a pixel are. */
12
12
  margin: 12,
13
13
  /**
14
14
  * A stroke lower than this, in units, is a line, not writing — a dot, a minus, a bar, an
@@ -36,6 +36,22 @@ export const CROP = Object.freeze({
36
36
  /** The tallest digit a crop takes, px: its ink then still stays within the margin. */
37
37
  tallest: 256,
38
38
  });
39
+ /**
40
+ * A stroke's width for the rule of the crop: its style's, in whole tenths of a unit, or null for
41
+ * a stroke without a style.
42
+ */
43
+ export function widthOf(stroke) {
44
+ return stroke.style === undefined ? null : tenths(stroke.style.width);
45
+ }
46
+ /**
47
+ * N, the ink of a stroke's line on a crop with its digits `drawn` px high, of a set whose scale
48
+ * takes `unit`, H, in tenths: the line is N / (12 × H) px wide. Without a style, `width` null,
49
+ * N = H × max(D′, 12), the line max(D′, 12) / 12 px; with one of `width` W tenths,
50
+ * N = 12 × max(W × D′, H), its width at the crop's scale and 1 px at least. A whole number.
51
+ */
52
+ export function inkOf(width, drawn, unit) {
53
+ return width === null ? unit * Math.max(drawn, 12) : 12 * Math.max(width * drawn, unit);
54
+ }
39
55
  /**
40
56
  * The bundles of the strokes on a sheet: strokes no lower than `CROP.flat`, joined by a chain
41
57
  * of boxes that touch or overlap, whenever each was written. Each bundle by its IDs ascending, the
@@ -67,20 +83,27 @@ export function cropOf(strokes, groups, digit = CROP.digit, also) {
67
83
  const box = around(also === undefined ? extents : [...extents, also]);
68
84
  const median = medianTenths(extents);
69
85
  const unit = Math.min(median, CROP.ceiling * 10);
70
- let size = sizeOf(box, unit, digit);
71
- // At a digit of 1 px every sheet fits: its sides are 200 px, for (14000 / 150 + 24) px is less.
86
+ const widthById = new Map(strokes.map((stroke) => [stroke.id, widthOf(stroke)]));
87
+ const widths = [...new Set(widthById.values())];
88
+ // N of the widest line of strokes of these widths, at a height drawn.
89
+ const widest = (of, drawn) => Math.max(...of.map((width) => inkOf(width, drawn, unit)));
90
+ const sizeAt = (drawn) => sizeOf(box, unit, drawn, widest(widths, drawn));
91
+ let size = sizeAt(digit);
92
+ // At a digit of 1 px every sheet fits: its sides are 200 px, for (14000 / 150 + 24) px is less,
93
+ // and a line 50 units wide is then under 2 px, within the least margin.
72
94
  while (!fits(size) && size.drawn > 1)
73
- size = sizeOf(box, unit, size.drawn - 1);
95
+ size = sizeAt(size.drawn - 1);
74
96
  const { drawn, width, height, top, left } = size;
75
- // The line's width in twelfths of a px, 12 at least.
76
- const line = Math.max(drawn, 12);
77
- // Places on the crop as whole numbers, X × 24 × unit. A frame reaches past the points by half the
78
- // line and by the pixel at its edge, which the raster still shades: (line / 24 + 1) px.
97
+ // N of the widest line: the line is N / (12 × unit) px wide.
98
+ const line = widest(widths, drawn);
99
+ // Places on the crop as whole numbers, X × 24 × unit. A frame reaches past the points by half its
100
+ // group's widest line and by the pixel at its edge, which the raster still shades: N / (24 ×
101
+ // unit) + 1 px.
79
102
  const across = (x) => 24 * ((x - box.minX) * drawn + left * unit);
80
103
  const down = (y) => 24 * ((y - box.minY) * drawn + top * unit);
81
- const reach = (line + 24) * unit;
82
- const frames = groups.map(({ label }, n) => {
104
+ const frames = groups.map(({ label, ids }, n) => {
83
105
  const frame = around(members[n] ?? []);
106
+ const reach = widest([...new Set(ids.map((id) => widthById.get(id) ?? null))], drawn) + 24 * unit;
84
107
  return {
85
108
  label,
86
109
  box: [
@@ -98,7 +121,7 @@ export function cropOf(strokes, groups, digit = CROP.digit, also) {
98
121
  drawn,
99
122
  median: median / 10,
100
123
  scale: (10 * drawn) / unit,
101
- line: line / 12,
124
+ line: line / (12 * unit),
102
125
  width,
103
126
  height,
104
127
  margins,
@@ -190,15 +213,20 @@ function medianTenths(extents) {
190
213
  .sort(byNumber);
191
214
  return heights[Math.floor((heights.length - 1) / 2)] ?? CROP.flat * 10;
192
215
  }
193
- function sizeOf(box, unit, drawn) {
194
- const width = ceilDiv((box.maxX - box.minX) * drawn, unit) + 2 * CROP.margin;
195
- const height = ceilDiv((box.maxY - box.minY) * drawn, unit) + 2 * CROP.margin;
216
+ /**
217
+ * The size at a height drawn, of strokes whose widest line is `ink`, N: the margin holds half that
218
+ * line and a pixel, ⌈N / (24 × unit)⌉ + 1 px, and `CROP.margin` at least.
219
+ */
220
+ function sizeOf(box, unit, drawn, ink) {
221
+ const margin = Math.max(CROP.margin, ceilDiv(ink, 24 * unit) + 1);
222
+ const width = ceilDiv((box.maxX - box.minX) * drawn, unit) + 2 * margin;
223
+ const height = ceilDiv((box.maxY - box.minY) * drawn, unit) + 2 * margin;
196
224
  return {
197
225
  drawn,
198
226
  width: Math.max(width, CROP.side),
199
227
  height: Math.max(height, CROP.side),
200
- top: CROP.margin + floorDiv(Math.max(CROP.side - height, 0), 2),
201
- left: CROP.margin + floorDiv(Math.max(CROP.side - width, 0), 2),
228
+ top: margin + floorDiv(Math.max(CROP.side - height, 0), 2),
229
+ left: margin + floorDiv(Math.max(CROP.side - width, 0), 2),
202
230
  };
203
231
  }
204
232
  /** Whether every model of Claude takes the crop as it is: no side too long, no tokens too many. */
@@ -0,0 +1,46 @@
1
+ import type { InkPoint, Stroke } from "./ink.js";
2
+ import type { Point } from "./screen.js";
3
+ /**
4
+ * What an eraser that takes a part leaves of a stroke: the eraser's path, the sheet's points of
5
+ * one press, is a line `radius` wide on either side, round at its ends — a tap is a dot of that
6
+ * radius —, and the stroke's line, the segments between its points, loses every stretch that lies
7
+ * within it. Computed exactly, not by samples: each segment of the stroke is inside each capsule
8
+ * of the path, the segment between two of its points widened by the radius, along one interval,
9
+ * for a capsule is convex.
10
+ *
11
+ * Each stretch the eraser left is a piece: a point of its own where the eraser's edge crossed the
12
+ * stroke, unless that is a point of the stroke's, then the stroke's points within it, and a point
13
+ * of its own at its other end. A point of its own lies on its segment, its time and its pressure
14
+ * between those of the segment's ends, so the pressure stays the pointer's and the time within the
15
+ * stroke's. A stretch shorter than `MIN_STEP`, the least step the recorder keeps, is no piece. A
16
+ * stroke of one point, or of points all in one place, is covered whole or not at all.
17
+ *
18
+ * `radius` is in the stroke's own units — on the tracing paper, the paper's. The element gives
19
+ * the eraser's radius and the half width of the stroke's line (`reachOf`), so that no ink a piece
20
+ * keeps lies within the eraser's path: what the eraser comes near, by `Work.near`, it cuts.
21
+ *
22
+ * The path may be given a segment at a time (`add`), as the eraser moves, and the pieces asked for
23
+ * at any time, as the screen shows the strokes under an eraser still down.
24
+ */
25
+ export declare class Cut {
26
+ #private;
27
+ readonly stroke: Stroke;
28
+ readonly radius: number;
29
+ constructor(stroke: Stroke, radius: number);
30
+ /**
31
+ * The eraser goes from `from` to `to`, a dot where they are one point: what it covers of the
32
+ * stroke is added. Whether it covered anything of it that it had not.
33
+ */
34
+ add(from: Point, to: Point): boolean;
35
+ /**
36
+ * The pieces the eraser leaves of the stroke, each the points of one, along the stroke: none
37
+ * when it covered the stroke whole, and `null` when it did not cross it. A stroke whose every
38
+ * stretch left is shorter than `MIN_STEP` leaves none.
39
+ */
40
+ pieces(): InkPoint[][] | null;
41
+ }
42
+ /**
43
+ * The pieces an eraser's path, its points of one press, leaves of a stroke: as `Cut` gives them,
44
+ * `null` when the path does not cross it.
45
+ */
46
+ export declare function cutOf(stroke: Stroke, path: readonly Point[], radius: number): InkPoint[][] | null;
@@ -0,0 +1,297 @@
1
+ import { boxOf } from "./geometry.js";
2
+ import { MIN_STEP } from "./recorder.js";
3
+ /**
4
+ * What an eraser that takes a part leaves of a stroke: the eraser's path, the sheet's points of
5
+ * one press, is a line `radius` wide on either side, round at its ends — a tap is a dot of that
6
+ * radius —, and the stroke's line, the segments between its points, loses every stretch that lies
7
+ * within it. Computed exactly, not by samples: each segment of the stroke is inside each capsule
8
+ * of the path, the segment between two of its points widened by the radius, along one interval,
9
+ * for a capsule is convex.
10
+ *
11
+ * Each stretch the eraser left is a piece: a point of its own where the eraser's edge crossed the
12
+ * stroke, unless that is a point of the stroke's, then the stroke's points within it, and a point
13
+ * of its own at its other end. A point of its own lies on its segment, its time and its pressure
14
+ * between those of the segment's ends, so the pressure stays the pointer's and the time within the
15
+ * stroke's. A stretch shorter than `MIN_STEP`, the least step the recorder keeps, is no piece. A
16
+ * stroke of one point, or of points all in one place, is covered whole or not at all.
17
+ *
18
+ * `radius` is in the stroke's own units — on the tracing paper, the paper's. The element gives
19
+ * the eraser's radius and the half width of the stroke's line (`reachOf`), so that no ink a piece
20
+ * keeps lies within the eraser's path: what the eraser comes near, by `Work.near`, it cuts.
21
+ *
22
+ * The path may be given a segment at a time (`add`), as the eraser moves, and the pieces asked for
23
+ * at any time, as the screen shows the strokes under an eraser still down.
24
+ */
25
+ export class Cut {
26
+ stroke;
27
+ radius;
28
+ /** The stroke's box, widened by the radius: a capsule that misses it misses every segment. */
29
+ #reach;
30
+ /** The parts of each segment the eraser covered, by the segment's place: merged intervals. */
31
+ #covered = new Map();
32
+ /** Whether the stroke has no length: one point, or every point in one place. */
33
+ #still;
34
+ /** Whether a stroke of no length is covered: its one place is within a capsule. */
35
+ #whole = false;
36
+ constructor(stroke, radius) {
37
+ if (stroke.points.length === 0)
38
+ throw new TypeError(`the stroke ${stroke.id} has no points`);
39
+ if (!(radius >= 0 && Number.isFinite(radius))) {
40
+ throw new RangeError(`an eraser's radius is a number from 0, not ${radius}`);
41
+ }
42
+ this.stroke = stroke;
43
+ this.radius = radius;
44
+ const [first, ...rest] = stroke.points;
45
+ this.#still = rest.every(({ x, y }) => x === first.x && y === first.y);
46
+ const box = boxOf(stroke.points);
47
+ this.#reach = {
48
+ minX: box.minX - radius,
49
+ minY: box.minY - radius,
50
+ maxX: box.maxX + radius,
51
+ maxY: box.maxY + radius,
52
+ };
53
+ }
54
+ /**
55
+ * The eraser goes from `from` to `to`, a dot where they are one point: what it covers of the
56
+ * stroke is added. Whether it covered anything of it that it had not.
57
+ */
58
+ add(from, to) {
59
+ const reach = this.#reach;
60
+ if (Math.max(from.x, to.x) < reach.minX ||
61
+ Math.min(from.x, to.x) > reach.maxX ||
62
+ Math.max(from.y, to.y) < reach.minY ||
63
+ Math.min(from.y, to.y) > reach.maxY) {
64
+ return false;
65
+ }
66
+ const { points } = this.stroke;
67
+ const r = this.radius;
68
+ const left = Math.min(from.x, to.x) - r;
69
+ const right = Math.max(from.x, to.x) + r;
70
+ const top = Math.min(from.y, to.y) - r;
71
+ const bottom = Math.max(from.y, to.y) + r;
72
+ if (this.#still) {
73
+ if (this.#whole)
74
+ return false;
75
+ this.#whole = withinCapsule(points[0], from, to, r);
76
+ return this.#whole;
77
+ }
78
+ let more = false;
79
+ for (let k = 1; k < points.length; k++) {
80
+ const a = points[k - 1];
81
+ const b = points[k];
82
+ if (Math.max(a.x, b.x) < left ||
83
+ Math.min(a.x, b.x) > right ||
84
+ Math.max(a.y, b.y) < top ||
85
+ Math.min(a.y, b.y) > bottom) {
86
+ continue;
87
+ }
88
+ const span = capsuleSpan(a, b, from, to, r);
89
+ if (span !== null && this.#cover(k - 1, span))
90
+ more = true;
91
+ }
92
+ return more;
93
+ }
94
+ /**
95
+ * The pieces the eraser leaves of the stroke, each the points of one, along the stroke: none
96
+ * when it covered the stroke whole, and `null` when it did not cross it. A stroke whose every
97
+ * stretch left is shorter than `MIN_STEP` leaves none.
98
+ */
99
+ pieces() {
100
+ if (this.#still)
101
+ return this.#whole ? [] : null;
102
+ if (this.#covered.size === 0)
103
+ return null;
104
+ const { points } = this.stroke;
105
+ const last = points.length - 1;
106
+ // The stretches left, as places along the stroke: the segment's place and the share of it.
107
+ const left = [];
108
+ let from = 0;
109
+ for (let k = 0; k < last; k++) {
110
+ for (const [start, end] of this.#covered.get(k) ?? []) {
111
+ if (k + start > from)
112
+ left.push([from, k + start]);
113
+ from = Math.max(from, k + end);
114
+ }
115
+ }
116
+ if (from < last)
117
+ left.push([from, last]);
118
+ const pieces = [];
119
+ for (const [start, end] of left) {
120
+ if (lengthOf(points, start, end) < MIN_STEP)
121
+ continue;
122
+ const piece = [pointAt(points, start)];
123
+ for (let k = Math.floor(start) + 1; k < end; k++)
124
+ piece.push(points[k]);
125
+ piece.push(pointAt(points, end));
126
+ pieces.push(piece);
127
+ }
128
+ // A touch at an end of the stroke too slight for the numbers to move its point: no cut.
129
+ const [only] = pieces;
130
+ if (pieces.length === 1 && only?.length === points.length) {
131
+ if (only.every((point, k) => samePlace(point, points[k])))
132
+ return null;
133
+ }
134
+ return pieces;
135
+ }
136
+ /** An interval of a segment covered: merged with those it overlaps. Whether it adds to them. */
137
+ #cover(segment, [start, end]) {
138
+ // A touch at one point covers nothing a piece would lose.
139
+ if (end <= start)
140
+ return false;
141
+ const intervals = this.#covered.get(segment) ?? [];
142
+ let low = start;
143
+ let high = end;
144
+ const kept = [];
145
+ let added = true;
146
+ for (const interval of intervals) {
147
+ const [a, b] = interval;
148
+ if (b < low || a > high) {
149
+ kept.push(interval);
150
+ continue;
151
+ }
152
+ if (a <= low && b >= high)
153
+ added = false;
154
+ low = Math.min(low, a);
155
+ high = Math.max(high, b);
156
+ }
157
+ if (!added)
158
+ return false;
159
+ kept.push([low, high]);
160
+ kept.sort((x, y) => x[0] - y[0]);
161
+ this.#covered.set(segment, kept);
162
+ return true;
163
+ }
164
+ }
165
+ /**
166
+ * The pieces an eraser's path, its points of one press, leaves of a stroke: as `Cut` gives them,
167
+ * `null` when the path does not cross it.
168
+ */
169
+ export function cutOf(stroke, path, radius) {
170
+ const cut = new Cut(stroke, radius);
171
+ const [first] = path;
172
+ if (first !== undefined)
173
+ cut.add(first, first);
174
+ for (let k = 1; k < path.length; k++)
175
+ cut.add(path[k - 1], path[k]);
176
+ return cut.pieces();
177
+ }
178
+ function samePlace(a, b) {
179
+ return a.x === b.x && a.y === b.y && a.t === b.t && a.p === b.p;
180
+ }
181
+ /** Whether a point lies within `radius` of the segment pq. */
182
+ function withinCapsule(point, p, q, radius) {
183
+ const ux = q.x - p.x;
184
+ const uy = q.y - p.y;
185
+ const length = ux * ux + uy * uy;
186
+ const along = length === 0
187
+ ? 0
188
+ : Math.min(1, Math.max(0, ((point.x - p.x) * ux + (point.y - p.y) * uy) / length));
189
+ const dx = point.x - (p.x + along * ux);
190
+ const dy = point.y - (p.y + along * uy);
191
+ return dx * dx + dy * dy <= radius * radius;
192
+ }
193
+ /**
194
+ * Where the segment ab lies within the capsule of pq and `radius`, as shares of the way from a to
195
+ * b, or `null` when it does not. The capsule is the disks at p and at q and the strip between
196
+ * them; it is convex, so the parts of ab in each join into one interval.
197
+ */
198
+ function capsuleSpan(a, b, p, q, radius) {
199
+ let low = Number.POSITIVE_INFINITY;
200
+ let high = Number.NEGATIVE_INFINITY;
201
+ const join = (span) => {
202
+ if (span === null)
203
+ return;
204
+ low = Math.min(low, span[0]);
205
+ high = Math.max(high, span[1]);
206
+ };
207
+ join(diskSpan(a, b, p, radius));
208
+ if (q.x !== p.x || q.y !== p.y) {
209
+ join(diskSpan(a, b, q, radius));
210
+ join(stripSpan(a, b, p, q, radius));
211
+ }
212
+ return low <= high ? [low, high] : null;
213
+ }
214
+ /** Where ab lies within the disk of centre c and `radius`: a quadratic in the share s. */
215
+ function diskSpan(a, b, c, radius) {
216
+ const dx = b.x - a.x;
217
+ const dy = b.y - a.y;
218
+ const fx = a.x - c.x;
219
+ const fy = a.y - c.y;
220
+ const qa = dx * dx + dy * dy;
221
+ const qb = 2 * (dx * fx + dy * fy);
222
+ const qc = fx * fx + fy * fy - radius * radius;
223
+ if (qa === 0)
224
+ return qc <= 0 ? [0, 1] : null;
225
+ const discriminant = qb * qb - 4 * qa * qc;
226
+ if (discriminant < 0)
227
+ return null;
228
+ const root = Math.sqrt(discriminant);
229
+ return clipped((-qb - root) / (2 * qa), (-qb + root) / (2 * qa));
230
+ }
231
+ /**
232
+ * Where ab lies within the strip of pq: its projection on pq within the segment, and its distance
233
+ * from pq's line within `radius`. Two linear bounds on the share s.
234
+ */
235
+ function stripSpan(a, b, p, q, radius) {
236
+ const ux = q.x - p.x;
237
+ const uy = q.y - p.y;
238
+ const length = ux * ux + uy * uy;
239
+ const dx = b.x - a.x;
240
+ const dy = b.y - a.y;
241
+ const fx = a.x - p.x;
242
+ const fy = a.y - p.y;
243
+ // Along pq, times its length squared: from 0 to that length squared.
244
+ const along = linearSpan(fx * ux + fy * uy, dx * ux + dy * uy, 0, length);
245
+ // Across pq, times its length: within the radius times the length on either side.
246
+ const width = radius * Math.sqrt(length);
247
+ const across = linearSpan(ux * fy - uy * fx, ux * dy - uy * dx, -width, width);
248
+ if (along === null || across === null)
249
+ return null;
250
+ return clipped(Math.max(along[0], across[0]), Math.min(along[1], across[1]));
251
+ }
252
+ /** The shares s at which `value + s × slope` lies from `least` to `most`, or `null`. */
253
+ function linearSpan(value, slope, least, most) {
254
+ if (slope === 0)
255
+ return value >= least && value <= most ? [-Infinity, Infinity] : null;
256
+ const one = (least - value) / slope;
257
+ const other = (most - value) / slope;
258
+ return [Math.min(one, other), Math.max(one, other)];
259
+ }
260
+ /** An interval of shares within the segment, 0 to 1, or `null` when none of it is. */
261
+ function clipped(low, high) {
262
+ const start = Math.max(0, low);
263
+ const end = Math.min(1, high);
264
+ return start <= end ? [start, end] : null;
265
+ }
266
+ /** The point at a place along a stroke: a point of its own where the place is no point of it. */
267
+ function pointAt(points, place) {
268
+ const k = Math.floor(place);
269
+ const share = place - k;
270
+ const a = points[k];
271
+ if (share === 0)
272
+ return a;
273
+ const b = points[k + 1];
274
+ return {
275
+ x: between(a.x, b.x, share),
276
+ y: between(a.y, b.y, share),
277
+ t: between(a.t, b.t, share),
278
+ p: between(a.p, b.p, share),
279
+ };
280
+ }
281
+ /** A value a share of the way from one to another, never past either. */
282
+ function between(from, to, share) {
283
+ const value = from + (to - from) * share;
284
+ return Math.min(Math.max(value, Math.min(from, to)), Math.max(from, to));
285
+ }
286
+ /** How long a stroke's line is between two places along it. */
287
+ function lengthOf(points, start, end) {
288
+ let length = 0;
289
+ let previous = pointAt(points, start);
290
+ for (let k = Math.floor(start) + 1; k < end; k++) {
291
+ const point = points[k];
292
+ length += Math.hypot(point.x - previous.x, point.y - previous.y);
293
+ previous = point;
294
+ }
295
+ const last = pointAt(points, end);
296
+ return length + Math.hypot(last.x - previous.x, last.y - previous.y);
297
+ }
@@ -1,27 +1,45 @@
1
1
  import type { Stroke } from "./ink.js";
2
2
  import { type PointerSample } from "./recorder.js";
3
3
  import type { Point } from "./screen.js";
4
+ import type { EraserMode } from "./style.js";
4
5
  /** `button` of a pen's eraser, its other end (Pointer Events Level 3). */
5
6
  export declare const ERASER_BUTTON = 5;
6
7
  /**
7
- * The eraser: one press of a pointer takes off the sheet, whole, every stroke its path comes
8
- * near — a tap is a path of one point. The strokes it comes near are pending while it is
9
- * down, and when it lifts they go to the handler the eraser is made with, in one action, with the
10
- * page's time of the lift: one undo brings them all back. `pointercancel` drops the press and
11
- * erases nothing. One pointer erases at a time, with the primary button or a pen's eraser.
8
+ * What a press of the eraser went through, as it lifts: its mode, its radius, in sheet units, and
9
+ * its path, the places it went through in sight from where it went down — a tap is a path of one
10
+ * point, and each two places in a row are a segment it passed along.
11
+ */
12
+ export interface Erasure {
13
+ readonly mode: EraserMode;
14
+ readonly reach: number;
15
+ readonly path: readonly Point[];
16
+ }
17
+ /**
18
+ * The eraser: one press of a pointer takes off the sheet every stroke its path comes near — a tap
19
+ * is a path of one point —, whole, or in the mode "part" only what it passes over. The strokes it
20
+ * comes near are pending while it is down, and when it lifts they go to the handler the eraser is
21
+ * made with, with the press's mode, radius and path, for one action, with the page's time of the
22
+ * lift: one undo brings them all back. `pointercancel` drops the press and erases nothing. One
23
+ * pointer erases at a time, with the primary button or a pen's eraser.
12
24
  */
13
25
  export declare class Eraser {
14
26
  #private;
15
- constructor(near: (from: Point, to: Point, reach: number) => readonly Stroke[], finished: (ids: readonly number[], time: number) => void);
27
+ constructor(near: (from: Point, to: Point, reach: number) => readonly Stroke[], finished: (ids: readonly number[], time: number, erasure: Erasure) => void);
16
28
  /** The `pointerId` of the pointer erasing, or nothing between presses. */
17
29
  get pointer(): number | undefined;
18
30
  /** The IDs of the strokes the press under way has come near: they go when it lifts. */
19
31
  get pending(): ReadonlySet<number>;
20
32
  /**
21
- * `pointerdown`: a press begins here, `reach` sheet units wide on either side, if none is under
22
- * way and the primary button or a pen's eraser went down. It takes what lies under it at once.
33
+ * The press under way, its path so far: what the screen shows of an eraser that takes a part
34
+ * while it is down. Nothing between presses.
35
+ */
36
+ get erasure(): Erasure | undefined;
37
+ /**
38
+ * `pointerdown`: a press begins here, `reach` sheet units wide on either side, in its mode, if
39
+ * none is under way and the primary button or a pen's eraser went down. It takes what lies under
40
+ * it at once.
23
41
  */
24
- down(sample: PointerSample, reach: number): boolean;
42
+ down(sample: PointerSample, reach: number, mode?: EraserMode): boolean;
25
43
  /**
26
44
  * `pointermove`, once for each coalesced event: the eraser goes on to here, and says whether it
27
45
  * came near a stroke it had not. A move without its button held ends the press where it was.
@@ -5,11 +5,12 @@ export const ERASER_BUTTON = 5;
5
5
  const ERASER_HELD = 32;
6
6
  const NONE = new Set();
7
7
  /**
8
- * The eraser: one press of a pointer takes off the sheet, whole, every stroke its path comes
9
- * near — a tap is a path of one point. The strokes it comes near are pending while it is
10
- * down, and when it lifts they go to the handler the eraser is made with, in one action, with the
11
- * page's time of the lift: one undo brings them all back. `pointercancel` drops the press and
12
- * erases nothing. One pointer erases at a time, with the primary button or a pen's eraser.
8
+ * The eraser: one press of a pointer takes off the sheet every stroke its path comes near — a tap
9
+ * is a path of one point —, whole, or in the mode "part" only what it passes over. The strokes it
10
+ * comes near are pending while it is down, and when it lifts they go to the handler the eraser is
11
+ * made with, with the press's mode, radius and path, for one action, with the page's time of the
12
+ * lift: one undo brings them all back. `pointercancel` drops the press and erases nothing. One
13
+ * pointer erases at a time, with the primary button or a pen's eraser.
13
14
  */
14
15
  export class Eraser {
15
16
  #near;
@@ -28,10 +29,21 @@ export class Eraser {
28
29
  return this.#press?.hits ?? NONE;
29
30
  }
30
31
  /**
31
- * `pointerdown`: a press begins here, `reach` sheet units wide on either side, if none is under
32
- * way and the primary button or a pen's eraser went down. It takes what lies under it at once.
32
+ * The press under way, its path so far: what the screen shows of an eraser that takes a part
33
+ * while it is down. Nothing between presses.
33
34
  */
34
- down(sample, reach) {
35
+ get erasure() {
36
+ const press = this.#press;
37
+ return press === undefined
38
+ ? undefined
39
+ : { mode: press.mode, reach: press.reach, path: press.path };
40
+ }
41
+ /**
42
+ * `pointerdown`: a press begins here, `reach` sheet units wide on either side, in its mode, if
43
+ * none is under way and the primary button or a pen's eraser went down. It takes what lies under
44
+ * it at once.
45
+ */
46
+ down(sample, reach, mode = "stroke") {
35
47
  if (this.#press !== undefined)
36
48
  return false;
37
49
  let held;
@@ -46,6 +58,8 @@ export class Eraser {
46
58
  pointerId: sample.pointerId,
47
59
  held,
48
60
  reach,
61
+ mode,
62
+ path: [],
49
63
  last: point,
50
64
  time: sample.time,
51
65
  hits: new Set(),
@@ -105,6 +119,9 @@ export class Eraser {
105
119
  }
106
120
  /** The strokes near the eraser's way from its last place to `to`: whether any was new. */
107
121
  #take(press, to) {
122
+ // Only a cut reads the way: one taking whole strokes keeps none of it.
123
+ if (press.mode === "part")
124
+ press.path.push(to);
108
125
  let took = false;
109
126
  for (const stroke of this.#near(press.last, to, press.reach)) {
110
127
  if (press.hits.has(stroke.id))
@@ -120,8 +137,13 @@ export class Eraser {
120
137
  return false;
121
138
  // The press is no longer under way by the time the handler has its strokes.
122
139
  this.#press = undefined;
123
- if (press.hits.size > 0)
124
- this.#finished([...press.hits], time);
140
+ if (press.hits.size > 0) {
141
+ this.#finished([...press.hits], time, {
142
+ mode: press.mode,
143
+ reach: press.reach,
144
+ path: press.path,
145
+ });
146
+ }
125
147
  return true;
126
148
  }
127
149
  }
@@ -77,6 +77,11 @@ export interface HandsSheet {
77
77
  writer(): number | undefined;
78
78
  /** The stroke or the erasing of a pointer goes, as on `pointercancel`. */
79
79
  drop(pointerId: number): void;
80
+ /**
81
+ * The stroke or the erasing of a pointer ends where it was, its `pointerup` never to come: as on
82
+ * a lost capture.
83
+ */
84
+ end(pointerId: number): void;
80
85
  /**
81
86
  * Two fingers moved right on the screen by `dx` CSS pixels and down it by `dy`, or left and up
82
87
  * for negative ones: the sheet goes with them. `fresh` marks a gesture's first step.
@@ -109,6 +114,8 @@ export type Role = "writes" | "waits" | "scrolls" | "palm" | "idle";
109
114
  * - One pointer writes at a time, and the sheet's recorder and eraser say which.
110
115
  * - A pen's touch puts the page in pen mode and takes away the stroke of a touch under way: it was
111
116
  * a palm. While a pen or a mouse writes, a touch does nothing until it lifts.
117
+ * - A pen's touch while another pen is down ends what that one writes or erases where it was, and
118
+ * the new pen writes: a screen has one pen, and the other lost its way up.
112
119
  * - A touch wider or taller than `PALM_PX`, at its `pointerdown` or at any move, is a palm: it does
113
120
  * nothing until it lifts, and what it was writing goes.
114
121
  * - Without pen mode the first finger writes. A second that comes down within `PAIR_MS`, before the