@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,258 @@
|
|
|
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, type Canvas, type SKRSContext2D } from '@napi-rs/canvas';
|
|
14
|
+
import type { IconKind, Moon } from '@loksly/weather-astro';
|
|
15
|
+
|
|
16
|
+
/** Cloud body: three lobes and a flat base. */
|
|
17
|
+
function cloud(ctx: SKRSContext2D, cx: number, cy: number, s: number): void {
|
|
18
|
+
const r = s * 0.22;
|
|
19
|
+
ctx.beginPath();
|
|
20
|
+
ctx.arc(cx - s * 0.22, cy + s * 0.04, r, Math.PI * 0.5, Math.PI * 1.5);
|
|
21
|
+
ctx.arc(cx - s * 0.06, cy - s * 0.14, r * 1.25, Math.PI, Math.PI * 1.85);
|
|
22
|
+
ctx.arc(cx + s * 0.22, cy + s * 0.02, r * 0.95, Math.PI * 1.4, Math.PI * 0.5);
|
|
23
|
+
ctx.closePath();
|
|
24
|
+
ctx.fill();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function sun(ctx: SKRSContext2D, cx: number, cy: number, s: number, rays = true): void {
|
|
28
|
+
const r = s * 0.19;
|
|
29
|
+
ctx.beginPath();
|
|
30
|
+
ctx.arc(cx, cy, r, 0, Math.PI * 2);
|
|
31
|
+
ctx.fill();
|
|
32
|
+
if (!rays) return;
|
|
33
|
+
ctx.lineWidth = Math.max(1.5, s * 0.045);
|
|
34
|
+
ctx.lineCap = 'round';
|
|
35
|
+
for (let i = 0; i < 8; i += 1) {
|
|
36
|
+
const a = (i * Math.PI) / 4;
|
|
37
|
+
ctx.beginPath();
|
|
38
|
+
ctx.moveTo(cx + Math.cos(a) * r * 1.45, cy + Math.sin(a) * r * 1.45);
|
|
39
|
+
ctx.lineTo(cx + Math.cos(a) * r * 1.95, cy + Math.sin(a) * r * 1.95);
|
|
40
|
+
ctx.stroke();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Crescent moon: a disc with an offset disc subtracted from it.
|
|
46
|
+
*
|
|
47
|
+
* The cut-out is done on a **separate canvas** and then stamped. An earlier
|
|
48
|
+
* version applied `destination-out` directly to the target canvas, and that
|
|
49
|
+
* does not cut the moon: it punches a hole through everything underneath. On
|
|
50
|
+
* the card's gradient it came out as a black disc, and on a photo it would
|
|
51
|
+
* have been a bite taken out of the picture.
|
|
52
|
+
*/
|
|
53
|
+
function moon(ctx: SKRSContext2D, cx: number, cy: number, s: number): void {
|
|
54
|
+
const r = s * 0.24;
|
|
55
|
+
const pad = 2;
|
|
56
|
+
const side = Math.ceil(r * 2 + pad * 2);
|
|
57
|
+
const off = createCanvas(side, side) as Canvas;
|
|
58
|
+
const o = off.getContext('2d');
|
|
59
|
+
const c = side / 2;
|
|
60
|
+
|
|
61
|
+
o.fillStyle = ctx.fillStyle as string;
|
|
62
|
+
o.beginPath();
|
|
63
|
+
o.arc(c, c, r, 0, Math.PI * 2);
|
|
64
|
+
o.fill();
|
|
65
|
+
|
|
66
|
+
// Fine here: the canvas is otherwise empty, so only the moon gets cut.
|
|
67
|
+
o.globalCompositeOperation = 'destination-out';
|
|
68
|
+
o.beginPath();
|
|
69
|
+
o.arc(c + r * 0.55, c - r * 0.42, r * 0.92, 0, Math.PI * 2);
|
|
70
|
+
o.fill();
|
|
71
|
+
|
|
72
|
+
ctx.drawImage(off, cx - c, cy - c);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Drops or streaks under the cloud. `len` stretches them: short for drizzle, long for rain. */
|
|
76
|
+
function drops(ctx: SKRSContext2D, cx: number, cy: number, s: number, len: number): void {
|
|
77
|
+
ctx.lineWidth = Math.max(1.5, s * 0.05);
|
|
78
|
+
ctx.lineCap = 'round';
|
|
79
|
+
for (const dx of [-0.2, 0, 0.2]) {
|
|
80
|
+
ctx.beginPath();
|
|
81
|
+
ctx.moveTo(cx + s * dx + s * 0.05, cy + s * 0.22);
|
|
82
|
+
ctx.lineTo(cx + s * dx - s * 0.02, cy + s * (0.22 + len));
|
|
83
|
+
ctx.stroke();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function snowflakes(ctx: SKRSContext2D, cx: number, cy: number, s: number): void {
|
|
88
|
+
ctx.lineWidth = Math.max(1.2, s * 0.035);
|
|
89
|
+
ctx.lineCap = 'round';
|
|
90
|
+
for (const dx of [-0.2, 0.05]) {
|
|
91
|
+
const x = cx + s * dx;
|
|
92
|
+
const y = cy + s * 0.3;
|
|
93
|
+
for (let i = 0; i < 3; i += 1) {
|
|
94
|
+
const a = (i * Math.PI) / 3;
|
|
95
|
+
ctx.beginPath();
|
|
96
|
+
ctx.moveTo(x - Math.cos(a) * s * 0.07, y - Math.sin(a) * s * 0.07);
|
|
97
|
+
ctx.lineTo(x + Math.cos(a) * s * 0.07, y + Math.sin(a) * s * 0.07);
|
|
98
|
+
ctx.stroke();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function bolt(ctx: SKRSContext2D, cx: number, cy: number, s: number): void {
|
|
104
|
+
ctx.beginPath();
|
|
105
|
+
ctx.moveTo(cx + s * 0.06, cy + s * 0.16);
|
|
106
|
+
ctx.lineTo(cx - s * 0.1, cy + s * 0.34);
|
|
107
|
+
ctx.lineTo(cx + s * 0.01, cy + s * 0.34);
|
|
108
|
+
ctx.lineTo(cx - s * 0.06, cy + s * 0.5);
|
|
109
|
+
ctx.lineTo(cx + s * 0.14, cy + s * 0.28);
|
|
110
|
+
ctx.lineTo(cx + s * 0.03, cy + s * 0.28);
|
|
111
|
+
ctx.closePath();
|
|
112
|
+
ctx.fill();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function fogLines(ctx: SKRSContext2D, cx: number, cy: number, s: number): void {
|
|
116
|
+
ctx.lineWidth = Math.max(1.5, s * 0.055);
|
|
117
|
+
ctx.lineCap = 'round';
|
|
118
|
+
for (const [i, w] of [0.34, 0.4, 0.28].entries()) {
|
|
119
|
+
const y = cy + s * (0.2 + i * 0.13);
|
|
120
|
+
ctx.beginPath();
|
|
121
|
+
ctx.moveTo(cx - s * w, y);
|
|
122
|
+
ctx.lineTo(cx + s * w, y);
|
|
123
|
+
ctx.stroke();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Paints the weather icon.
|
|
129
|
+
*
|
|
130
|
+
* `color` is used for both fill and stroke: these are single-ink icons, which
|
|
131
|
+
* is what holds up well over a background photo with a dimming veil on top.
|
|
132
|
+
*/
|
|
133
|
+
export function drawWeatherIcon(
|
|
134
|
+
ctx: SKRSContext2D,
|
|
135
|
+
kind: IconKind,
|
|
136
|
+
cx: number,
|
|
137
|
+
cy: number,
|
|
138
|
+
size: number,
|
|
139
|
+
color: string,
|
|
140
|
+
): void {
|
|
141
|
+
ctx.save();
|
|
142
|
+
ctx.fillStyle = color;
|
|
143
|
+
ctx.strokeStyle = color;
|
|
144
|
+
|
|
145
|
+
switch (kind) {
|
|
146
|
+
case 'sun':
|
|
147
|
+
sun(ctx, cx, cy, size);
|
|
148
|
+
break;
|
|
149
|
+
case 'moon':
|
|
150
|
+
moon(ctx, cx, cy, size);
|
|
151
|
+
break;
|
|
152
|
+
case 'sun-cloud':
|
|
153
|
+
sun(ctx, cx - size * 0.16, cy - size * 0.18, size * 0.72);
|
|
154
|
+
cloud(ctx, cx + size * 0.06, cy + size * 0.1, size);
|
|
155
|
+
break;
|
|
156
|
+
case 'moon-cloud':
|
|
157
|
+
moon(ctx, cx - size * 0.16, cy - size * 0.2, size * 0.72);
|
|
158
|
+
cloud(ctx, cx + size * 0.06, cy + size * 0.1, size);
|
|
159
|
+
break;
|
|
160
|
+
case 'cloud':
|
|
161
|
+
cloud(ctx, cx, cy, size);
|
|
162
|
+
break;
|
|
163
|
+
case 'fog':
|
|
164
|
+
cloud(ctx, cx, cy - size * 0.12, size * 0.9);
|
|
165
|
+
fogLines(ctx, cx, cy, size);
|
|
166
|
+
break;
|
|
167
|
+
case 'drizzle':
|
|
168
|
+
cloud(ctx, cx, cy - size * 0.1, size);
|
|
169
|
+
drops(ctx, cx, cy - size * 0.1, size, 0.14);
|
|
170
|
+
break;
|
|
171
|
+
case 'rain':
|
|
172
|
+
cloud(ctx, cx, cy - size * 0.12, size);
|
|
173
|
+
drops(ctx, cx, cy - size * 0.12, size, 0.26);
|
|
174
|
+
break;
|
|
175
|
+
case 'snow':
|
|
176
|
+
cloud(ctx, cx, cy - size * 0.14, size);
|
|
177
|
+
snowflakes(ctx, cx, cy - size * 0.14, size);
|
|
178
|
+
break;
|
|
179
|
+
case 'storm':
|
|
180
|
+
cloud(ctx, cx, cy - size * 0.14, size);
|
|
181
|
+
bolt(ctx, cx, cy - size * 0.14, size);
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
ctx.restore();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The moon in its actual phase: the lit disc minus the shadowed part.
|
|
189
|
+
*
|
|
190
|
+
* The shadow is bounded by two curves: the **limb** (the half circle on the
|
|
191
|
+
* dark side) and the **terminator**, which seen face-on projects as a half
|
|
192
|
+
* ellipse with horizontal semi-axis `R·(1−2k)`, where `k` is the illuminated
|
|
193
|
+
* fraction. That semi-axis is signed, and that is the whole trick:
|
|
194
|
+
*
|
|
195
|
+
* - `k < 0.5` → positive: the terminator falls on the lit side and the
|
|
196
|
+
* shadow goes past the centre, which is what a thin crescent looks like.
|
|
197
|
+
* - `k > 0.5` → negative: it moves into the dark side and the shadow is left
|
|
198
|
+
* as a thin sliver, which is a gibbous moon.
|
|
199
|
+
*
|
|
200
|
+
* It is traced as a polygon instead of with `ellipse()` because the sweep
|
|
201
|
+
* direction of the arcs changes with the sign of the semi-axis and ends up as
|
|
202
|
+
* code full of flags; with parametric points the same loop works for all
|
|
203
|
+
* eight phases.
|
|
204
|
+
*
|
|
205
|
+
* The cut-out also goes on a separate canvas, for the same reason as the
|
|
206
|
+
* simple moon: on the target canvas `destination-out` would punch through the
|
|
207
|
+
* background.
|
|
208
|
+
*/
|
|
209
|
+
export function drawMoonPhase(
|
|
210
|
+
ctx: SKRSContext2D,
|
|
211
|
+
moon: Moon,
|
|
212
|
+
cx: number,
|
|
213
|
+
cy: number,
|
|
214
|
+
size: number,
|
|
215
|
+
color: string,
|
|
216
|
+
): void {
|
|
217
|
+
const r = size / 2;
|
|
218
|
+
const pad = 2;
|
|
219
|
+
const side = Math.ceil(size + pad * 2);
|
|
220
|
+
const off = createCanvas(side, side) as Canvas;
|
|
221
|
+
const o = off.getContext('2d');
|
|
222
|
+
const c = side / 2;
|
|
223
|
+
|
|
224
|
+
// Full disc, plus a faint ring so that an almost-new moon does not vanish.
|
|
225
|
+
o.fillStyle = color;
|
|
226
|
+
o.beginPath();
|
|
227
|
+
o.arc(c, c, r, 0, Math.PI * 2);
|
|
228
|
+
o.fill();
|
|
229
|
+
|
|
230
|
+
const f = 1 - 2 * moon.illumination;
|
|
231
|
+
// In the northern hemisphere a waxing moon is lit from the right.
|
|
232
|
+
const dark = moon.waxing ? -1 : 1;
|
|
233
|
+
const STEPS = 48;
|
|
234
|
+
|
|
235
|
+
o.globalCompositeOperation = 'destination-out';
|
|
236
|
+
o.beginPath();
|
|
237
|
+
for (let i = 0; i <= STEPS; i += 1) {
|
|
238
|
+
const u = (i / STEPS) * Math.PI;
|
|
239
|
+
o.lineTo(c + dark * r * Math.sin(u), c - r * Math.cos(u));
|
|
240
|
+
}
|
|
241
|
+
for (let i = STEPS; i >= 0; i -= 1) {
|
|
242
|
+
const u = (i / STEPS) * Math.PI;
|
|
243
|
+
o.lineTo(c - dark * r * f * Math.sin(u), c - r * Math.cos(u));
|
|
244
|
+
}
|
|
245
|
+
o.closePath();
|
|
246
|
+
o.fill();
|
|
247
|
+
|
|
248
|
+
ctx.save();
|
|
249
|
+
ctx.drawImage(off, cx - c, cy - c);
|
|
250
|
+
// Ring around the whole disc: without it, a new moon would be an invisible
|
|
251
|
+
// hole, indistinguishable from "nothing was drawn here".
|
|
252
|
+
ctx.strokeStyle = 'rgba(255,255,255,0.22)';
|
|
253
|
+
ctx.lineWidth = Math.max(1, size * 0.02);
|
|
254
|
+
ctx.beginPath();
|
|
255
|
+
ctx.arc(cx, cy, r, 0, Math.PI * 2);
|
|
256
|
+
ctx.stroke();
|
|
257
|
+
ctx.restore();
|
|
258
|
+
}
|