@loksly/lcd-canvas-kit 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +64 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/paint.d.ts +32 -0
- package/dist/paint.js +60 -0
- package/dist/pixelshift.d.ts +18 -0
- package/dist/pixelshift.js +21 -0
- package/dist/sink.d.ts +13 -0
- package/dist/sink.js +20 -0
- package/dist/sparkline.d.ts +40 -0
- package/dist/sparkline.js +43 -0
- package/dist/text.d.ts +56 -0
- package/dist/text.js +148 -0
- package/dist/weathericons.d.ts +44 -0
- package/dist/weathericons.js +227 -0
- package/package.json +48 -4
- package/src/index.ts +6 -0
- package/src/paint.ts +77 -0
- package/src/pixelshift.ts +35 -0
- package/src/sink.ts +28 -0
- package/src/sparkline.ts +70 -0
- package/src/text.ts +183 -0
- package/src/weathericons.ts +258 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Weather icons, hand-drawn with canvas paths.
|
|
3
|
+
*
|
|
4
|
+
* Why drawn rather than taken from a font: the system fonts (DejaVu) do not
|
|
5
|
+
* ship reliable weather glyphs (just as they ship no emoji, which is why text
|
|
6
|
+
* drawing avoids them too), and an empty box in the middle of the panel is
|
|
7
|
+
* worse than no icon at all. Drawing them takes a hundred lines and matches
|
|
8
|
+
* the rest of the panel, whose other decorations are drawn as well.
|
|
9
|
+
*
|
|
10
|
+
* Every icon is painted inside a box of side `size` centred on (`cx`, `cy`),
|
|
11
|
+
* so the caller only has to choose the centre.
|
|
12
|
+
*/
|
|
13
|
+
import { createCanvas } from '@napi-rs/canvas';
|
|
14
|
+
/** Cloud body: three lobes and a flat base. */
|
|
15
|
+
function cloud(ctx, cx, cy, s) {
|
|
16
|
+
const r = s * 0.22;
|
|
17
|
+
ctx.beginPath();
|
|
18
|
+
ctx.arc(cx - s * 0.22, cy + s * 0.04, r, Math.PI * 0.5, Math.PI * 1.5);
|
|
19
|
+
ctx.arc(cx - s * 0.06, cy - s * 0.14, r * 1.25, Math.PI, Math.PI * 1.85);
|
|
20
|
+
ctx.arc(cx + s * 0.22, cy + s * 0.02, r * 0.95, Math.PI * 1.4, Math.PI * 0.5);
|
|
21
|
+
ctx.closePath();
|
|
22
|
+
ctx.fill();
|
|
23
|
+
}
|
|
24
|
+
function sun(ctx, cx, cy, s, rays = true) {
|
|
25
|
+
const r = s * 0.19;
|
|
26
|
+
ctx.beginPath();
|
|
27
|
+
ctx.arc(cx, cy, r, 0, Math.PI * 2);
|
|
28
|
+
ctx.fill();
|
|
29
|
+
if (!rays)
|
|
30
|
+
return;
|
|
31
|
+
ctx.lineWidth = Math.max(1.5, s * 0.045);
|
|
32
|
+
ctx.lineCap = 'round';
|
|
33
|
+
for (let i = 0; i < 8; i += 1) {
|
|
34
|
+
const a = (i * Math.PI) / 4;
|
|
35
|
+
ctx.beginPath();
|
|
36
|
+
ctx.moveTo(cx + Math.cos(a) * r * 1.45, cy + Math.sin(a) * r * 1.45);
|
|
37
|
+
ctx.lineTo(cx + Math.cos(a) * r * 1.95, cy + Math.sin(a) * r * 1.95);
|
|
38
|
+
ctx.stroke();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Crescent moon: a disc with an offset disc subtracted from it.
|
|
43
|
+
*
|
|
44
|
+
* The cut-out is done on a **separate canvas** and then stamped. An earlier
|
|
45
|
+
* version applied `destination-out` directly to the target canvas, and that
|
|
46
|
+
* does not cut the moon: it punches a hole through everything underneath. On
|
|
47
|
+
* the card's gradient it came out as a black disc, and on a photo it would
|
|
48
|
+
* have been a bite taken out of the picture.
|
|
49
|
+
*/
|
|
50
|
+
function moon(ctx, cx, cy, s) {
|
|
51
|
+
const r = s * 0.24;
|
|
52
|
+
const pad = 2;
|
|
53
|
+
const side = Math.ceil(r * 2 + pad * 2);
|
|
54
|
+
const off = createCanvas(side, side);
|
|
55
|
+
const o = off.getContext('2d');
|
|
56
|
+
const c = side / 2;
|
|
57
|
+
o.fillStyle = ctx.fillStyle;
|
|
58
|
+
o.beginPath();
|
|
59
|
+
o.arc(c, c, r, 0, Math.PI * 2);
|
|
60
|
+
o.fill();
|
|
61
|
+
// Fine here: the canvas is otherwise empty, so only the moon gets cut.
|
|
62
|
+
o.globalCompositeOperation = 'destination-out';
|
|
63
|
+
o.beginPath();
|
|
64
|
+
o.arc(c + r * 0.55, c - r * 0.42, r * 0.92, 0, Math.PI * 2);
|
|
65
|
+
o.fill();
|
|
66
|
+
ctx.drawImage(off, cx - c, cy - c);
|
|
67
|
+
}
|
|
68
|
+
/** Drops or streaks under the cloud. `len` stretches them: short for drizzle, long for rain. */
|
|
69
|
+
function drops(ctx, cx, cy, s, len) {
|
|
70
|
+
ctx.lineWidth = Math.max(1.5, s * 0.05);
|
|
71
|
+
ctx.lineCap = 'round';
|
|
72
|
+
for (const dx of [-0.2, 0, 0.2]) {
|
|
73
|
+
ctx.beginPath();
|
|
74
|
+
ctx.moveTo(cx + s * dx + s * 0.05, cy + s * 0.22);
|
|
75
|
+
ctx.lineTo(cx + s * dx - s * 0.02, cy + s * (0.22 + len));
|
|
76
|
+
ctx.stroke();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function snowflakes(ctx, cx, cy, s) {
|
|
80
|
+
ctx.lineWidth = Math.max(1.2, s * 0.035);
|
|
81
|
+
ctx.lineCap = 'round';
|
|
82
|
+
for (const dx of [-0.2, 0.05]) {
|
|
83
|
+
const x = cx + s * dx;
|
|
84
|
+
const y = cy + s * 0.3;
|
|
85
|
+
for (let i = 0; i < 3; i += 1) {
|
|
86
|
+
const a = (i * Math.PI) / 3;
|
|
87
|
+
ctx.beginPath();
|
|
88
|
+
ctx.moveTo(x - Math.cos(a) * s * 0.07, y - Math.sin(a) * s * 0.07);
|
|
89
|
+
ctx.lineTo(x + Math.cos(a) * s * 0.07, y + Math.sin(a) * s * 0.07);
|
|
90
|
+
ctx.stroke();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function bolt(ctx, cx, cy, s) {
|
|
95
|
+
ctx.beginPath();
|
|
96
|
+
ctx.moveTo(cx + s * 0.06, cy + s * 0.16);
|
|
97
|
+
ctx.lineTo(cx - s * 0.1, cy + s * 0.34);
|
|
98
|
+
ctx.lineTo(cx + s * 0.01, cy + s * 0.34);
|
|
99
|
+
ctx.lineTo(cx - s * 0.06, cy + s * 0.5);
|
|
100
|
+
ctx.lineTo(cx + s * 0.14, cy + s * 0.28);
|
|
101
|
+
ctx.lineTo(cx + s * 0.03, cy + s * 0.28);
|
|
102
|
+
ctx.closePath();
|
|
103
|
+
ctx.fill();
|
|
104
|
+
}
|
|
105
|
+
function fogLines(ctx, cx, cy, s) {
|
|
106
|
+
ctx.lineWidth = Math.max(1.5, s * 0.055);
|
|
107
|
+
ctx.lineCap = 'round';
|
|
108
|
+
for (const [i, w] of [0.34, 0.4, 0.28].entries()) {
|
|
109
|
+
const y = cy + s * (0.2 + i * 0.13);
|
|
110
|
+
ctx.beginPath();
|
|
111
|
+
ctx.moveTo(cx - s * w, y);
|
|
112
|
+
ctx.lineTo(cx + s * w, y);
|
|
113
|
+
ctx.stroke();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Paints the weather icon.
|
|
118
|
+
*
|
|
119
|
+
* `color` is used for both fill and stroke: these are single-ink icons, which
|
|
120
|
+
* is what holds up well over a background photo with a dimming veil on top.
|
|
121
|
+
*/
|
|
122
|
+
export function drawWeatherIcon(ctx, kind, cx, cy, size, color) {
|
|
123
|
+
ctx.save();
|
|
124
|
+
ctx.fillStyle = color;
|
|
125
|
+
ctx.strokeStyle = color;
|
|
126
|
+
switch (kind) {
|
|
127
|
+
case 'sun':
|
|
128
|
+
sun(ctx, cx, cy, size);
|
|
129
|
+
break;
|
|
130
|
+
case 'moon':
|
|
131
|
+
moon(ctx, cx, cy, size);
|
|
132
|
+
break;
|
|
133
|
+
case 'sun-cloud':
|
|
134
|
+
sun(ctx, cx - size * 0.16, cy - size * 0.18, size * 0.72);
|
|
135
|
+
cloud(ctx, cx + size * 0.06, cy + size * 0.1, size);
|
|
136
|
+
break;
|
|
137
|
+
case 'moon-cloud':
|
|
138
|
+
moon(ctx, cx - size * 0.16, cy - size * 0.2, size * 0.72);
|
|
139
|
+
cloud(ctx, cx + size * 0.06, cy + size * 0.1, size);
|
|
140
|
+
break;
|
|
141
|
+
case 'cloud':
|
|
142
|
+
cloud(ctx, cx, cy, size);
|
|
143
|
+
break;
|
|
144
|
+
case 'fog':
|
|
145
|
+
cloud(ctx, cx, cy - size * 0.12, size * 0.9);
|
|
146
|
+
fogLines(ctx, cx, cy, size);
|
|
147
|
+
break;
|
|
148
|
+
case 'drizzle':
|
|
149
|
+
cloud(ctx, cx, cy - size * 0.1, size);
|
|
150
|
+
drops(ctx, cx, cy - size * 0.1, size, 0.14);
|
|
151
|
+
break;
|
|
152
|
+
case 'rain':
|
|
153
|
+
cloud(ctx, cx, cy - size * 0.12, size);
|
|
154
|
+
drops(ctx, cx, cy - size * 0.12, size, 0.26);
|
|
155
|
+
break;
|
|
156
|
+
case 'snow':
|
|
157
|
+
cloud(ctx, cx, cy - size * 0.14, size);
|
|
158
|
+
snowflakes(ctx, cx, cy - size * 0.14, size);
|
|
159
|
+
break;
|
|
160
|
+
case 'storm':
|
|
161
|
+
cloud(ctx, cx, cy - size * 0.14, size);
|
|
162
|
+
bolt(ctx, cx, cy - size * 0.14, size);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
ctx.restore();
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The moon in its actual phase: the lit disc minus the shadowed part.
|
|
169
|
+
*
|
|
170
|
+
* The shadow is bounded by two curves: the **limb** (the half circle on the
|
|
171
|
+
* dark side) and the **terminator**, which seen face-on projects as a half
|
|
172
|
+
* ellipse with horizontal semi-axis `R·(1−2k)`, where `k` is the illuminated
|
|
173
|
+
* fraction. That semi-axis is signed, and that is the whole trick:
|
|
174
|
+
*
|
|
175
|
+
* - `k < 0.5` → positive: the terminator falls on the lit side and the
|
|
176
|
+
* shadow goes past the centre, which is what a thin crescent looks like.
|
|
177
|
+
* - `k > 0.5` → negative: it moves into the dark side and the shadow is left
|
|
178
|
+
* as a thin sliver, which is a gibbous moon.
|
|
179
|
+
*
|
|
180
|
+
* It is traced as a polygon instead of with `ellipse()` because the sweep
|
|
181
|
+
* direction of the arcs changes with the sign of the semi-axis and ends up as
|
|
182
|
+
* code full of flags; with parametric points the same loop works for all
|
|
183
|
+
* eight phases.
|
|
184
|
+
*
|
|
185
|
+
* The cut-out also goes on a separate canvas, for the same reason as the
|
|
186
|
+
* simple moon: on the target canvas `destination-out` would punch through the
|
|
187
|
+
* background.
|
|
188
|
+
*/
|
|
189
|
+
export function drawMoonPhase(ctx, moon, cx, cy, size, color) {
|
|
190
|
+
const r = size / 2;
|
|
191
|
+
const pad = 2;
|
|
192
|
+
const side = Math.ceil(size + pad * 2);
|
|
193
|
+
const off = createCanvas(side, side);
|
|
194
|
+
const o = off.getContext('2d');
|
|
195
|
+
const c = side / 2;
|
|
196
|
+
// Full disc, plus a faint ring so that an almost-new moon does not vanish.
|
|
197
|
+
o.fillStyle = color;
|
|
198
|
+
o.beginPath();
|
|
199
|
+
o.arc(c, c, r, 0, Math.PI * 2);
|
|
200
|
+
o.fill();
|
|
201
|
+
const f = 1 - 2 * moon.illumination;
|
|
202
|
+
// In the northern hemisphere a waxing moon is lit from the right.
|
|
203
|
+
const dark = moon.waxing ? -1 : 1;
|
|
204
|
+
const STEPS = 48;
|
|
205
|
+
o.globalCompositeOperation = 'destination-out';
|
|
206
|
+
o.beginPath();
|
|
207
|
+
for (let i = 0; i <= STEPS; i += 1) {
|
|
208
|
+
const u = (i / STEPS) * Math.PI;
|
|
209
|
+
o.lineTo(c + dark * r * Math.sin(u), c - r * Math.cos(u));
|
|
210
|
+
}
|
|
211
|
+
for (let i = STEPS; i >= 0; i -= 1) {
|
|
212
|
+
const u = (i / STEPS) * Math.PI;
|
|
213
|
+
o.lineTo(c - dark * r * f * Math.sin(u), c - r * Math.cos(u));
|
|
214
|
+
}
|
|
215
|
+
o.closePath();
|
|
216
|
+
o.fill();
|
|
217
|
+
ctx.save();
|
|
218
|
+
ctx.drawImage(off, cx - c, cy - c);
|
|
219
|
+
// Ring around the whole disc: without it, a new moon would be an invisible
|
|
220
|
+
// hole, indistinguishable from "nothing was drawn here".
|
|
221
|
+
ctx.strokeStyle = 'rgba(255,255,255,0.22)';
|
|
222
|
+
ctx.lineWidth = Math.max(1, size * 0.02);
|
|
223
|
+
ctx.beginPath();
|
|
224
|
+
ctx.arc(cx, cy, r, 0, Math.PI * 2);
|
|
225
|
+
ctx.stroke();
|
|
226
|
+
ctx.restore();
|
|
227
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loksly/lcd-canvas-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Drawing helpers for small always-on LCD panels on @napi-rs/canvas: tracked text, small caps, word wrap, sparklines, pixel shift against image retention, weather and moon icons.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Loksly",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Loksly/aimonitor.git",
|
|
11
|
+
"directory": "packages/lcd-canvas-kit"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"canvas",
|
|
15
|
+
"napi-rs",
|
|
16
|
+
"lcd",
|
|
17
|
+
"dashboard",
|
|
18
|
+
"sparkline",
|
|
19
|
+
"burn-in",
|
|
20
|
+
"weather-icons"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"source": "./src/index.ts",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"src",
|
|
32
|
+
"!src/**/*.test.ts",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=20"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.json",
|
|
41
|
+
"test": "node --conditions=source --import tsx --test 'src/**/*.test.ts'",
|
|
42
|
+
"check": "tsc -p tsconfig.json --noEmit"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@loksly/weather-astro": "0.2.1"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@napi-rs/canvas": ">=0.1.65 <2"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.ts
ADDED
package/src/paint.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Painting primitives: colour arithmetic, a seeded random generator, a stable
|
|
3
|
+
* string hash, gradients and ellipses. Everything is deterministic: the same
|
|
4
|
+
* inputs give the same drawing byte for byte, which is what makes caching
|
|
5
|
+
* rendered sprites and reproducible previews possible.
|
|
6
|
+
*/
|
|
7
|
+
import type { SKRSContext2D } from '@napi-rs/canvas';
|
|
8
|
+
|
|
9
|
+
/** The 2D context type of @napi-rs/canvas. */
|
|
10
|
+
export type X = SKRSContext2D;
|
|
11
|
+
export type RGB = [number, number, number];
|
|
12
|
+
export const TAU = Math.PI * 2;
|
|
13
|
+
|
|
14
|
+
/** `#RRGGBB` → RGB. */
|
|
15
|
+
export const hex = (h: string): RGB => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16)) as RGB;
|
|
16
|
+
/** Linear mix of two colours, `t` from 0 (a) to 1 (b). */
|
|
17
|
+
export const mix = (a: RGB, b: RGB, t: number): RGB => [0, 1, 2].map((i) => a[i]! + (b[i]! - a[i]!) * t) as RGB;
|
|
18
|
+
/** RGB (and alpha) → CSS `rgba(…)`. */
|
|
19
|
+
export const css = (c: RGB, a = 1) => `rgba(${c.map(Math.round).join(',')},${a})`;
|
|
20
|
+
export const clamp = (v: number, lo = 0, hi = 1) => Math.max(lo, Math.min(hi, v));
|
|
21
|
+
/** Smoothstep between two edges. */
|
|
22
|
+
export const smooth = (e0: number, e1: number, v: number) => {
|
|
23
|
+
const t = clamp((v - e0) / (e1 - e0));
|
|
24
|
+
return t * t * (3 - 2 * t);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** mulberry32: small, fast and seeded. */
|
|
28
|
+
export function rng(seed: number): () => number {
|
|
29
|
+
let s = seed | 0;
|
|
30
|
+
return () => {
|
|
31
|
+
s = (s + 0x6d2b79f5) | 0;
|
|
32
|
+
let t = Math.imul(s ^ (s >>> 15), 1 | s);
|
|
33
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
34
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Stable string hash (FNV-1a), to derive colours and seeds from ids. */
|
|
39
|
+
export function hashString(s: string): number {
|
|
40
|
+
let h = 0x811c9dc5;
|
|
41
|
+
for (let i = 0; i < s.length; i++) {
|
|
42
|
+
h ^= s.charCodeAt(i);
|
|
43
|
+
h = Math.imul(h, 0x01000193);
|
|
44
|
+
}
|
|
45
|
+
return h >>> 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Starts a path with an ellipse (degenerate radii are clamped). */
|
|
49
|
+
export function ellipse(c: X, x: number, y: number, rx: number, ry: number, rot = 0): void {
|
|
50
|
+
c.beginPath();
|
|
51
|
+
c.ellipse(x, y, Math.max(0.01, rx), Math.max(0.01, ry), rot, 0, TAU);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Radial gradient from colour stops. */
|
|
55
|
+
export function radial(c: X, x: number, y: number, r0: number, r1: number, stops: [number, string][], fx = x, fy = y) {
|
|
56
|
+
const g = c.createRadialGradient(fx, fy, r0, x, y, r1);
|
|
57
|
+
for (const [o, col] of stops) g.addColorStop(o, col);
|
|
58
|
+
return g;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Linear gradient from colour stops. */
|
|
62
|
+
export function linear(c: X, x0: number, y0: number, x1: number, y1: number, stops: [number, string][]) {
|
|
63
|
+
const g = c.createLinearGradient(x0, y0, x1, y1);
|
|
64
|
+
for (const [o, col] of stops) g.addColorStop(o, col);
|
|
65
|
+
return g;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Cubic Bézier curve sampled as a polyline. */
|
|
69
|
+
export function bezierPts(p: [number, number][], steps = 40): [number, number][] {
|
|
70
|
+
const out: [number, number][] = [];
|
|
71
|
+
for (let i = 0; i <= steps; i++) {
|
|
72
|
+
const t = i / steps, u = 1 - t;
|
|
73
|
+
out.push([0, 1].map((k) => u * u * u * p[0]![k]! + 3 * u * u * t * p[1]![k]! + 3 * u * t * t * p[2]![k]! + t * t * t * p[3]![k]!) as [number, number]);
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pixel shift against image retention: moves the whole layout by a pixel or
|
|
3
|
+
* two, on a slow cycle, so that nothing sits on exactly the same pixels for
|
|
4
|
+
* hours. Invisible from a metre away.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface PixelShift {
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
/** Largest offset, in px. */
|
|
10
|
+
amplitude: number;
|
|
11
|
+
/** How long each step of the cycle lasts, in ms. */
|
|
12
|
+
periodMs: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Shift {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A slow cycle of positions, so that a pixel that is off is never off for good. */
|
|
21
|
+
const CYCLE = [
|
|
22
|
+
[0, 0],
|
|
23
|
+
[1, 0],
|
|
24
|
+
[1, 0.5],
|
|
25
|
+
[1, 1],
|
|
26
|
+
[0.5, 1],
|
|
27
|
+
[0, 1],
|
|
28
|
+
] as const;
|
|
29
|
+
|
|
30
|
+
/** The offset to apply at `now`. */
|
|
31
|
+
export function pixelShift(now: number, o: PixelShift): Shift {
|
|
32
|
+
if (!o.enabled) return { x: 0, y: 0 };
|
|
33
|
+
const step = CYCLE[Math.floor(now / o.periodMs) % CYCLE.length]!;
|
|
34
|
+
return { x: Math.round(step[0] * o.amplitude), y: Math.round(step[1] * o.amplitude) };
|
|
35
|
+
}
|
package/src/sink.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a rendered frame goes. Anything with `send` will do: a USB panel
|
|
3
|
+
* client, a file another process forwards, an HTTP display.
|
|
4
|
+
*/
|
|
5
|
+
import { renameSync, writeFileSync } from 'node:fs';
|
|
6
|
+
|
|
7
|
+
export interface FrameSink {
|
|
8
|
+
send(frame: Buffer): Promise<void>;
|
|
9
|
+
close?(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Writes each frame to a file, atomically (temporary file in the same
|
|
14
|
+
* directory, then rename), so that a reader polling it never sees half a frame.
|
|
15
|
+
*/
|
|
16
|
+
export class FileSink implements FrameSink {
|
|
17
|
+
readonly path: string;
|
|
18
|
+
|
|
19
|
+
constructor(path: string) {
|
|
20
|
+
this.path = path;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async send(frame: Buffer): Promise<void> {
|
|
24
|
+
const tmp = `${this.path}.tmp`;
|
|
25
|
+
writeFileSync(tmp, frame);
|
|
26
|
+
renameSync(tmp, this.path);
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/sparkline.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sparkline geometry (bar-style).
|
|
3
|
+
*
|
|
4
|
+
* Pure computation: it does not touch the canvas, so it can be tested without
|
|
5
|
+
* rendering anything. It is drawn with bars rather than a line because the
|
|
6
|
+
* rest of the panel already speaks that language (bar gauges), and at this
|
|
7
|
+
* size a 1 px line on a dark background gets lost.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface SparkBar {
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
w: number;
|
|
14
|
+
h: number;
|
|
15
|
+
/** Position in the original series. */
|
|
16
|
+
index: number;
|
|
17
|
+
value: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SparkOptions {
|
|
21
|
+
/** Gap between bars. */
|
|
22
|
+
gap?: number;
|
|
23
|
+
/** Minimum height, so that a zero value still marks its slot. */
|
|
24
|
+
minHeight?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Fixed denominator instead of the series maximum. Needed when the values
|
|
27
|
+
* are already a meaningful ratio: per-core usage is measured against 100 %,
|
|
28
|
+
* and normalising it against the busiest core would make an idle machine
|
|
29
|
+
* look saturated.
|
|
30
|
+
*/
|
|
31
|
+
max?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Lays out `values` in `w` × `h`, with the origin at the top-left corner of
|
|
36
|
+
* the box (bars grow upwards from the bottom).
|
|
37
|
+
*
|
|
38
|
+
* By default the scale is relative to the series maximum: what matters is the
|
|
39
|
+
* shape ("am I way above normal today or the same as usual?"), not the
|
|
40
|
+
* absolute value, which is usually printed next to it in figures. Pass
|
|
41
|
+
* `opts.max` for an absolute scale.
|
|
42
|
+
*/
|
|
43
|
+
export function sparkBars(values: number[], w: number, h: number, opts: SparkOptions = {}): SparkBar[] {
|
|
44
|
+
const n = values.length;
|
|
45
|
+
if (n === 0 || w <= 0 || h <= 0) return [];
|
|
46
|
+
|
|
47
|
+
const gap = opts.gap ?? 2;
|
|
48
|
+
const minHeight = opts.minHeight ?? 1;
|
|
49
|
+
|
|
50
|
+
// With many bars the gaps eat up the width; the gap is sacrificed before the
|
|
51
|
+
// bar, since the bar is what carries the data.
|
|
52
|
+
const effGap = n > 1 && (w - gap * (n - 1)) / n < 1 ? 0 : gap;
|
|
53
|
+
const barW = Math.max(1, (w - effGap * (n - 1)) / n);
|
|
54
|
+
|
|
55
|
+
const max = opts.max ?? Math.max(...values);
|
|
56
|
+
return values.map((value, index) => {
|
|
57
|
+
// All-zero series: no valid denominator, so every bar is at the minimum. A
|
|
58
|
+
// division by zero would paint NaN and the canvas would silently swallow it.
|
|
59
|
+
const ratio = max > 0 ? Math.min(1, Math.max(0, value / max)) : 0;
|
|
60
|
+
const barH = Math.max(minHeight, Math.round(h * ratio));
|
|
61
|
+
return {
|
|
62
|
+
x: Math.round(index * (barW + effGap)),
|
|
63
|
+
y: Math.round(h - barH),
|
|
64
|
+
w: Math.max(1, Math.round(barW)),
|
|
65
|
+
h: barH,
|
|
66
|
+
index,
|
|
67
|
+
value,
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
}
|
package/src/text.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text drawing for small, high-contrast panels: tracked (letter-spaced) text,
|
|
3
|
+
* small caps, truncation with an ellipsis, fitting to a width and word-wrapped
|
|
4
|
+
* paragraphs with a hard line limit.
|
|
5
|
+
*/
|
|
6
|
+
import { GlobalFonts, type SKRSContext2D } from '@napi-rs/canvas';
|
|
7
|
+
|
|
8
|
+
let defaultFamily = 'sans-serif';
|
|
9
|
+
|
|
10
|
+
/** Family used when `TextOpts.family` is not given. */
|
|
11
|
+
export function setDefaultFamily(family: string): void {
|
|
12
|
+
defaultFamily = family;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Registers font files under your own family names. Register by absolute path
|
|
17
|
+
* and refer to those names: fontconfig may report families as composites
|
|
18
|
+
* ("DejaVu Sans,DejaVu Sans Condensed"). Returns the paths that failed.
|
|
19
|
+
*/
|
|
20
|
+
export function registerFontFiles(fonts: Record<string, string>): string[] {
|
|
21
|
+
return Object.entries(fonts).filter(([family, path]) => !GlobalFonts.registerFromPath(path, family)).map(([, path]) => path);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface TextOpts {
|
|
25
|
+
size: number;
|
|
26
|
+
family?: string;
|
|
27
|
+
color?: string;
|
|
28
|
+
/** Extra spacing between characters, in px. */
|
|
29
|
+
tracking?: number;
|
|
30
|
+
/** Small caps: everything upper-case, with originally lower-case letters drawn smaller. */
|
|
31
|
+
smallCaps?: boolean;
|
|
32
|
+
align?: 'left' | 'right' | 'center';
|
|
33
|
+
/** Height of the small caps relative to a capital letter. */
|
|
34
|
+
capRatio?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Glyph {
|
|
38
|
+
ch: string;
|
|
39
|
+
size: number;
|
|
40
|
+
width: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function glyphs(ctx: SKRSContext2D, text: string, o: TextOpts): Glyph[] {
|
|
44
|
+
const family = o.family ?? defaultFamily;
|
|
45
|
+
const small = o.smallCaps === true;
|
|
46
|
+
const capSize = o.size * (o.capRatio ?? 0.76);
|
|
47
|
+
const out: Glyph[] = [];
|
|
48
|
+
for (const ch of text) {
|
|
49
|
+
// In small caps, a lower-case letter is drawn upper-case at a smaller size.
|
|
50
|
+
const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
|
|
51
|
+
const size = isLower ? capSize : o.size;
|
|
52
|
+
const draw = small ? ch.toUpperCase() : ch;
|
|
53
|
+
ctx.font = `${size}px "${family}"`;
|
|
54
|
+
out.push({ ch: draw, size, width: ctx.measureText(draw).width });
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function advance(gs: Glyph[], tracking: number): number {
|
|
60
|
+
if (!gs.length) return 0;
|
|
61
|
+
// Tracking goes between characters, not after the last one.
|
|
62
|
+
return gs.reduce((a, g) => a + g.width, 0) + tracking * (gs.length - 1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Width the text would take with this tracking, without drawing it. */
|
|
66
|
+
export function measureTracked(ctx: SKRSContext2D, text: string, o: TextOpts): number {
|
|
67
|
+
return advance(glyphs(ctx, text, o), o.tracking ?? 0);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Draws character by character to apply tracking and small caps, which the
|
|
72
|
+
* canvas API does not offer. `y` is the baseline. Returns the width.
|
|
73
|
+
*/
|
|
74
|
+
export function drawTracked(ctx: SKRSContext2D, text: string, x: number, y: number, o: TextOpts): number {
|
|
75
|
+
const family = o.family ?? defaultFamily;
|
|
76
|
+
const tracking = o.tracking ?? 0;
|
|
77
|
+
const gs = glyphs(ctx, text, o);
|
|
78
|
+
const total = advance(gs, tracking);
|
|
79
|
+
let cursor = o.align === 'right' ? x - total : o.align === 'center' ? x - total / 2 : x;
|
|
80
|
+
if (o.color) ctx.fillStyle = o.color;
|
|
81
|
+
ctx.textBaseline = 'alphabetic';
|
|
82
|
+
for (const g of gs) {
|
|
83
|
+
ctx.font = `${g.size}px "${family}"`;
|
|
84
|
+
ctx.fillText(g.ch, cursor, y);
|
|
85
|
+
cursor += g.width + tracking;
|
|
86
|
+
}
|
|
87
|
+
return total;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Truncates with an ellipsis until the text fits in `maxWidth`.
|
|
92
|
+
*
|
|
93
|
+
* The search is binary rather than character by character: measuring is
|
|
94
|
+
* expensive (it walks glyph by glyph to apply tracking), so the naive loop is
|
|
95
|
+
* quadratic. With 240-character details that cost hundreds of milliseconds
|
|
96
|
+
* per frame.
|
|
97
|
+
*/
|
|
98
|
+
export function truncate(ctx: SKRSContext2D, text: string, maxWidth: number, o: TextOpts): string {
|
|
99
|
+
if (measureTracked(ctx, text, o) <= maxWidth) return text;
|
|
100
|
+
const chars = [...text];
|
|
101
|
+
const candidate = (n: number) => `${chars.slice(0, n).join('').trimEnd()}…`;
|
|
102
|
+
let lo = 0;
|
|
103
|
+
let hi = chars.length;
|
|
104
|
+
while (lo < hi) {
|
|
105
|
+
const mid = Math.ceil((lo + hi) / 2);
|
|
106
|
+
if (measureTracked(ctx, candidate(mid), o) <= maxWidth) lo = mid;
|
|
107
|
+
else hi = mid - 1;
|
|
108
|
+
}
|
|
109
|
+
return lo > 0 ? candidate(lo) : '…';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Largest size at which the whole text fits, never going below `minSize`.
|
|
114
|
+
* Useful for labels that identify something (e.g. a project name): shrinking
|
|
115
|
+
* them is better than cutting them.
|
|
116
|
+
*/
|
|
117
|
+
export function fitSize(ctx: SKRSContext2D, text: string, maxWidth: number, o: TextOpts, minSize: number): number {
|
|
118
|
+
let size = o.size;
|
|
119
|
+
while (size > minSize && measureTracked(ctx, text, { ...o, size }) > maxWidth) size -= 1;
|
|
120
|
+
return size;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Draws the text truncated to the available width. */
|
|
124
|
+
export function drawClipped(
|
|
125
|
+
ctx: SKRSContext2D,
|
|
126
|
+
text: string,
|
|
127
|
+
x: number,
|
|
128
|
+
y: number,
|
|
129
|
+
maxWidth: number,
|
|
130
|
+
o: TextOpts,
|
|
131
|
+
): number {
|
|
132
|
+
return drawTracked(ctx, truncate(ctx, text, maxWidth, o), x, y, o);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Longest prefix of `text` that fits in `maxWidth`, backing off to the last
|
|
137
|
+
* space so words are not split. If a single word does not fit, it cuts hard:
|
|
138
|
+
* splitting a long path is better than leaving the line empty.
|
|
139
|
+
*
|
|
140
|
+
* The search is binary because measuring is expensive: `measureTracked` walks
|
|
141
|
+
* character by character to apply tracking.
|
|
142
|
+
*/
|
|
143
|
+
function fitPrefix(ctx: SKRSContext2D, text: string, maxWidth: number, o: TextOpts): number {
|
|
144
|
+
if (measureTracked(ctx, text, o) <= maxWidth) return text.length;
|
|
145
|
+
let lo = 1;
|
|
146
|
+
let hi = text.length;
|
|
147
|
+
while (lo < hi) {
|
|
148
|
+
const mid = Math.ceil((lo + hi) / 2);
|
|
149
|
+
if (measureTracked(ctx, text.slice(0, mid), o) <= maxWidth) lo = mid;
|
|
150
|
+
else hi = mid - 1;
|
|
151
|
+
}
|
|
152
|
+
const space = text.lastIndexOf(' ', lo);
|
|
153
|
+
return space > 0 ? space : lo;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Splits the text into at most `maxLines` lines that fit in `maxWidth`.
|
|
158
|
+
* The last one is truncated with an ellipsis if text is left over, so it never
|
|
159
|
+
* overflows.
|
|
160
|
+
*/
|
|
161
|
+
export function wrapLines(
|
|
162
|
+
ctx: SKRSContext2D,
|
|
163
|
+
text: string,
|
|
164
|
+
maxWidth: number,
|
|
165
|
+
maxLines: number,
|
|
166
|
+
o: TextOpts,
|
|
167
|
+
): string[] {
|
|
168
|
+
const clean = text.replace(/\s+/g, ' ').trim();
|
|
169
|
+
if (!clean || maxLines < 1 || maxWidth <= 0) return [];
|
|
170
|
+
|
|
171
|
+
const lines: string[] = [];
|
|
172
|
+
let rest = clean;
|
|
173
|
+
while (rest && lines.length < maxLines) {
|
|
174
|
+
if (lines.length === maxLines - 1) {
|
|
175
|
+
lines.push(truncate(ctx, rest, maxWidth, o));
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
const cut = fitPrefix(ctx, rest, maxWidth, o);
|
|
179
|
+
lines.push(rest.slice(0, cut).trimEnd());
|
|
180
|
+
rest = rest.slice(cut).trimStart();
|
|
181
|
+
}
|
|
182
|
+
return lines;
|
|
183
|
+
}
|