@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
package/lib/core/outline.js
CHANGED
|
@@ -4,47 +4,181 @@ import { getStroke } from "perfect-freehand";
|
|
|
4
4
|
* of 3 px on its sheet 1000 wide.
|
|
5
5
|
*/
|
|
6
6
|
export const INK_WIDTH = 3;
|
|
7
|
-
/**
|
|
8
|
-
const
|
|
7
|
+
/** A pen's line at no pressure, and at full pressure, in widths of a finger's line. */
|
|
8
|
+
export const PEN_THINNEST = 0.5;
|
|
9
|
+
export const PEN_WIDEST = 2;
|
|
10
|
+
/**
|
|
11
|
+
* The power of the pressure in a pen's width, below 1: Apple Pencil in WebKit gives writing a
|
|
12
|
+
* pressure of about 0–0.2, its median about 0.1 (measurements 1.8), and the curve widens the line
|
|
13
|
+
* more in that low range than a straight one would.
|
|
14
|
+
*/
|
|
15
|
+
const PEN_POWER = 0.8;
|
|
16
|
+
/** How much of a point's pressure is its own: the rest is the point's before it, so that the line
|
|
17
|
+
* does not jump with the pointer's noise. */
|
|
18
|
+
const PEN_FOLLOW = 0.65;
|
|
19
|
+
/**
|
|
20
|
+
* A pen's line at a pressure, in widths of a finger's line: `PEN_THINNEST` at none, `PEN_WIDEST`
|
|
21
|
+
* at full, and between by the pressure to the power `PEN_POWER`. A pressure past 0–1 is taken as
|
|
22
|
+
* the nearer end.
|
|
23
|
+
*/
|
|
24
|
+
export function penWidth(pressure) {
|
|
25
|
+
const p = Math.min(Math.max(pressure, 0), 1);
|
|
26
|
+
return PEN_THINNEST + (PEN_WIDEST - PEN_THINNEST) * p ** PEN_POWER;
|
|
27
|
+
}
|
|
9
28
|
/**
|
|
10
29
|
* How far the ink reaches from a stroke's points, in sheet units, at most: half the widest line, a
|
|
11
|
-
* pen's at full pressure.
|
|
12
|
-
* pressure))`, so 2.25 units with a pressure of 1.
|
|
30
|
+
* pen's at full pressure.
|
|
13
31
|
*/
|
|
14
|
-
export const INK_REACH = INK_WIDTH *
|
|
32
|
+
export const INK_REACH = (INK_WIDTH * PEN_WIDEST) / 2;
|
|
15
33
|
/**
|
|
16
34
|
* Every option of perfect-freehand 1.2.3 is given: its code's defaults are not its README's.
|
|
17
35
|
* The pressure is the pointer's own, the third number of each point, never simulated, and
|
|
18
|
-
* the ends are round, without a taper.
|
|
36
|
+
* the ends are round, without a taper. The library's radius is `size × easing(0.5 − thinning ×
|
|
37
|
+
* (0.5 − pressure))`: a pen's `thinning` of 1 hands `easing` the pressure itself, and half the
|
|
38
|
+
* pen's width is the radius. With a `thinning` of 0 the library calls no `easing`, and the radius
|
|
39
|
+
* is half the size.
|
|
19
40
|
*/
|
|
20
41
|
const OPTIONS = {
|
|
21
42
|
size: INK_WIDTH,
|
|
22
43
|
smoothing: 0.5,
|
|
23
44
|
streamline: 0.5,
|
|
24
45
|
simulatePressure: false,
|
|
25
|
-
easing: (pressure) => pressure,
|
|
46
|
+
easing: (pressure) => penWidth(pressure) / 2,
|
|
26
47
|
start: { cap: true, taper: false },
|
|
27
48
|
end: { cap: true, taper: false },
|
|
28
49
|
};
|
|
50
|
+
/** perfect-freehand's `thinning` of a pen's line, and of an even one. */
|
|
51
|
+
const PEN_THINNING = 1;
|
|
52
|
+
const EVEN = 0;
|
|
29
53
|
/** Whether a stroke's line widens with its pressure on the screen: only a pen's does. */
|
|
30
54
|
export function followsPressure(stroke) {
|
|
31
55
|
return stroke.kind === "pen";
|
|
32
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* The speed at which a pen of the kind "pen" without a pen's pressure draws its finest line, in
|
|
59
|
+
* sheet units a millisecond: a quick stroke of a finger or a mouse. Slower, the line is wider.
|
|
60
|
+
*/
|
|
61
|
+
export const SPEED_FINEST = 1.5;
|
|
62
|
+
/**
|
|
63
|
+
* The share of a pen's line, of the kind "pen", that its speed sets without a pen's pressure: as
|
|
64
|
+
* a pressure, 0.45 at its first point, then each point's leaning on the one's before it by
|
|
65
|
+
* `SPEED_FOLLOW`, from 0.75 at rest down to 0.15 at `SPEED_FINEST` and faster.
|
|
66
|
+
*/
|
|
67
|
+
const SPEED_START = 0.45;
|
|
68
|
+
const SPEED_FOLLOW = 0.25;
|
|
69
|
+
const SPEED_SLOWEST = 0.75;
|
|
70
|
+
const SPEED_RANGE = 0.6;
|
|
71
|
+
/**
|
|
72
|
+
* How a pen's line of the kind "pen" thins at its ends: its width there is this share of what it
|
|
73
|
+
* would be, and it widens to all of it over `TAPER_LENGTH` widths along the stroke.
|
|
74
|
+
*/
|
|
75
|
+
const TAPER_LEAST = 0.45;
|
|
76
|
+
const TAPER_LENGTH = 4;
|
|
33
77
|
/**
|
|
34
78
|
* The outline of a stroke on the screen, as perfect-freehand draws it: a closed polygon around the
|
|
35
|
-
* stroke's points, in sheet units, to be filled.
|
|
36
|
-
*
|
|
37
|
-
*
|
|
79
|
+
* stroke's points, in sheet units, to be filled. `complete` says the stroke has ended: its last
|
|
80
|
+
* point is then taken as it is, rather than eased toward. A stroke without a style is 3 units wide,
|
|
81
|
+
* black in the theme's ink: a pen's line widens with its pressure, and a mouse's or a
|
|
82
|
+
* finger's is as wide all along. A stroke with a style is its width: a liner's and a marker's line
|
|
83
|
+
* as wide all along; one of the kind "pen" wider and finer by a pen's pressure, as a pen without a
|
|
84
|
+
* style, or without one by the speed between its points, slow wider, and finer at its start, and
|
|
85
|
+
* at its end once it is complete. Its pressure stays the pointer's: a width from the speed is
|
|
86
|
+
* never written into a point.
|
|
38
87
|
*/
|
|
39
88
|
export function outline(stroke, complete) {
|
|
40
|
-
|
|
89
|
+
const { style } = stroke;
|
|
90
|
+
if (style === undefined) {
|
|
91
|
+
return polygon(stroke, followsPressure(stroke) ? PEN_THINNING : EVEN, complete);
|
|
92
|
+
}
|
|
93
|
+
if (style.pen !== "pen")
|
|
94
|
+
return styled(stroke.points.map(() => 1), stroke, style.width, complete);
|
|
95
|
+
const factors = stroke.kind === "pen" ? pressed(stroke.points) : stroke.points.map(speedOf(stroke.points));
|
|
96
|
+
return styled(tapered(factors, stroke.points, style.width, complete), stroke, style.width, complete);
|
|
97
|
+
}
|
|
98
|
+
/** A pen's pressure, eased from point to point, as the width it gives the line, in widths. */
|
|
99
|
+
function pressed(points) {
|
|
100
|
+
return eased(points).map(penWidth);
|
|
101
|
+
}
|
|
102
|
+
/** Each point's pressure leaning on the point's before it, so the line widens and narrows smoothly. */
|
|
103
|
+
function eased(points) {
|
|
104
|
+
let last;
|
|
105
|
+
return points.map(({ p }) => {
|
|
106
|
+
last = last === undefined ? p : (1 - PEN_FOLLOW) * last + PEN_FOLLOW * p;
|
|
107
|
+
return last;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/** The width the speed gives each point of a line without a pen's pressure, in widths. */
|
|
111
|
+
function speedOf(points) {
|
|
112
|
+
let share = SPEED_START;
|
|
113
|
+
return (point, k) => {
|
|
114
|
+
const before = points[k - 1];
|
|
115
|
+
// A point at the time of the one before it has no speed of its own, and changes nothing.
|
|
116
|
+
if (before !== undefined && point.t > before.t) {
|
|
117
|
+
const speed = Math.hypot(point.x - before.x, point.y - before.y) / (point.t - before.t);
|
|
118
|
+
const target = SPEED_SLOWEST - SPEED_RANGE * Math.min(speed / SPEED_FINEST, 1);
|
|
119
|
+
share = (1 - SPEED_FOLLOW) * share + SPEED_FOLLOW * target;
|
|
120
|
+
}
|
|
121
|
+
return penWidth(share);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Widths of a line, finer at its start, and at its end once it is complete. A dot or a dash of two
|
|
126
|
+
* points keeps its width.
|
|
127
|
+
*/
|
|
128
|
+
function tapered(factors, points, width, complete) {
|
|
129
|
+
if (points.length <= 2)
|
|
130
|
+
return factors;
|
|
131
|
+
const along = [];
|
|
132
|
+
let length = 0;
|
|
133
|
+
for (const [k, point] of points.entries()) {
|
|
134
|
+
const before = points[k - 1];
|
|
135
|
+
if (before !== undefined)
|
|
136
|
+
length += Math.hypot(point.x - before.x, point.y - before.y);
|
|
137
|
+
along.push(length);
|
|
138
|
+
}
|
|
139
|
+
const reach = TAPER_LENGTH * width;
|
|
140
|
+
const share = (distance) => Math.max(TAPER_LEAST, Math.min(1, TAPER_LEAST + ((1 - TAPER_LEAST) * distance) / reach));
|
|
141
|
+
return factors.map((factor, k) => {
|
|
142
|
+
const from = along[k] ?? 0;
|
|
143
|
+
const thinned = complete ? Math.min(share(from), share(length - from)) : share(from);
|
|
144
|
+
return factor * thinned;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The outline of a stroke in a style, each point its width in widths of the style's: the library
|
|
149
|
+
* takes the width as each point's pressure and halves it, so its radius is the style's width times
|
|
150
|
+
* that, halved. Its size is the line's without a style, and its easing scales it to the style's
|
|
151
|
+
* width: the size also sets how far the library smooths the outline and how much of the stroke's
|
|
152
|
+
* start it passes over, which a wide marker would otherwise draw as a chord. A liner as wide as
|
|
153
|
+
* the line without a style draws a finger's very line.
|
|
154
|
+
*/
|
|
155
|
+
function styled(factors, stroke, width, complete) {
|
|
156
|
+
const points = stroke.points.map(({ x, y }, k) => [x, y, factors[k] ?? 1]);
|
|
157
|
+
const scale = width / INK_WIDTH;
|
|
158
|
+
return freehand(points, {
|
|
159
|
+
...OPTIONS,
|
|
160
|
+
easing: (factor) => (factor * scale) / 2,
|
|
161
|
+
thinning: PEN_THINNING,
|
|
162
|
+
last: complete,
|
|
163
|
+
});
|
|
41
164
|
}
|
|
42
165
|
function polygon(stroke, thinning, complete) {
|
|
43
|
-
const
|
|
166
|
+
const pressures = eased(stroke.points);
|
|
167
|
+
const points = stroke.points.map(({ x, y }, k) => [x, y, pressures[k] ?? 0]);
|
|
168
|
+
return freehand(points, { ...OPTIONS, thinning, last: complete });
|
|
169
|
+
}
|
|
170
|
+
/** perfect-freehand's outline of points [x, y, pressure], with the few points it draws amiss. */
|
|
171
|
+
function freehand(points, options) {
|
|
44
172
|
// One point alone perfect-freehand pairs with another a unit off and draws as an oval. The same
|
|
45
173
|
// point twice it draws as a round dot, as wide as the line.
|
|
46
|
-
const [only] = points;
|
|
174
|
+
const [only, end] = points;
|
|
47
175
|
if (points.length === 1 && only !== undefined)
|
|
48
176
|
points.push(only);
|
|
49
|
-
|
|
177
|
+
// Two points it takes as the first and four of its own toward the second, of a pressure of 0.5
|
|
178
|
+
// whatever the two had: three are put between them here, at the pressures between.
|
|
179
|
+
if (points.length === 2 && only !== undefined && end !== undefined) {
|
|
180
|
+
const between = [1, 2, 3].map((quarter) => only.map((value, i) => value + ((end[i] ?? value) - value) * (quarter / 4)));
|
|
181
|
+
points.splice(1, 0, ...between);
|
|
182
|
+
}
|
|
183
|
+
return getStroke(points, options);
|
|
50
184
|
}
|
package/lib/core/png.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The PNG of
|
|
3
|
-
* same pixels give the same bytes in every browser
|
|
2
|
+
* The PNG of an image row by row from the top, grey, a byte to a pixel, or RGB, three bytes to a
|
|
3
|
+
* pixel, as `channels` says: own code, so that the same pixels give the same bytes in every browser
|
|
4
|
+
* and in Node, as no browser's canvas promises.
|
|
4
5
|
* The file is the least PNG holds: its signature, the header, one chunk of data and the end,
|
|
5
6
|
* no ancillary chunk, for a decoder may ignore them (PNG Third Edition, 11.3). The data is a zlib
|
|
6
7
|
* stream of one deflate block with the fixed Huffman codes (RFC 1950, RFC 1951): every row
|
|
7
8
|
* unfiltered, and a repeat looked for in two places only — the byte before, which takes the runs of
|
|
8
9
|
* white, and the row above, which takes the rows a stroke runs straight down through.
|
|
9
10
|
*/
|
|
10
|
-
export declare function encodePNG(
|
|
11
|
+
export declare function encodePNG(pixels: Uint8Array, width: number, height: number, channels?: 1 | 3): Uint8Array<ArrayBuffer>;
|
|
11
12
|
export declare function crc32(bytes: Uint8Array): number;
|
|
12
13
|
/** zlib's checksum of the data before compression: two sums modulo 65521 (RFC 1950). */
|
|
13
14
|
export declare function adler32(bytes: Uint8Array): number;
|
package/lib/core/png.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The PNG of
|
|
3
|
-
* same pixels give the same bytes in every browser
|
|
2
|
+
* The PNG of an image row by row from the top, grey, a byte to a pixel, or RGB, three bytes to a
|
|
3
|
+
* pixel, as `channels` says: own code, so that the same pixels give the same bytes in every browser
|
|
4
|
+
* and in Node, as no browser's canvas promises.
|
|
4
5
|
* The file is the least PNG holds: its signature, the header, one chunk of data and the end,
|
|
5
6
|
* no ancillary chunk, for a decoder may ignore them (PNG Third Edition, 11.3). The data is a zlib
|
|
6
7
|
* stream of one deflate block with the fixed Huffman codes (RFC 1950, RFC 1951): every row
|
|
7
8
|
* unfiltered, and a repeat looked for in two places only — the byte before, which takes the runs of
|
|
8
9
|
* white, and the row above, which takes the rows a stroke runs straight down through.
|
|
9
10
|
*/
|
|
10
|
-
export function encodePNG(
|
|
11
|
+
export function encodePNG(pixels, width, height, channels = 1) {
|
|
11
12
|
for (const [name, side] of [
|
|
12
13
|
["width", width],
|
|
13
14
|
["height", height],
|
|
@@ -16,22 +17,26 @@ export function encodePNG(gray, width, height) {
|
|
|
16
17
|
throw new RangeError(`the image's ${name} is ${side}, not a whole number from 1 to ${MOST}`);
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
|
-
if (
|
|
20
|
-
throw new RangeError(
|
|
20
|
+
if (channels !== 1 && channels !== 3) {
|
|
21
|
+
throw new RangeError(`an image of ${channels} channels: grey has 1, RGB 3`);
|
|
22
|
+
}
|
|
23
|
+
const across = width * channels;
|
|
24
|
+
if (pixels.length !== across * height) {
|
|
25
|
+
throw new RangeError(`${pixels.length} bytes are no image ${width} × ${height} of ${channels} channels`);
|
|
21
26
|
}
|
|
22
27
|
// Each row after its filter, 0: none.
|
|
23
|
-
const stride =
|
|
28
|
+
const stride = across + 1;
|
|
24
29
|
const rows = new Uint8Array(stride * height);
|
|
25
30
|
for (let row = 0; row < height; row++) {
|
|
26
|
-
rows.set(
|
|
31
|
+
rows.set(pixels.subarray(row * across, (row + 1) * across), row * stride + 1);
|
|
27
32
|
}
|
|
28
33
|
const header = new Uint8Array(13);
|
|
29
34
|
const view = new DataView(header.buffer);
|
|
30
35
|
view.setUint32(0, width);
|
|
31
36
|
view.setUint32(4, height);
|
|
32
|
-
// A depth of 8 bits, the colour type of grey, and the one method of compression,
|
|
33
|
-
// and of no interlacing there is.
|
|
34
|
-
header.set([8, 0, 0, 0, 0], 8);
|
|
37
|
+
// A depth of 8 bits, the colour type of grey, 0, or of RGB, 2, and the one method of compression,
|
|
38
|
+
// of filtering and of no interlacing there is.
|
|
39
|
+
header.set([8, channels === 1 ? 0 : 2, 0, 0, 0], 8);
|
|
35
40
|
const stream = deflate(rows, stride);
|
|
36
41
|
const zlib = new Uint8Array(2 + stream.length + 4);
|
|
37
42
|
// The deflate method with a window of 32 KiB, and the fastest algorithm; FCHECK makes 0x7801 a
|
package/lib/core/raster.d.ts
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Crop } from "./crop.js";
|
|
2
2
|
import type { Stroke } from "./ink.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* a
|
|
10
|
-
*
|
|
4
|
+
* How many bytes a pixel of a crop of these strokes takes: 1, grey, when none of them has a
|
|
5
|
+
* style, and 3, RGB, when one has.
|
|
6
|
+
*/
|
|
7
|
+
export declare function channelsOf(strokes: readonly Stroke[]): 1 | 3;
|
|
8
|
+
/**
|
|
9
|
+
* The pixels of a crop, row by row from the top, of the strokes the crop was made of: grey, a
|
|
10
|
+
* byte each, white 255 and the ink 0, when no stroke has a style, and RGB, three bytes each, when
|
|
11
|
+
* one has (`channelsOf`). Every segment of a stroke is a line as wide all along, with round ends,
|
|
12
|
+
* and a stroke of one point a dot as wide as the line: a pixel takes the share of it that the line
|
|
13
|
+
* covers, along a ramp a pixel wide at the line's edge. In grey a pixel is the darkest of what
|
|
14
|
+
* covers it. In RGB the strokes are laid on white by their IDs ascending, each as one layer — what
|
|
15
|
+
* its segments cover of a pixel is the most any of them does, so where it crosses itself it is no
|
|
16
|
+
* darker —, its line its width at the crop's scale and 1 px at least: a stroke without a style
|
|
17
|
+
* black and opaque; of the kind pen or liner its colour at its opacity over what lies under it;
|
|
18
|
+
* of the kind marker what lies under it multiplied by its colour, at its opacity. The places are
|
|
19
|
+
* the crop's own whole numbers, from the points in tenths of a unit, as a sample keeps them. Only
|
|
20
|
+
* adding, subtracting, multiplying, dividing, Math.sqrt and Math.round go into a pixel, which
|
|
21
|
+
* ECMAScript rounds alike in every engine, so the bytes are the same in each.
|
|
11
22
|
*/
|
|
12
23
|
export declare function rasterOf(strokes: readonly Stroke[], crop: Crop): Uint8Array<ArrayBuffer>;
|
|
13
|
-
/** The PNG of a crop: its pixels, encoded. */
|
|
24
|
+
/** The PNG of a crop: its pixels, encoded, grey or RGB as they are. */
|
|
14
25
|
export declare function pictureOf(strokes: readonly Stroke[], crop: Crop): Uint8Array<ArrayBuffer>;
|
package/lib/core/raster.js
CHANGED
|
@@ -1,47 +1,137 @@
|
|
|
1
|
+
import { inkOf, widthOf } from "./crop.js";
|
|
1
2
|
import { tenths } from "./geometry.js";
|
|
2
3
|
import { encodePNG } from "./png.js";
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
* How many bytes a pixel of a crop of these strokes takes: 1, grey, when none of them has a
|
|
6
|
+
* style, and 3, RGB, when one has.
|
|
7
|
+
*/
|
|
8
|
+
export function channelsOf(strokes) {
|
|
9
|
+
return strokes.some((stroke) => stroke.style !== undefined) ? 3 : 1;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The pixels of a crop, row by row from the top, of the strokes the crop was made of: grey, a
|
|
13
|
+
* byte each, white 255 and the ink 0, when no stroke has a style, and RGB, three bytes each, when
|
|
14
|
+
* one has (`channelsOf`). Every segment of a stroke is a line as wide all along, with round ends,
|
|
15
|
+
* and a stroke of one point a dot as wide as the line: a pixel takes the share of it that the line
|
|
16
|
+
* covers, along a ramp a pixel wide at the line's edge. In grey a pixel is the darkest of what
|
|
17
|
+
* covers it. In RGB the strokes are laid on white by their IDs ascending, each as one layer — what
|
|
18
|
+
* its segments cover of a pixel is the most any of them does, so where it crosses itself it is no
|
|
19
|
+
* darker —, its line its width at the crop's scale and 1 px at least: a stroke without a style
|
|
20
|
+
* black and opaque; of the kind pen or liner its colour at its opacity over what lies under it;
|
|
21
|
+
* of the kind marker what lies under it multiplied by its colour, at its opacity. The places are
|
|
22
|
+
* the crop's own whole numbers, from the points in tenths of a unit, as a sample keeps them. Only
|
|
23
|
+
* adding, subtracting, multiplying, dividing, Math.sqrt and Math.round go into a pixel, which
|
|
24
|
+
* ECMAScript rounds alike in every engine, so the bytes are the same in each.
|
|
11
25
|
*/
|
|
12
26
|
export function rasterOf(strokes, crop) {
|
|
13
|
-
|
|
27
|
+
return channelsOf(strokes) === 1 ? grayOf(strokes, crop) : colourOf(strokes, crop);
|
|
28
|
+
}
|
|
29
|
+
/** The PNG of a crop: its pixels, encoded, grey or RGB as they are. */
|
|
30
|
+
export function pictureOf(strokes, crop) {
|
|
31
|
+
return encodePNG(rasterOf(strokes, crop), crop.width, crop.height, channelsOf(strokes));
|
|
32
|
+
}
|
|
33
|
+
/** A point's place on the crop: a whole number over the median, one division, as the crop's own. */
|
|
34
|
+
function spotsOf(stroke, crop) {
|
|
14
35
|
const [top, left] = crop.margins;
|
|
15
36
|
const { box, median: unit } = crop.exact;
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
// A whole number over the median: one division, as the crop's own numbers are.
|
|
19
|
-
const spot = ({ x, y }) => ({
|
|
37
|
+
const { drawn } = crop;
|
|
38
|
+
return stroke.points.map(({ x, y }) => ({
|
|
20
39
|
x: ((tenths(x) - box.minX) * drawn + left * unit) / unit,
|
|
21
40
|
y: ((tenths(y) - box.minY) * drawn + top * unit) / unit,
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
/** Each segment of a stroke on the crop, by its ends: a stroke of one point is one of no length. */
|
|
44
|
+
function eachSegment(spots, each) {
|
|
45
|
+
const [first] = spots;
|
|
46
|
+
if (spots.length === 1 && first !== undefined)
|
|
47
|
+
each(first, first);
|
|
48
|
+
for (let k = 1; k < spots.length; k++) {
|
|
49
|
+
const [a, b] = [spots[k - 1], spots[k]];
|
|
50
|
+
if (a !== undefined && b !== undefined)
|
|
51
|
+
each(a, b);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function grayOf(strokes, crop) {
|
|
55
|
+
const { width, height, drawn } = crop;
|
|
56
|
+
const gray = new Uint8Array(width * height).fill(255);
|
|
57
|
+
const reach = Math.max(drawn, 12) / 24;
|
|
58
|
+
const darken = (at, cover) => {
|
|
59
|
+
const value = Math.round(255 * (1 - Math.min(1, cover)));
|
|
60
|
+
if (value < (gray[at] ?? 255))
|
|
61
|
+
gray[at] = value;
|
|
62
|
+
};
|
|
63
|
+
for (const stroke of strokes) {
|
|
64
|
+
eachSegment(spotsOf(stroke, crop), (a, b) => paint(width, height, a, b, reach, darken));
|
|
33
65
|
}
|
|
34
66
|
return gray;
|
|
35
67
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
68
|
+
function colourOf(strokes, crop) {
|
|
69
|
+
const { width, height, drawn } = crop;
|
|
70
|
+
const unit = crop.exact.median;
|
|
71
|
+
const rgb = new Uint8Array(3 * width * height).fill(255);
|
|
72
|
+
// What the stroke being laid covers of each pixel, cleared after it within its own box.
|
|
73
|
+
const covered = new Float64Array(width * height);
|
|
74
|
+
const cover = (at, share) => {
|
|
75
|
+
const own = Math.min(1, share);
|
|
76
|
+
if (own > (covered[at] ?? 0))
|
|
77
|
+
covered[at] = own;
|
|
78
|
+
};
|
|
79
|
+
for (const stroke of [...strokes].sort((a, b) => a.id - b.id)) {
|
|
80
|
+
const reach = inkOf(widthOf(stroke), drawn, unit) / (24 * unit);
|
|
81
|
+
const spots = spotsOf(stroke, crop);
|
|
82
|
+
eachSegment(spots, (a, b) => paint(width, height, a, b, reach, cover));
|
|
83
|
+
const { style } = stroke;
|
|
84
|
+
const opacity = style === undefined ? 1 : style.opacity / 100;
|
|
85
|
+
const [red, green, blue] = style === undefined ? [0, 0, 0] : coloursOf(style.color);
|
|
86
|
+
const multiply = style?.pen === "marker";
|
|
87
|
+
const lay = (under, colour, share) => Math.round(under * (1 - share) + (multiply ? (under * colour) / 255 : colour) * share);
|
|
88
|
+
const box = boxOf(spots, reach, width, height);
|
|
89
|
+
for (let row = box.top; row <= box.bottom; row++) {
|
|
90
|
+
for (let column = box.left; column <= box.right; column++) {
|
|
91
|
+
const at = row * width + column;
|
|
92
|
+
const share = covered[at] ?? 0;
|
|
93
|
+
if (share === 0)
|
|
94
|
+
continue;
|
|
95
|
+
covered[at] = 0;
|
|
96
|
+
const a = opacity * share;
|
|
97
|
+
rgb[3 * at] = lay(rgb[3 * at] ?? 255, red, a);
|
|
98
|
+
rgb[3 * at + 1] = lay(rgb[3 * at + 1] ?? 255, green, a);
|
|
99
|
+
rgb[3 * at + 2] = lay(rgb[3 * at + 2] ?? 255, blue, a);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return rgb;
|
|
104
|
+
}
|
|
105
|
+
/** A colour `#rrggbb` as its three channels. */
|
|
106
|
+
function coloursOf(colour) {
|
|
107
|
+
const channel = (at) => Number.parseInt(colour.slice(at, at + 2), 16);
|
|
108
|
+
return [channel(1), channel(3), channel(5)];
|
|
109
|
+
}
|
|
110
|
+
/** The pixels a stroke's line may shade, within the crop: as far as `paint` looks. */
|
|
111
|
+
function boxOf(spots, reach, width, height) {
|
|
112
|
+
let left = Number.POSITIVE_INFINITY;
|
|
113
|
+
let top = Number.POSITIVE_INFINITY;
|
|
114
|
+
let right = Number.NEGATIVE_INFINITY;
|
|
115
|
+
let bottom = Number.NEGATIVE_INFINITY;
|
|
116
|
+
for (const { x, y } of spots) {
|
|
117
|
+
left = Math.min(left, x);
|
|
118
|
+
top = Math.min(top, y);
|
|
119
|
+
right = Math.max(right, x);
|
|
120
|
+
bottom = Math.max(bottom, y);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
left: Math.max(0, Math.floor(left - reach - 1)),
|
|
124
|
+
top: Math.max(0, Math.floor(top - reach - 1)),
|
|
125
|
+
right: Math.min(width - 1, Math.ceil(right + reach + 1)),
|
|
126
|
+
bottom: Math.min(height - 1, Math.ceil(bottom + reach + 1)),
|
|
127
|
+
};
|
|
39
128
|
}
|
|
40
129
|
/**
|
|
41
|
-
* One segment's ink on the pixels near it
|
|
42
|
-
*
|
|
130
|
+
* One segment's ink on the pixels near it: `take` gets each pixel it covers, by its place, with
|
|
131
|
+
* how much of it the line covers, above 0 and 1 or more within the line. Its ends go in one order
|
|
132
|
+
* whichever way it was written, so a stroke drawn back to front gives the same bytes.
|
|
43
133
|
*/
|
|
44
|
-
function paint(
|
|
134
|
+
function paint(width, height, from, to, reach, take) {
|
|
45
135
|
const forward = from.x < to.x || (from.x === to.x && from.y <= to.y);
|
|
46
136
|
const a = forward ? from : to;
|
|
47
137
|
const b = forward ? to : from;
|
|
@@ -61,12 +151,8 @@ function paint(gray, width, height, from, to, reach) {
|
|
|
61
151
|
const ex = cx - (a.x + along * dx);
|
|
62
152
|
const ey = cy - (a.y + along * dy);
|
|
63
153
|
const cover = reach + 0.5 - Math.sqrt(ex * ex + ey * ey);
|
|
64
|
-
if (cover
|
|
65
|
-
|
|
66
|
-
const value = Math.round(255 * (1 - Math.min(1, cover)));
|
|
67
|
-
const at = row * width + column;
|
|
68
|
-
if (value < (gray[at] ?? 255))
|
|
69
|
-
gray[at] = value;
|
|
154
|
+
if (cover > 0)
|
|
155
|
+
take(row * width + column, cover);
|
|
70
156
|
}
|
|
71
157
|
}
|
|
72
158
|
}
|
package/lib/core/recorder.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Clock } from "./clock.js";
|
|
2
2
|
import type { Stroke } from "./ink.js";
|
|
3
|
+
import type { StrokeStyle } from "./style.js";
|
|
3
4
|
/**
|
|
4
5
|
* What the recorder takes of a pointer event, and of each event coalesced into it. The DOM-free
|
|
5
6
|
* part sees no DOM, so the element hands it this much, the place already in sheet units.
|
|
@@ -45,8 +46,11 @@ export declare class Recorder {
|
|
|
45
46
|
get live(): Stroke | undefined;
|
|
46
47
|
/** The `pointerId` of the pointer writing, or nothing between strokes. */
|
|
47
48
|
get pointer(): number | undefined;
|
|
48
|
-
/**
|
|
49
|
-
|
|
49
|
+
/**
|
|
50
|
+
* `pointerdown`: a stroke begins here, if none is under way and the primary button went down,
|
|
51
|
+
* in the style given, which it keeps; without one it has none.
|
|
52
|
+
*/
|
|
53
|
+
down(sample: PointerSample, style?: StrokeStyle): boolean;
|
|
50
54
|
/**
|
|
51
55
|
* `pointermove`, once for each coalesced event: the stroke goes on here. A move without the
|
|
52
56
|
* primary button held ends the stroke where it was, its `pointerup` lost.
|
|
@@ -70,4 +74,11 @@ export declare class Recorder {
|
|
|
70
74
|
* `pointercancel` — its ID and its times are the last sheet's — and the next stroke is `nextId`.
|
|
71
75
|
*/
|
|
72
76
|
restart(nextId: number): void;
|
|
77
|
+
/**
|
|
78
|
+
* `count` IDs in a row for strokes the recorder does not write — the pieces of a cut, numbered
|
|
79
|
+
* when the eraser lifts —, the first of them given: the next stroke takes the ID after them, so
|
|
80
|
+
* the sheet's IDs have one source and none is used twice. A stroke under way keeps the ID it
|
|
81
|
+
* took when it began.
|
|
82
|
+
*/
|
|
83
|
+
reserve(count: number): number;
|
|
73
84
|
}
|
package/lib/core/recorder.js
CHANGED
|
@@ -33,18 +33,22 @@ export class Recorder {
|
|
|
33
33
|
get pointer() {
|
|
34
34
|
return this.#live?.pointerId;
|
|
35
35
|
}
|
|
36
|
-
/**
|
|
37
|
-
|
|
36
|
+
/**
|
|
37
|
+
* `pointerdown`: a stroke begins here, if none is under way and the primary button went down,
|
|
38
|
+
* in the style given, which it keeps; without one it has none.
|
|
39
|
+
*/
|
|
40
|
+
down(sample, style) {
|
|
38
41
|
if (this.#live !== undefined || sample.button !== PRIMARY)
|
|
39
42
|
return false;
|
|
40
43
|
const point = this.#point(sample, sample.pressure);
|
|
41
|
-
|
|
44
|
+
const live = {
|
|
42
45
|
id: this.#nextId++,
|
|
43
46
|
kind: sample.kind,
|
|
44
47
|
points: [point],
|
|
45
48
|
pointerId: sample.pointerId,
|
|
46
49
|
latest: point,
|
|
47
50
|
};
|
|
51
|
+
this.#live = style === undefined ? live : { ...live, style };
|
|
48
52
|
return true;
|
|
49
53
|
}
|
|
50
54
|
/**
|
|
@@ -104,6 +108,17 @@ export class Recorder {
|
|
|
104
108
|
this.#live = undefined;
|
|
105
109
|
this.#nextId = nextId;
|
|
106
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* `count` IDs in a row for strokes the recorder does not write — the pieces of a cut, numbered
|
|
113
|
+
* when the eraser lifts —, the first of them given: the next stroke takes the ID after them, so
|
|
114
|
+
* the sheet's IDs have one source and none is used twice. A stroke under way keeps the ID it
|
|
115
|
+
* took when it began.
|
|
116
|
+
*/
|
|
117
|
+
reserve(count) {
|
|
118
|
+
const first = this.#nextId;
|
|
119
|
+
this.#nextId += count;
|
|
120
|
+
return first;
|
|
121
|
+
}
|
|
107
122
|
#finish() {
|
|
108
123
|
const live = this.#live;
|
|
109
124
|
if (live === undefined)
|
|
@@ -112,7 +127,8 @@ export class Recorder {
|
|
|
112
127
|
live.points.push(live.latest);
|
|
113
128
|
// The stroke is no longer under way by the time the handler has it.
|
|
114
129
|
this.#live = undefined;
|
|
115
|
-
|
|
130
|
+
const { id, kind, style, points } = live;
|
|
131
|
+
this.#finished(style === undefined ? { id, kind, points } : { id, kind, style, points });
|
|
116
132
|
return true;
|
|
117
133
|
}
|
|
118
134
|
/**
|
package/lib/core/sheet.d.ts
CHANGED
|
@@ -40,12 +40,13 @@ export declare const PICTURE_BYTES: number;
|
|
|
40
40
|
/**
|
|
41
41
|
* The picture of a work: the page, and the ink past it, right and down, with no room past the
|
|
42
42
|
* ink, at a pixel a unit while its rows hold no more than `PICTURE_BYTES`, and scaled down to hold
|
|
43
|
-
* them otherwise. `
|
|
43
|
+
* them otherwise. `bytes` is what a pixel takes, 1 in grey and 3 in RGB, and each row takes one
|
|
44
|
+
* byte more, its filter's. `scale` is pixels a unit.
|
|
44
45
|
*/
|
|
45
46
|
export declare function pictureOf(ink: {
|
|
46
47
|
readonly maxX: number;
|
|
47
48
|
readonly maxY: number;
|
|
48
|
-
} | undefined): {
|
|
49
|
+
} | undefined, bytes?: 1 | 3): {
|
|
49
50
|
readonly width: number;
|
|
50
51
|
readonly height: number;
|
|
51
52
|
readonly scale: number;
|
package/lib/core/sheet.js
CHANGED
|
@@ -33,12 +33,14 @@ export const PICTURE_BYTES = (SHEET.width + 1) * SHEET.height;
|
|
|
33
33
|
/**
|
|
34
34
|
* The picture of a work: the page, and the ink past it, right and down, with no room past the
|
|
35
35
|
* ink, at a pixel a unit while its rows hold no more than `PICTURE_BYTES`, and scaled down to hold
|
|
36
|
-
* them otherwise. `
|
|
36
|
+
* them otherwise. `bytes` is what a pixel takes, 1 in grey and 3 in RGB, and each row takes one
|
|
37
|
+
* byte more, its filter's. `scale` is pixels a unit.
|
|
37
38
|
*/
|
|
38
|
-
export function pictureOf(ink) {
|
|
39
|
+
export function pictureOf(ink, bytes = 1) {
|
|
39
40
|
const { width, height } = extentOf(ink, { width: 0, height: 0 });
|
|
40
|
-
// The scale s whose rows, (width s + 1) × height s, are PICTURE_BYTES: the root of a
|
|
41
|
-
|
|
41
|
+
// The scale s whose rows, (bytes × width s + 1) × height s, are PICTURE_BYTES: the root of a
|
|
42
|
+
// quadratic.
|
|
43
|
+
const area = bytes * width * height;
|
|
42
44
|
const fit = (Math.sqrt(height * height + 4 * area * PICTURE_BYTES) - height) / (2 * area);
|
|
43
45
|
const scale = Math.min(1, fit);
|
|
44
46
|
return {
|