@altnovatrail/canvas 0.3.37 → 0.3.39
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/geometry.js +2 -2
- package/lib/core/grid.d.ts +11 -4
- package/lib/core/grid.js +33 -8
- package/lib/core/hands.d.ts +14 -7
- package/lib/core/hands.js +58 -7
- package/lib/core/history.js +3 -3
- package/lib/core/marks.d.ts +2 -0
- package/lib/core/marks.js +11 -6
- package/lib/core/notation.d.ts +29 -0
- package/lib/core/notation.js +180 -0
- package/lib/core/screen.d.ts +10 -11
- package/lib/core/screen.js +13 -14
- package/lib/core/sheet.d.ts +46 -3
- package/lib/core/sheet.js +46 -3
- package/lib/core/submission.js +5 -1
- package/lib/core/task.d.ts +9 -4
- package/lib/core/task.js +7 -2
- package/lib/core/tracing.d.ts +49 -13
- package/lib/core/tracing.js +110 -31
- package/lib/core/zoom.d.ts +19 -8
- package/lib/core/zoom.js +36 -15
- package/lib/element/icons.d.ts +90 -5
- package/lib/element/icons.js +94 -5
- package/lib/element/index.d.ts +32 -13
- package/lib/element/index.js +712 -240
- package/lib/element/labels.d.ts +30 -5
- package/lib/element/labels.js +32 -5
- package/lib/element/notation.d.ts +2 -0
- package/lib/element/notation.js +94 -0
- package/lib/element/session.js +4 -4
- package/lib/element/tools.d.ts +31 -16
- package/lib/element/tools.js +87 -54
- package/lib/element/tracing.d.ts +49 -5
- package/lib/element/tracing.js +446 -10
- package/package.json +3 -3
package/lib/core/sheet.d.ts
CHANGED
|
@@ -1,9 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The handwriting sheet in logical units: 1000 wide and 1400 tall,
|
|
3
|
-
* corner
|
|
4
|
-
* these units, whatever the device's screen
|
|
2
|
+
* The task's page on the handwriting sheet in logical units: 1000 wide and 1400 tall, at the
|
|
3
|
+
* sheet's top left corner, the origin, with y pointing down, one scale on both axes (R06). Every
|
|
4
|
+
* layer of the sheet counts in these units, whatever the device's screen; the task's zones, the
|
|
5
|
+
* workbook's lines and the areas of a worked example lie on the page.
|
|
5
6
|
*/
|
|
6
7
|
export declare const SHEET: Readonly<{
|
|
7
8
|
readonly width: 1000;
|
|
8
9
|
readonly height: 1400;
|
|
9
10
|
}>;
|
|
11
|
+
/**
|
|
12
|
+
* How far the sheet goes from its origin, in units: ten pages across and ten down. The learner
|
|
13
|
+
* writes past the page, to the right of it and below it, and a point beyond this is no point of a
|
|
14
|
+
* history: the bound keeps the rules' whole numbers small and a history's numbers sane (R06).
|
|
15
|
+
*/
|
|
16
|
+
export declare const REACH: Readonly<{
|
|
17
|
+
readonly width: 10000;
|
|
18
|
+
readonly height: 14000;
|
|
19
|
+
}>;
|
|
20
|
+
/** A size on the sheet, in units. */
|
|
21
|
+
export interface Size {
|
|
22
|
+
readonly width: number;
|
|
23
|
+
readonly height: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The sheet as large as it is laid out (R06): the page, and past it and past the furthest ink a
|
|
27
|
+
* `room` more — the part of the sheet in sight, so that there is always somewhere to move it to and
|
|
28
|
+
* write —, within `REACH`. `ink` is the right and the bottom of the furthest strokes, none for an
|
|
29
|
+
* empty sheet.
|
|
30
|
+
*/
|
|
31
|
+
export declare function extentOf(ink: {
|
|
32
|
+
readonly maxX: number;
|
|
33
|
+
readonly maxY: number;
|
|
34
|
+
} | undefined, room: Size): Size;
|
|
35
|
+
/**
|
|
36
|
+
* The bytes of a work's picture at most, before its encoding: the page's rows at a pixel a unit,
|
|
37
|
+
* each with its filter byte, which keep its PNG within the page's bound of bytes (R83).
|
|
38
|
+
*/
|
|
39
|
+
export declare const PICTURE_BYTES: number;
|
|
40
|
+
/**
|
|
41
|
+
* The picture of a work (R83): the page, and the ink past it, right and down, with no room past the
|
|
42
|
+
* ink, at a pixel a unit while its rows hold no more than `PICTURE_BYTES`, and scaled down to hold
|
|
43
|
+
* them otherwise. `scale` is pixels a unit.
|
|
44
|
+
*/
|
|
45
|
+
export declare function pictureOf(ink: {
|
|
46
|
+
readonly maxX: number;
|
|
47
|
+
readonly maxY: number;
|
|
48
|
+
} | undefined): {
|
|
49
|
+
readonly width: number;
|
|
50
|
+
readonly height: number;
|
|
51
|
+
readonly scale: number;
|
|
52
|
+
};
|
package/lib/core/sheet.js
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The handwriting sheet in logical units: 1000 wide and 1400 tall,
|
|
3
|
-
* corner
|
|
4
|
-
* these units, whatever the device's screen
|
|
2
|
+
* The task's page on the handwriting sheet in logical units: 1000 wide and 1400 tall, at the
|
|
3
|
+
* sheet's top left corner, the origin, with y pointing down, one scale on both axes (R06). Every
|
|
4
|
+
* layer of the sheet counts in these units, whatever the device's screen; the task's zones, the
|
|
5
|
+
* workbook's lines and the areas of a worked example lie on the page.
|
|
5
6
|
*/
|
|
6
7
|
export const SHEET = Object.freeze({ width: 1000, height: 1400 });
|
|
8
|
+
/**
|
|
9
|
+
* How far the sheet goes from its origin, in units: ten pages across and ten down. The learner
|
|
10
|
+
* writes past the page, to the right of it and below it, and a point beyond this is no point of a
|
|
11
|
+
* history: the bound keeps the rules' whole numbers small and a history's numbers sane (R06).
|
|
12
|
+
*/
|
|
13
|
+
export const REACH = Object.freeze({ width: 10000, height: 14000 });
|
|
14
|
+
/**
|
|
15
|
+
* The sheet as large as it is laid out (R06): the page, and past it and past the furthest ink a
|
|
16
|
+
* `room` more — the part of the sheet in sight, so that there is always somewhere to move it to and
|
|
17
|
+
* write —, within `REACH`. `ink` is the right and the bottom of the furthest strokes, none for an
|
|
18
|
+
* empty sheet.
|
|
19
|
+
*/
|
|
20
|
+
export function extentOf(ink, room) {
|
|
21
|
+
const right = Math.max(SHEET.width, ink?.maxX ?? 0);
|
|
22
|
+
const bottom = Math.max(SHEET.height, ink?.maxY ?? 0);
|
|
23
|
+
return {
|
|
24
|
+
width: Math.min(REACH.width, Math.ceil(right + room.width)),
|
|
25
|
+
height: Math.min(REACH.height, Math.ceil(bottom + room.height)),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The bytes of a work's picture at most, before its encoding: the page's rows at a pixel a unit,
|
|
30
|
+
* each with its filter byte, which keep its PNG within the page's bound of bytes (R83).
|
|
31
|
+
*/
|
|
32
|
+
export const PICTURE_BYTES = (SHEET.width + 1) * SHEET.height;
|
|
33
|
+
/**
|
|
34
|
+
* The picture of a work (R83): the page, and the ink past it, right and down, with no room past the
|
|
35
|
+
* ink, at a pixel a unit while its rows hold no more than `PICTURE_BYTES`, and scaled down to hold
|
|
36
|
+
* them otherwise. `scale` is pixels a unit.
|
|
37
|
+
*/
|
|
38
|
+
export function pictureOf(ink) {
|
|
39
|
+
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 quadratic.
|
|
41
|
+
const area = width * height;
|
|
42
|
+
const fit = (Math.sqrt(height * height + 4 * area * PICTURE_BYTES) - height) / (2 * area);
|
|
43
|
+
const scale = Math.min(1, fit);
|
|
44
|
+
return {
|
|
45
|
+
width: Math.max(1, Math.floor(width * scale)),
|
|
46
|
+
height: Math.max(1, Math.floor(height * scale)),
|
|
47
|
+
scale,
|
|
48
|
+
};
|
|
49
|
+
}
|
package/lib/core/submission.js
CHANGED
|
@@ -74,7 +74,11 @@ export function paperWorkForCheck(strokes, paper, tracing, rect, picture, digit)
|
|
|
74
74
|
const submission = {
|
|
75
75
|
crop: numbers,
|
|
76
76
|
groups,
|
|
77
|
-
tracing: {
|
|
77
|
+
tracing: {
|
|
78
|
+
at: [tracing.at[0], tracing.at[1]],
|
|
79
|
+
hidden: tracing.hidden,
|
|
80
|
+
scale: tracing.scale ?? 1,
|
|
81
|
+
},
|
|
78
82
|
digest: digestOf(strokes, numbers, groups, paper),
|
|
79
83
|
};
|
|
80
84
|
return Object.freeze({
|
package/lib/core/task.d.ts
CHANGED
|
@@ -6,13 +6,18 @@ import { type Page } from "./page.js";
|
|
|
6
6
|
export declare const SHEET_KIND = "handwriting_sheet";
|
|
7
7
|
export declare const WORKBOOK_KIND = "workbook";
|
|
8
8
|
/**
|
|
9
|
-
* What a press of the primary button does on the sheet: the pen writes, the eraser erases (R22)
|
|
10
|
-
* pen's eraser erases whatever the tool, if the task allows the
|
|
11
|
-
* spec).
|
|
9
|
+
* What a press of the primary button does on the sheet: the pen writes, the eraser erases (R22),
|
|
10
|
+
* the hand moves the sheet (R84). A pen's eraser erases whatever the tool, if the task allows the
|
|
11
|
+
* eraser (R28). spec's type (R74 spec).
|
|
12
12
|
*/
|
|
13
13
|
export type { Tool };
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* The tools of the module format a task allows, in the order their buttons stand: the pen, the
|
|
16
|
+
* eraser. The hand is no tool of the format: every sheet the learner writes on has it.
|
|
17
|
+
*/
|
|
15
18
|
export declare const TOOLS: readonly Tool[];
|
|
19
|
+
/** The tool that moves the sheet and writes nothing (R84). */
|
|
20
|
+
export declare const HAND: Tool;
|
|
16
21
|
export declare function isTool(value: unknown): value is Tool;
|
|
17
22
|
/** A paragraph of a task's statement, or a piece of text of a workbook's line. */
|
|
18
23
|
export interface TextPart {
|
package/lib/core/task.js
CHANGED
|
@@ -10,10 +10,15 @@ import { pageOf, samePage } from "./page.js";
|
|
|
10
10
|
/** The canvas kinds of the module format: the handwriting sheet and the workbook. */
|
|
11
11
|
export const SHEET_KIND = "handwriting_sheet";
|
|
12
12
|
export const WORKBOOK_KIND = "workbook";
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* The tools of the module format a task allows, in the order their buttons stand: the pen, the
|
|
15
|
+
* eraser. The hand is no tool of the format: every sheet the learner writes on has it.
|
|
16
|
+
*/
|
|
14
17
|
export const TOOLS = Object.freeze(["pen", "eraser"]);
|
|
18
|
+
/** The tool that moves the sheet and writes nothing (R84). */
|
|
19
|
+
export const HAND = "hand";
|
|
15
20
|
export function isTool(value) {
|
|
16
|
-
return TOOLS.includes(value);
|
|
21
|
+
return value === HAND || TOOLS.includes(value);
|
|
17
22
|
}
|
|
18
23
|
/** The template of a sheet without a task: squares of 50 units, no statement, both tools. */
|
|
19
24
|
export const NO_TASK = Object.freeze({
|
package/lib/core/tracing.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { TracingState } from "@altnovatrail/spec/canvas";
|
|
1
|
+
import type { PaperGrid, TracingReading, TracingState } from "@altnovatrail/spec/canvas";
|
|
2
2
|
import type { Box } from "./geometry.js";
|
|
3
|
+
import type { Grid } from "./grid.js";
|
|
3
4
|
import type { Stroke } from "./ink.js";
|
|
4
5
|
import type { Point } from "./screen.js";
|
|
5
6
|
/**
|
|
@@ -21,6 +22,34 @@ export declare const TRACING: Readonly<{
|
|
|
21
22
|
leap: 100;
|
|
22
23
|
dots: 25;
|
|
23
24
|
}>;
|
|
25
|
+
/**
|
|
26
|
+
* The tracing paper's scale (R89): from `min` to `max` times its own size, as the mockup's
|
|
27
|
+
* `Zadacha-masshtab` has it; a button's press multiplies it by `button`, a notch of the wheel by
|
|
28
|
+
* `wheel`, and a scale is kept in hundredths, as the percentage shows it.
|
|
29
|
+
*/
|
|
30
|
+
export declare const SCALE: Readonly<{
|
|
31
|
+
min: 0.4;
|
|
32
|
+
max: 4;
|
|
33
|
+
button: 1.2;
|
|
34
|
+
wheel: 1.08;
|
|
35
|
+
}>;
|
|
36
|
+
/** A scale kept to the rule's bounds, in hundredths (R89). */
|
|
37
|
+
export declare function scaleOf(scale: number): number;
|
|
38
|
+
/** The scale of a tracing state: 1 where it has none (R89). */
|
|
39
|
+
export declare function scaleIn(state: TracingState): number;
|
|
40
|
+
/** A size as large as `scale` makes it. */
|
|
41
|
+
export declare function scaled(size: Size, scale: number): Size;
|
|
42
|
+
/**
|
|
43
|
+
* The grids of a task from paper's sheet in the order its button turns them, round, the first as
|
|
44
|
+
* the task comes (R88).
|
|
45
|
+
*/
|
|
46
|
+
export declare const PAPER_GRIDS: readonly PaperGrid[];
|
|
47
|
+
/** Whether a value is a grid of a task from paper's sheet. */
|
|
48
|
+
export declare function isPaperGrid(value: unknown): value is PaperGrid;
|
|
49
|
+
/** The rulings of a task from paper's grid, or none for a blank sheet (R88). */
|
|
50
|
+
export declare function rulingsOf(grid: PaperGrid): Grid | undefined;
|
|
51
|
+
/** The grid the button turns a task from paper's grid to: the next, round (R88). */
|
|
52
|
+
export declare function nextGrid(grid: PaperGrid): PaperGrid;
|
|
24
53
|
/** The tracing paper's size on the sheet, in units. */
|
|
25
54
|
export interface Size {
|
|
26
55
|
readonly width: number;
|
|
@@ -37,19 +66,26 @@ export declare function sizeOf(width: number, height: number): Size;
|
|
|
37
66
|
export declare const TRACING_START: TracingState;
|
|
38
67
|
/**
|
|
39
68
|
* A tracing state a consumer gives (R74 spec), checked and frozen: an object of `at`, [x, y] in
|
|
40
|
-
* units, finite numbers,
|
|
41
|
-
* puts the tracing paper of `size` wholly off the sheet
|
|
42
|
-
* not such a state.
|
|
69
|
+
* units, finite numbers, `hidden`, true or false, and `scale`, if any, from 0.4 to 4; other fields
|
|
70
|
+
* are passed over. A state that puts the tracing paper of `size` at its scale wholly off the sheet
|
|
71
|
+
* is a TypeError, as is anything else that is not such a state.
|
|
43
72
|
*/
|
|
44
73
|
export declare function tracingStateOf(value: unknown, size: Size): TracingState;
|
|
45
74
|
/**
|
|
46
75
|
* The fields of a tracing state at `path`, checked and frozen: `at`, [x, y] in units, finite
|
|
47
|
-
* numbers,
|
|
48
|
-
* TypeError. Where it lies on the sheet is the caller's
|
|
76
|
+
* numbers, `hidden`, true or false, and `scale`, from 0.4 to 4, 1 where it is absent; other
|
|
77
|
+
* fields are passed over. Anything else is a TypeError. Where it lies on the sheet is the caller's
|
|
78
|
+
* to check.
|
|
49
79
|
*/
|
|
50
80
|
export declare function tracingFieldsOf(value: unknown, path: string): TracingState;
|
|
51
81
|
/** Whether the tracing paper of `size` at `at` lies wholly off the sheet, touching it at most. */
|
|
52
82
|
export declare function whollyOff(at: readonly [number, number], size: Size): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* A reading of the statement a consumer gives (R89), checked and frozen: `state` "ready" or
|
|
85
|
+
* "busy", or "done" with `statements`, from none to 20 strings; other fields are passed over.
|
|
86
|
+
* Anything else is a TypeError.
|
|
87
|
+
*/
|
|
88
|
+
export declare function readingOf(value: unknown): TracingReading;
|
|
53
89
|
/** Whether two tracing states say the same. */
|
|
54
90
|
export declare function sameTracing(a: TracingState, b: TracingState): boolean;
|
|
55
91
|
/** The tracing paper's rectangle on the sheet, in units. */
|
|
@@ -62,22 +98,22 @@ export declare function keptOn(at: readonly [number, number], size: Size): reado
|
|
|
62
98
|
/** Whether a point of the sheet lies on the tracing paper, its edges too. */
|
|
63
99
|
export declare function onTracing(point: Point, at: readonly [number, number], size: Size): boolean;
|
|
64
100
|
/**
|
|
65
|
-
* A point of the sheet as a stroke on the tracing paper holds it: from the
|
|
66
|
-
* kept to the paper (R74 spec).
|
|
101
|
+
* A point of the sheet as a stroke on the tracing paper of `size` at `scale` holds it: from the
|
|
102
|
+
* paper's top left corner, in its own units, kept to the paper (R74 spec; R89).
|
|
67
103
|
*/
|
|
68
|
-
export declare function toTracing(point: Point, at: readonly [number, number], size: Size): Point;
|
|
104
|
+
export declare function toTracing(point: Point, at: readonly [number, number], size: Size, scale?: number): Point;
|
|
69
105
|
/**
|
|
70
|
-
* A stroke of the tracing paper where it lies on the sheet: its points
|
|
71
|
-
* the sheet, should the paper lie in part beyond it — the rules of the groups and the crop hold on
|
|
106
|
+
* A stroke of the tracing paper where it lies on the sheet: its points scaled by `scale` and moved
|
|
107
|
+
* by `at`, and kept to the sheet, should the paper lie in part beyond it — the rules of the groups and the crop hold on
|
|
72
108
|
* the sheet alone (R24, R25). The same ID, kind and times.
|
|
73
109
|
*/
|
|
74
|
-
export declare function onSheet(stroke: Stroke, at: readonly [number, number]): Stroke;
|
|
110
|
+
export declare function onSheet(stroke: Stroke, at: readonly [number, number], scale?: number): Stroke;
|
|
75
111
|
/**
|
|
76
112
|
* The box of a tracing paper's stroke where it lies on the sheet, from the box of its points on the
|
|
77
113
|
* paper: the box of `onSheet`'s points, for moving and keeping to the sheet keep the order of the
|
|
78
114
|
* numbers, without a copy of the stroke.
|
|
79
115
|
*/
|
|
80
|
-
export declare function boxOnSheet(box: Box, at: readonly [number, number]): Box;
|
|
116
|
+
export declare function boxOnSheet(box: Box, at: readonly [number, number], scale?: number): Box;
|
|
81
117
|
/**
|
|
82
118
|
* The tracing paper's rectangle on the sheet in whole tenths of a unit, as the crop counts (R25),
|
|
83
119
|
* kept to the sheet: the part of it a crop of the work takes in.
|
package/lib/core/tracing.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { record, shown } from "./fields.js";
|
|
2
2
|
import { tenths } from "./geometry.js";
|
|
3
|
-
import {
|
|
3
|
+
import { REACH } from "./sheet.js";
|
|
4
4
|
/**
|
|
5
5
|
* The tracing paper's rule (R87), in sheet units: the crop is drawn as large as fits within
|
|
6
6
|
* `width` × `height`, in its own proportions; it starts at `at`, right of the bar of buttons, which
|
|
@@ -15,6 +15,53 @@ export const TRACING = Object.freeze({
|
|
|
15
15
|
leap: 100,
|
|
16
16
|
dots: 25,
|
|
17
17
|
});
|
|
18
|
+
/**
|
|
19
|
+
* The tracing paper's scale (R89): from `min` to `max` times its own size, as the mockup's
|
|
20
|
+
* `Zadacha-masshtab` has it; a button's press multiplies it by `button`, a notch of the wheel by
|
|
21
|
+
* `wheel`, and a scale is kept in hundredths, as the percentage shows it.
|
|
22
|
+
*/
|
|
23
|
+
export const SCALE = Object.freeze({ min: 0.4, max: 4, button: 1.2, wheel: 1.08 });
|
|
24
|
+
/** A scale kept to the rule's bounds, in hundredths (R89). */
|
|
25
|
+
export function scaleOf(scale) {
|
|
26
|
+
return Math.round(clamp(scale, SCALE.min, SCALE.max) * 100) / 100;
|
|
27
|
+
}
|
|
28
|
+
/** The scale of a tracing state: 1 where it has none (R89). */
|
|
29
|
+
export function scaleIn(state) {
|
|
30
|
+
return state.scale ?? 1;
|
|
31
|
+
}
|
|
32
|
+
/** A size as large as `scale` makes it. */
|
|
33
|
+
export function scaled(size, scale) {
|
|
34
|
+
return { width: size.width * scale, height: size.height * scale };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The grids of a task from paper's sheet in the order its button turns them, round, the first as
|
|
38
|
+
* the task comes (R88).
|
|
39
|
+
*/
|
|
40
|
+
export const PAPER_GRIDS = Object.freeze([
|
|
41
|
+
"dots",
|
|
42
|
+
"lines",
|
|
43
|
+
"blank",
|
|
44
|
+
"squares",
|
|
45
|
+
]);
|
|
46
|
+
/** Each grid's rulings, in sheet units, as the mockup draws them: a blank sheet has none (R88). */
|
|
47
|
+
const RULINGS = Object.freeze({
|
|
48
|
+
dots: Object.freeze({ kind: "dots", step: TRACING.dots }),
|
|
49
|
+
lines: Object.freeze({ kind: "lines", step: 40 }),
|
|
50
|
+
blank: undefined,
|
|
51
|
+
squares: Object.freeze({ kind: "squares", step: TRACING.dots }),
|
|
52
|
+
});
|
|
53
|
+
/** Whether a value is a grid of a task from paper's sheet. */
|
|
54
|
+
export function isPaperGrid(value) {
|
|
55
|
+
return typeof value === "string" && PAPER_GRIDS.includes(value);
|
|
56
|
+
}
|
|
57
|
+
/** The rulings of a task from paper's grid, or none for a blank sheet (R88). */
|
|
58
|
+
export function rulingsOf(grid) {
|
|
59
|
+
return RULINGS[grid];
|
|
60
|
+
}
|
|
61
|
+
/** The grid the button turns a task from paper's grid to: the next, round (R88). */
|
|
62
|
+
export function nextGrid(grid) {
|
|
63
|
+
return PAPER_GRIDS[(PAPER_GRIDS.indexOf(grid) + 1) % PAPER_GRIDS.length];
|
|
64
|
+
}
|
|
18
65
|
/** The size before the crop's picture is read: the largest the tracing paper may be. */
|
|
19
66
|
export const LARGEST = Object.freeze({ width: TRACING.width, height: TRACING.height });
|
|
20
67
|
/**
|
|
@@ -31,24 +78,26 @@ export function sizeOf(width, height) {
|
|
|
31
78
|
export const TRACING_START = Object.freeze({
|
|
32
79
|
at: Object.freeze([TRACING.at[0], TRACING.at[1]]),
|
|
33
80
|
hidden: false,
|
|
81
|
+
scale: 1,
|
|
34
82
|
});
|
|
35
83
|
/**
|
|
36
84
|
* A tracing state a consumer gives (R74 spec), checked and frozen: an object of `at`, [x, y] in
|
|
37
|
-
* units, finite numbers,
|
|
38
|
-
* puts the tracing paper of `size` wholly off the sheet
|
|
39
|
-
* not such a state.
|
|
85
|
+
* units, finite numbers, `hidden`, true or false, and `scale`, if any, from 0.4 to 4; other fields
|
|
86
|
+
* are passed over. A state that puts the tracing paper of `size` at its scale wholly off the sheet
|
|
87
|
+
* is a TypeError, as is anything else that is not such a state.
|
|
40
88
|
*/
|
|
41
89
|
export function tracingStateOf(value, size) {
|
|
42
90
|
const state = tracingFieldsOf(value, "tracing");
|
|
43
|
-
if (whollyOff(state.at, size)) {
|
|
91
|
+
if (whollyOff(state.at, scaled(size, scaleIn(state)))) {
|
|
44
92
|
throw new TypeError(`tracing.at: ${shown(state.at)} puts the tracing paper wholly off the sheet`);
|
|
45
93
|
}
|
|
46
94
|
return state;
|
|
47
95
|
}
|
|
48
96
|
/**
|
|
49
97
|
* The fields of a tracing state at `path`, checked and frozen: `at`, [x, y] in units, finite
|
|
50
|
-
* numbers,
|
|
51
|
-
* TypeError. Where it lies on the sheet is the caller's
|
|
98
|
+
* numbers, `hidden`, true or false, and `scale`, from 0.4 to 4, 1 where it is absent; other
|
|
99
|
+
* fields are passed over. Anything else is a TypeError. Where it lies on the sheet is the caller's
|
|
100
|
+
* to check.
|
|
52
101
|
*/
|
|
53
102
|
export function tracingFieldsOf(value, path) {
|
|
54
103
|
const fields = record(value, path);
|
|
@@ -61,17 +110,47 @@ export function tracingFieldsOf(value, path) {
|
|
|
61
110
|
if (typeof fields.hidden !== "boolean") {
|
|
62
111
|
throw new TypeError(`${path}.hidden: true or false, not ${shown(fields.hidden)}`);
|
|
63
112
|
}
|
|
113
|
+
const scale = fields.scale ?? 1;
|
|
114
|
+
if (typeof scale !== "number" ||
|
|
115
|
+
!Number.isFinite(scale) ||
|
|
116
|
+
scale < SCALE.min ||
|
|
117
|
+
scale > SCALE.max) {
|
|
118
|
+
throw new TypeError(`${path}.scale: a number from ${SCALE.min} to ${SCALE.max}, not ${shown(scale)}`);
|
|
119
|
+
}
|
|
64
120
|
const [x, y] = at;
|
|
65
|
-
return Object.freeze({ at: Object.freeze([x, y]), hidden: fields.hidden });
|
|
121
|
+
return Object.freeze({ at: Object.freeze([x, y]), hidden: fields.hidden, scale });
|
|
66
122
|
}
|
|
67
123
|
/** Whether the tracing paper of `size` at `at` lies wholly off the sheet, touching it at most. */
|
|
68
124
|
export function whollyOff(at, size) {
|
|
69
125
|
const [x, y] = at;
|
|
70
|
-
return x >=
|
|
126
|
+
return x >= REACH.width || y >= REACH.height || x + size.width <= 0 || y + size.height <= 0;
|
|
127
|
+
}
|
|
128
|
+
/** The most statements a reading holds, as the attempt's tasks are (R162 spec). */
|
|
129
|
+
const STATEMENTS = 20;
|
|
130
|
+
/**
|
|
131
|
+
* A reading of the statement a consumer gives (R89), checked and frozen: `state` "ready" or
|
|
132
|
+
* "busy", or "done" with `statements`, from none to 20 strings; other fields are passed over.
|
|
133
|
+
* Anything else is a TypeError.
|
|
134
|
+
*/
|
|
135
|
+
export function readingOf(value) {
|
|
136
|
+
const fields = record(value, "reading");
|
|
137
|
+
const state = fields.state;
|
|
138
|
+
if (state === "ready" || state === "busy")
|
|
139
|
+
return Object.freeze({ state });
|
|
140
|
+
if (state !== "done") {
|
|
141
|
+
throw new TypeError(`reading.state: "ready", "busy" or "done", not ${shown(state)}`);
|
|
142
|
+
}
|
|
143
|
+
const statements = fields.statements;
|
|
144
|
+
if (!Array.isArray(statements) ||
|
|
145
|
+
statements.length > STATEMENTS ||
|
|
146
|
+
!statements.every((each) => typeof each === "string")) {
|
|
147
|
+
throw new TypeError(`reading.statements: up to ${STATEMENTS} strings, the statements read, not ${shown(statements)}`);
|
|
148
|
+
}
|
|
149
|
+
return Object.freeze({ state, statements: Object.freeze([...statements]) });
|
|
71
150
|
}
|
|
72
151
|
/** Whether two tracing states say the same. */
|
|
73
152
|
export function sameTracing(a, b) {
|
|
74
|
-
return a.at[0] === b.at[0] && a.at[1] === b.at[1] && a.hidden === b.hidden;
|
|
153
|
+
return (a.at[0] === b.at[0] && a.at[1] === b.at[1] && a.hidden === b.hidden && scaleIn(a) === scaleIn(b));
|
|
75
154
|
}
|
|
76
155
|
/** The tracing paper's rectangle on the sheet, in units. */
|
|
77
156
|
export function rectOf(at, size) {
|
|
@@ -83,8 +162,8 @@ export function rectOf(at, size) {
|
|
|
83
162
|
*/
|
|
84
163
|
export function keptOn(at, size) {
|
|
85
164
|
return Object.freeze([
|
|
86
|
-
clamp(at[0], 0, Math.max(0,
|
|
87
|
-
clamp(at[1], 0, Math.max(0,
|
|
165
|
+
clamp(at[0], 0, Math.max(0, REACH.width - size.width)),
|
|
166
|
+
clamp(at[1], 0, Math.max(0, REACH.height - size.height)),
|
|
88
167
|
]);
|
|
89
168
|
}
|
|
90
169
|
/** Whether a point of the sheet lies on the tracing paper, its edges too. */
|
|
@@ -93,27 +172,27 @@ export function onTracing(point, at, size) {
|
|
|
93
172
|
return point.x >= minX && point.x <= maxX && point.y >= minY && point.y <= maxY;
|
|
94
173
|
}
|
|
95
174
|
/**
|
|
96
|
-
* A point of the sheet as a stroke on the tracing paper holds it: from the
|
|
97
|
-
* kept to the paper (R74 spec).
|
|
175
|
+
* A point of the sheet as a stroke on the tracing paper of `size` at `scale` holds it: from the
|
|
176
|
+
* paper's top left corner, in its own units, kept to the paper (R74 spec; R89).
|
|
98
177
|
*/
|
|
99
|
-
export function toTracing(point, at, size) {
|
|
178
|
+
export function toTracing(point, at, size, scale = 1) {
|
|
100
179
|
return {
|
|
101
|
-
x: clamp(point.x - at[0], 0, size.width),
|
|
102
|
-
y: clamp(point.y - at[1], 0, size.height),
|
|
180
|
+
x: clamp((point.x - at[0]) / scale, 0, size.width),
|
|
181
|
+
y: clamp((point.y - at[1]) / scale, 0, size.height),
|
|
103
182
|
};
|
|
104
183
|
}
|
|
105
184
|
/**
|
|
106
|
-
* A stroke of the tracing paper where it lies on the sheet: its points
|
|
107
|
-
* the sheet, should the paper lie in part beyond it — the rules of the groups and the crop hold on
|
|
185
|
+
* A stroke of the tracing paper where it lies on the sheet: its points scaled by `scale` and moved
|
|
186
|
+
* by `at`, and kept to the sheet, should the paper lie in part beyond it — the rules of the groups and the crop hold on
|
|
108
187
|
* the sheet alone (R24, R25). The same ID, kind and times.
|
|
109
188
|
*/
|
|
110
|
-
export function onSheet(stroke, at) {
|
|
189
|
+
export function onSheet(stroke, at, scale = 1) {
|
|
111
190
|
return Object.freeze({
|
|
112
191
|
id: stroke.id,
|
|
113
192
|
kind: stroke.kind,
|
|
114
193
|
points: Object.freeze(stroke.points.map(({ x, y, t, p }) => Object.freeze({
|
|
115
|
-
x: clamp(x + at[0], 0,
|
|
116
|
-
y: clamp(y + at[1], 0,
|
|
194
|
+
x: clamp(x * scale + at[0], 0, REACH.width),
|
|
195
|
+
y: clamp(y * scale + at[1], 0, REACH.height),
|
|
117
196
|
t,
|
|
118
197
|
p,
|
|
119
198
|
}))),
|
|
@@ -124,12 +203,12 @@ export function onSheet(stroke, at) {
|
|
|
124
203
|
* paper: the box of `onSheet`'s points, for moving and keeping to the sheet keep the order of the
|
|
125
204
|
* numbers, without a copy of the stroke.
|
|
126
205
|
*/
|
|
127
|
-
export function boxOnSheet(box, at) {
|
|
206
|
+
export function boxOnSheet(box, at, scale = 1) {
|
|
128
207
|
return {
|
|
129
|
-
minX: clamp(box.minX + at[0], 0,
|
|
130
|
-
minY: clamp(box.minY + at[1], 0,
|
|
131
|
-
maxX: clamp(box.maxX + at[0], 0,
|
|
132
|
-
maxY: clamp(box.maxY + at[1], 0,
|
|
208
|
+
minX: clamp(box.minX * scale + at[0], 0, REACH.width),
|
|
209
|
+
minY: clamp(box.minY * scale + at[1], 0, REACH.height),
|
|
210
|
+
maxX: clamp(box.maxX * scale + at[0], 0, REACH.width),
|
|
211
|
+
maxY: clamp(box.maxY * scale + at[1], 0, REACH.height),
|
|
133
212
|
};
|
|
134
213
|
}
|
|
135
214
|
/**
|
|
@@ -138,10 +217,10 @@ export function boxOnSheet(box, at) {
|
|
|
138
217
|
*/
|
|
139
218
|
export function tenthsOf(rect) {
|
|
140
219
|
return {
|
|
141
|
-
minX: tenths(clamp(rect.minX, 0,
|
|
142
|
-
minY: tenths(clamp(rect.minY, 0,
|
|
143
|
-
maxX: tenths(clamp(rect.maxX, 0,
|
|
144
|
-
maxY: tenths(clamp(rect.maxY, 0,
|
|
220
|
+
minX: tenths(clamp(rect.minX, 0, REACH.width)),
|
|
221
|
+
minY: tenths(clamp(rect.minY, 0, REACH.height)),
|
|
222
|
+
maxX: tenths(clamp(rect.maxX, 0, REACH.width)),
|
|
223
|
+
maxY: tenths(clamp(rect.maxY, 0, REACH.height)),
|
|
145
224
|
};
|
|
146
225
|
}
|
|
147
226
|
/**
|
package/lib/core/zoom.d.ts
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
|
+
/** The smallest and the largest zoom of the sheet: half its fit, and twice it (R73). */
|
|
2
|
+
export declare const ZOOM_MIN = 0.5;
|
|
3
|
+
export declare const ZOOM_MAX = 2;
|
|
1
4
|
/**
|
|
2
|
-
* The
|
|
3
|
-
*
|
|
4
|
-
* be read at 200 % (WCAG 1.4.4, by the technique G178; R90 spec).
|
|
5
|
+
* The step of Zoom in and Zoom out, a quarter of the fit (R73): from any zoom a pinch left, a
|
|
6
|
+
* press goes to the next quarter, so 50, 75, … 200 % are where the buttons stop.
|
|
5
7
|
*/
|
|
6
|
-
export declare const
|
|
8
|
+
export declare const ZOOM_STEP = 0.25;
|
|
9
|
+
/** The zoom `value` kept within its bounds; anything that is not a number is the fit. */
|
|
10
|
+
export declare function clampZoom(value: number): number;
|
|
11
|
+
/**
|
|
12
|
+
* The next quarter from `value`, larger or smaller, within the bounds. A zoom within a hair of a
|
|
13
|
+
* quarter counts as that quarter, so that the arithmetic of a pinch never makes a press a no-op.
|
|
14
|
+
*/
|
|
15
|
+
export declare function stepZoom(value: number, larger: boolean): number;
|
|
7
16
|
/**
|
|
8
17
|
* The page's zoom (R73): one for every sheet of the page, from 100 % when the page is loaded, as
|
|
9
18
|
* the pen mode is (R29). The next task is a new element (R20), and the zoom stays: a learner who
|
|
10
|
-
* needs it larger asks once. It lives in memory alone: nothing is stored or sent.
|
|
19
|
+
* needs it larger or smaller asks once. It lives in memory alone: nothing is stored or sent.
|
|
11
20
|
*/
|
|
12
21
|
export declare class PageZoom {
|
|
13
22
|
#private;
|
|
14
|
-
/** The zoom now: 1 for the fit, 2 for twice it. */
|
|
23
|
+
/** The zoom now: 1 for the fit, 2 for twice it, 0.5 for half. */
|
|
15
24
|
get value(): number;
|
|
16
|
-
/** Whether there is a larger zoom to
|
|
25
|
+
/** Whether there is a larger zoom to go to, for Zoom in, or a smaller one, for Zoom out. */
|
|
17
26
|
get larger(): boolean;
|
|
18
27
|
get smaller(): boolean;
|
|
19
|
-
/** A step
|
|
28
|
+
/** A step to the next quarter, larger or smaller, if there is one (R73). */
|
|
20
29
|
step(larger: boolean): boolean;
|
|
30
|
+
/** The zoom set to `value` within its bounds — a pinch's —: every watcher hears of a change. */
|
|
31
|
+
set(value: number): boolean;
|
|
21
32
|
/** `watcher` hears of every change from now on, until the function this gives is called. */
|
|
22
33
|
watch(watcher: () => void): () => void;
|
|
23
34
|
}
|
package/lib/core/zoom.js
CHANGED
|
@@ -1,34 +1,55 @@
|
|
|
1
|
+
/** The smallest and the largest zoom of the sheet: half its fit, and twice it (R73). */
|
|
2
|
+
export const ZOOM_MIN = 0.5;
|
|
3
|
+
export const ZOOM_MAX = 2;
|
|
1
4
|
/**
|
|
2
|
-
* The
|
|
3
|
-
*
|
|
4
|
-
* be read at 200 % (WCAG 1.4.4, by the technique G178; R90 spec).
|
|
5
|
+
* The step of Zoom in and Zoom out, a quarter of the fit (R73): from any zoom a pinch left, a
|
|
6
|
+
* press goes to the next quarter, so 50, 75, … 200 % are where the buttons stop.
|
|
5
7
|
*/
|
|
6
|
-
export const
|
|
8
|
+
export const ZOOM_STEP = 0.25;
|
|
9
|
+
/** The zoom `value` kept within its bounds; anything that is not a number is the fit. */
|
|
10
|
+
export function clampZoom(value) {
|
|
11
|
+
if (!Number.isFinite(value))
|
|
12
|
+
return 1;
|
|
13
|
+
return Math.min(Math.max(value, ZOOM_MIN), ZOOM_MAX);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The next quarter from `value`, larger or smaller, within the bounds. A zoom within a hair of a
|
|
17
|
+
* quarter counts as that quarter, so that the arithmetic of a pinch never makes a press a no-op.
|
|
18
|
+
*/
|
|
19
|
+
export function stepZoom(value, larger) {
|
|
20
|
+
const quarters = value / ZOOM_STEP;
|
|
21
|
+
const next = larger ? Math.floor(quarters + 1e-6) + 1 : Math.ceil(quarters - 1e-6) - 1;
|
|
22
|
+
return clampZoom(next * ZOOM_STEP);
|
|
23
|
+
}
|
|
7
24
|
/**
|
|
8
25
|
* The page's zoom (R73): one for every sheet of the page, from 100 % when the page is loaded, as
|
|
9
26
|
* the pen mode is (R29). The next task is a new element (R20), and the zoom stays: a learner who
|
|
10
|
-
* needs it larger asks once. It lives in memory alone: nothing is stored or sent.
|
|
27
|
+
* needs it larger or smaller asks once. It lives in memory alone: nothing is stored or sent.
|
|
11
28
|
*/
|
|
12
29
|
export class PageZoom {
|
|
13
|
-
#
|
|
30
|
+
#value = 1;
|
|
14
31
|
#watchers = new Set();
|
|
15
|
-
/** The zoom now: 1 for the fit, 2 for twice it. */
|
|
32
|
+
/** The zoom now: 1 for the fit, 2 for twice it, 0.5 for half. */
|
|
16
33
|
get value() {
|
|
17
|
-
return
|
|
34
|
+
return this.#value;
|
|
18
35
|
}
|
|
19
|
-
/** Whether there is a larger zoom to
|
|
36
|
+
/** Whether there is a larger zoom to go to, for Zoom in, or a smaller one, for Zoom out. */
|
|
20
37
|
get larger() {
|
|
21
|
-
return this.#
|
|
38
|
+
return this.#value < ZOOM_MAX;
|
|
22
39
|
}
|
|
23
40
|
get smaller() {
|
|
24
|
-
return this.#
|
|
41
|
+
return this.#value > ZOOM_MIN;
|
|
25
42
|
}
|
|
26
|
-
/** A step
|
|
43
|
+
/** A step to the next quarter, larger or smaller, if there is one (R73). */
|
|
27
44
|
step(larger) {
|
|
28
|
-
|
|
29
|
-
|
|
45
|
+
return this.set(stepZoom(this.#value, larger));
|
|
46
|
+
}
|
|
47
|
+
/** The zoom set to `value` within its bounds — a pinch's —: every watcher hears of a change. */
|
|
48
|
+
set(value) {
|
|
49
|
+
const next = clampZoom(value);
|
|
50
|
+
if (next === this.#value)
|
|
30
51
|
return false;
|
|
31
|
-
this.#
|
|
52
|
+
this.#value = next;
|
|
32
53
|
for (const watcher of [...this.#watchers])
|
|
33
54
|
watcher();
|
|
34
55
|
return true;
|