@arraypress/waveform-player 1.17.0 → 1.18.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/README.md +4 -4
- package/dist/waveform-player.cjs +81 -99
- package/dist/waveform-player.cjs.map +2 -2
- package/dist/waveform-player.css +1 -1
- package/dist/waveform-player.esm.js +12 -16
- package/dist/waveform-player.esm.js.map +3 -3
- package/dist/waveform-player.js +81 -99
- package/dist/waveform-player.min.js +12 -16
- package/dist/waveform-player.min.js.map +3 -3
- package/index.d.ts +8 -11
- package/package.json +1 -1
- package/src/css/waveform-player.css +21 -3
- package/src/js/core.js +50 -54
- package/src/js/drawing.js +43 -23
- package/src/js/themes.js +12 -22
- package/src/js/utils.js +2 -7
package/index.d.ts
CHANGED
|
@@ -99,6 +99,8 @@ export interface WaveformPlayerOptions {
|
|
|
99
99
|
barSpacing?: number;
|
|
100
100
|
/** Rounded bar-cap radius in pixels (bars/mirror). `0` = square. @default 1 */
|
|
101
101
|
barRadius?: number;
|
|
102
|
+
/** Gradient axis when waveformColor/progressColor is an array of stops. @default 'vertical' */
|
|
103
|
+
waveformGradient?: 'vertical' | 'horizontal' | 'diagonal';
|
|
102
104
|
|
|
103
105
|
// ── Colours ───────────────────────────────────────────────────
|
|
104
106
|
/** Force a colour preset, or `null` to auto-detect. @default null */
|
|
@@ -110,12 +112,8 @@ export interface WaveformPlayerOptions {
|
|
|
110
112
|
waveformColor?: string | string[] | null;
|
|
111
113
|
/** Played-through colour. Also accepts an array of stops for a gradient. */
|
|
112
114
|
progressColor?: string | string[] | null;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
textColor?: string | null;
|
|
116
|
-
textSecondaryColor?: string | null;
|
|
117
|
-
backgroundColor?: string | null;
|
|
118
|
-
borderColor?: string | null;
|
|
115
|
+
// DOM chrome colours are CSS variables now (--wfp-button-color,
|
|
116
|
+
// --wfp-text-color, --wfp-text-secondary-color), not options.
|
|
119
117
|
|
|
120
118
|
// ── Playback ──────────────────────────────────────────────────
|
|
121
119
|
/** Initial playback rate. @default 1 */
|
|
@@ -149,7 +147,7 @@ export interface WaveformPlayerOptions {
|
|
|
149
147
|
buttonSize?: number | string | null;
|
|
150
148
|
/** Show transport controls. @default true */
|
|
151
149
|
showControls?: boolean;
|
|
152
|
-
/** Show the info (title/
|
|
150
|
+
/** Show the info (title/artist) block. @default true */
|
|
153
151
|
showInfo?: boolean;
|
|
154
152
|
/** Show current/total time. @default true */
|
|
155
153
|
showTime?: boolean;
|
|
@@ -186,7 +184,7 @@ export interface WaveformPlayerOptions {
|
|
|
186
184
|
|
|
187
185
|
// ── Content metadata ──────────────────────────────────────────
|
|
188
186
|
title?: string | null;
|
|
189
|
-
|
|
187
|
+
artist?: string | null;
|
|
190
188
|
artwork?: string | null;
|
|
191
189
|
album?: string;
|
|
192
190
|
/** Message shown when audio fails to load. @default 'Unable to load audio' */
|
|
@@ -231,8 +229,7 @@ export interface WaveformTimeUpdateEventDetail {
|
|
|
231
229
|
export interface WaveformTrackDetail {
|
|
232
230
|
url: string;
|
|
233
231
|
title: string | null;
|
|
234
|
-
|
|
235
|
-
artist?: string;
|
|
232
|
+
artist: string | null;
|
|
236
233
|
artwork: string | null;
|
|
237
234
|
/** Chapter markers for the track (forwarded so controllers don't re-fetch). */
|
|
238
235
|
markers?: WaveformMarker[];
|
|
@@ -303,7 +300,7 @@ export declare class WaveformPlayer {
|
|
|
303
300
|
/** Load (or replace) the audio URL and regenerate the waveform. */
|
|
304
301
|
load(url: string): Promise<void>;
|
|
305
302
|
/** Load a new track without re-instantiating; updates metadata then plays. */
|
|
306
|
-
loadTrack(url: string, title?: string | null,
|
|
303
|
+
loadTrack(url: string, title?: string | null, artist?: string | null, options?: WaveformPlayerOptions): Promise<void>;
|
|
307
304
|
/** Seek to an absolute time in seconds (self mode). */
|
|
308
305
|
seekTo(seconds: number): void;
|
|
309
306
|
/** Seek to a fraction of total duration, 0..1 (self mode). */
|
package/package.json
CHANGED
|
@@ -6,6 +6,20 @@
|
|
|
6
6
|
/* Neutral accent for the keyboard-focus ring + active states. Monochrome
|
|
7
7
|
by default (shadcn-style); override this variable to re-tint it. */
|
|
8
8
|
--wfp-accent: #71717a;
|
|
9
|
+
|
|
10
|
+
/* DOM chrome colours (dark defaults). Override any of these to re-theme the
|
|
11
|
+
button/text without touching JS. Canvas colours stay JS options. */
|
|
12
|
+
--wfp-button-color: rgba(255, 255, 255, 0.9);
|
|
13
|
+
--wfp-text-color: #ffffff;
|
|
14
|
+
--wfp-text-secondary-color: rgba(255, 255, 255, 0.6);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/* Light theme — the player adds .waveform-theme-light when it detects a light
|
|
18
|
+
page (or with colorPreset: 'light'). */
|
|
19
|
+
.waveform-player.waveform-theme-light {
|
|
20
|
+
--wfp-button-color: rgba(0, 0, 0, 0.8);
|
|
21
|
+
--wfp-text-color: #333333;
|
|
22
|
+
--wfp-text-secondary-color: rgba(0, 0, 0, 0.6);
|
|
9
23
|
}
|
|
10
24
|
|
|
11
25
|
.waveform-player * {
|
|
@@ -39,7 +53,7 @@
|
|
|
39
53
|
border-radius: 50%;
|
|
40
54
|
border: 2px solid currentColor;
|
|
41
55
|
background: transparent;
|
|
42
|
-
color: inherit;
|
|
56
|
+
color: var(--wfp-button-color, inherit);
|
|
43
57
|
cursor: pointer;
|
|
44
58
|
display: flex;
|
|
45
59
|
align-items: center;
|
|
@@ -154,13 +168,15 @@
|
|
|
154
168
|
}
|
|
155
169
|
|
|
156
170
|
.waveform-title {
|
|
171
|
+
color: var(--wfp-text-color);
|
|
157
172
|
white-space: nowrap;
|
|
158
173
|
overflow: hidden;
|
|
159
174
|
text-overflow: ellipsis;
|
|
160
175
|
font-weight: 500;
|
|
161
176
|
}
|
|
162
177
|
|
|
163
|
-
.waveform-
|
|
178
|
+
.waveform-artist {
|
|
179
|
+
color: var(--wfp-text-secondary-color);
|
|
164
180
|
font-size: 11px;
|
|
165
181
|
white-space: nowrap;
|
|
166
182
|
overflow: hidden;
|
|
@@ -168,6 +184,7 @@
|
|
|
168
184
|
}
|
|
169
185
|
|
|
170
186
|
.waveform-time {
|
|
187
|
+
color: var(--wfp-text-secondary-color);
|
|
171
188
|
font-size: 11px;
|
|
172
189
|
white-space: nowrap;
|
|
173
190
|
flex-shrink: 0;
|
|
@@ -175,6 +192,7 @@
|
|
|
175
192
|
|
|
176
193
|
/* BPM display */
|
|
177
194
|
.waveform-bpm {
|
|
195
|
+
color: var(--wfp-text-secondary-color);
|
|
178
196
|
font-size: 11px;
|
|
179
197
|
white-space: nowrap;
|
|
180
198
|
flex-shrink: 0;
|
|
@@ -476,7 +494,7 @@
|
|
|
476
494
|
font-size: 12px;
|
|
477
495
|
}
|
|
478
496
|
|
|
479
|
-
.waveform-
|
|
497
|
+
.waveform-artist,
|
|
480
498
|
.waveform-time,
|
|
481
499
|
.waveform-bpm {
|
|
482
500
|
font-size: 10px;
|
package/src/js/core.js
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
DEFAULT_SAMPLES
|
|
18
18
|
} from './utils.js';
|
|
19
19
|
|
|
20
|
-
import {DEFAULT_OPTIONS, STYLE_DEFAULTS, getColorPreset, COLOR_PRESETS} from './themes.js';
|
|
20
|
+
import {DEFAULT_OPTIONS, STYLE_DEFAULTS, getColorPreset, COLOR_PRESETS, detectColorScheme} from './themes.js';
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Placeholder shown when an artwork URL fails to load (404 / broken) — a muted
|
|
@@ -96,6 +96,9 @@ export class WaveformPlayer {
|
|
|
96
96
|
// explicit colorPreset or hand-set colour is never overridden.
|
|
97
97
|
this._autoTheme = this.options.colorPreset == null || !COLOR_PRESETS[this.options.colorPreset];
|
|
98
98
|
this._presetKeys = [];
|
|
99
|
+
// Resolved scheme name for the CSS theme class (CSS provides the DOM
|
|
100
|
+
// chrome colours via --wfp-* variables).
|
|
101
|
+
this._scheme = (this.options.colorPreset && COLOR_PRESETS[this.options.colorPreset]) ? this.options.colorPreset : detectColorScheme();
|
|
99
102
|
|
|
100
103
|
// Apply preset colors only if individual colors aren't explicitly set
|
|
101
104
|
for (const [key, value] of Object.entries(preset)) {
|
|
@@ -230,7 +233,7 @@ export class WaveformPlayer {
|
|
|
230
233
|
*
|
|
231
234
|
* Clears the container, resolves button alignment (`auto` → `bottom` for
|
|
232
235
|
* the `bars` style, `center` otherwise), and conditionally renders the play
|
|
233
|
-
* button, info row (artwork/title/
|
|
236
|
+
* button, info row (artwork/title/artist), BPM badge, playback-speed
|
|
234
237
|
* menu, and time display based on the relevant `show*` options. Caches the
|
|
235
238
|
* canvas, controls, and text elements onto `this`, then sizes the canvas.
|
|
236
239
|
* @private
|
|
@@ -258,14 +261,11 @@ export class WaveformPlayer {
|
|
|
258
261
|
if (isPreview) {
|
|
259
262
|
this.container.classList.add('waveform-layout-preview');
|
|
260
263
|
}
|
|
264
|
+
this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');
|
|
261
265
|
|
|
262
266
|
// Build play button HTML (conditional)
|
|
263
267
|
const buttonHTML = this.options.showControls ? `
|
|
264
|
-
<button class="waveform-btn${this.options.buttonStyle === 'minimal' ? ' waveform-btn-minimal' : ''}" aria-label="Play/Pause" style="
|
|
265
|
-
border-color: ${this.options.buttonColor};
|
|
266
|
-
color: ${this.options.buttonColor};
|
|
267
|
-
${this.options.buttonSize != null ? `--wfp-btn-size: ${typeof this.options.buttonSize === 'number' ? `${this.options.buttonSize}px` : this.options.buttonSize};` : ''}
|
|
268
|
-
">
|
|
268
|
+
<button class="waveform-btn${this.options.buttonStyle === 'minimal' ? ' waveform-btn-minimal' : ''}" aria-label="Play/Pause"${this.options.buttonSize != null ? ` style="--wfp-btn-size: ${typeof this.options.buttonSize === 'number' ? `${this.options.buttonSize}px` : this.options.buttonSize};"` : ''}>
|
|
269
269
|
<span class="waveform-icon-play">${this.options.playIcon}</span>
|
|
270
270
|
<span class="waveform-icon-pause" style="display:none;">${this.options.pauseIcon}</span>
|
|
271
271
|
</button>
|
|
@@ -284,12 +284,12 @@ export class WaveformPlayer {
|
|
|
284
284
|
">
|
|
285
285
|
` : ''}
|
|
286
286
|
<div class="waveform-text">
|
|
287
|
-
<span class="waveform-title"
|
|
288
|
-
${this.options.
|
|
287
|
+
<span class="waveform-title"></span>
|
|
288
|
+
${this.options.artist ? `<span class="waveform-artist">${this.options.artist}</span>` : ''}
|
|
289
289
|
</div>
|
|
290
290
|
<div class="waveform-meta" style="display: flex; align-items: center; gap: 1rem;">
|
|
291
291
|
${this.options.showBPM ? `
|
|
292
|
-
<span class="waveform-bpm" style="
|
|
292
|
+
<span class="waveform-bpm" style="display: none;">
|
|
293
293
|
<span class="bpm-value">--</span> BPM
|
|
294
294
|
</span>
|
|
295
295
|
` : ''}
|
|
@@ -306,7 +306,7 @@ export class WaveformPlayer {
|
|
|
306
306
|
</div>
|
|
307
307
|
` : ''}
|
|
308
308
|
${this.options.showTime ? `
|
|
309
|
-
<span class="waveform-time"
|
|
309
|
+
<span class="waveform-time">
|
|
310
310
|
<span class="time-current">0:00</span> / <span class="time-total">0:00</span>
|
|
311
311
|
</span>
|
|
312
312
|
` : ''}
|
|
@@ -341,7 +341,7 @@ export class WaveformPlayer {
|
|
|
341
341
|
this.canvas = this.container.querySelector('canvas');
|
|
342
342
|
this.ctx = this.canvas.getContext('2d');
|
|
343
343
|
this.titleEl = this.container.querySelector('.waveform-title');
|
|
344
|
-
this.
|
|
344
|
+
this.artistEl = this.container.querySelector('.waveform-artist');
|
|
345
345
|
this.artworkEl = this.container.querySelector('.waveform-artwork');
|
|
346
346
|
if (this.artworkEl) {
|
|
347
347
|
// Fall back to the placeholder tile if the cover URL fails to load.
|
|
@@ -701,7 +701,7 @@ export class WaveformPlayer {
|
|
|
701
701
|
// Set metadata
|
|
702
702
|
navigator.mediaSession.metadata = new MediaMetadata({
|
|
703
703
|
title: this.options.title || 'Unknown Track',
|
|
704
|
-
artist: this.options.
|
|
704
|
+
artist: this.options.artist || '',
|
|
705
705
|
album: this.options.album || '',
|
|
706
706
|
artwork: this.options.artwork ? [
|
|
707
707
|
{src: this.options.artwork, sizes: '512x512', type: 'image/jpeg'}
|
|
@@ -811,7 +811,9 @@ export class WaveformPlayer {
|
|
|
811
811
|
this._dragging = false;
|
|
812
812
|
try { this.canvas.releasePointerCapture(e.pointerId); } catch (err) { /* ignore */ }
|
|
813
813
|
this._seekFromPointer(e.clientX); // commit the seek now — audio jumps once
|
|
814
|
-
|
|
814
|
+
// Hide the drag tooltip on release, unless hover-time keeps it (or
|
|
815
|
+
// the seekbar handle is being hovered), to avoid a hide/show flicker.
|
|
816
|
+
if (!this._seekHover && !this.options.showHoverTime) this._hideHoverTip();
|
|
815
817
|
this._updateSeekHandle();
|
|
816
818
|
};
|
|
817
819
|
this.canvas.addEventListener('pointerup', endDrag);
|
|
@@ -945,19 +947,19 @@ export class WaveformPlayer {
|
|
|
945
947
|
*
|
|
946
948
|
* Pauses any current playback, fully resets the audio element (self mode),
|
|
947
949
|
* clears error/marker/progress state, merges the new metadata into
|
|
948
|
-
* `this.options`, updates the
|
|
950
|
+
* `this.options`, updates the artist/artwork DOM, then calls
|
|
949
951
|
* {@link WaveformPlayer#load}. Auto-plays the new track unless
|
|
950
952
|
* `options.autoplay === false`.
|
|
951
953
|
* @param {string} url - Audio URL.
|
|
952
954
|
* @param {string|null} [title=null] - Track title; keeps the existing
|
|
953
955
|
* title when null.
|
|
954
|
-
* @param {string|null} [
|
|
955
|
-
* the
|
|
956
|
+
* @param {string|null} [artist=null] - Track artist; pass `''` to hide
|
|
957
|
+
* the artist row, or null to keep the existing one.
|
|
956
958
|
* @param {Object} [options={}] - Additional options to merge (e.g.
|
|
957
959
|
* `preload`, `artwork`, `markers`, `autoplay`).
|
|
958
960
|
* @returns {Promise<void>}
|
|
959
961
|
*/
|
|
960
|
-
async loadTrack(url, title = null,
|
|
962
|
+
async loadTrack(url, title = null, artist = null, options = {}) {
|
|
961
963
|
// Stop current playback and clear state
|
|
962
964
|
if (this.isPlaying) {
|
|
963
965
|
this.pause();
|
|
@@ -989,7 +991,7 @@ export class WaveformPlayer {
|
|
|
989
991
|
this.options = mergeOptions(this.options, {
|
|
990
992
|
url,
|
|
991
993
|
title: title || this.options.title,
|
|
992
|
-
|
|
994
|
+
artist: artist || this.options.artist,
|
|
993
995
|
...options
|
|
994
996
|
});
|
|
995
997
|
|
|
@@ -999,12 +1001,12 @@ export class WaveformPlayer {
|
|
|
999
1001
|
}
|
|
1000
1002
|
|
|
1001
1003
|
// Update UI elements
|
|
1002
|
-
if (this.
|
|
1003
|
-
if (
|
|
1004
|
-
this.
|
|
1005
|
-
this.
|
|
1006
|
-
} else if (
|
|
1007
|
-
this.
|
|
1004
|
+
if (this.artistEl) {
|
|
1005
|
+
if (artist) {
|
|
1006
|
+
this.artistEl.textContent = artist;
|
|
1007
|
+
this.artistEl.style.display = '';
|
|
1008
|
+
} else if (artist === '') {
|
|
1009
|
+
this.artistEl.style.display = 'none';
|
|
1008
1010
|
}
|
|
1009
1011
|
}
|
|
1010
1012
|
|
|
@@ -1240,20 +1242,23 @@ export class WaveformPlayer {
|
|
|
1240
1242
|
}
|
|
1241
1243
|
|
|
1242
1244
|
/**
|
|
1243
|
-
* Build
|
|
1244
|
-
*
|
|
1245
|
+
* Build the time tooltip that follows the pointer over the waveform. The
|
|
1246
|
+
* element is always created so **drag-scrub** can show the time you're
|
|
1247
|
+
* dragging to; the *hover* reveal is gated on `showHoverTime`. Works in both
|
|
1245
1248
|
* self and external modes (it only needs {@link WaveformPlayer#getSeekDuration}).
|
|
1246
|
-
* Uses the same pointer-X → percentage math as seeking.
|
|
1247
1249
|
* @private
|
|
1248
1250
|
*/
|
|
1249
1251
|
setupHoverTime() {
|
|
1250
|
-
if (!this.
|
|
1252
|
+
if (!this.seekEl) return;
|
|
1251
1253
|
const tip = document.createElement('div');
|
|
1252
1254
|
tip.className = 'waveform-hover-time';
|
|
1253
1255
|
tip.setAttribute('aria-hidden', 'true');
|
|
1254
1256
|
this.seekEl.appendChild(tip);
|
|
1255
1257
|
this.hoverTimeEl = tip;
|
|
1256
1258
|
|
|
1259
|
+
// Without showHoverTime the tip is drag-only — no hover listeners.
|
|
1260
|
+
if (!this.options.showHoverTime) return;
|
|
1261
|
+
|
|
1257
1262
|
// Hover updates the tip; the drag handlers also call _updateHoverTip so
|
|
1258
1263
|
// the time follows the playhead while scrubbing (Spotify-style). The
|
|
1259
1264
|
// guard avoids a double-update when a drag is in progress (the canvas
|
|
@@ -1304,6 +1309,12 @@ export class WaveformPlayer {
|
|
|
1304
1309
|
this.drawWaveform();
|
|
1305
1310
|
this._updateSeekHandle();
|
|
1306
1311
|
this._updateHoverTip(clientX);
|
|
1312
|
+
// Live time readout while scrubbing (Spotify-style): the audio keeps
|
|
1313
|
+
// playing at its old position; the display shows where release lands.
|
|
1314
|
+
const dur = this.getSeekDuration();
|
|
1315
|
+
if (dur && this.currentTimeEl) {
|
|
1316
|
+
this.currentTimeEl.textContent = formatTime(this.progress * dur);
|
|
1317
|
+
}
|
|
1307
1318
|
}
|
|
1308
1319
|
|
|
1309
1320
|
/**
|
|
@@ -1667,34 +1678,22 @@ export class WaveformPlayer {
|
|
|
1667
1678
|
* @public
|
|
1668
1679
|
*/
|
|
1669
1680
|
refreshTheme() {
|
|
1670
|
-
if (!this._autoTheme
|
|
1681
|
+
if (!this._autoTheme) return;
|
|
1682
|
+
this._scheme = detectColorScheme();
|
|
1671
1683
|
const preset = getColorPreset(this.options.colorPreset);
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
if (this.options[key] !== preset[key]) {
|
|
1675
|
-
this.options[key] = preset[key];
|
|
1676
|
-
changed = true;
|
|
1677
|
-
}
|
|
1684
|
+
for (const key of (this._presetKeys || [])) {
|
|
1685
|
+
if (key in preset) this.options[key] = preset[key];
|
|
1678
1686
|
}
|
|
1679
|
-
|
|
1687
|
+
this._applyThemeColors();
|
|
1680
1688
|
}
|
|
1681
1689
|
|
|
1682
1690
|
/**
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1691
|
+
* Sync the theme class (CSS drives the DOM chrome colours via `--wfp-*`
|
|
1692
|
+
* variables) and redraw the canvas with the resolved waveform colours.
|
|
1685
1693
|
* @private
|
|
1686
1694
|
*/
|
|
1687
1695
|
_applyThemeColors() {
|
|
1688
|
-
|
|
1689
|
-
if (this.playBtn) {
|
|
1690
|
-
this.playBtn.style.borderColor = o.buttonColor;
|
|
1691
|
-
this.playBtn.style.color = o.buttonColor;
|
|
1692
|
-
}
|
|
1693
|
-
if (this.titleEl) this.titleEl.style.color = o.textColor;
|
|
1694
|
-
if (this.subtitleEl) this.subtitleEl.style.color = o.textSecondaryColor;
|
|
1695
|
-
this.container.querySelectorAll('.waveform-time, .waveform-bpm').forEach((el) => {
|
|
1696
|
-
el.style.color = o.textSecondaryColor;
|
|
1697
|
-
});
|
|
1696
|
+
this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');
|
|
1698
1697
|
if (this.canvas) this.drawWaveform();
|
|
1699
1698
|
}
|
|
1700
1699
|
|
|
@@ -1820,16 +1819,13 @@ export class WaveformPlayer {
|
|
|
1820
1819
|
* directly: `WaveformBar.play(event.detail)`.
|
|
1821
1820
|
*
|
|
1822
1821
|
* @private
|
|
1823
|
-
* @return {{url:string,title:?string,
|
|
1822
|
+
* @return {{url:string,title:?string,artist:?string,artwork:?string,player:WaveformPlayer}}
|
|
1824
1823
|
*/
|
|
1825
1824
|
_buildTrackDetail() {
|
|
1826
1825
|
return {
|
|
1827
1826
|
url: this.options.url,
|
|
1828
1827
|
title: this.options.title,
|
|
1829
|
-
|
|
1830
|
-
// Core has no separate `artist` option; mirror subtitle so the
|
|
1831
|
-
// published event detail is self-consistent for controllers.
|
|
1832
|
-
artist: this.options.artist || this.options.subtitle,
|
|
1828
|
+
artist: this.options.artist,
|
|
1833
1829
|
artwork: this.options.artwork,
|
|
1834
1830
|
markers: this.options.markers,
|
|
1835
1831
|
waveform: this.options.waveform,
|
package/src/js/drawing.js
CHANGED
|
@@ -7,22 +7,38 @@ import {resampleData, clamp} from './utils.js';
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Resolve a fill value that may be a CSS colour string OR an array of colour
|
|
10
|
-
* stops (rendered as a
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
10
|
+
* stops (rendered as a canvas gradient). Bundle-light gradient support: pass
|
|
11
|
+
* e.g. `waveformColor: ['#fafafa', '#71717a']`. A single-element array collapses
|
|
12
|
+
* to that one colour; a multi-element array is spread evenly along the axis set
|
|
13
|
+
* by `options.waveformGradient`:
|
|
14
|
+
* - `'vertical'` (default) a canvas-height gradient (colour keyed to
|
|
15
|
+
* absolute Y, so short bars occupy only the lower band).
|
|
16
|
+
* - `'horizontal'` left -> right across the whole waveform (a hue sweep).
|
|
17
|
+
* - `'diagonal'` top-left -> bottom-right.
|
|
14
18
|
* @private
|
|
15
19
|
* @param {CanvasRenderingContext2D} ctx - Canvas context used to build the gradient.
|
|
16
20
|
* @param {string|string[]} value - A CSS colour string, or an array of colour stops.
|
|
17
|
-
* @param {
|
|
18
|
-
* @
|
|
21
|
+
* @param {HTMLCanvasElement} canvas - Canvas (provides the device-pixel span).
|
|
22
|
+
* @param {Object} [options] - Drawing options; reads `waveformGradient`.
|
|
23
|
+
* @returns {string|CanvasGradient} The original string, or a linear gradient.
|
|
19
24
|
*/
|
|
20
|
-
function makeFill(ctx, value,
|
|
25
|
+
function makeFill(ctx, value, canvas, options) {
|
|
21
26
|
if (!Array.isArray(value)) return value;
|
|
22
|
-
if (value.length
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
if (value.length < 2) return value[0];
|
|
28
|
+
const w = canvas.width;
|
|
29
|
+
const h = canvas.height;
|
|
30
|
+
const dir = options && options.waveformGradient;
|
|
31
|
+
const [x0, y0, x1, y1] =
|
|
32
|
+
dir === 'horizontal' ? [0, 0, w, 0] :
|
|
33
|
+
dir === 'diagonal' ? [0, 0, w, h] :
|
|
34
|
+
[0, 0, 0, h];
|
|
35
|
+
try {
|
|
36
|
+
const grad = ctx.createLinearGradient(x0, y0, x1, y1);
|
|
37
|
+
value.forEach((c, i) => grad.addColorStop(i / (value.length - 1), c));
|
|
38
|
+
return grad;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
return value[0]; // a bad stop shouldn't abort the whole render
|
|
41
|
+
}
|
|
26
42
|
}
|
|
27
43
|
|
|
28
44
|
/**
|
|
@@ -120,8 +136,8 @@ export function drawBars(ctx, canvas, peaks, progress, options) {
|
|
|
120
136
|
const height = canvas.height;
|
|
121
137
|
const progressWidth = progress * canvas.width;
|
|
122
138
|
const radii = barRadii(options, dpr);
|
|
123
|
-
const baseFill = makeFill(ctx, options.color,
|
|
124
|
-
const progFill = makeFill(ctx, options.progressColor,
|
|
139
|
+
const baseFill = makeFill(ctx, options.color, canvas, options);
|
|
140
|
+
const progFill = makeFill(ctx, options.progressColor, canvas, options);
|
|
125
141
|
|
|
126
142
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
127
143
|
|
|
@@ -184,8 +200,8 @@ export function drawMirror(ctx, canvas, peaks, progress, options) {
|
|
|
184
200
|
const r = barRadiusPx(options, dpr);
|
|
185
201
|
const topRadii = [r, r, 0, 0]; // round the upper cap
|
|
186
202
|
const botRadii = [0, 0, r, r]; // round the lower cap
|
|
187
|
-
const baseFill = makeFill(ctx, options.color,
|
|
188
|
-
const progFill = makeFill(ctx, options.progressColor,
|
|
203
|
+
const baseFill = makeFill(ctx, options.color, canvas, options);
|
|
204
|
+
const progFill = makeFill(ctx, options.progressColor, canvas, options);
|
|
189
205
|
|
|
190
206
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
191
207
|
|
|
@@ -252,12 +268,16 @@ export function drawLine(ctx, canvas, peaks, progress, options) {
|
|
|
252
268
|
* @returns {void}
|
|
253
269
|
*/
|
|
254
270
|
const drawCurve = (color, lineWidth, endProgress = 1, addGlow = false) => {
|
|
271
|
+
// strokeStyle accepts a gradient; shadowColor must be a solid string, so
|
|
272
|
+
// collapse a stop array to its last stop for the glow.
|
|
273
|
+
const stroke = makeFill(ctx, color, canvas, options);
|
|
274
|
+
const solid = Array.isArray(color) ? color[color.length - 1] : color;
|
|
255
275
|
if (addGlow) {
|
|
256
276
|
ctx.shadowBlur = 12;
|
|
257
|
-
ctx.shadowColor =
|
|
277
|
+
ctx.shadowColor = solid;
|
|
258
278
|
}
|
|
259
279
|
|
|
260
|
-
ctx.strokeStyle =
|
|
280
|
+
ctx.strokeStyle = stroke;
|
|
261
281
|
ctx.lineWidth = lineWidth;
|
|
262
282
|
ctx.lineCap = 'round';
|
|
263
283
|
ctx.lineJoin = 'round';
|
|
@@ -350,8 +370,8 @@ export function drawBlocks(ctx, canvas, peaks, progress, options) {
|
|
|
350
370
|
const blockGap = 2 * dpr;
|
|
351
371
|
const progressWidth = progress * canvas.width;
|
|
352
372
|
const centerY = height / 2;
|
|
353
|
-
const baseFill = makeFill(ctx, options.color,
|
|
354
|
-
const progFill = makeFill(ctx, options.progressColor,
|
|
373
|
+
const baseFill = makeFill(ctx, options.color, canvas, options);
|
|
374
|
+
const progFill = makeFill(ctx, options.progressColor, canvas, options);
|
|
355
375
|
|
|
356
376
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
357
377
|
|
|
@@ -402,8 +422,8 @@ export function drawDots(ctx, canvas, peaks, progress, options) {
|
|
|
402
422
|
const dotRadius = Math.max(1.5 * dpr, barWidth / 2);
|
|
403
423
|
const progressWidth = progress * canvas.width;
|
|
404
424
|
const centerY = height / 2;
|
|
405
|
-
const baseFill = makeFill(ctx, options.color,
|
|
406
|
-
const progFill = makeFill(ctx, options.progressColor,
|
|
425
|
+
const baseFill = makeFill(ctx, options.color, canvas, options);
|
|
426
|
+
const progFill = makeFill(ctx, options.progressColor, canvas, options);
|
|
407
427
|
|
|
408
428
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
409
429
|
|
|
@@ -452,7 +472,7 @@ export function drawSeekbar(ctx, canvas, peaks, progress, options) {
|
|
|
452
472
|
ctx.clearRect(0, 0, width, height);
|
|
453
473
|
|
|
454
474
|
// Draw background track
|
|
455
|
-
ctx.fillStyle = options.color || 'rgba(255, 255, 255, 0.2)';
|
|
475
|
+
ctx.fillStyle = makeFill(ctx, options.color, canvas, options) || 'rgba(255, 255, 255, 0.2)';
|
|
456
476
|
|
|
457
477
|
// Rounded background track
|
|
458
478
|
capsulePath(ctx, borderRadius, width, centerY, barHeight);
|
|
@@ -471,7 +491,7 @@ export function drawSeekbar(ctx, canvas, peaks, progress, options) {
|
|
|
471
491
|
// enabled (the bar); a standalone seekbar stays at full progress.
|
|
472
492
|
ctx.save();
|
|
473
493
|
ctx.globalAlpha = options.seekHandle && !active ? 0.7 : 1;
|
|
474
|
-
ctx.fillStyle = options.progressColor || 'rgba(255, 255, 255, 0.9)';
|
|
494
|
+
ctx.fillStyle = makeFill(ctx, options.progressColor, canvas, options) || 'rgba(255, 255, 255, 0.9)';
|
|
475
495
|
capsulePath(ctx, borderRadius, progressWidth, centerY, barHeight);
|
|
476
496
|
ctx.fill();
|
|
477
497
|
ctx.restore();
|
package/src/js/themes.js
CHANGED
|
@@ -93,23 +93,11 @@ export function detectColorScheme() {
|
|
|
93
93
|
export const COLOR_PRESETS = {
|
|
94
94
|
dark: {
|
|
95
95
|
waveformColor: 'rgba(255, 255, 255, 0.3)',
|
|
96
|
-
progressColor: 'rgba(255, 255, 255, 0.9)'
|
|
97
|
-
buttonColor: 'rgba(255, 255, 255, 0.9)',
|
|
98
|
-
buttonHoverColor: 'rgba(255, 255, 255, 1)',
|
|
99
|
-
textColor: '#ffffff',
|
|
100
|
-
textSecondaryColor: 'rgba(255, 255, 255, 0.6)',
|
|
101
|
-
backgroundColor: 'rgba(255, 255, 255, 0.03)',
|
|
102
|
-
borderColor: 'rgba(255, 255, 255, 0.1)'
|
|
96
|
+
progressColor: 'rgba(255, 255, 255, 0.9)'
|
|
103
97
|
},
|
|
104
98
|
light: {
|
|
105
99
|
waveformColor: 'rgba(0, 0, 0, 0.2)',
|
|
106
|
-
progressColor: 'rgba(0, 0, 0, 0.8)'
|
|
107
|
-
buttonColor: 'rgba(0, 0, 0, 0.8)',
|
|
108
|
-
buttonHoverColor: 'rgba(0, 0, 0, 0.9)',
|
|
109
|
-
textColor: '#333333',
|
|
110
|
-
textSecondaryColor: 'rgba(0, 0, 0, 0.6)',
|
|
111
|
-
backgroundColor: 'rgba(0, 0, 0, 0.02)',
|
|
112
|
-
borderColor: 'rgba(0, 0, 0, 0.1)'
|
|
100
|
+
progressColor: 'rgba(0, 0, 0, 0.8)'
|
|
113
101
|
}
|
|
114
102
|
};
|
|
115
103
|
|
|
@@ -196,18 +184,20 @@ export const DEFAULT_OPTIONS = {
|
|
|
196
184
|
// Rounded bar caps (px). 0 = square; 1 = soft caps (default). Applies to bars/mirror.
|
|
197
185
|
barRadius: 1,
|
|
198
186
|
|
|
187
|
+
// Gradient axis when waveformColor/progressColor is an array of stops:
|
|
188
|
+
// 'vertical' (top->bottom canvas gradient), 'horizontal' (hue sweep across the
|
|
189
|
+
// waveform) or 'diagonal'. Ignored for single colours.
|
|
190
|
+
waveformGradient: 'vertical',
|
|
191
|
+
|
|
199
192
|
// Color preset: null = auto-detect, 'dark' = force dark, 'light' = force light
|
|
200
193
|
colorPreset: null,
|
|
201
194
|
|
|
202
|
-
//
|
|
195
|
+
// Canvas colours (null = inherit the resolved preset; arrays = gradient
|
|
196
|
+
// stops, see waveformGradient). The DOM chrome (button, title, meta) is
|
|
197
|
+
// themed via CSS variables (--wfp-button-color / --wfp-text-color /
|
|
198
|
+
// --wfp-text-secondary-color), not JS options.
|
|
203
199
|
waveformColor: null,
|
|
204
200
|
progressColor: null,
|
|
205
|
-
buttonColor: null,
|
|
206
|
-
buttonHoverColor: null,
|
|
207
|
-
textColor: null,
|
|
208
|
-
textSecondaryColor: null,
|
|
209
|
-
backgroundColor: null,
|
|
210
|
-
borderColor: null,
|
|
211
201
|
|
|
212
202
|
// Features
|
|
213
203
|
autoplay: false,
|
|
@@ -241,7 +231,7 @@ export const DEFAULT_OPTIONS = {
|
|
|
241
231
|
|
|
242
232
|
// Content
|
|
243
233
|
title: null,
|
|
244
|
-
|
|
234
|
+
artist: null,
|
|
245
235
|
artwork: null,
|
|
246
236
|
album: '',
|
|
247
237
|
|
package/src/js/utils.js
CHANGED
|
@@ -157,6 +157,7 @@ export function parseDataAttributes(element) {
|
|
|
157
157
|
// `data-waveform-style`; the canonical long form wins if both are set.
|
|
158
158
|
if (element.dataset.style) options.waveformStyle = element.dataset.style;
|
|
159
159
|
if (element.dataset.waveformStyle) options.waveformStyle = element.dataset.waveformStyle;
|
|
160
|
+
if (element.dataset.waveformGradient) options.waveformGradient = element.dataset.waveformGradient;
|
|
160
161
|
setNum('barWidth');
|
|
161
162
|
setNum('barSpacing');
|
|
162
163
|
setNum('barRadius');
|
|
@@ -176,12 +177,6 @@ export function parseDataAttributes(element) {
|
|
|
176
177
|
// Individual color customization
|
|
177
178
|
if (element.dataset.waveformColor) options.waveformColor = parseColorValue(element.dataset.waveformColor);
|
|
178
179
|
if (element.dataset.progressColor) options.progressColor = parseColorValue(element.dataset.progressColor);
|
|
179
|
-
if (element.dataset.buttonColor) options.buttonColor = element.dataset.buttonColor;
|
|
180
|
-
if (element.dataset.buttonHoverColor) options.buttonHoverColor = element.dataset.buttonHoverColor;
|
|
181
|
-
if (element.dataset.textColor) options.textColor = element.dataset.textColor;
|
|
182
|
-
if (element.dataset.textSecondaryColor) options.textSecondaryColor = element.dataset.textSecondaryColor;
|
|
183
|
-
if (element.dataset.backgroundColor) options.backgroundColor = element.dataset.backgroundColor;
|
|
184
|
-
if (element.dataset.borderColor) options.borderColor = element.dataset.borderColor;
|
|
185
180
|
|
|
186
181
|
// Legacy support for old attribute names
|
|
187
182
|
if (element.dataset.color) options.waveformColor = element.dataset.color;
|
|
@@ -201,7 +196,7 @@ export function parseDataAttributes(element) {
|
|
|
201
196
|
|
|
202
197
|
// Content and metadata
|
|
203
198
|
if (element.dataset.title) options.title = element.dataset.title;
|
|
204
|
-
if (element.dataset.
|
|
199
|
+
if (element.dataset.artist) options.artist = element.dataset.artist;
|
|
205
200
|
if (element.dataset.album) options.album = element.dataset.album;
|
|
206
201
|
if (element.dataset.artwork) options.artwork = element.dataset.artwork;
|
|
207
202
|
|