@altnovatrail/canvas 0.3.41 → 0.3.42

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.
@@ -77,6 +77,11 @@ export interface HandsSheet {
77
77
  writer(): number | undefined;
78
78
  /** The stroke or the erasing of a pointer goes, as on `pointercancel`. */
79
79
  drop(pointerId: number): void;
80
+ /**
81
+ * The stroke or the erasing of a pointer ends where it was, its `pointerup` never to come: as on
82
+ * a lost capture.
83
+ */
84
+ end(pointerId: number): void;
80
85
  /**
81
86
  * Two fingers moved right on the screen by `dx` CSS pixels and down it by `dy`, or left and up
82
87
  * for negative ones: the sheet goes with them. `fresh` marks a gesture's first step.
@@ -109,6 +114,8 @@ export type Role = "writes" | "waits" | "scrolls" | "palm" | "idle";
109
114
  * - One pointer writes at a time, and the sheet's recorder and eraser say which.
110
115
  * - A pen's touch puts the page in pen mode and takes away the stroke of a touch under way: it was
111
116
  * a palm. While a pen or a mouse writes, a touch does nothing until it lifts.
117
+ * - A pen's touch while another pen is down ends what that one writes or erases where it was, and
118
+ * the new pen writes: a screen has one pen, and the other lost its way up.
112
119
  * - A touch wider or taller than `PALM_PX`, at its `pointerdown` or at any move, is a palm: it does
113
120
  * nothing until it lifts, and what it was writing goes.
114
121
  * - Without pen mode the first finger writes. A second that comes down within `PAIR_MS`, before the
package/lib/core/hands.js CHANGED
@@ -85,6 +85,8 @@ export class PenMode {
85
85
  * - One pointer writes at a time, and the sheet's recorder and eraser say which.
86
86
  * - A pen's touch puts the page in pen mode and takes away the stroke of a touch under way: it was
87
87
  * a palm. While a pen or a mouse writes, a touch does nothing until it lifts.
88
+ * - A pen's touch while another pen is down ends what that one writes or erases where it was, and
89
+ * the new pen writes: a screen has one pen, and the other lost its way up.
88
90
  * - A touch wider or taller than `PALM_PX`, at its `pointerdown` or at any move, is a palm: it does
89
91
  * nothing until it lifts, and what it was writing goes.
90
92
  * - Without pen mode the first finger writes. A second that comes down within `PAIR_MS`, before the
@@ -191,6 +193,16 @@ export class Hands {
191
193
  }
192
194
  #pen(contact) {
193
195
  this.#mode.on = true;
196
+ // A pen still down when a pen comes lost its way up: the system took its touch and told the
197
+ // page nothing, as an iPad's selection did (measurements 1.8). What it wrote ends where it
198
+ // was, or the sheet would wait for its lift for good.
199
+ for (const [id, hand] of this.#hands) {
200
+ if (hand.kind !== "pen")
201
+ continue;
202
+ if (this.#sheet.writer() === id)
203
+ this.#sheet.end(id);
204
+ this.#hands.delete(id);
205
+ }
194
206
  // A touch writing when a pen comes was a palm, and its stroke goes. A gesture under way ends,
195
207
  // and every touch down now does nothing until it lifts.
196
208
  const writer = this.#sheet.writer();
@@ -5,10 +5,18 @@ import type { Stroke } from "./ink.js";
5
5
  * of 3 px on its sheet 1000 wide.
6
6
  */
7
7
  export declare const INK_WIDTH = 3;
8
+ /** A pen's line at no pressure, and at full pressure, in widths of a finger's line. */
9
+ export declare const PEN_THINNEST = 0.5;
10
+ export declare const PEN_WIDEST = 2;
11
+ /**
12
+ * A pen's line at a pressure, in widths of a finger's line: `PEN_THINNEST` at none, `PEN_WIDEST`
13
+ * at full, and between by the pressure to the power `PEN_POWER`. A pressure past 0–1 is taken as
14
+ * the nearer end.
15
+ */
16
+ export declare function penWidth(pressure: number): number;
8
17
  /**
9
18
  * How far the ink reaches from a stroke's points, in sheet units, at most: half the widest line, a
10
- * pen's at full pressure. perfect-freehand's radius at a point is `size × (0.5 − thinning × (0.5 −
11
- * pressure))`, so 2.25 units with a pressure of 1.
19
+ * pen's at full pressure.
12
20
  */
13
21
  export declare const INK_REACH: number;
14
22
  /** Whether a stroke's line widens with its pressure on the screen: only a pen's does. */
@@ -4,28 +4,52 @@ 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
- /** How much a pen's pressure changes the width: from half of it to one and a half. */
8
- const PEN_THINNING = 0.5;
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. perfect-freehand's radius at a point is `size × (0.5 − thinning × (0.5 −
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 * (0.5 + PEN_THINNING / 2);
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";
@@ -37,14 +61,25 @@ export function followsPressure(stroke) {
37
61
  * then taken as it is, rather than eased toward.
38
62
  */
39
63
  export function outline(stroke, complete) {
40
- return polygon(stroke, followsPressure(stroke) ? PEN_THINNING : 0, complete);
64
+ return polygon(stroke, followsPressure(stroke) ? PEN_THINNING : EVEN, complete);
41
65
  }
42
66
  function polygon(stroke, thinning, complete) {
43
- const points = stroke.points.map(({ x, y, p }) => [x, y, p]);
67
+ // Each point's pressure leans on the point's before it: the line widens and narrows smoothly.
68
+ let eased;
69
+ const points = stroke.points.map(({ x, y, p }) => {
70
+ eased = eased === undefined ? p : (1 - PEN_FOLLOW) * eased + PEN_FOLLOW * p;
71
+ return [x, y, eased];
72
+ });
44
73
  // One point alone perfect-freehand pairs with another a unit off and draws as an oval. The same
45
74
  // point twice it draws as a round dot, as wide as the line.
46
- const [only] = points;
75
+ const [only, end] = points;
47
76
  if (points.length === 1 && only !== undefined)
48
77
  points.push(only);
78
+ // Two points it takes as the first and four of its own toward the second, of a pressure of 0.5
79
+ // whatever the two had: three are put between them here, at the pressures between.
80
+ if (points.length === 2 && only !== undefined && end !== undefined) {
81
+ const between = [1, 2, 3].map((quarter) => only.map((value, i) => value + ((end[i] ?? value) - value) * (quarter / 4)));
82
+ points.splice(1, 0, ...between);
83
+ }
49
84
  return getStroke(points, { ...OPTIONS, thinning, last: complete });
50
85
  }
@@ -193,6 +193,11 @@ const ERASER_RADIUS_PX = 10;
193
193
  // writing and the buttons' pointer, though not their keys. A worked example's canvas
194
194
  // takes no pointer at all: a finger or a pen on its sheet scrolls, and zooms, as the browser does
195
195
  // on the statement, and a long press or a right click opens no menu over the sheet all the same.
196
+ // No text of the element is selected, nor does a long press open a menu of the system over it: on
197
+ // an iPad a pen's double tap or long press, the way a dot or an equals sign is written, would
198
+ // select the nearest text, a button's label out of sight among it, and the system's menu of the
199
+ // selection would come up over the sheet (measurements 1.8). A screen reader reads what it read
200
+ // before.
196
201
  // The theme is the consumer's, by custom properties that the shadow tree inherits; with
197
202
  // forced colours the system's colours stand in for them. The element is horizontal whatever writing
198
203
  // mode the page is in: a sheet has no other.
@@ -205,7 +210,10 @@ const ERASER_RADIUS_PX = 10;
205
210
  // is three pixels across at the fit and above it, and smaller by the zoom below it, so that a sheet
206
211
  // zoomed out is no field of dots.
207
212
  const STYLE = `
208
- :host { display: flex; flex-direction: column; isolation: isolate; writing-mode: horizontal-tb; }
213
+ :host {
214
+ display: flex; flex-direction: column; isolation: isolate; writing-mode: horizontal-tb;
215
+ user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
216
+ }
209
217
  :host([hidden]) { display: none; }
210
218
  .frame {
211
219
  position: relative; flex: 1 1 auto; min-height: 0; display: grid;
@@ -342,10 +350,7 @@ const STYLE = `
342
350
  position: relative; aspect-ratio: ${SHEET.width} / ${SHEET.height};
343
351
  background: var(--novatrail-canvas-paper, ${PAPER});
344
352
  }
345
- .stage {
346
- position: sticky; top: 0; left: 0; max-height: 100%; overflow: clip;
347
- user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
348
- }
353
+ .stage { position: sticky; top: 0; left: 0; max-height: 100%; overflow: clip; }
349
354
  .spot { position: absolute; visibility: hidden; }
350
355
  .stage > * { position: absolute; inset: 0; width: 100%; height: 100%; }
351
356
  .stage > :not(canvas) { pointer-events: none; }
@@ -356,6 +361,7 @@ const STYLE = `
356
361
  .grid.dots { stroke-width: calc(3px * min(1, var(--zoom, 1))); stroke-linecap: round; }
357
362
  .print {
358
363
  all: initial; font-family: inherit; visibility: inherit; pointer-events: none; direction: ltr;
364
+ user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
359
365
  font-variant-numeric: tabular-nums; font-size: ${LAYOUT.size}px;
360
366
  color: var(--novatrail-canvas-ink, ${INK});
361
367
  }
@@ -763,6 +769,10 @@ export class NovatrailCanvas extends HTMLElement {
763
769
  #hands = new Hands({
764
770
  writer: () => this.#writer(),
765
771
  drop: (pointerId) => this.#drop(pointerId),
772
+ end: (pointerId) => {
773
+ this.#recorder.end(pointerId);
774
+ this.#eraser.end(pointerId);
775
+ },
766
776
  scroll: (dx, dy, fresh) => this.#scroll(dx, dy, fresh),
767
777
  zoom: (ratio, x, y) => this.#pinch(ratio, x, y),
768
778
  across: () => overflowOf(this.#viewport, "x") > EDGE,
@@ -799,7 +809,7 @@ export class NovatrailCanvas extends HTMLElement {
799
809
  */
800
810
  #held;
801
811
  #tool = "pen";
802
- /** The pointer the hand moves the sheet by, and where it was on the screen last. */
812
+ /** The pointer the hand moves the sheet by, of its type, and where it was on the screen last. */
803
813
  #pan;
804
814
  /** A task from paper's grid, the learner's. */
805
815
  #paperGrid = "dots";
@@ -927,6 +937,9 @@ export class NovatrailCanvas extends HTMLElement {
927
937
  // The bar comes first, as the row of buttons before it did: Tab reaches it before the sheet.
928
938
  this.#wrapper.append(this.#tools.rail, this.#viewport, this.#ring);
929
939
  root.append(this.#wrapper);
940
+ // The styles keep every text of the element from a selection; a selection that a script or an
941
+ // engine starts anyway does not start here.
942
+ root.addEventListener("selectstart", (event) => event.preventDefault());
930
943
  this.#lay(NO_TASK);
931
944
  this.#refresh();
932
945
  this.#canvas.addEventListener("pointerdown", (event) => this.#down(event));
@@ -948,6 +961,15 @@ export class NovatrailCanvas extends HTMLElement {
948
961
  // canvas's event comes up to the stage, and so does the stage's own, where a worked example's
949
962
  // sheet takes the pointer instead.
950
963
  this.#stage.addEventListener("contextmenu", (event) => event.preventDefault());
964
+ // A pen's touch on the canvas starts none of the system's gestures: on an iPad a long press or a
965
+ // double tap would select text and open the menu of the selection, and a pen writes by pointer
966
+ // events alone, which come before the touch events. A finger's touch is left as it is.
967
+ for (const type of ["touchstart", "touchmove"]) {
968
+ this.#canvas.addEventListener(type, (event) => {
969
+ if (byStylus(event))
970
+ event.preventDefault();
971
+ }, { passive: false });
972
+ }
951
973
  // A new colour of the ink, from the consumer's theme or the page's colour scheme, ends a
952
974
  // transition of the canvas's colour a millisecond long: the ink is drawn anew in it.
953
975
  this.#canvas.addEventListener("transitionend", () => this.#request());
@@ -1909,9 +1931,17 @@ export class NovatrailCanvas extends HTMLElement {
1909
1931
  // The hand moves the sheet under a pen, a finger or a mouse alike, and writes nothing: one
1910
1932
  // pointer at a time, and the others do nothing while it moves.
1911
1933
  if (this.#tool === HAND) {
1934
+ // A pen that moves the sheet lost its way up when a pen comes down again: a screen has one.
1935
+ if (this.#pan?.kind === "pen" && event.pointerType === "pen")
1936
+ this.#endPan();
1912
1937
  if (this.#pan !== undefined)
1913
1938
  return;
1914
- this.#pan = { pointerId: event.pointerId, x: event.clientX, y: event.clientY };
1939
+ this.#pan = {
1940
+ pointerId: event.pointerId,
1941
+ kind: event.pointerType,
1942
+ x: event.clientX,
1943
+ y: event.clientY,
1944
+ };
1915
1945
  this.#latch = undefined;
1916
1946
  this.#latchAcross = undefined;
1917
1947
  this.#sheet.classList.add("panning");
@@ -1972,7 +2002,7 @@ export class NovatrailCanvas extends HTMLElement {
1972
2002
  const pan = this.#pan;
1973
2003
  if (pan !== undefined && pan.pointerId === event.pointerId) {
1974
2004
  this.#scroll(event.clientX - pan.x, event.clientY - pan.y, false);
1975
- this.#pan = { pointerId: pan.pointerId, x: event.clientX, y: event.clientY };
2005
+ this.#pan = { ...pan, x: event.clientX, y: event.clientY };
1976
2006
  this.#flush();
1977
2007
  return;
1978
2008
  }
@@ -3220,6 +3250,14 @@ function contactOf(event) {
3220
3250
  time: event.timeStamp,
3221
3251
  };
3222
3252
  }
3253
+ /**
3254
+ * Whether a touch event is a pen's: WebKit gives a pen's touch the `touchType` "stylus". A touch
3255
+ * without it is taken for a finger's, and is left as it is.
3256
+ */
3257
+ function byStylus(event) {
3258
+ const touches = [...event.changedTouches];
3259
+ return touches.some((touch) => touch.touchType === "stylus");
3260
+ }
3223
3261
  /** The scroll an event's fingers owe a scroller, in whole pixels, if any: the rest waits. */
3224
3262
  function owed(latch) {
3225
3263
  if (!latch)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altnovatrail/canvas",
3
- "version": "0.3.41",
3
+ "version": "0.3.42",
4
4
  "description": "NovaTrail's canvases: the workplace of a task, where the learner writes, draws and types.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {