-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnowPlayingCard.ts
More file actions
290 lines (268 loc) Β· 9.19 KB
/
nowPlayingCard.ts
File metadata and controls
290 lines (268 loc) Β· 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import type { Track } from "./tauri/track";
import { resolveRemoteImage } from "./tauri/artwork";
import { dominantColor } from "./dominantColor";
/**
* Build a 1080Γ1080 square "Now Playing" share card on an offscreen
* Canvas and return the PNG Blob. Same Canvas-only philosophy as
* `wrappedCard.ts` β text uses the WebView's native font stack, no
* external dep. The visual recipe:
*
* 1. Backdrop = blurred + darkened copy of the cover (or a neutral
* gradient when there's no artwork).
* 2. Cover artwork centred at ~58 % of the card width with rounded
* corners and a generous drop-shadow.
* 3. Title (big bold) + artist (lighter) + album (small dim) stacked
* below the cover.
* 4. WaveFlow brand strip + "Now Playing" eyebrow at the bottom.
*
* The cover image is read **before** rendering so we can extract its
* dominant colour and use it for the bottom-of-card accent strip β
* that little flourish keeps the card visually coherent with the
* artwork instead of a generic dark backdrop on every share.
*/
export interface NowPlayingCardOptions {
labels: {
nowPlaying: string;
on: string; // "on WaveFlow" or equivalent
};
}
const SIZE = 1080;
export async function renderNowPlayingCard(
track: Track,
opts: NowPlayingCardOptions,
): Promise<Blob> {
const canvas = document.createElement("canvas");
canvas.width = SIZE;
canvas.height = SIZE;
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Canvas 2D context unavailable");
// Smoother image scaling for the inevitable cover upscale below.
// Browsers default to "low" which produces visibly soft cover art on
// a 1080Γ1080 card.
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "high";
// ----- Load cover (if any) ------------------------------------------
// Prefer the full-resolution artwork over the thumbnails β `_2x` is
// 128Γ128 and `_1x` is 64Γ64 (see src-tauri/src/thumbnails.rs), both
// far too small for the 580 px cover slot and 1320 px blurred
// backdrop on this 1080Β² card. The original `artwork_path` is the
// image the scanner extracted from the audio file (typically
// 500β1500 px square), which gives a crisp share card.
const coverSrc = resolveRemoteImage(
track.artwork_path ?? track.artwork_path_2x ?? track.artwork_path_1x,
null,
);
let coverImg: HTMLImageElement | null = null;
if (coverSrc) {
try {
coverImg = await loadImage(coverSrc);
} catch {
coverImg = null;
}
}
// ----- Dominant colour ----------------------------------------------
let accent: { r: number; g: number; b: number } = { r: 30, g: 30, b: 30 };
if (coverSrc) {
try {
accent = await dominantColor(coverSrc);
} catch {
// Fall through to the neutral default β a Now Playing card
// without artwork-derived accent still looks fine.
}
}
// ----- Backdrop ------------------------------------------------------
if (coverImg) {
// Real CSS-style blur via the 2D context filter. Both
// Chromium-WebView2 (Windows) and WebKitGTK 2.40+ (Linux) support
// it; macOS WebKit has supported it for years. Falls back to a no-
// op string on the off chance an old engine doesn't recognise it,
// which gives a slightly less blurry but still readable backdrop.
ctx.save();
ctx.filter = "blur(60px) brightness(0.6)";
// Draw a bit past the edges so the blur's transparent halo doesn't
// bleed into the canvas border.
ctx.drawImage(coverImg, -80, -80, SIZE + 160, SIZE + 160);
ctx.restore();
// Tinted gradient overlay so the foreground text + cover stay
// readable on bright artwork.
const wash = ctx.createLinearGradient(0, 0, 0, SIZE);
wash.addColorStop(0, `rgba(0,0,0,0.45)`);
wash.addColorStop(1, `rgba(${darken(accent, 0.4)},0.75)`);
ctx.fillStyle = wash;
ctx.fillRect(0, 0, SIZE, SIZE);
} else {
const grad = ctx.createLinearGradient(0, 0, SIZE, SIZE);
grad.addColorStop(0, `rgb(${accent.r},${accent.g},${accent.b})`);
grad.addColorStop(1, "#0c0c0c");
ctx.fillStyle = grad;
ctx.fillRect(0, 0, SIZE, SIZE);
}
// ----- Eyebrow -------------------------------------------------------
ctx.fillStyle = "rgba(255,255,255,0.7)";
ctx.font =
"600 28px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif";
ctx.textAlign = "center";
ctx.fillText(opts.labels.nowPlaying.toUpperCase(), SIZE / 2, 80);
// ----- Cover (centerpiece) ------------------------------------------
const coverSize = 580;
const coverX = (SIZE - coverSize) / 2;
const coverY = 140;
// Soft drop shadow under the cover.
ctx.save();
ctx.shadowColor = "rgba(0,0,0,0.5)";
ctx.shadowBlur = 40;
ctx.shadowOffsetY = 12;
drawCard(
ctx,
coverX,
coverY,
coverSize,
coverSize,
32,
"rgba(255,255,255,0.05)",
);
ctx.restore();
if (coverImg) {
drawRoundedImage(ctx, coverImg, coverX, coverY, coverSize, coverSize, 32);
} else {
// Music-note placeholder when there's no artwork.
ctx.fillStyle = "rgba(255,255,255,0.3)";
ctx.font =
"900 200px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif";
ctx.textAlign = "center";
ctx.fillText("βͺ", SIZE / 2, coverY + coverSize / 2 + 80);
}
// ----- Track info ---------------------------------------------------
const infoY = coverY + coverSize + 70;
ctx.fillStyle = "#fff";
ctx.font =
"800 52px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif";
ctx.textAlign = "center";
ctx.fillText(truncate(ctx, track.title, SIZE - 160), SIZE / 2, infoY);
if (track.artist_name) {
ctx.fillStyle = "rgba(255,255,255,0.8)";
ctx.font =
"500 34px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif";
ctx.fillText(
truncate(ctx, track.artist_name, SIZE - 160),
SIZE / 2,
infoY + 50,
);
}
if (track.album_title) {
ctx.fillStyle = "rgba(255,255,255,0.5)";
ctx.font =
"400 26px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif";
ctx.fillText(
truncate(ctx, track.album_title, SIZE - 200),
SIZE / 2,
infoY + 90,
);
}
// ----- Accent strip + brand ----------------------------------------
// Thin accent bar across the bottom in the artwork's dominant
// colour so each card carries a hint of the cover's palette.
ctx.fillStyle = `rgb(${accent.r},${accent.g},${accent.b})`;
ctx.fillRect(0, SIZE - 8, SIZE, 8);
ctx.fillStyle = "rgba(255,255,255,0.5)";
ctx.font =
"600 24px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif";
ctx.textAlign = "center";
ctx.fillText(`${opts.labels.on} Β· WaveFlow`, SIZE / 2, SIZE - 36);
return await new Promise<Blob>((resolve, reject) => {
canvas.toBlob(
(blob) => {
if (blob) resolve(blob);
else reject(new Error("canvas.toBlob returned null"));
},
"image/png",
0.95,
);
});
}
// =============================================================================
// Helpers
// =============================================================================
function loadImage(src: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const img = new Image();
img.crossOrigin = "anonymous";
img.onload = () => resolve(img);
img.onerror = (e) => reject(e);
img.src = src;
});
}
function roundedRect(
ctx: CanvasRenderingContext2D,
x: number,
y: number,
w: number,
h: number,
r: number,
) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function drawCard(
ctx: CanvasRenderingContext2D,
x: number,
y: number,
w: number,
h: number,
r: number,
fill: string,
) {
ctx.fillStyle = fill;
roundedRect(ctx, x, y, w, h, r);
ctx.fill();
}
function drawRoundedImage(
ctx: CanvasRenderingContext2D,
img: HTMLImageElement,
x: number,
y: number,
w: number,
h: number,
r: number,
) {
ctx.save();
roundedRect(ctx, x, y, w, h, r);
ctx.clip();
// Cover-fit: scale + centre-crop so square covers fit a square
// frame even if the source isn't 1:1.
const ratio = Math.max(w / img.width, h / img.height);
const drawW = img.width * ratio;
const drawH = img.height * ratio;
ctx.drawImage(img, x + (w - drawW) / 2, y + (h - drawH) / 2, drawW, drawH);
ctx.restore();
}
function truncate(
ctx: CanvasRenderingContext2D,
text: string,
maxWidth: number,
): string {
if (ctx.measureText(text).width <= maxWidth) return text;
let s = text;
while (s.length > 1 && ctx.measureText(s + "β¦").width > maxWidth) {
s = s.slice(0, -1);
}
return s + "β¦";
}
function darken(
c: { r: number; g: number; b: number },
factor: number,
): string {
const r = Math.max(0, Math.round(c.r * factor));
const g = Math.max(0, Math.round(c.g * factor));
const b = Math.max(0, Math.round(c.b * factor));
return `${r},${g},${b}`;
}