@arraypress/waveform-player 1.27.0 → 1.28.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/dist/waveform-player-no-autoinit.cjs +221 -115
- package/dist/waveform-player-no-autoinit.esm.js +5 -5
- package/dist/waveform-player.cjs +221 -115
- package/dist/waveform-player.esm.js +5 -5
- package/dist/waveform-player.js +221 -115
- package/dist/waveform-player.min.js +5 -5
- package/index.d.ts +43 -12
- package/package.json +1 -1
- package/src/js/bpm.js +11 -5
- package/src/js/core.js +292 -124
- package/src/js/drawing.js +19 -4
- package/src/js/themes.js +3 -1
- package/src/js/utils.js +6 -4
package/src/js/drawing.js
CHANGED
|
@@ -5,6 +5,21 @@
|
|
|
5
5
|
|
|
6
6
|
import {resampleData, clamp} from './utils.js';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* How many bars of `pitch` device pixels (width + spacing) fit across the
|
|
10
|
+
* canvas. A zero, negative or non-numeric pitch fits none: `width / 0` is
|
|
11
|
+
* `Infinity`, and resampleData() would loop until the tab ran out of memory.
|
|
12
|
+
* normalizeOptions() keeps `barWidth` above zero, but options stay publicly
|
|
13
|
+
* mutable, so the drawers guard the division themselves.
|
|
14
|
+
* @param {HTMLCanvasElement} canvas - Canvas element (device-pixel width).
|
|
15
|
+
* @param {number} pitch - Bar width plus spacing, in device pixels.
|
|
16
|
+
* @returns {number} Bar count, 0 when the pitch is unusable.
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
19
|
+
function fitBars(canvas, pitch) {
|
|
20
|
+
return pitch > 0 ? Math.floor(canvas.width / pitch) : 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
8
23
|
/**
|
|
9
24
|
* Resolve a fill value that may be a CSS colour string OR an array of colour
|
|
10
25
|
* stops (rendered as a canvas gradient). Bundle-light gradient support: pass
|
|
@@ -131,7 +146,7 @@ export function drawBars(ctx, canvas, peaks, progress, options) {
|
|
|
131
146
|
const dpr = window.devicePixelRatio || 1;
|
|
132
147
|
const barWidth = options.barWidth * dpr;
|
|
133
148
|
const barSpacing = options.barSpacing * dpr;
|
|
134
|
-
const barCount =
|
|
149
|
+
const barCount = fitBars(canvas, barWidth + barSpacing);
|
|
135
150
|
const resampledPeaks = resampleData(peaks, barCount);
|
|
136
151
|
const height = canvas.height;
|
|
137
152
|
const progressWidth = progress * canvas.width;
|
|
@@ -192,7 +207,7 @@ export function drawMirror(ctx, canvas, peaks, progress, options) {
|
|
|
192
207
|
const dpr = window.devicePixelRatio || 1;
|
|
193
208
|
const barWidth = options.barWidth * dpr;
|
|
194
209
|
const barSpacing = options.barSpacing * dpr;
|
|
195
|
-
const barCount =
|
|
210
|
+
const barCount = fitBars(canvas, barWidth + barSpacing);
|
|
196
211
|
const resampledPeaks = resampleData(peaks, barCount);
|
|
197
212
|
const height = canvas.height;
|
|
198
213
|
const centerY = height / 2;
|
|
@@ -363,7 +378,7 @@ export function drawBlocks(ctx, canvas, peaks, progress, options) {
|
|
|
363
378
|
const dpr = window.devicePixelRatio || 1;
|
|
364
379
|
const barWidth = (options.barWidth || 3) * dpr;
|
|
365
380
|
const barSpacing = (options.barSpacing || 1) * dpr;
|
|
366
|
-
const barCount =
|
|
381
|
+
const barCount = fitBars(canvas, barWidth + barSpacing);
|
|
367
382
|
const resampledPeaks = resampleData(peaks, barCount);
|
|
368
383
|
const height = canvas.height;
|
|
369
384
|
const blockSize = 4 * dpr;
|
|
@@ -416,7 +431,7 @@ export function drawDots(ctx, canvas, peaks, progress, options) {
|
|
|
416
431
|
const dpr = window.devicePixelRatio || 1;
|
|
417
432
|
const barWidth = (options.barWidth || 2) * dpr;
|
|
418
433
|
const barSpacing = (options.barSpacing || 3) * dpr;
|
|
419
|
-
const barCount =
|
|
434
|
+
const barCount = fitBars(canvas, barWidth + barSpacing);
|
|
420
435
|
const resampledPeaks = resampleData(peaks, barCount);
|
|
421
436
|
const height = canvas.height;
|
|
422
437
|
const dotRadius = Math.max(1.5 * dpr, barWidth / 2);
|
package/src/js/themes.js
CHANGED
|
@@ -464,7 +464,9 @@ const ENUMS = {
|
|
|
464
464
|
const NUMBERS = {
|
|
465
465
|
height: {min: 1, integer: true},
|
|
466
466
|
samples: {min: 1, integer: true},
|
|
467
|
-
|
|
467
|
+
// A zero-width bar is invisible anyway, and zero width + zero spacing
|
|
468
|
+
// made the drawers' bar count Infinity (see fitBars in drawing.js).
|
|
469
|
+
barWidth: {min: 0.1},
|
|
468
470
|
barSpacing: {min: 0},
|
|
469
471
|
barRadius: {min: 0},
|
|
470
472
|
bpm: {min: 1},
|
package/src/js/utils.js
CHANGED
|
@@ -344,6 +344,12 @@ export function parseDataAttributes(element) {
|
|
|
344
344
|
setLength('buttonSize');
|
|
345
345
|
setLength('buttonRadius');
|
|
346
346
|
|
|
347
|
+
// Legacy names first, so the canonical attribute below wins when both are
|
|
348
|
+
// set — the same rule as `data-style` / `data-src`. `data-color` takes the
|
|
349
|
+
// same values as `data-waveform-color`, gradient stop arrays included.
|
|
350
|
+
if (element.dataset.color) options.waveformColor = parseColorValue(element.dataset.color);
|
|
351
|
+
if (element.dataset.theme) options.colorPreset = element.dataset.theme;
|
|
352
|
+
|
|
347
353
|
// Color preset
|
|
348
354
|
if (element.dataset.colorPreset) options.colorPreset = element.dataset.colorPreset;
|
|
349
355
|
|
|
@@ -351,10 +357,6 @@ export function parseDataAttributes(element) {
|
|
|
351
357
|
if (element.dataset.waveformColor) options.waveformColor = parseColorValue(element.dataset.waveformColor);
|
|
352
358
|
if (element.dataset.progressColor) options.progressColor = parseColorValue(element.dataset.progressColor);
|
|
353
359
|
|
|
354
|
-
// Legacy support for old attribute names
|
|
355
|
-
if (element.dataset.color) options.waveformColor = element.dataset.color;
|
|
356
|
-
if (element.dataset.theme) options.colorPreset = element.dataset.theme;
|
|
357
|
-
|
|
358
360
|
// Feature flags
|
|
359
361
|
setBool('autoplay');
|
|
360
362
|
setBool('showControls');
|