@altnovatrail/canvas 0.3.34 → 0.3.36
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 +4 -2
- package/lib/core/crop.js +4 -3
- package/lib/core/history.d.ts +6 -4
- package/lib/core/history.js +13 -6
- package/lib/core/index.d.ts +2 -1
- package/lib/core/index.js +4 -4
- package/lib/core/marks.d.ts +31 -10
- package/lib/core/marks.js +131 -14
- package/lib/core/submission.d.ts +23 -1
- package/lib/core/submission.js +49 -1
- package/lib/core/tracing.d.ts +110 -0
- package/lib/core/tracing.js +219 -0
- package/lib/core/work.d.ts +16 -6
- package/lib/core/work.js +36 -11
- package/lib/element/icons.d.ts +13 -0
- package/lib/element/icons.js +13 -0
- package/lib/element/index.d.ts +32 -4
- package/lib/element/index.js +589 -65
- package/lib/element/labels.d.ts +7 -0
- package/lib/element/labels.js +10 -0
- package/lib/element/tools.d.ts +11 -3
- package/lib/element/tools.js +48 -6
- package/lib/element/tracing.d.ts +58 -0
- package/lib/element/tracing.js +303 -0
- package/package.json +3 -3
package/lib/element/index.js
CHANGED
|
@@ -42,13 +42,19 @@
|
|
|
42
42
|
// element's (R74). Zoom in and Zoom out step the page's zoom of the sheet from its fit to twice
|
|
43
43
|
// it, and a zoomed sheet scrolls across as well as down, with two fingers, the wheel and the keys
|
|
44
44
|
// (R73).
|
|
45
|
+
//
|
|
46
|
+
// A task from the learner's paper it shows on a dotted sheet, with the task's crop over it as
|
|
47
|
+
// tracing paper, which the learner moves by its grip, hides and shows by the button atop the tools,
|
|
48
|
+
// and writes on as on the sheet: a stroke begun on the paper is the paper's and moves with it, and
|
|
49
|
+
// an erasing begun on it takes the paper's strokes alone. The work for checking is the crop of the
|
|
50
|
+
// paper and of the sheet's strokes, with the task's crop beside it (R87).
|
|
45
51
|
import { cellOf, cellsOf, filledOf, sheetFilledOf } from "../core/boxes.js";
|
|
46
52
|
import { Clock } from "../core/clock.js";
|
|
47
53
|
import { ERASER_BUTTON, Eraser } from "../core/eraser.js";
|
|
48
54
|
import { DEMO_START, demoStateOf, exampleOf, exampleTemplate, filledBy, fillValue, sameExample, } from "../core/example.js";
|
|
49
55
|
import { sameItems } from "../core/fields.js";
|
|
50
56
|
import { boxOf } from "../core/geometry.js";
|
|
51
|
-
import { gridPath } from "../core/grid.js";
|
|
57
|
+
import { GRID_STEP, gridPath } from "../core/grid.js";
|
|
52
58
|
import { Hands } from "../core/hands.js";
|
|
53
59
|
import { begins, sameHistory } from "../core/history.js";
|
|
54
60
|
import { NO_MANIFEST, settingsOf } from "../core/manifest.js";
|
|
@@ -59,8 +65,9 @@ import { encodePNG } from "../core/png.js";
|
|
|
59
65
|
import { Recorder } from "../core/recorder.js";
|
|
60
66
|
import { fitScale, K_MAX, scaleFor, toSheet } from "../core/screen.js";
|
|
61
67
|
import { SHEET } from "../core/sheet.js";
|
|
62
|
-
import { cellsForCheck, workForCheck } from "../core/submission.js";
|
|
68
|
+
import { cellsForCheck, paperWorkForCheck, workForCheck } from "../core/submission.js";
|
|
63
69
|
import { isTool, isWorkbook, NO_TASK, sameLayer, sameStatement, taskOf, templateOf, wholeSheet, } from "../core/task.js";
|
|
70
|
+
import { boxOnSheet, keptOn, layOver, onSheet, onTracing, sameTracing, TRACING, TRACING_START, toTracing, rectOf as tracingRect, tracingStateOf, whollyOff, } from "../core/tracing.js";
|
|
64
71
|
import { nextIdOf, resumeOf, Work } from "../core/work.js";
|
|
65
72
|
import { placeOver, SVG, write } from "./attributes.js";
|
|
66
73
|
import { knows, labelsFor, say, sayAround } from "./labels.js";
|
|
@@ -70,6 +77,7 @@ import { attachProbe } from "./probe.js";
|
|
|
70
77
|
import { PAGE_ZOOM, PEN_MODE } from "./session.js";
|
|
71
78
|
import { Statement } from "./statement.js";
|
|
72
79
|
import { Tools } from "./tools.js";
|
|
80
|
+
import { TracingPaper } from "./tracing.js";
|
|
73
81
|
/** The name the element is registered under. */
|
|
74
82
|
export const TAG = "novatrail-canvas";
|
|
75
83
|
/** The colour of the ink unless the consumer's theme gives one. */
|
|
@@ -88,6 +96,17 @@ const HISTORY_CHANGE = "historychange";
|
|
|
88
96
|
const TOOL_CHANGE = "toolchange";
|
|
89
97
|
/** The event the element sends when the learner opens a part of the demo, or hides or shows it. */
|
|
90
98
|
const DEMO_CHANGE = "demochange";
|
|
99
|
+
/** The event the element sends when the learner moves the tracing paper, or hides or shows it. */
|
|
100
|
+
const TRACING_CHANGE = "tracingchange";
|
|
101
|
+
/**
|
|
102
|
+
* A task from paper's template: a dotted sheet, no statement, the pen and the eraser (R87; R162
|
|
103
|
+
* spec).
|
|
104
|
+
*/
|
|
105
|
+
const PAPER_TEMPLATE = Object.freeze({
|
|
106
|
+
grid: Object.freeze({ kind: "dots", step: TRACING.dots }),
|
|
107
|
+
statement: Object.freeze([]),
|
|
108
|
+
tools: NO_TASK.tools,
|
|
109
|
+
});
|
|
91
110
|
/** What Next and Demo control, by their IDs in the shadow root: the region, and the list (R42). */
|
|
92
111
|
const DEMO_IDS = { status: "demo-status", parts: "demo-parts" };
|
|
93
112
|
/**
|
|
@@ -95,6 +114,34 @@ const DEMO_IDS = { status: "demo-status", parts: "demo-parts" };
|
|
|
95
114
|
* which would read as «wrong».
|
|
96
115
|
*/
|
|
97
116
|
const MARK = "#2563eb";
|
|
117
|
+
/**
|
|
118
|
+
* The colours of a verdict's tick and cross, over its white halo: a green, and an amber rather than
|
|
119
|
+
* a red, for «not yet» is no failure; each holds more than 3:1 on white, and the halo holds them
|
|
120
|
+
* over any sheet or photo of a page (R162 spec).
|
|
121
|
+
*/
|
|
122
|
+
const RIGHT = "#15803d";
|
|
123
|
+
const NOT_YET = "#b45309";
|
|
124
|
+
/** The size of a verdict's halo, in CSS pixels: the same on every screen, whatever the scale. */
|
|
125
|
+
const VERDICT_PX = 28;
|
|
126
|
+
/** How far a verdict stands off the right of its answer's box, in CSS pixels. */
|
|
127
|
+
const VERDICT_GAP_PX = 4;
|
|
128
|
+
/** A verdict's tick and cross, on the 24 × 24 of the halo's circle. */
|
|
129
|
+
const TICK = "M7 12.5l3.5 3.5L17 8.5";
|
|
130
|
+
const CROSS = "M8 8l8 8M16 8l-8 8";
|
|
131
|
+
/**
|
|
132
|
+
* The colour of the tutor's wavy line of "Show where" unless the consumer's theme gives one: an
|
|
133
|
+
* orange, as the mockup's, 3.58:1 on white, over a white halo that holds it over any sheet or photo
|
|
134
|
+
* of a page (R87; R162 spec).
|
|
135
|
+
*/
|
|
136
|
+
const WAVY = "#e8590c";
|
|
137
|
+
/**
|
|
138
|
+
* The wavy line, in sheet units: half a wave half a square of the sheet long, its crests this high
|
|
139
|
+
* off its middle, this far under its place, and reaching this far past each side of it.
|
|
140
|
+
*/
|
|
141
|
+
const WAVY_HALF = GRID_STEP / 4;
|
|
142
|
+
const WAVY_CREST = 3.5;
|
|
143
|
+
const WAVY_BELOW = 12;
|
|
144
|
+
const WAVY_REACH = 10;
|
|
98
145
|
/** How far a frame stands off the points of its strokes, in sheet units, as canvas-api's (R26). */
|
|
99
146
|
const MARK_PADDING = 10;
|
|
100
147
|
/** The rounding of a frame's corners, in sheet units. */
|
|
@@ -297,11 +344,40 @@ canvas {
|
|
|
297
344
|
touch-action: none;
|
|
298
345
|
}
|
|
299
346
|
.sheet.example canvas { pointer-events: none; touch-action: auto; }
|
|
347
|
+
.tracing { mix-blend-mode: multiply; }
|
|
348
|
+
.tracing .edge {
|
|
349
|
+
fill: none; stroke: rgba(60, 60, 67, 0.3); stroke-width: 1px; vector-effect: non-scaling-stroke;
|
|
350
|
+
}
|
|
351
|
+
.stage > .grip {
|
|
352
|
+
pointer-events: auto; inset: auto; left: 0; top: 0; width: 68px; height: 44px; margin: 0;
|
|
353
|
+
padding: 0; border: none; border-radius: 22px; background: transparent; cursor: grab;
|
|
354
|
+
touch-action: none; display: flex; align-items: center; justify-content: center;
|
|
355
|
+
}
|
|
356
|
+
.stage > .grip[hidden] { display: none; }
|
|
357
|
+
.grip .bar {
|
|
358
|
+
width: 40px; height: 5px; border-radius: 3px;
|
|
359
|
+
background: color-mix(in srgb, var(--novatrail-canvas-ink, ${INK}) 30%, transparent);
|
|
360
|
+
}
|
|
361
|
+
.grip.dragging { cursor: grabbing; }
|
|
362
|
+
.grip.dragging .bar { background: var(--novatrail-canvas-accent, ${ACCENT}); }
|
|
363
|
+
.grip:focus-visible { outline: 2px solid var(--novatrail-canvas-accent, ${ACCENT}); }
|
|
364
|
+
.stage > .tip { inset: auto; width: auto; height: auto; pointer-events: none; }
|
|
300
365
|
.marks rect {
|
|
301
366
|
fill: none; stroke: var(--novatrail-canvas-mark, ${MARK}); stroke-width: 2px;
|
|
302
367
|
vector-effect: non-scaling-stroke;
|
|
303
368
|
}
|
|
304
|
-
.marks rect.under { opacity: 0.25; }
|
|
369
|
+
.marks rect.under, .marks g.under { opacity: 0.25; }
|
|
370
|
+
.marks .halo { fill: #fff; stroke: rgba(0, 0, 0, 0.2); stroke-width: 1px; vector-effect: non-scaling-stroke; }
|
|
371
|
+
.marks .sign {
|
|
372
|
+
fill: none; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round;
|
|
373
|
+
}
|
|
374
|
+
.marks .right .sign { stroke: ${RIGHT}; }
|
|
375
|
+
.marks .wavy path {
|
|
376
|
+
fill: none; stroke-linecap: round; stroke-linejoin: round; vector-effect: non-scaling-stroke;
|
|
377
|
+
}
|
|
378
|
+
.marks .wavy .glow { stroke: #fff; stroke-width: 6.4px; }
|
|
379
|
+
.marks .wavy .line { stroke: var(--novatrail-canvas-wavy, ${WAVY}); stroke-width: 2.4px; }
|
|
380
|
+
.marks .not-yet .sign { stroke: ${NOT_YET}; }
|
|
305
381
|
@media (forced-colors: active) {
|
|
306
382
|
.sheet { outline: 1px solid CanvasText; }
|
|
307
383
|
.grid { stroke: GrayText; }
|
|
@@ -310,6 +386,14 @@ canvas {
|
|
|
310
386
|
.print rect.filled { stroke: Highlight; }
|
|
311
387
|
canvas { color: CanvasText; }
|
|
312
388
|
.marks rect { stroke: Highlight; }
|
|
389
|
+
.marks .halo { fill: Canvas; stroke: CanvasText; }
|
|
390
|
+
.marks .right .sign, .marks .not-yet .sign { stroke: Highlight; }
|
|
391
|
+
.marks .wavy .glow { stroke: Canvas; }
|
|
392
|
+
.marks .wavy .line { stroke: Highlight; }
|
|
393
|
+
.tracing { mix-blend-mode: normal; }
|
|
394
|
+
.tracing .edge { stroke: CanvasText; }
|
|
395
|
+
.grip .bar, .grip.dragging .bar { background: ButtonText; }
|
|
396
|
+
.grip:focus-visible { outline-color: Highlight; }
|
|
313
397
|
.tools { background: Canvas; color: CanvasText; border: 1px solid CanvasText; }
|
|
314
398
|
.tools .line { background: CanvasText; }
|
|
315
399
|
.tools button {
|
|
@@ -365,6 +449,80 @@ function marksLayer() {
|
|
|
365
449
|
svg.setAttribute("role", "none");
|
|
366
450
|
return svg;
|
|
367
451
|
}
|
|
452
|
+
/** A frame of a mark, round its place. */
|
|
453
|
+
function frameNode() {
|
|
454
|
+
const rect = document.createElementNS(SVG, "rect");
|
|
455
|
+
rect.setAttribute("rx", String(MARK_CORNER));
|
|
456
|
+
return rect;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* A verdict of a task from paper (R162 spec): a tick or a cross on a white circle, on the 24 × 24
|
|
460
|
+
* that its place scales.
|
|
461
|
+
*/
|
|
462
|
+
function verdictNode(correct) {
|
|
463
|
+
const g = document.createElementNS(SVG, "g");
|
|
464
|
+
g.setAttribute("class", correct ? "right" : "not-yet");
|
|
465
|
+
const halo = document.createElementNS(SVG, "circle");
|
|
466
|
+
halo.setAttribute("class", "halo");
|
|
467
|
+
halo.setAttribute("cx", "12");
|
|
468
|
+
halo.setAttribute("cy", "12");
|
|
469
|
+
halo.setAttribute("r", "11.5");
|
|
470
|
+
const sign = document.createElementNS(SVG, "path");
|
|
471
|
+
sign.setAttribute("class", "sign");
|
|
472
|
+
sign.setAttribute("d", correct ? TICK : CROSS);
|
|
473
|
+
g.append(halo, sign);
|
|
474
|
+
return g;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* The wavy line of "Show where" (R87): the line over a wider white one, its halo, both
|
|
478
|
+
* of the path the marks give them.
|
|
479
|
+
*/
|
|
480
|
+
function wavyNode() {
|
|
481
|
+
const g = document.createElementNS(SVG, "g");
|
|
482
|
+
g.setAttribute("class", "wavy");
|
|
483
|
+
for (const name of ["glow", "line"]) {
|
|
484
|
+
const path = document.createElementNS(SVG, "path");
|
|
485
|
+
path.setAttribute("class", name);
|
|
486
|
+
g.append(path);
|
|
487
|
+
}
|
|
488
|
+
return g;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Where a wavy line stands, in sheet units, by its place: under it, past each side, from crest to
|
|
492
|
+
* trough, kept to the sheet by `half` its line.
|
|
493
|
+
*/
|
|
494
|
+
function wavyAt(box, half) {
|
|
495
|
+
const y = Math.min(box.maxY + WAVY_BELOW, SHEET.height - WAVY_CREST - half);
|
|
496
|
+
return {
|
|
497
|
+
minX: Math.max(box.minX - WAVY_REACH, half),
|
|
498
|
+
minY: y - WAVY_CREST,
|
|
499
|
+
maxX: Math.min(box.maxX + WAVY_REACH, SHEET.width - half),
|
|
500
|
+
maxY: y + WAVY_CREST,
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
/** The path of a wavy line across its box, by half waves from its left. */
|
|
504
|
+
function wavyPath({ minX, minY, maxX, maxY }) {
|
|
505
|
+
const y = (minY + maxY) / 2;
|
|
506
|
+
// A quadratic curve's crest is half way to its control point.
|
|
507
|
+
const control = maxY - minY;
|
|
508
|
+
const at = (n) => String(Math.round(n * 10) / 10);
|
|
509
|
+
let d = `M${at(minX)} ${at(y)}`;
|
|
510
|
+
for (let x = minX, up = true; x < maxX; x += WAVY_HALF, up = !up) {
|
|
511
|
+
const to = Math.min(x + WAVY_HALF, maxX);
|
|
512
|
+
d += ` Q${at((x + to) / 2)} ${at(up ? y - control : y + control)} ${at(to)} ${at(y)}`;
|
|
513
|
+
}
|
|
514
|
+
return d;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Where a verdict stands, in sheet units, by its answer's box: off its right edge, at the middle of
|
|
518
|
+
* its height, its size the same on every screen (`k` CSS pixels to the unit), kept to the sheet.
|
|
519
|
+
*/
|
|
520
|
+
function verdictAt(box, k) {
|
|
521
|
+
const size = VERDICT_PX / k;
|
|
522
|
+
const minX = Math.min(Math.max(box.maxX + VERDICT_GAP_PX / k, 0), SHEET.width - size);
|
|
523
|
+
const minY = Math.min(Math.max((box.minY + box.maxY - size) / 2, 0), SHEET.height - size);
|
|
524
|
+
return { minX, minY, maxX: minX + size, maxY: minY + size };
|
|
525
|
+
}
|
|
368
526
|
/** An HTML layer of the sheet, for the module's widget from stage 4: empty till then. */
|
|
369
527
|
function htmlLayer(name) {
|
|
370
528
|
const div = document.createElement("div");
|
|
@@ -425,7 +583,23 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
425
583
|
},
|
|
426
584
|
next: () => this.#openNext(),
|
|
427
585
|
toggle: () => this.#toggleDemo(),
|
|
586
|
+
trace: () => this.#trace(),
|
|
428
587
|
}, DEMO_IDS);
|
|
588
|
+
/** A task from paper's tracing paper: its layer, its grip and the crop's picture (R87). */
|
|
589
|
+
#tracingPaper = new TracingPaper({
|
|
590
|
+
move: (at) => this.#moveTracing(at),
|
|
591
|
+
moved: () => this.#tracingMoved(),
|
|
592
|
+
});
|
|
593
|
+
/** The task from paper as the consumer gave it, and where its tracing paper lies (R87). */
|
|
594
|
+
#paper;
|
|
595
|
+
#tracing;
|
|
596
|
+
/** The tracing paper's state the learner's move began at: the move tells only if it changed it. */
|
|
597
|
+
#moveFrom;
|
|
598
|
+
/** The layer the stroke under way is written on, and the erasing under way erases (R87). */
|
|
599
|
+
#writeOn;
|
|
600
|
+
#eraseOn;
|
|
601
|
+
/** Each stroke of the tracing paper where it lies on the sheet, for the place it was moved to. */
|
|
602
|
+
#placed = new WeakMap();
|
|
429
603
|
/** The sheet's clock, for its strokes and its actions alike (R22). */
|
|
430
604
|
#clock = new Clock();
|
|
431
605
|
/**
|
|
@@ -438,11 +612,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
438
612
|
#telling = false;
|
|
439
613
|
/** The input: a finished stroke goes onto the work, and the work has changed. */
|
|
440
614
|
#recorder = new Recorder((stroke) => {
|
|
441
|
-
this.#work.write(stroke);
|
|
615
|
+
this.#work.write(stroke, this.#writeOn);
|
|
442
616
|
this.#changed();
|
|
443
617
|
}, this.#clock);
|
|
444
618
|
/** The eraser: what a press came near leaves the work when it lifts, if still on the sheet. */
|
|
445
|
-
#eraser = new Eraser((from, to, reach) => this.#work.near(from, to, reach), (ids, time) => {
|
|
619
|
+
#eraser = new Eraser((from, to, reach) => this.#work.near(from, to, reach, this.#eraseOn), (ids, time) => {
|
|
446
620
|
if (this.#work.erase(ids, time))
|
|
447
621
|
this.#changed();
|
|
448
622
|
});
|
|
@@ -515,11 +689,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
515
689
|
*/
|
|
516
690
|
#equal;
|
|
517
691
|
#shown = [];
|
|
518
|
-
/**
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
};
|
|
692
|
+
/**
|
|
693
|
+
* The strokes on the sheet by ID, the tracing paper's from its corner, kept while the sheet's
|
|
694
|
+
* strokes are the same array.
|
|
695
|
+
*/
|
|
696
|
+
#strokesById = { strokes: [], byId: new Map() };
|
|
523
697
|
/** The box of each stroke's points, worked out once: a stroke never changes (R22). */
|
|
524
698
|
#pointBoxes = new WeakMap();
|
|
525
699
|
/** The stroke under way, and how many of its points the marks have been checked against. */
|
|
@@ -572,11 +746,12 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
572
746
|
this.#print.setAttribute("class", "print");
|
|
573
747
|
// The constructor reads no attribute: the labels of a demo's boxes are English till `lang` comes.
|
|
574
748
|
this.#nameBoxes(null);
|
|
749
|
+
this.#nameGrip(null);
|
|
575
750
|
this.#lines.setAttribute("role", "list");
|
|
576
751
|
this.#print.append(this.#lines);
|
|
577
752
|
this.#template.append(this.#grid, this.#print);
|
|
578
753
|
this.#show.append(this.#parts.layer);
|
|
579
|
-
this.#stage.append(this.#template, this.#widget, this.#show, this.#sketch, this.#canvas, this.#marksLayer);
|
|
754
|
+
this.#stage.append(this.#template, this.#widget, this.#show, this.#tracingPaper.layer, this.#sketch, this.#canvas, this.#marksLayer, this.#tracingPaper.grip, this.#tracingPaper.tip);
|
|
580
755
|
this.#sheet.append(this.#stage, this.#parts.spot);
|
|
581
756
|
this.#column.append(this.#statement.element, this.#parts.list, this.#parts.status, this.#sheet);
|
|
582
757
|
this.#viewport.append(this.#column);
|
|
@@ -624,8 +799,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
624
799
|
this.#adopt("tool");
|
|
625
800
|
this.#adopt("task");
|
|
626
801
|
this.#adopt("workedExample");
|
|
627
|
-
|
|
802
|
+
this.#adopt("paper");
|
|
803
|
+
// The demo's state after its example, and the tracing paper's after its task from paper: each
|
|
804
|
+
// is a TypeError without it.
|
|
628
805
|
this.#adopt("demoState");
|
|
806
|
+
this.#adopt("tracing");
|
|
629
807
|
this.#adopt("manifest");
|
|
630
808
|
this.#adopt("media");
|
|
631
809
|
// The history before the marks: they hold on to its strokes.
|
|
@@ -679,6 +857,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
679
857
|
if (this.#example !== undefined) {
|
|
680
858
|
throw new TypeError(`<${TAG}> shows a worked example: a task is shown by a new element, or once the example is undefined`);
|
|
681
859
|
}
|
|
860
|
+
if (this.#paper !== undefined) {
|
|
861
|
+
throw new TypeError(`<${TAG}> shows a task from paper: a task is shown by a new element, or once the paper is undefined`);
|
|
862
|
+
}
|
|
682
863
|
const checked = task === undefined ? undefined : taskOf(task);
|
|
683
864
|
const template = templateOf(checked);
|
|
684
865
|
this.#task = task;
|
|
@@ -740,6 +921,87 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
740
921
|
this.#present(exampleTemplate(checked));
|
|
741
922
|
}
|
|
742
923
|
}
|
|
924
|
+
/**
|
|
925
|
+
* A task from the learner's paper (R87; R162 spec): `statement`, the task's crop as app cut it, a
|
|
926
|
+
* Blob of a JPEG or a PNG. The element shows it in place of a task and of a worked example: a
|
|
927
|
+
* dotted sheet with no statement, the pen and the eraser, and the crop over it as tracing paper,
|
|
928
|
+
* at the element's own place for it, shown, which `tracing` tells. While a task or an example is
|
|
929
|
+
* shown it is a TypeError, and so is what is not such a task. Another task from paper, of
|
|
930
|
+
* another crop, lays its crop at the start again, and the history stays: the next task is a new
|
|
931
|
+
* element (R20); one of the same crop changes nothing. `undefined` takes it off, but not while the
|
|
932
|
+
* history has strokes on the tracing paper, a TypeError. Either drops a stroke or an erasing under
|
|
933
|
+
* way on the tracing paper, as a new history drops the one under the pen. The crop's picture is
|
|
934
|
+
* read as it comes; till then the paper has its largest size.
|
|
935
|
+
*/
|
|
936
|
+
get paper() {
|
|
937
|
+
return this.#paper;
|
|
938
|
+
}
|
|
939
|
+
set paper(paper) {
|
|
940
|
+
if (paper === this.#paper)
|
|
941
|
+
return;
|
|
942
|
+
if (paper === undefined) {
|
|
943
|
+
if (this.#work.traced) {
|
|
944
|
+
throw new TypeError(`<${TAG}> has strokes on the tracing paper: the task from paper goes with a new element, or a history without them`);
|
|
945
|
+
}
|
|
946
|
+
this.#leaveTracing(false);
|
|
947
|
+
this.#paper = undefined;
|
|
948
|
+
this.#tracing = undefined;
|
|
949
|
+
this.#tracingPaper.show(undefined, () => { });
|
|
950
|
+
this.#whole = false;
|
|
951
|
+
this.#present(NO_TASK);
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
if (this.#example !== undefined) {
|
|
955
|
+
throw new TypeError(`<${TAG}> shows a worked example: a task from paper is shown by a new element, or once the example is undefined`);
|
|
956
|
+
}
|
|
957
|
+
if (this.#task !== undefined) {
|
|
958
|
+
throw new TypeError(`<${TAG}> shows a task: a task from paper is shown by a new element, or once the task is undefined`);
|
|
959
|
+
}
|
|
960
|
+
const statement = typeof paper === "object" && paper !== null ? paper.statement : undefined;
|
|
961
|
+
if (!(statement instanceof Blob)) {
|
|
962
|
+
throw new TypeError(`paper.statement: the task's crop, a Blob of a JPEG or a PNG, not ${typeof statement}`);
|
|
963
|
+
}
|
|
964
|
+
// A consumer that renders the same crop anew in a new object keeps the paper where it lies.
|
|
965
|
+
if (this.#paper?.statement === statement) {
|
|
966
|
+
this.#paper = paper;
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
this.#leaveTracing(false);
|
|
970
|
+
this.#paper = paper;
|
|
971
|
+
this.#tracing = TRACING_START;
|
|
972
|
+
this.#tracingPaper.show(statement, () => this.#sized());
|
|
973
|
+
this.#whole = true;
|
|
974
|
+
this.#present(PAPER_TEMPLATE);
|
|
975
|
+
this.#request();
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* Where the tracing paper of the task from paper lies on the sheet, its top left corner in sheet
|
|
979
|
+
* units, and whether it is hidden (R87): frozen, and the same object until it changes;
|
|
980
|
+
* `undefined` without a task from paper. The learner moves it by its grip and hides or shows it by
|
|
981
|
+
* the button atop the tools, which a `tracingchange` tells; the consumer keeps it and gives it
|
|
982
|
+
* back, which tells nothing. Given before the task from paper it is a TypeError, and so is a state
|
|
983
|
+
* that is not one, or one that puts the paper wholly off the sheet; `undefined` changes nothing.
|
|
984
|
+
* Till the crop's picture is read the paper has its largest size: a state that puts the paper of
|
|
985
|
+
* its real size wholly off the sheet then moves to the nearest place wholly on it, with no event.
|
|
986
|
+
*/
|
|
987
|
+
get tracing() {
|
|
988
|
+
return this.#tracing;
|
|
989
|
+
}
|
|
990
|
+
set tracing(state) {
|
|
991
|
+
if (state === undefined)
|
|
992
|
+
return;
|
|
993
|
+
if (this.#paper === undefined) {
|
|
994
|
+
throw new TypeError(`<${TAG}> shows no task from paper: the tracing paper's state is given after the paper`);
|
|
995
|
+
}
|
|
996
|
+
const checked = tracingStateOf(state, this.#tracingPaper.size);
|
|
997
|
+
const shown = this.#tracing;
|
|
998
|
+
if (shown !== undefined && sameTracing(shown, checked))
|
|
999
|
+
return;
|
|
1000
|
+
this.#tracing = checked;
|
|
1001
|
+
this.#replace();
|
|
1002
|
+
this.#refresh();
|
|
1003
|
+
this.#request();
|
|
1004
|
+
}
|
|
743
1005
|
/**
|
|
744
1006
|
* How far the learner has gone through the worked example's demo (R42): `opened`, how many of
|
|
745
1007
|
* its parts are open, from the first, and `hidden`, whether the whole demo is hidden — frozen,
|
|
@@ -859,7 +1121,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
859
1121
|
throw new TypeError(`<${TAG}> shows a worked example, whose sheet has no history of writing`);
|
|
860
1122
|
}
|
|
861
1123
|
// undefined, and undefined alone, is the empty history: null is a value, and a wrong one.
|
|
862
|
-
const work = Work.restore(history === undefined ? [] : history, this.#clock);
|
|
1124
|
+
const work = Work.restore(history === undefined ? [] : history, this.#clock, this.#paper !== undefined);
|
|
863
1125
|
const after = work.history;
|
|
864
1126
|
// The empty history is a new sheet, which a consumer asks for on purpose.
|
|
865
1127
|
if (after.length > 0 && begins(after, before)) {
|
|
@@ -933,6 +1195,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
933
1195
|
this.#watchDensity();
|
|
934
1196
|
this.#watchColours();
|
|
935
1197
|
this.#pictures.connect();
|
|
1198
|
+
this.#tracingPaper.connect();
|
|
936
1199
|
// The page's pen mode, on every sheet of it (R29). The row shows a change once the event that
|
|
937
1200
|
// made it is over: by then a pen that came down writes, and every row of the page waits for its
|
|
938
1201
|
// stroke. The sheet tells the page whether it is written on, and hears when a stroke ends.
|
|
@@ -966,6 +1229,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
966
1229
|
return;
|
|
967
1230
|
}
|
|
968
1231
|
this.#tools.label(this.getAttribute("lang"));
|
|
1232
|
+
this.#nameGrip();
|
|
969
1233
|
this.#nameRegion();
|
|
970
1234
|
this.#nameBoxes();
|
|
971
1235
|
this.#markBoxes();
|
|
@@ -986,6 +1250,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
986
1250
|
return { before, value, after, lang };
|
|
987
1251
|
};
|
|
988
1252
|
}
|
|
1253
|
+
/** The tracing paper's grip is named in the labels' language, and its tooltip says it (R87). */
|
|
1254
|
+
#nameGrip(tag = this.getAttribute("lang")) {
|
|
1255
|
+
const { lang, labels } = labelsFor(tag);
|
|
1256
|
+
this.#tracingPaper.label(labels.moveTask, lang);
|
|
1257
|
+
}
|
|
989
1258
|
/**
|
|
990
1259
|
* Takes back the last action on the sheet — the last stroke written, the last erasing, or the
|
|
991
1260
|
* clearing — and says whether there was one (R19, R22), with a `historychange` if there was
|
|
@@ -1027,6 +1296,26 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1027
1296
|
if (this.#example !== undefined)
|
|
1028
1297
|
return null;
|
|
1029
1298
|
const { digit } = this.#settings;
|
|
1299
|
+
const paper = this.#paper;
|
|
1300
|
+
const tracing = this.#tracing;
|
|
1301
|
+
if (paper !== undefined && tracing !== undefined) {
|
|
1302
|
+
// The sheet as it is now: the crop's picture, read by now or soon, does not change it.
|
|
1303
|
+
const strokes = this.#work.strokes.map((stroke) => this.#onSheet(stroke, tracing.at));
|
|
1304
|
+
if (strokes.length === 0)
|
|
1305
|
+
return null;
|
|
1306
|
+
const written = this.#work;
|
|
1307
|
+
const onTracing = (id) => written.onTracing(id);
|
|
1308
|
+
const { size, gray } = await this.#tracingPaper.read;
|
|
1309
|
+
const rect = tracingRect(tracing.at, size);
|
|
1310
|
+
const work = paperWorkForCheck(strokes, { at: tracing.at, onTracing }, tracing, rect, gray, digit);
|
|
1311
|
+
if (work === null)
|
|
1312
|
+
return null;
|
|
1313
|
+
return Object.freeze({
|
|
1314
|
+
statement: paper.statement,
|
|
1315
|
+
png: blobOf(work.png),
|
|
1316
|
+
submission: work.submission,
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1030
1319
|
if (isWorkbook(this.#shownTemplate)) {
|
|
1031
1320
|
const work = cellsForCheck(this.#work.strokes, this.#cells, digit);
|
|
1032
1321
|
if (work === null)
|
|
@@ -1046,7 +1335,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1046
1335
|
* unit, 1000 × 1400, whatever is filled and whether the work was sent to be checked or not; an
|
|
1047
1336
|
* empty surface, and a worked example's, is white. The strokes are those `exportWork()` takes, as
|
|
1048
1337
|
* the history has them: not the stroke under way, but those under an eraser not yet lifted. Each
|
|
1049
|
-
* is the outline the screen fills, black on white with no theme and no template
|
|
1338
|
+
* is the outline the screen fills, black on white with no theme and no template — on a task from
|
|
1339
|
+
* paper the strokes of both layers, the tracing paper's where it lies, hidden or not, with the
|
|
1340
|
+
* crop's picture laid in grey and multiplied (R87) —, and the pixels
|
|
1050
1341
|
* go through the crop's encoder, grey a byte each: so the picture looks as the sheet does, and
|
|
1051
1342
|
* holds at most 1,576,640 bytes, for the encoder spends at most 9 bits on a byte of a row, under
|
|
1052
1343
|
* the 4 MiB of a stored work.
|
|
@@ -1063,8 +1354,19 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1063
1354
|
context.fillStyle = "#fff";
|
|
1064
1355
|
context.fillRect(0, 0, width, height);
|
|
1065
1356
|
context.fillStyle = "#000";
|
|
1066
|
-
|
|
1067
|
-
|
|
1357
|
+
const tracing = this.#tracing;
|
|
1358
|
+
const strokes = this.#work.strokes;
|
|
1359
|
+
const work = this.#work;
|
|
1360
|
+
for (const stroke of strokes) {
|
|
1361
|
+
if (tracing !== undefined && work.onTracing(stroke.id)) {
|
|
1362
|
+
context.setTransform(1, 0, 0, 1, tracing.at[0], tracing.at[1]);
|
|
1363
|
+
context.fill(this.#finished(stroke).path);
|
|
1364
|
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
1365
|
+
}
|
|
1366
|
+
else {
|
|
1367
|
+
context.fill(this.#finished(stroke).path);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1068
1370
|
const { data } = context.getImageData(0, 0, width, height);
|
|
1069
1371
|
// The canvas's memory goes at once, not at a collection: WebKit caps what all canvases hold.
|
|
1070
1372
|
canvas.width = 0;
|
|
@@ -1074,6 +1376,20 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1074
1376
|
const gray = new Uint8Array(width * height);
|
|
1075
1377
|
for (let at = 0; at < gray.length; at++)
|
|
1076
1378
|
gray[at] = data[4 * at];
|
|
1379
|
+
// A task from paper's crop lies over it where the tracing paper lies, shown or hidden, a pixel a
|
|
1380
|
+
// unit, as the crop of the work has it (R87).
|
|
1381
|
+
if (tracing !== undefined) {
|
|
1382
|
+
const { size, gray: picture } = await this.#tracingPaper.read;
|
|
1383
|
+
if (picture !== null) {
|
|
1384
|
+
const rect = tracingRect(tracing.at, size);
|
|
1385
|
+
layOver(gray, width, picture, {
|
|
1386
|
+
left: rect.minX,
|
|
1387
|
+
top: rect.minY,
|
|
1388
|
+
right: rect.maxX,
|
|
1389
|
+
bottom: rect.maxY,
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1077
1393
|
return blobOf(encodePNG(gray, width, height));
|
|
1078
1394
|
}
|
|
1079
1395
|
disconnectedCallback() {
|
|
@@ -1097,6 +1413,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1097
1413
|
// A stroke that ended here may be the one the other rows of the page waited for.
|
|
1098
1414
|
PEN_MODE.settle();
|
|
1099
1415
|
this.#pictures.disconnect();
|
|
1416
|
+
this.#tracingPaper.disconnect();
|
|
1100
1417
|
this.#resize.disconnect();
|
|
1101
1418
|
this.#unwatchDensity?.();
|
|
1102
1419
|
this.#unwatchDensity = undefined;
|
|
@@ -1160,6 +1477,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1160
1477
|
* would land on a sheet that takes none. A TypeError says which.
|
|
1161
1478
|
*/
|
|
1162
1479
|
#mayShowExample() {
|
|
1480
|
+
if (this.#paper !== undefined) {
|
|
1481
|
+
throw new TypeError(`<${TAG}> shows a task from paper: a worked example is shown by a new element, or once the paper is undefined`);
|
|
1482
|
+
}
|
|
1163
1483
|
if (this.#task !== undefined) {
|
|
1164
1484
|
throw new TypeError(`<${TAG}> shows a task: a worked example is shown by a new element, or once the task is undefined`);
|
|
1165
1485
|
}
|
|
@@ -1269,7 +1589,12 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1269
1589
|
}
|
|
1270
1590
|
/** Every stroke the sheet has known, erased or not, as its history wrote them. */
|
|
1271
1591
|
#known() {
|
|
1272
|
-
|
|
1592
|
+
const tracing = this.#tracing;
|
|
1593
|
+
return this.#work.history.flatMap((event) => {
|
|
1594
|
+
if (event.kind !== "write")
|
|
1595
|
+
return [];
|
|
1596
|
+
return [tracing === undefined ? event.stroke : this.#onSheet(event.stroke, tracing.at)];
|
|
1597
|
+
});
|
|
1273
1598
|
}
|
|
1274
1599
|
/**
|
|
1275
1600
|
* The marks of a submission, as `marksOf` checked them, that have a place on this sheet, each
|
|
@@ -1277,7 +1602,12 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1277
1602
|
*/
|
|
1278
1603
|
#places(given) {
|
|
1279
1604
|
const cells = isWorkbook(this.#shownTemplate) ? this.#cells : null;
|
|
1280
|
-
const
|
|
1605
|
+
const tracing = this.#tracing;
|
|
1606
|
+
const work = this.#work;
|
|
1607
|
+
const paper = tracing === undefined
|
|
1608
|
+
? undefined
|
|
1609
|
+
: { at: tracing.at, onTracing: (id) => work.onTracing(id) };
|
|
1610
|
+
const places = placesOf(given, this.#known(), cells, undefined, paper);
|
|
1281
1611
|
return given.marks.flatMap(({ name }, n) => {
|
|
1282
1612
|
const placed = places[n];
|
|
1283
1613
|
return placed ? [{ name, placed }] : [];
|
|
@@ -1294,20 +1624,27 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1294
1624
|
return (shown !== undefined &&
|
|
1295
1625
|
shown.name === name &&
|
|
1296
1626
|
sameItems(shown.placed.ids, placed.ids) &&
|
|
1297
|
-
sameBox(shown.placed.cell ?? null, placed.cell ?? null)
|
|
1627
|
+
sameBox(shown.placed.cell ?? null, placed.cell ?? null) &&
|
|
1628
|
+
shown.placed.correct === placed.correct &&
|
|
1629
|
+
shown.placed.wavy === placed.wavy &&
|
|
1630
|
+
(placed.correct === undefined || sameBox(shown.placed.box, placed.box)) &&
|
|
1631
|
+
(!placed.wavy || sameBox(shown.placed.box, placed.box)));
|
|
1298
1632
|
});
|
|
1299
1633
|
if (same)
|
|
1300
1634
|
return;
|
|
1301
|
-
for (const {
|
|
1302
|
-
|
|
1635
|
+
for (const { node } of this.#shown)
|
|
1636
|
+
node.remove();
|
|
1303
1637
|
this.#shown = marks.map(({ name, placed }) => {
|
|
1304
|
-
const
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1638
|
+
const node = placed.correct !== undefined
|
|
1639
|
+
? verdictNode(placed.correct)
|
|
1640
|
+
: placed.wavy
|
|
1641
|
+
? wavyNode()
|
|
1642
|
+
: frameNode();
|
|
1643
|
+
node.setAttribute("role", "img");
|
|
1644
|
+
node.setAttribute("aria-label", name);
|
|
1645
|
+
node.setAttribute("display", "none");
|
|
1646
|
+
this.#marksLayer.append(node);
|
|
1647
|
+
return { name, placed, node, frame: null, under: false, drawn: "none" };
|
|
1311
1648
|
});
|
|
1312
1649
|
this.#request();
|
|
1313
1650
|
}
|
|
@@ -1336,9 +1673,25 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1336
1673
|
const screen = this.#view();
|
|
1337
1674
|
if (screen === undefined)
|
|
1338
1675
|
return;
|
|
1339
|
-
const
|
|
1676
|
+
const onSheetSample = this.#sample(event, screen);
|
|
1340
1677
|
const erases = this.#shownTemplate.tools.includes("eraser") &&
|
|
1341
|
-
(this.#tool === "eraser" ||
|
|
1678
|
+
(this.#tool === "eraser" || onSheetSample.button === ERASER_BUTTON);
|
|
1679
|
+
// On a task from paper a press begun on the tracing paper in sight writes or erases on it, its
|
|
1680
|
+
// points from its corner; one begun elsewhere, on the sheet (R87).
|
|
1681
|
+
const tracing = this.#tracing;
|
|
1682
|
+
const size = this.#tracingPaper.size;
|
|
1683
|
+
const traced = tracing !== undefined &&
|
|
1684
|
+
!tracing.hidden &&
|
|
1685
|
+
this.#tracingPaper.ready &&
|
|
1686
|
+
onTracing(onSheetSample, tracing.at, size);
|
|
1687
|
+
const layer = traced ? "tracing" : undefined;
|
|
1688
|
+
if (erases)
|
|
1689
|
+
this.#eraseOn = tracing === undefined ? undefined : (layer ?? "sheet");
|
|
1690
|
+
else
|
|
1691
|
+
this.#writeOn = layer;
|
|
1692
|
+
const sample = traced && tracing !== undefined
|
|
1693
|
+
? { ...onSheetSample, ...toTracing(onSheetSample, tracing.at, size) }
|
|
1694
|
+
: onSheetSample;
|
|
1342
1695
|
const began = erases
|
|
1343
1696
|
? this.#eraser.down(sample, ERASER_RADIUS_PX / screen.k + INK_REACH)
|
|
1344
1697
|
: this.#recorder.down(sample);
|
|
@@ -1376,7 +1729,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1376
1729
|
}
|
|
1377
1730
|
if (screen === undefined)
|
|
1378
1731
|
continue;
|
|
1379
|
-
const sample = this.#sample(each, screen);
|
|
1732
|
+
const sample = this.#sample(each, screen, this.#layerOf(input));
|
|
1380
1733
|
const moved = input === this.#eraser ? input.move(sample, inSight(each, screen)) : input.move(sample);
|
|
1381
1734
|
changed = moved || changed;
|
|
1382
1735
|
}
|
|
@@ -1388,12 +1741,13 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1388
1741
|
const input = this.#inputOf(event.pointerId);
|
|
1389
1742
|
if (input !== undefined) {
|
|
1390
1743
|
const screen = this.#view();
|
|
1744
|
+
const layer = this.#layerOf(input);
|
|
1391
1745
|
if (screen === undefined)
|
|
1392
1746
|
input.end(event.pointerId);
|
|
1393
1747
|
else if (input === this.#eraser)
|
|
1394
|
-
input.up(this.#sample(event, screen), inSight(event, screen));
|
|
1748
|
+
input.up(this.#sample(event, screen, layer), inSight(event, screen));
|
|
1395
1749
|
else
|
|
1396
|
-
input.up(this.#sample(event, screen));
|
|
1750
|
+
input.up(this.#sample(event, screen, layer));
|
|
1397
1751
|
}
|
|
1398
1752
|
this.#lifted(event.pointerId);
|
|
1399
1753
|
}
|
|
@@ -1428,8 +1782,21 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1428
1782
|
return this.#eraser;
|
|
1429
1783
|
return undefined;
|
|
1430
1784
|
}
|
|
1431
|
-
|
|
1432
|
-
|
|
1785
|
+
/** The layer a pointer's stroke or erasing is on: the tracing paper's, or the sheet's (R87). */
|
|
1786
|
+
#layerOf(input) {
|
|
1787
|
+
const on = input === this.#eraser ? this.#eraseOn : this.#writeOn;
|
|
1788
|
+
return on === "tracing" ? "tracing" : undefined;
|
|
1789
|
+
}
|
|
1790
|
+
/**
|
|
1791
|
+
* A pointer's place on the sheet, or on the tracing paper, `layer`, from its corner and kept to
|
|
1792
|
+
* it (R87).
|
|
1793
|
+
*/
|
|
1794
|
+
#sample(event, screen, layer) {
|
|
1795
|
+
const point = toSheet(screen, event.clientX - screen.left, event.clientY - screen.top);
|
|
1796
|
+
const tracing = this.#tracing;
|
|
1797
|
+
const { x, y } = layer === "tracing" && tracing !== undefined
|
|
1798
|
+
? toTracing(point, tracing.at, this.#tracingPaper.size)
|
|
1799
|
+
: point;
|
|
1433
1800
|
return {
|
|
1434
1801
|
pointerId: event.pointerId,
|
|
1435
1802
|
kind: event.pointerType,
|
|
@@ -1760,6 +2127,106 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1760
2127
|
const detail = Object.freeze({ tool });
|
|
1761
2128
|
this.dispatchEvent(new CustomEvent(TOOL_CHANGE, { bubbles: true, composed: true, detail }));
|
|
1762
2129
|
}
|
|
2130
|
+
/**
|
|
2131
|
+
* The learner hid the tracing paper with the button atop the tools, or showed it again (R87): its
|
|
2132
|
+
* strokes go out of sight with it and stay in the history and in the work, and whoever listens
|
|
2133
|
+
* hears of the new state. A stroke or an erasing under way on the paper ends where it is, as on a
|
|
2134
|
+
* lost capture: the pointer goes on over a paper that is not there.
|
|
2135
|
+
*/
|
|
2136
|
+
#trace() {
|
|
2137
|
+
if (this.#tracing?.hidden === false)
|
|
2138
|
+
this.#leaveTracing(true);
|
|
2139
|
+
const state = this.#tracing;
|
|
2140
|
+
if (this.#paper === undefined || state === undefined)
|
|
2141
|
+
return;
|
|
2142
|
+
this.#tracing = Object.freeze({ at: state.at, hidden: !state.hidden });
|
|
2143
|
+
this.#refresh();
|
|
2144
|
+
this.#request();
|
|
2145
|
+
this.#tracingChanged();
|
|
2146
|
+
}
|
|
2147
|
+
/**
|
|
2148
|
+
* The learner's grip moves the tracing paper to a place, kept to the sheet whole (R87): its
|
|
2149
|
+
* strokes move with it, and the marks on them too. The move tells of itself once it is over.
|
|
2150
|
+
*/
|
|
2151
|
+
#moveTracing(to) {
|
|
2152
|
+
const state = this.#tracing;
|
|
2153
|
+
if (state === undefined)
|
|
2154
|
+
return to;
|
|
2155
|
+
this.#moveFrom ??= state;
|
|
2156
|
+
const at = keptOn(to, this.#tracingPaper.size);
|
|
2157
|
+
if (at[0] === state.at[0] && at[1] === state.at[1])
|
|
2158
|
+
return state.at;
|
|
2159
|
+
this.#tracing = Object.freeze({ at, hidden: state.hidden });
|
|
2160
|
+
this.#request();
|
|
2161
|
+
return at;
|
|
2162
|
+
}
|
|
2163
|
+
/** The learner's move of the tracing paper is over: whoever listens hears of it, if it moved. */
|
|
2164
|
+
#tracingMoved() {
|
|
2165
|
+
const from = this.#moveFrom;
|
|
2166
|
+
this.#moveFrom = undefined;
|
|
2167
|
+
const state = this.#tracing;
|
|
2168
|
+
if (from === undefined || state === undefined || sameTracing(from, state))
|
|
2169
|
+
return;
|
|
2170
|
+
// The marks were placed by the strokes where they lay: a submission of the paper elsewhere is
|
|
2171
|
+
// not this sheet's any more, and they go (R26).
|
|
2172
|
+
this.#replace();
|
|
2173
|
+
this.#tracingChanged();
|
|
2174
|
+
}
|
|
2175
|
+
/**
|
|
2176
|
+
* A stroke or an erasing under way on the tracing paper ends where it is, `end`, or goes, as on
|
|
2177
|
+
* `pointercancel`: the paper it was on goes out of sight or away (R87).
|
|
2178
|
+
*/
|
|
2179
|
+
#leaveTracing(end) {
|
|
2180
|
+
const writer = this.#writeOn === "tracing" ? this.#recorder.pointer : undefined;
|
|
2181
|
+
const eraser = this.#eraseOn === "tracing" ? this.#eraser.pointer : undefined;
|
|
2182
|
+
// The recorder's handler writes the stroke on the layer it began on, so the layer goes after.
|
|
2183
|
+
if (writer !== undefined && !end && this.#recorder.cancel(writer))
|
|
2184
|
+
this.#request();
|
|
2185
|
+
if (writer !== undefined && end)
|
|
2186
|
+
this.#recorder.end(writer);
|
|
2187
|
+
if (eraser !== undefined && !end && this.#eraser.cancel(eraser))
|
|
2188
|
+
this.#request();
|
|
2189
|
+
if (eraser !== undefined && end)
|
|
2190
|
+
this.#eraser.end(eraser);
|
|
2191
|
+
if (writer !== undefined)
|
|
2192
|
+
this.#writeOn = undefined;
|
|
2193
|
+
if (eraser !== undefined)
|
|
2194
|
+
this.#eraseOn = undefined;
|
|
2195
|
+
}
|
|
2196
|
+
/**
|
|
2197
|
+
* The crop's picture is read, and the tracing paper has its size (R87): a state the consumer gave,
|
|
2198
|
+
* checked against the largest paper, that puts the paper of this size wholly off the sheet moves
|
|
2199
|
+
* to the nearest place wholly on it, with its strokes and their marks. It tells nothing, as the
|
|
2200
|
+
* state given did not.
|
|
2201
|
+
*/
|
|
2202
|
+
#sized() {
|
|
2203
|
+
const state = this.#tracing;
|
|
2204
|
+
const size = this.#tracingPaper.size;
|
|
2205
|
+
if (state !== undefined && whollyOff(state.at, size)) {
|
|
2206
|
+
this.#tracing = Object.freeze({ at: keptOn(state.at, size), hidden: state.hidden });
|
|
2207
|
+
this.#replace();
|
|
2208
|
+
}
|
|
2209
|
+
this.#request();
|
|
2210
|
+
}
|
|
2211
|
+
/** Whoever listens hears of the tracing paper's new state, which the learner chose (R87). */
|
|
2212
|
+
#tracingChanged() {
|
|
2213
|
+
const detail = this.#tracing;
|
|
2214
|
+
this.dispatchEvent(new CustomEvent(TRACING_CHANGE, { bubbles: true, composed: true, detail }));
|
|
2215
|
+
}
|
|
2216
|
+
/**
|
|
2217
|
+
* A stroke where it lies on the sheet: one of the tracing paper's moved to where the paper lies,
|
|
2218
|
+
* once for each place (R87); the sheet's as it is.
|
|
2219
|
+
*/
|
|
2220
|
+
#onSheet(stroke, at) {
|
|
2221
|
+
if (!this.#work.onTracing(stroke.id))
|
|
2222
|
+
return stroke;
|
|
2223
|
+
const kept = this.#placed.get(stroke);
|
|
2224
|
+
if (kept !== undefined && kept.at[0] === at[0] && kept.at[1] === at[1])
|
|
2225
|
+
return kept.stroke;
|
|
2226
|
+
const placed = onSheet(stroke, at);
|
|
2227
|
+
this.#placed.set(stroke, { at, stroke: placed });
|
|
2228
|
+
return placed;
|
|
2229
|
+
}
|
|
1763
2230
|
/**
|
|
1764
2231
|
* The learner opened the next part of the demo with the row's Next (R42). The list grows above
|
|
1765
2232
|
* the sheet, and the layout keeps a sheet scrolled into sight where it was at once, before the
|
|
@@ -1916,6 +2383,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1916
2383
|
finger: this.#finger,
|
|
1917
2384
|
opens: this.#nextPart() !== undefined,
|
|
1918
2385
|
expanded: demo?.hidden !== true,
|
|
2386
|
+
traced: this.#tracing?.hidden !== true,
|
|
1919
2387
|
});
|
|
1920
2388
|
this.#nameRegion();
|
|
1921
2389
|
}
|
|
@@ -1963,6 +2431,8 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1963
2431
|
#rowKind() {
|
|
1964
2432
|
if (this.#example !== undefined)
|
|
1965
2433
|
return "example";
|
|
2434
|
+
if (this.#paper !== undefined)
|
|
2435
|
+
return "paper";
|
|
1966
2436
|
return isWorkbook(this.#shownTemplate) ? "workbook" : "sheet";
|
|
1967
2437
|
}
|
|
1968
2438
|
#request() {
|
|
@@ -1984,10 +2454,18 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1984
2454
|
if (!(height > 0))
|
|
1985
2455
|
return;
|
|
1986
2456
|
const viewBox = `${screen.vx} ${screen.vy} ${width} ${height}`;
|
|
2457
|
+
// The tracing paper and its grip follow the sheet, and the paper's place and state.
|
|
2458
|
+
this.#tracingPaper.place(this.#tracing, screen);
|
|
1987
2459
|
if (viewBox === this.#viewBox)
|
|
1988
2460
|
return;
|
|
1989
2461
|
this.#viewBox = viewBox;
|
|
1990
|
-
for (const layer of [
|
|
2462
|
+
for (const layer of [
|
|
2463
|
+
this.#template,
|
|
2464
|
+
this.#show,
|
|
2465
|
+
this.#tracingPaper.layer,
|
|
2466
|
+
this.#sketch,
|
|
2467
|
+
this.#marksLayer,
|
|
2468
|
+
]) {
|
|
1991
2469
|
layer.setAttribute("viewBox", viewBox);
|
|
1992
2470
|
}
|
|
1993
2471
|
}
|
|
@@ -2003,25 +2481,39 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
2003
2481
|
return;
|
|
2004
2482
|
const byId = this.#byId();
|
|
2005
2483
|
const pending = this.#eraser.pending;
|
|
2484
|
+
const tracing = this.#tracing;
|
|
2485
|
+
const work = this.#work;
|
|
2486
|
+
// A stroke of the tracing paper is where the paper lies, and out of sight while it is hidden
|
|
2487
|
+
// (R87). Its box moves with the paper, and the stroke is not copied for each place of a drag.
|
|
2006
2488
|
const onScreen = (id) => {
|
|
2007
2489
|
const stroke = pending.has(id) ? undefined : byId.get(id);
|
|
2008
|
-
|
|
2490
|
+
if (stroke === undefined)
|
|
2491
|
+
return undefined;
|
|
2492
|
+
if (tracing === undefined || !work.onTracing(id))
|
|
2493
|
+
return this.#pointBoxOf(stroke);
|
|
2494
|
+
return tracing.hidden ? undefined : boxOnSheet(this.#pointBoxOf(stroke), tracing.at);
|
|
2009
2495
|
};
|
|
2010
2496
|
const half = 1 / screen.k;
|
|
2011
2497
|
for (const mark of this.#shown) {
|
|
2012
2498
|
const shown = shownOf(mark.placed, onScreen);
|
|
2013
|
-
const frame = shown &&
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2499
|
+
const frame = shown && mark.placed.correct !== undefined
|
|
2500
|
+
? verdictAt(shown, screen.k)
|
|
2501
|
+
: shown && mark.placed.wavy
|
|
2502
|
+
? wavyAt(shown, half)
|
|
2503
|
+
: shown && {
|
|
2504
|
+
minX: Math.max(shown.minX - MARK_PADDING, half),
|
|
2505
|
+
minY: Math.max(shown.minY - MARK_PADDING, half),
|
|
2506
|
+
maxX: Math.min(shown.maxX + MARK_PADDING, SHEET.width - half),
|
|
2507
|
+
maxY: Math.min(shown.maxY + MARK_PADDING, SHEET.height - half),
|
|
2508
|
+
};
|
|
2019
2509
|
if (this.#writing === undefined || frame === null)
|
|
2020
2510
|
mark.under = false;
|
|
2021
2511
|
else if (!mark.under) {
|
|
2022
2512
|
// A frame that came or moved while the stroke was under way is checked against all of it.
|
|
2023
2513
|
const points = sameBox(frame, mark.frame) ? fresh : this.#writing.stroke.points;
|
|
2024
|
-
|
|
2514
|
+
// A stroke on the tracing paper holds its points from the paper's corner (R87).
|
|
2515
|
+
const [dx, dy] = this.#writeOn === "tracing" ? (this.#tracing?.at ?? [0, 0]) : [0, 0];
|
|
2516
|
+
mark.under = points.some((point) => near({ ...point, x: point.x + dx, y: point.y + dy }, frame, INK_REACH + half));
|
|
2025
2517
|
}
|
|
2026
2518
|
mark.frame = frame;
|
|
2027
2519
|
const drawn = frame === null
|
|
@@ -2030,17 +2522,28 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
2030
2522
|
if (drawn === mark.drawn)
|
|
2031
2523
|
continue;
|
|
2032
2524
|
mark.drawn = drawn;
|
|
2033
|
-
const {
|
|
2525
|
+
const { node } = mark;
|
|
2034
2526
|
if (frame === null) {
|
|
2035
|
-
|
|
2527
|
+
node.setAttribute("display", "none");
|
|
2036
2528
|
continue;
|
|
2037
2529
|
}
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2530
|
+
if (node instanceof SVGRectElement) {
|
|
2531
|
+
node.setAttribute("x", String(frame.minX));
|
|
2532
|
+
node.setAttribute("y", String(frame.minY));
|
|
2533
|
+
node.setAttribute("width", String(frame.maxX - frame.minX));
|
|
2534
|
+
node.setAttribute("height", String(frame.maxY - frame.minY));
|
|
2535
|
+
}
|
|
2536
|
+
else if (mark.placed.wavy) {
|
|
2537
|
+
const d = wavyPath(frame);
|
|
2538
|
+
for (const path of node.children)
|
|
2539
|
+
path.setAttribute("d", d);
|
|
2540
|
+
}
|
|
2541
|
+
else {
|
|
2542
|
+
const scale = (frame.maxX - frame.minX) / 24;
|
|
2543
|
+
node.setAttribute("transform", `translate(${frame.minX} ${frame.minY}) scale(${scale})`);
|
|
2544
|
+
}
|
|
2545
|
+
node.classList.toggle("under", mark.under);
|
|
2546
|
+
node.removeAttribute("display");
|
|
2044
2547
|
}
|
|
2045
2548
|
}
|
|
2046
2549
|
/**
|
|
@@ -2065,10 +2568,10 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
2065
2568
|
/** The strokes on the sheet by ID: each action on the work gives a new array of them (R19). */
|
|
2066
2569
|
#byId() {
|
|
2067
2570
|
const strokes = this.#work.strokes;
|
|
2068
|
-
if (this.#
|
|
2069
|
-
this.#
|
|
2571
|
+
if (this.#strokesById.strokes !== strokes) {
|
|
2572
|
+
this.#strokesById = { strokes, byId: new Map(strokes.map((stroke) => [stroke.id, stroke])) };
|
|
2070
2573
|
}
|
|
2071
|
-
return this.#
|
|
2574
|
+
return this.#strokesById.byId;
|
|
2072
2575
|
}
|
|
2073
2576
|
#pointBoxOf(stroke) {
|
|
2074
2577
|
let box = this.#pointBoxes.get(stroke);
|
|
@@ -2103,26 +2606,47 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
2103
2606
|
const down = (height / screen.height) * screen.k;
|
|
2104
2607
|
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
2105
2608
|
context.clearRect(0, 0, width, height);
|
|
2106
|
-
context.setTransform(across, 0, 0, down, -screen.vx * across, -screen.vy * down);
|
|
2107
2609
|
context.fillStyle = colour;
|
|
2108
2610
|
const right = screen.vx + screen.width / screen.k;
|
|
2109
2611
|
const bottom = screen.vy + screen.height / screen.k;
|
|
2110
2612
|
const erasing = this.#eraser.pending;
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2613
|
+
// A task from paper's tracing paper: its strokes over the sheet's, moved to where it lies, and
|
|
2614
|
+
// none while it is hidden (R87).
|
|
2615
|
+
const tracing = this.#tracing;
|
|
2616
|
+
const work = this.#work;
|
|
2617
|
+
const shown = tracing !== undefined && !tracing.hidden;
|
|
2618
|
+
const draw = (stroke, dx, dy) => {
|
|
2114
2619
|
// The outline keeps within half the widest line of the points: twice that is out of sight.
|
|
2115
2620
|
const box = this.#pointBoxOf(stroke);
|
|
2116
|
-
if (box.maxY + 2 * INK_REACH < screen.vy || box.minY - 2 * INK_REACH > bottom)
|
|
2117
|
-
|
|
2118
|
-
if (box.maxX + 2 * INK_REACH < screen.vx || box.minX - 2 * INK_REACH > right)
|
|
2119
|
-
|
|
2621
|
+
if (box.maxY + dy + 2 * INK_REACH < screen.vy || box.minY + dy - 2 * INK_REACH > bottom)
|
|
2622
|
+
return;
|
|
2623
|
+
if (box.maxX + dx + 2 * INK_REACH < screen.vx || box.minX + dx - 2 * INK_REACH > right)
|
|
2624
|
+
return;
|
|
2120
2625
|
const ink = this.#finished(stroke);
|
|
2121
|
-
if (ink.bottom >= screen.vy && ink.top <= bottom)
|
|
2626
|
+
if (ink.bottom + dy >= screen.vy && ink.top + dy <= bottom)
|
|
2122
2627
|
context.fill(ink.path);
|
|
2628
|
+
};
|
|
2629
|
+
context.setTransform(across, 0, 0, down, -screen.vx * across, -screen.vy * down);
|
|
2630
|
+
const traced = [];
|
|
2631
|
+
for (const stroke of work.strokes) {
|
|
2632
|
+
if (erasing.has(stroke.id))
|
|
2633
|
+
continue;
|
|
2634
|
+
if (tracing === undefined || !work.onTracing(stroke.id))
|
|
2635
|
+
draw(stroke, 0, 0);
|
|
2636
|
+
else if (shown)
|
|
2637
|
+
traced.push(stroke);
|
|
2123
2638
|
}
|
|
2124
2639
|
const live = this.#recorder.live;
|
|
2125
|
-
|
|
2640
|
+
const liveTraced = this.#writeOn === "tracing";
|
|
2641
|
+
if (live !== undefined && !liveTraced)
|
|
2642
|
+
context.fill(pathOf(this.#outline(live, false)));
|
|
2643
|
+
if (!shown)
|
|
2644
|
+
return;
|
|
2645
|
+
const [dx, dy] = tracing.at;
|
|
2646
|
+
context.setTransform(across, 0, 0, down, (dx - screen.vx) * across, (dy - screen.vy) * down);
|
|
2647
|
+
for (const stroke of traced)
|
|
2648
|
+
draw(stroke, dx, dy);
|
|
2649
|
+
if (live !== undefined && liveTraced)
|
|
2126
2650
|
context.fill(pathOf(this.#outline(live, false)));
|
|
2127
2651
|
}
|
|
2128
2652
|
#finished(stroke) {
|