millirec 0.1.2 → 0.2.0
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/CHANGELOG.md +65 -0
- package/README.md +171 -58
- package/dist/char-cnn/cnn-web.wasm +0 -0
- package/dist/char-cnn/core.d.ts +3 -1
- package/dist/char-cnn/core.js +145 -99
- package/dist/char-cnn/index.d.ts +1 -1
- package/dist/char-cnn/index.js +198 -136
- package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -1
- package/dist/char-cnn/millirec-char-cnn.min.js +1 -1
- package/dist/char-cnn/options/accurate/tiny.head.bin +0 -0
- package/dist/char-cnn/options/accurate.js +2 -2
- package/dist/char-cnn/options/accurate.json +1 -1
- package/dist/char-cnn/options/all/all-base.head.bin +0 -0
- package/dist/char-cnn/options/all/all-base.q4.bin +0 -0
- package/dist/char-cnn/options/all/all-tiny.bin +0 -0
- package/dist/char-cnn/options/all/all-tiny.head.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-base-p25.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-tiny.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-tiny.head.bin +0 -0
- package/dist/char-cnn/options/all-fast.js +17 -0
- package/dist/char-cnn/options/all-fast.json +15 -0
- package/dist/char-cnn/options/all-small/all-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/all-small/all-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/all-small.js +11 -0
- package/dist/char-cnn/options/all-small.json +9 -0
- package/dist/char-cnn/options/all.js +17 -0
- package/dist/char-cnn/options/all.json +15 -0
- package/dist/char-cnn/options/digits/digits-base.head.bin +0 -0
- package/dist/char-cnn/options/digits/digits-tiny.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-base-p25.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-tiny.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-tiny.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast.js +17 -0
- package/dist/char-cnn/options/digits-fast.json +15 -0
- package/dist/char-cnn/options/digits-small/digits-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/digits-small/digits-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/digits-small.js +11 -0
- package/dist/char-cnn/options/digits-small.json +9 -0
- package/dist/char-cnn/options/digits.js +2 -2
- package/dist/char-cnn/options/digits.json +1 -1
- package/dist/char-cnn/options/fast/base-p25.head.bin +0 -0
- package/dist/char-cnn/options/fast/tiny.head.bin +0 -0
- package/dist/char-cnn/options/fast.js +2 -2
- package/dist/char-cnn/options/fast.json +1 -1
- package/dist/char-cnn/options/lowercase/lowercase-tiny.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast.js +17 -0
- package/dist/char-cnn/options/lowercase-fast.json +15 -0
- package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/lowercase-small.js +11 -0
- package/dist/char-cnn/options/lowercase-small.json +9 -0
- package/dist/char-cnn/options/lowercase.js +2 -2
- package/dist/char-cnn/options/lowercase.json +1 -1
- package/dist/char-cnn/options/small/tiny.head.bin +0 -0
- package/dist/char-cnn/options/small/tiny.q4.bin +0 -0
- package/dist/char-cnn/options/small.js +2 -2
- package/dist/char-cnn/options/small.json +1 -1
- package/dist/char-lite/core.d.ts +1 -0
- package/dist/char-lite/core.js +628 -0
- package/dist/char-lite/index.d.ts +28 -8
- package/dist/char-lite/index.js +845 -717
- package/dist/char-lite/millirec-char-lite.min.js +1 -1
- package/dist/char-lite/sets.json +17 -17
- package/dist/index.d.ts +35 -0
- package/dist/index.js +88 -0
- package/dist/overlap/millirec-overlap.min.js +7 -7
- package/dist/pad/index.d.ts +8 -0
- package/dist/pad/index.js +482 -0
- package/package.json +21 -7
- package/dist/char-cnn/options/small/tiny.bin +0 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AttachOptions, Pad, Strokes, Stroke, Reading } from '../char-lite/index.js'
|
|
2
|
+
export type { AttachOptions, Pad, ResultDetail, ClearDetail } from '../char-lite/index.js'
|
|
3
|
+
|
|
4
|
+
/** Anything attachPad can drive: char-cnn, char-lite, their cores, or your own. */
|
|
5
|
+
export interface Recognizer { recognize(strokes: Strokes | Stroke, opts?: { only?: string | string[] | Set<string> }): Promise<Reading[] | null> }
|
|
6
|
+
|
|
7
|
+
/** Mount a drawing pad in `el` that reads each drawing through `recognizer`; `el` fires 'millirec:result' and 'millirec:clear'. */
|
|
8
|
+
export declare function attachPad(el: HTMLElement, recognizer: Recognizer, opts?: AttachOptions): Pad
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
// ../../widget/scribble.js
|
|
2
|
+
var SCRIBBLE = {
|
|
3
|
+
turns: 5,
|
|
4
|
+
// reversals along the principal axis (no letter reaches 6; the scribble reaches 8)
|
|
5
|
+
retrace: 0.5,
|
|
6
|
+
// share of the path laid back over its own ground (letters <= 0.54 at ANY turn count)
|
|
7
|
+
winding: 3.5,
|
|
8
|
+
// path length / bbox diagonal (letters at 5 turns reach 3.29; the scribble 6.14)
|
|
9
|
+
cover: 0.6,
|
|
10
|
+
// how completely this stroke and the existing ink overlap, per axis
|
|
11
|
+
minLength: 40,
|
|
12
|
+
// px of path — a small wiggle is not a scribble
|
|
13
|
+
minSpan: 12,
|
|
14
|
+
// px of bbox diagonal — nor is a dense scrub of one point
|
|
15
|
+
deadband: 0.22
|
|
16
|
+
// a reversal must retrace this much of the axis's extent
|
|
17
|
+
};
|
|
18
|
+
var INK_HALF = 2;
|
|
19
|
+
function grow(b) {
|
|
20
|
+
return { x0: b.x0 - INK_HALF, y0: b.y0 - INK_HALF, x1: b.x1 + INK_HALF, y1: b.y1 + INK_HALF, w: b.w + 2 * INK_HALF, h: b.h + 2 * INK_HALF };
|
|
21
|
+
}
|
|
22
|
+
function bbox(pts) {
|
|
23
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
24
|
+
for (const p of pts) {
|
|
25
|
+
const x = p.x !== void 0 ? p.x : p[0], y = p.y !== void 0 ? p.y : p[1];
|
|
26
|
+
if (x < x0) x0 = x;
|
|
27
|
+
if (x > x1) x1 = x;
|
|
28
|
+
if (y < y0) y0 = y;
|
|
29
|
+
if (y > y1) y1 = y;
|
|
30
|
+
}
|
|
31
|
+
return { x0, y0, x1, y1, w: x1 - x0, h: y1 - y0 };
|
|
32
|
+
}
|
|
33
|
+
function turnsAlong(vals, deadband) {
|
|
34
|
+
let lo = Infinity, hi = -Infinity;
|
|
35
|
+
for (const v of vals) {
|
|
36
|
+
if (v < lo) lo = v;
|
|
37
|
+
if (v > hi) hi = v;
|
|
38
|
+
}
|
|
39
|
+
const band = (hi - lo) * deadband;
|
|
40
|
+
if (!(band > 0)) return 0;
|
|
41
|
+
let dir = 0, ext = vals[0], turns = 0;
|
|
42
|
+
for (const v of vals) {
|
|
43
|
+
if (dir > 0) {
|
|
44
|
+
if (v > ext) ext = v;
|
|
45
|
+
else if (v < ext - band) {
|
|
46
|
+
turns++;
|
|
47
|
+
dir = -1;
|
|
48
|
+
ext = v;
|
|
49
|
+
}
|
|
50
|
+
} else if (dir < 0) {
|
|
51
|
+
if (v < ext) ext = v;
|
|
52
|
+
else if (v > ext + band) {
|
|
53
|
+
turns++;
|
|
54
|
+
dir = 1;
|
|
55
|
+
ext = v;
|
|
56
|
+
}
|
|
57
|
+
} else if (v > ext + band) {
|
|
58
|
+
dir = 1;
|
|
59
|
+
ext = v;
|
|
60
|
+
} else if (v < ext - band) {
|
|
61
|
+
dir = -1;
|
|
62
|
+
ext = v;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return turns;
|
|
66
|
+
}
|
|
67
|
+
function axes(pts) {
|
|
68
|
+
let mx = 0, my = 0;
|
|
69
|
+
for (const p of pts) {
|
|
70
|
+
mx += p.x;
|
|
71
|
+
my += p.y;
|
|
72
|
+
}
|
|
73
|
+
mx /= pts.length;
|
|
74
|
+
my /= pts.length;
|
|
75
|
+
let sxx = 0, sxy = 0, syy = 0;
|
|
76
|
+
for (const p of pts) {
|
|
77
|
+
const dx = p.x - mx, dy = p.y - my;
|
|
78
|
+
sxx += dx * dx;
|
|
79
|
+
sxy += dx * dy;
|
|
80
|
+
syy += dy * dy;
|
|
81
|
+
}
|
|
82
|
+
const tr = sxx + syy, det = sxx * syy - sxy * sxy;
|
|
83
|
+
const l = tr / 2 + Math.sqrt(Math.max(0, tr * tr / 4 - det));
|
|
84
|
+
let ux = sxy, uy = l - sxx;
|
|
85
|
+
if (Math.hypot(ux, uy) < 1e-9) {
|
|
86
|
+
ux = 1;
|
|
87
|
+
uy = 0;
|
|
88
|
+
}
|
|
89
|
+
const n = Math.hypot(ux, uy);
|
|
90
|
+
return { ux: ux / n, uy: uy / n };
|
|
91
|
+
}
|
|
92
|
+
var RETRACE_R = 0.09;
|
|
93
|
+
var RETRACE_SKIP = 4;
|
|
94
|
+
function retraceOf(pts, span) {
|
|
95
|
+
const r = Math.max(2, span * RETRACE_R);
|
|
96
|
+
const step = r / 2;
|
|
97
|
+
const rs = [pts[0]];
|
|
98
|
+
let carry = 0;
|
|
99
|
+
for (let i = 1; i < pts.length; i++) {
|
|
100
|
+
const dx = pts[i].x - pts[i - 1].x, dy = pts[i].y - pts[i - 1].y;
|
|
101
|
+
let d = Math.hypot(dx, dy);
|
|
102
|
+
if (!(d > 0)) continue;
|
|
103
|
+
let t = (step - carry) / d;
|
|
104
|
+
while (t <= 1) {
|
|
105
|
+
rs.push({ x: pts[i - 1].x + dx * t, y: pts[i - 1].y + dy * t });
|
|
106
|
+
t += step / d;
|
|
107
|
+
}
|
|
108
|
+
carry = (carry + d) % step;
|
|
109
|
+
}
|
|
110
|
+
if (rs.length < 8) return 0;
|
|
111
|
+
const skip = Math.round(RETRACE_SKIP * r / step);
|
|
112
|
+
const key = (p) => `${Math.floor(p.x / r)},${Math.floor(p.y / r)}`;
|
|
113
|
+
const grid = /* @__PURE__ */ new Map();
|
|
114
|
+
rs.forEach((p, i) => {
|
|
115
|
+
const k = key(p);
|
|
116
|
+
if (!grid.has(k)) grid.set(k, []);
|
|
117
|
+
grid.get(k).push(i);
|
|
118
|
+
});
|
|
119
|
+
let hit = 0;
|
|
120
|
+
for (let i = 0; i < rs.length; i++) {
|
|
121
|
+
const p = rs[i];
|
|
122
|
+
const cx = Math.floor(p.x / r), cy = Math.floor(p.y / r);
|
|
123
|
+
let found = false;
|
|
124
|
+
for (let ax = -1; ax <= 1 && !found; ax++) {
|
|
125
|
+
for (let ay = -1; ay <= 1 && !found; ay++) {
|
|
126
|
+
for (const j of grid.get(`${cx + ax},${cy + ay}`) || []) {
|
|
127
|
+
if (Math.abs(i - j) <= skip) continue;
|
|
128
|
+
if (Math.hypot(rs[j].x - p.x, rs[j].y - p.y) <= r) {
|
|
129
|
+
found = true;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (found) hit++;
|
|
136
|
+
}
|
|
137
|
+
return hit / rs.length;
|
|
138
|
+
}
|
|
139
|
+
function scribbleFeatures(stroke, prior = [], opts = {}) {
|
|
140
|
+
const k = { ...SCRIBBLE, ...opts };
|
|
141
|
+
const pts = [];
|
|
142
|
+
for (const p of stroke) {
|
|
143
|
+
const q = { x: p.x !== void 0 ? p.x : p[0], y: p.y !== void 0 ? p.y : p[1] };
|
|
144
|
+
const last = pts[pts.length - 1];
|
|
145
|
+
if (!last || Math.hypot(q.x - last.x, q.y - last.y) > 0.75) pts.push(q);
|
|
146
|
+
}
|
|
147
|
+
if (pts.length < 4) return { turns: 0, cross: 0, retrace: 0, winding: 0, cover: 0, length: 0, span: 0 };
|
|
148
|
+
let length = 0;
|
|
149
|
+
for (let i = 1; i < pts.length; i++) length += Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y);
|
|
150
|
+
const b = bbox(pts);
|
|
151
|
+
const span = Math.hypot(b.w, b.h);
|
|
152
|
+
const { ux, uy } = axes(pts);
|
|
153
|
+
const major = pts.map((p) => p.x * ux + p.y * uy);
|
|
154
|
+
const minor = pts.map((p) => -p.x * uy + p.y * ux);
|
|
155
|
+
const turns = turnsAlong(major, k.deadband);
|
|
156
|
+
const cross = turnsAlong(minor, k.deadband);
|
|
157
|
+
let cover = 0;
|
|
158
|
+
const flat = [];
|
|
159
|
+
for (const st of prior) for (const p of st) flat.push(p);
|
|
160
|
+
if (flat.length) {
|
|
161
|
+
const a = grow(bbox(flat));
|
|
162
|
+
const c = grow(b);
|
|
163
|
+
const ox = Math.max(0, Math.min(a.x1, c.x1) - Math.max(a.x0, c.x0));
|
|
164
|
+
const oy = Math.max(0, Math.min(a.y1, c.y1) - Math.max(a.y0, c.y0));
|
|
165
|
+
cover = ox / Math.min(a.w, c.w) * (oy / Math.min(a.h, c.h));
|
|
166
|
+
}
|
|
167
|
+
return { turns, cross, retrace: retraceOf(pts, span), winding: span > 0 ? length / span : 0, cover, length, span };
|
|
168
|
+
}
|
|
169
|
+
function isScribble(stroke, prior = [], opts = {}) {
|
|
170
|
+
const k = { ...SCRIBBLE, ...opts };
|
|
171
|
+
if (!prior || !prior.length) return false;
|
|
172
|
+
const f = scribbleFeatures(stroke, prior, k);
|
|
173
|
+
return f.length >= k.minLength && f.span >= k.minSpan && f.turns >= k.turns && f.retrace >= k.retrace && f.winding >= k.winding && f.cover >= k.cover;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ../../widget/canvas.js
|
|
177
|
+
var ERASE_RADIUS = 12;
|
|
178
|
+
var now = () => typeof performance !== "undefined" && performance.now ? performance.now() : Date.now();
|
|
179
|
+
function createDrawCanvas({
|
|
180
|
+
width = 160,
|
|
181
|
+
height = 90,
|
|
182
|
+
ink = "#111827",
|
|
183
|
+
onStrokeEnd,
|
|
184
|
+
colorOf = null,
|
|
185
|
+
idleOverwrite = 0,
|
|
186
|
+
scribbleClear = false,
|
|
187
|
+
scribble = null,
|
|
188
|
+
onClear = null,
|
|
189
|
+
staleAlpha = 0.3,
|
|
190
|
+
onStale = null
|
|
191
|
+
}) {
|
|
192
|
+
const canvas = document.createElement("canvas");
|
|
193
|
+
const dpr = typeof devicePixelRatio !== "undefined" && devicePixelRatio || 1;
|
|
194
|
+
canvas.width = width * dpr;
|
|
195
|
+
canvas.height = height * dpr;
|
|
196
|
+
canvas.style.width = width + "px";
|
|
197
|
+
canvas.style.height = height + "px";
|
|
198
|
+
canvas.style.touchAction = "none";
|
|
199
|
+
const ctx = canvas.getContext("2d");
|
|
200
|
+
ctx.scale(dpr, dpr);
|
|
201
|
+
let strokes = [];
|
|
202
|
+
let current = [];
|
|
203
|
+
let activeId = null;
|
|
204
|
+
let erasing = false;
|
|
205
|
+
let nextId = 1;
|
|
206
|
+
let lastEndAt = 0;
|
|
207
|
+
let staleTimer = 0;
|
|
208
|
+
let stale = false;
|
|
209
|
+
let dropped = null;
|
|
210
|
+
let droppedPending = false;
|
|
211
|
+
function paint(st, color) {
|
|
212
|
+
if (st.length < 2) return;
|
|
213
|
+
ctx.strokeStyle = color || ink;
|
|
214
|
+
ctx.beginPath();
|
|
215
|
+
ctx.moveTo(st[0].x, st[0].y);
|
|
216
|
+
for (let i = 1; i < st.length; i++) ctx.lineTo(st[i].x, st[i].y);
|
|
217
|
+
ctx.stroke();
|
|
218
|
+
}
|
|
219
|
+
function paintFrom(st, from) {
|
|
220
|
+
if (st.length < 2 || from >= st.length - 1) return;
|
|
221
|
+
ctx.lineWidth = 2.5;
|
|
222
|
+
ctx.lineCap = "round";
|
|
223
|
+
ctx.lineJoin = "round";
|
|
224
|
+
ctx.globalAlpha = 1;
|
|
225
|
+
ctx.strokeStyle = ink;
|
|
226
|
+
ctx.beginPath();
|
|
227
|
+
ctx.moveTo(st[Math.max(0, from)].x, st[Math.max(0, from)].y);
|
|
228
|
+
for (let i = Math.max(1, from + 1); i < st.length; i++) ctx.lineTo(st[i].x, st[i].y);
|
|
229
|
+
ctx.stroke();
|
|
230
|
+
}
|
|
231
|
+
function render() {
|
|
232
|
+
ctx.clearRect(0, 0, width, height);
|
|
233
|
+
ctx.lineWidth = 2.5;
|
|
234
|
+
ctx.lineCap = "round";
|
|
235
|
+
ctx.lineJoin = "round";
|
|
236
|
+
ctx.globalAlpha = stale ? staleAlpha : 1;
|
|
237
|
+
for (const st of strokes) paint(st, colorOf && colorOf(st));
|
|
238
|
+
ctx.globalAlpha = 1;
|
|
239
|
+
paint(current, null);
|
|
240
|
+
}
|
|
241
|
+
function setStale(v) {
|
|
242
|
+
if (stale === v) return;
|
|
243
|
+
stale = v;
|
|
244
|
+
render();
|
|
245
|
+
if (onStale) onStale(v);
|
|
246
|
+
}
|
|
247
|
+
function armStale() {
|
|
248
|
+
clearTimeout(staleTimer);
|
|
249
|
+
staleTimer = 0;
|
|
250
|
+
const ms = (Number(idleOverwrite) || 0) * 1e3;
|
|
251
|
+
if (!ms || !strokes.length) {
|
|
252
|
+
setStale(false);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const left = ms - (now() - lastEndAt);
|
|
256
|
+
if (left <= 0) {
|
|
257
|
+
setStale(true);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
setStale(false);
|
|
261
|
+
staleTimer = setTimeout(() => setStale(true), left);
|
|
262
|
+
}
|
|
263
|
+
function dropAll(reason) {
|
|
264
|
+
if (!strokes.length) return;
|
|
265
|
+
dropped = strokes;
|
|
266
|
+
strokes = [];
|
|
267
|
+
clearTimeout(staleTimer);
|
|
268
|
+
staleTimer = 0;
|
|
269
|
+
setStale(false);
|
|
270
|
+
render();
|
|
271
|
+
if (onClear) onClear(reason);
|
|
272
|
+
}
|
|
273
|
+
function eraseAt(x, y) {
|
|
274
|
+
const kept = strokes.filter((st) => !st.some((p) => Math.hypot(p.x - x, p.y - y) <= ERASE_RADIUS));
|
|
275
|
+
if (kept.length === strokes.length) return false;
|
|
276
|
+
strokes = kept;
|
|
277
|
+
render();
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
let rect = null;
|
|
281
|
+
const getPos = (e) => {
|
|
282
|
+
const r = rect || (rect = canvas.getBoundingClientRect());
|
|
283
|
+
const sx = width / (canvas.clientWidth || width), sy = height / (canvas.clientHeight || height);
|
|
284
|
+
return { x: (e.clientX - r.left - canvas.clientLeft) * sx, y: (e.clientY - r.top - canvas.clientTop) * sy, p: e.pressure || 0.5 };
|
|
285
|
+
};
|
|
286
|
+
let erasedAny = false;
|
|
287
|
+
let activeType = null;
|
|
288
|
+
const swallow = (e) => e.preventDefault();
|
|
289
|
+
canvas.addEventListener("touchstart", swallow, { passive: false });
|
|
290
|
+
canvas.addEventListener("touchmove", swallow, { passive: false });
|
|
291
|
+
canvas.addEventListener("contextmenu", swallow);
|
|
292
|
+
canvas.style.userSelect = canvas.style.webkitUserSelect = "none";
|
|
293
|
+
canvas.style.webkitTouchCallout = "none";
|
|
294
|
+
canvas.addEventListener("pointerdown", (e) => {
|
|
295
|
+
if (e.button > 0) return;
|
|
296
|
+
if (activeId !== null) {
|
|
297
|
+
if (activeType === "pen" && e.pointerType !== "pen" && e.pointerId !== activeId) return;
|
|
298
|
+
finish({ pointerId: activeId });
|
|
299
|
+
}
|
|
300
|
+
e.preventDefault();
|
|
301
|
+
activeId = e.pointerId;
|
|
302
|
+
activeType = e.pointerType;
|
|
303
|
+
rect = canvas.getBoundingClientRect();
|
|
304
|
+
try {
|
|
305
|
+
canvas.setPointerCapture(e.pointerId);
|
|
306
|
+
} catch {
|
|
307
|
+
}
|
|
308
|
+
const pos = getPos(e);
|
|
309
|
+
if (erasing) {
|
|
310
|
+
erasedAny = eraseAt(pos.x, pos.y);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (stale && strokes.length) {
|
|
314
|
+
dropAll("idle");
|
|
315
|
+
droppedPending = true;
|
|
316
|
+
}
|
|
317
|
+
current = [pos];
|
|
318
|
+
render();
|
|
319
|
+
}, { passive: false });
|
|
320
|
+
canvas.addEventListener("pointermove", (e) => {
|
|
321
|
+
if (activeId !== e.pointerId) return;
|
|
322
|
+
e.preventDefault();
|
|
323
|
+
const co = e.getCoalescedEvents ? e.getCoalescedEvents() : null;
|
|
324
|
+
const events = co && co.length ? co : [e];
|
|
325
|
+
if (erasing) {
|
|
326
|
+
for (const ev of events) {
|
|
327
|
+
const p = getPos(ev);
|
|
328
|
+
if (eraseAt(p.x, p.y)) erasedAny = true;
|
|
329
|
+
}
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const from = current.length - 1;
|
|
333
|
+
for (const ev of events) current.push(getPos(ev));
|
|
334
|
+
paintFrom(current, from);
|
|
335
|
+
}, { passive: false });
|
|
336
|
+
const finish = (e, cancelled = false) => {
|
|
337
|
+
if (activeId !== e.pointerId) return;
|
|
338
|
+
try {
|
|
339
|
+
canvas.releasePointerCapture(e.pointerId);
|
|
340
|
+
} catch {
|
|
341
|
+
}
|
|
342
|
+
activeId = null;
|
|
343
|
+
activeType = null;
|
|
344
|
+
rect = null;
|
|
345
|
+
if (erasing) {
|
|
346
|
+
if (erasedAny) {
|
|
347
|
+
lastEndAt = now();
|
|
348
|
+
armStale();
|
|
349
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
350
|
+
}
|
|
351
|
+
erasedAny = false;
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (current.length > 0) {
|
|
355
|
+
let st = current;
|
|
356
|
+
if (st.length === 1) {
|
|
357
|
+
if (cancelled) {
|
|
358
|
+
current = [];
|
|
359
|
+
render();
|
|
360
|
+
if (droppedPending) {
|
|
361
|
+
droppedPending = false;
|
|
362
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
363
|
+
}
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const p = st[0];
|
|
367
|
+
st = [{ x: p.x - 1, y: p.y, p: p.p }, { x: p.x + 1, y: p.y, p: p.p }];
|
|
368
|
+
}
|
|
369
|
+
const aimedAt = strokes.length ? strokes : droppedPending ? dropped : null;
|
|
370
|
+
if (scribbleClear && aimedAt && aimedAt.length && isScribble(st, aimedAt, scribble || {})) {
|
|
371
|
+
current = [];
|
|
372
|
+
lastEndAt = now();
|
|
373
|
+
droppedPending = false;
|
|
374
|
+
if (strokes.length) dropAll("scribble");
|
|
375
|
+
else {
|
|
376
|
+
render();
|
|
377
|
+
if (onClear) onClear("scribble");
|
|
378
|
+
}
|
|
379
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
st.id = nextId++;
|
|
383
|
+
strokes = [...strokes, st];
|
|
384
|
+
current = [];
|
|
385
|
+
droppedPending = false;
|
|
386
|
+
lastEndAt = now();
|
|
387
|
+
armStale();
|
|
388
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
389
|
+
}
|
|
390
|
+
render();
|
|
391
|
+
};
|
|
392
|
+
canvas.addEventListener("pointerup", (e) => {
|
|
393
|
+
if (e.pointerId === activeId) e.preventDefault();
|
|
394
|
+
finish(e);
|
|
395
|
+
}, { passive: false });
|
|
396
|
+
canvas.addEventListener("pointercancel", (e) => finish(e, true), { passive: false });
|
|
397
|
+
canvas.addEventListener("lostpointercapture", (e) => {
|
|
398
|
+
if (!canvas.hasPointerCapture || !canvas.hasPointerCapture(e.pointerId)) finish(e, true);
|
|
399
|
+
});
|
|
400
|
+
return {
|
|
401
|
+
el: canvas,
|
|
402
|
+
getStrokes: () => strokes,
|
|
403
|
+
setInk(color) {
|
|
404
|
+
ink = color;
|
|
405
|
+
render();
|
|
406
|
+
},
|
|
407
|
+
setErasing(on) {
|
|
408
|
+
erasing = !!on;
|
|
409
|
+
canvas.style.cursor = erasing ? "cell" : "crosshair";
|
|
410
|
+
},
|
|
411
|
+
isErasing: () => erasing,
|
|
412
|
+
repaint: render,
|
|
413
|
+
/** True once the idle window has passed: the next stroke starts fresh. */
|
|
414
|
+
isStale: () => stale,
|
|
415
|
+
/**
|
|
416
|
+
* Put back what the pad cleared BY ITSELF — an idle overwrite or a
|
|
417
|
+
* scribble. An explicit `clear()` is not stashed: a caller clears the pad
|
|
418
|
+
* because it has banked the drawing, and restoring that would hand back a
|
|
419
|
+
* sample the writer already dealt with.
|
|
420
|
+
*/
|
|
421
|
+
undoClear() {
|
|
422
|
+
if (!dropped || !dropped.length) return false;
|
|
423
|
+
strokes = strokes.length ? [...dropped, ...strokes] : dropped;
|
|
424
|
+
dropped = null;
|
|
425
|
+
lastEndAt = now();
|
|
426
|
+
armStale();
|
|
427
|
+
render();
|
|
428
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
429
|
+
return true;
|
|
430
|
+
},
|
|
431
|
+
/** Change either overwrite behaviour, or the settled look, at runtime. */
|
|
432
|
+
configure(opts = {}) {
|
|
433
|
+
if ("idleOverwrite" in opts) idleOverwrite = Number(opts.idleOverwrite) || 0;
|
|
434
|
+
if ("scribbleClear" in opts) scribbleClear = !!opts.scribbleClear;
|
|
435
|
+
if ("scribble" in opts) scribble = opts.scribble;
|
|
436
|
+
if ("staleAlpha" in opts) staleAlpha = Number(opts.staleAlpha);
|
|
437
|
+
armStale();
|
|
438
|
+
},
|
|
439
|
+
clear() {
|
|
440
|
+
strokes = [];
|
|
441
|
+
current = [];
|
|
442
|
+
activeId = null;
|
|
443
|
+
activeType = null;
|
|
444
|
+
rect = null;
|
|
445
|
+
clearTimeout(staleTimer);
|
|
446
|
+
staleTimer = 0;
|
|
447
|
+
setStale(false);
|
|
448
|
+
droppedPending = false;
|
|
449
|
+
render();
|
|
450
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// ../../library/src/pad.js
|
|
456
|
+
function attachPad(el, recognizer, { width = 240, height = 240, ink = "#1e293b", idleOverwrite = 2, scribbleClear = true, scribble = null, staleAlpha, onStale, recognizeOptions } = {}) {
|
|
457
|
+
if (!recognizer || typeof recognizer.recognize !== "function") throw new TypeError("millirec: attachPad(el, recognizer) needs a recognizer with recognize(strokes)");
|
|
458
|
+
const pad = createDrawCanvas({
|
|
459
|
+
width,
|
|
460
|
+
height,
|
|
461
|
+
ink,
|
|
462
|
+
idleOverwrite,
|
|
463
|
+
scribbleClear,
|
|
464
|
+
scribble,
|
|
465
|
+
onStale,
|
|
466
|
+
...staleAlpha === void 0 ? {} : { staleAlpha },
|
|
467
|
+
onClear: (reason) => el.dispatchEvent(new CustomEvent("millirec:clear", { detail: { reason, undo: () => pad.undoClear() } })),
|
|
468
|
+
onStrokeEnd: async (strokes) => {
|
|
469
|
+
if (!strokes.length) {
|
|
470
|
+
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results: [], strokes: [] } }));
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const results = await (recognizeOptions ? recognizer.recognize(strokes, recognizeOptions) : recognizer.recognize(strokes));
|
|
474
|
+
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results, strokes } }));
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
el.appendChild(pad.el);
|
|
478
|
+
return { clear: pad.clear, undoClear: pad.undoClear, isStale: pad.isStale, configure: pad.configure, setInk: pad.setInk, repaint: pad.repaint, getStrokes: pad.getStrokes, canvas: pad.el };
|
|
479
|
+
}
|
|
480
|
+
export {
|
|
481
|
+
attachPad
|
|
482
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "millirec",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Small, fast handwriting recognition in the browser: drawn characters, digits and symbols,
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Small, fast handwriting recognition in the browser: drawn characters, digits and symbols — a CNN for standard alphabets, a point-cloud matcher you can teach any symbol.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Robert Wilcox",
|
|
7
7
|
"homepage": "https://millirec.com",
|
|
@@ -11,19 +11,25 @@
|
|
|
11
11
|
},
|
|
12
12
|
"bugs": "https://millirec.com/help.html",
|
|
13
13
|
"type": "module",
|
|
14
|
-
"main": "./dist/
|
|
15
|
-
"types": "./dist/
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
16
|
"typesVersions": {
|
|
17
17
|
"*": {
|
|
18
18
|
"char-lite": [
|
|
19
19
|
"dist/char-lite/index.d.ts"
|
|
20
20
|
],
|
|
21
|
+
"char-lite/core": [
|
|
22
|
+
"dist/char-lite/core.d.ts"
|
|
23
|
+
],
|
|
21
24
|
"char-cnn": [
|
|
22
25
|
"dist/char-cnn/index.d.ts"
|
|
23
26
|
],
|
|
24
27
|
"char-cnn/core": [
|
|
25
28
|
"dist/char-cnn/core.d.ts"
|
|
26
29
|
],
|
|
30
|
+
"pad": [
|
|
31
|
+
"dist/pad/index.d.ts"
|
|
32
|
+
],
|
|
27
33
|
"overlap": [
|
|
28
34
|
"dist/overlap/index.d.ts"
|
|
29
35
|
],
|
|
@@ -51,13 +57,17 @@
|
|
|
51
57
|
],
|
|
52
58
|
"exports": {
|
|
53
59
|
".": {
|
|
54
|
-
"types": "./dist/
|
|
55
|
-
"default": "./dist/
|
|
60
|
+
"types": "./dist/index.d.ts",
|
|
61
|
+
"default": "./dist/index.js"
|
|
56
62
|
},
|
|
57
63
|
"./char-lite": {
|
|
58
64
|
"types": "./dist/char-lite/index.d.ts",
|
|
59
65
|
"default": "./dist/char-lite/index.js"
|
|
60
66
|
},
|
|
67
|
+
"./char-lite/core": {
|
|
68
|
+
"types": "./dist/char-lite/core.d.ts",
|
|
69
|
+
"default": "./dist/char-lite/core.js"
|
|
70
|
+
},
|
|
61
71
|
"./char-cnn": {
|
|
62
72
|
"types": "./dist/char-cnn/index.d.ts",
|
|
63
73
|
"default": "./dist/char-cnn/index.js"
|
|
@@ -66,6 +76,10 @@
|
|
|
66
76
|
"types": "./dist/char-cnn/core.d.ts",
|
|
67
77
|
"default": "./dist/char-cnn/core.js"
|
|
68
78
|
},
|
|
79
|
+
"./pad": {
|
|
80
|
+
"types": "./dist/pad/index.d.ts",
|
|
81
|
+
"default": "./dist/pad/index.js"
|
|
82
|
+
},
|
|
69
83
|
"./char-lite/sets/*.json": "./dist/char-lite/sets/*.json",
|
|
70
84
|
"./char-cnn/options/*.json": "./dist/char-cnn/options/*.json",
|
|
71
85
|
"./overlap": {
|
|
@@ -88,7 +102,7 @@
|
|
|
88
102
|
],
|
|
89
103
|
"scripts": {
|
|
90
104
|
"build": "node build.mjs",
|
|
91
|
-
"test": "node build.mjs && node test/verify.mjs",
|
|
105
|
+
"test": "node build.mjs && node test/verify.mjs && node test/fixes.mjs && node test/front.mjs",
|
|
92
106
|
"prepack": "node build.mjs"
|
|
93
107
|
},
|
|
94
108
|
"engines": {
|
|
Binary file
|