@editframe/elements 0.16.7-beta.0 → 0.17.6-beta.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.
Files changed (101) hide show
  1. package/README.md +30 -0
  2. package/dist/DecoderResetFrequency.test.d.ts +1 -0
  3. package/dist/DecoderResetRecovery.test.d.ts +1 -0
  4. package/dist/DelayedLoadingState.d.ts +48 -0
  5. package/dist/DelayedLoadingState.integration.test.d.ts +1 -0
  6. package/dist/DelayedLoadingState.js +113 -0
  7. package/dist/DelayedLoadingState.test.d.ts +1 -0
  8. package/dist/EF_FRAMEGEN.d.ts +10 -1
  9. package/dist/EF_FRAMEGEN.js +199 -179
  10. package/dist/EF_INTERACTIVE.js +2 -6
  11. package/dist/EF_RENDERING.js +1 -3
  12. package/dist/JitTranscodingClient.browsertest.d.ts +1 -0
  13. package/dist/JitTranscodingClient.d.ts +167 -0
  14. package/dist/JitTranscodingClient.js +373 -0
  15. package/dist/JitTranscodingClient.test.d.ts +1 -0
  16. package/dist/LoadingDebounce.test.d.ts +1 -0
  17. package/dist/LoadingIndicator.browsertest.d.ts +0 -0
  18. package/dist/ManualScrubTest.test.d.ts +1 -0
  19. package/dist/ScrubResolvedFlashing.test.d.ts +1 -0
  20. package/dist/ScrubTrackIntegration.test.d.ts +1 -0
  21. package/dist/ScrubTrackManager.d.ts +96 -0
  22. package/dist/ScrubTrackManager.js +216 -0
  23. package/dist/ScrubTrackManager.test.d.ts +1 -0
  24. package/dist/SegmentSwitchLoading.test.d.ts +1 -0
  25. package/dist/VideoSeekFlashing.browsertest.d.ts +0 -0
  26. package/dist/VideoStuckDiagnostic.test.d.ts +1 -0
  27. package/dist/elements/CrossUpdateController.js +13 -15
  28. package/dist/elements/EFAudio.browsertest.d.ts +0 -0
  29. package/dist/elements/EFAudio.d.ts +1 -1
  30. package/dist/elements/EFAudio.js +30 -43
  31. package/dist/elements/EFCaptions.js +337 -373
  32. package/dist/elements/EFImage.js +64 -90
  33. package/dist/elements/EFMedia.d.ts +98 -33
  34. package/dist/elements/EFMedia.js +1169 -678
  35. package/dist/elements/EFSourceMixin.js +31 -48
  36. package/dist/elements/EFTemporal.d.ts +1 -0
  37. package/dist/elements/EFTemporal.js +266 -360
  38. package/dist/elements/EFTimegroup.d.ts +3 -1
  39. package/dist/elements/EFTimegroup.js +262 -323
  40. package/dist/elements/EFVideo.browsertest.d.ts +0 -0
  41. package/dist/elements/EFVideo.d.ts +90 -2
  42. package/dist/elements/EFVideo.js +408 -111
  43. package/dist/elements/EFWaveform.js +375 -411
  44. package/dist/elements/FetchMixin.js +14 -24
  45. package/dist/elements/MediaController.d.ts +30 -0
  46. package/dist/elements/TargetController.js +130 -156
  47. package/dist/elements/TimegroupController.js +17 -19
  48. package/dist/elements/durationConverter.js +15 -4
  49. package/dist/elements/parseTimeToMs.js +4 -10
  50. package/dist/elements/printTaskStatus.d.ts +2 -0
  51. package/dist/elements/printTaskStatus.js +11 -0
  52. package/dist/elements/updateAnimations.js +39 -59
  53. package/dist/getRenderInfo.js +58 -67
  54. package/dist/gui/ContextMixin.js +203 -288
  55. package/dist/gui/EFConfiguration.js +27 -43
  56. package/dist/gui/EFFilmstrip.js +440 -620
  57. package/dist/gui/EFFitScale.js +112 -135
  58. package/dist/gui/EFFocusOverlay.js +45 -61
  59. package/dist/gui/EFPreview.js +30 -49
  60. package/dist/gui/EFScrubber.js +78 -99
  61. package/dist/gui/EFTimeDisplay.js +49 -70
  62. package/dist/gui/EFToggleLoop.js +17 -34
  63. package/dist/gui/EFTogglePlay.js +37 -58
  64. package/dist/gui/EFWorkbench.js +66 -88
  65. package/dist/gui/TWMixin.js +2 -48
  66. package/dist/gui/TWMixin2.js +31 -0
  67. package/dist/gui/efContext.js +2 -6
  68. package/dist/gui/fetchContext.js +1 -3
  69. package/dist/gui/focusContext.js +1 -3
  70. package/dist/gui/focusedElementContext.js +2 -6
  71. package/dist/gui/playingContext.js +1 -4
  72. package/dist/index.js +5 -30
  73. package/dist/msToTimeCode.js +11 -13
  74. package/dist/style.css +2 -1
  75. package/package.json +3 -3
  76. package/src/elements/EFAudio.browsertest.ts +569 -0
  77. package/src/elements/EFAudio.ts +4 -6
  78. package/src/elements/EFCaptions.browsertest.ts +0 -1
  79. package/src/elements/EFImage.browsertest.ts +0 -1
  80. package/src/elements/EFMedia.browsertest.ts +147 -115
  81. package/src/elements/EFMedia.ts +1339 -307
  82. package/src/elements/EFTemporal.browsertest.ts +0 -1
  83. package/src/elements/EFTemporal.ts +11 -0
  84. package/src/elements/EFTimegroup.ts +73 -10
  85. package/src/elements/EFVideo.browsertest.ts +680 -0
  86. package/src/elements/EFVideo.ts +729 -50
  87. package/src/elements/EFWaveform.ts +4 -4
  88. package/src/elements/MediaController.ts +108 -0
  89. package/src/elements/__screenshots__/EFMedia.browsertest.ts/EFMedia-JIT-audio-playback-audioBufferTask-should-work-in-JIT-mode-without-URL-errors-1.png +0 -0
  90. package/src/elements/printTaskStatus.ts +16 -0
  91. package/src/elements/updateAnimations.ts +6 -0
  92. package/src/gui/TWMixin.ts +10 -3
  93. package/test/EFVideo.frame-tasks.browsertest.ts +524 -0
  94. package/test/EFVideo.framegen.browsertest.ts +118 -0
  95. package/test/createJitTestClips.ts +293 -0
  96. package/test/useAssetMSW.ts +49 -0
  97. package/test/useMSW.ts +31 -0
  98. package/types.json +1 -1
  99. package/dist/gui/TWMixin.css.js +0 -4
  100. /package/dist/elements/{TargetController.test.d.ts → TargetController.browsertest.d.ts} +0 -0
  101. /package/src/elements/{TargetController.test.ts → TargetController.browsertest.ts} +0 -0
@@ -1,391 +1,84 @@
1
- import { CSSStyleObserver } from "@bramus/style-observer";
2
- import { Task } from "@lit/task";
3
- import { LitElement, html, css } from "lit";
4
- import { property, state, customElement } from "lit/decorators.js";
5
- import { createRef, ref } from "lit/directives/ref.js";
6
1
  import { EF_INTERACTIVE } from "../EF_INTERACTIVE.js";
7
- import { EF_RENDERING } from "../EF_RENDERING.js";
8
- import { TWMixin } from "../gui/TWMixin.js";
9
- import { CrossUpdateController } from "./CrossUpdateController.js";
10
2
  import { EFTemporal } from "./EFTemporal.js";
11
3
  import { TargetController } from "./TargetController.js";
12
- var __defProp = Object.defineProperty;
13
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
14
- var __decorateClass = (decorators, target, key, kind) => {
15
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
16
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
17
- if (decorator = decorators[i])
18
- result = (kind ? decorator(target, key, result) : decorator(result)) || result;
19
- if (kind && result) __defProp(target, key, result);
20
- return result;
21
- };
22
- let EFWaveform = class extends EFTemporal(TWMixin(LitElement)) {
23
- constructor() {
24
- super(...arguments);
25
- this.canvasRef = createRef();
26
- this.ctx = null;
27
- this.styleObserver = null;
28
- this.mode = "bars";
29
- this.color = "currentColor";
30
- this.target = "";
31
- this.barSpacing = 0.5;
32
- this.targetElement = null;
33
- this.lineWidth = 4;
34
- this.targetController = new TargetController(this);
35
- this.frameTask = new Task(this, {
36
- autoRun: EF_INTERACTIVE,
37
- args: () => {
38
- return [
39
- this.targetElement,
40
- this.targetElement?.frequencyDataTask.value
41
- ];
42
- },
43
- task: async () => {
44
- if (!this.targetElement) return;
45
- await this.targetElement.frequencyDataTask.taskComplete;
46
- this.ctx ||= this.initCanvas();
47
- const ctx = this.ctx;
48
- if (!ctx) return;
49
- const frequencyData = this.targetElement.frequencyDataTask.value;
50
- const byteTimeData = this.targetElement.byteTimeDomainTask.value;
51
- if (!frequencyData || !byteTimeData) return;
52
- ctx.save();
53
- if (this.color === "currentColor") {
54
- const computedStyle = getComputedStyle(this);
55
- const currentColor = computedStyle.color;
56
- ctx.strokeStyle = currentColor;
57
- ctx.fillStyle = currentColor;
58
- } else {
59
- ctx.strokeStyle = this.color;
60
- ctx.fillStyle = this.color;
61
- }
62
- switch (this.mode) {
63
- case "bars":
64
- this.drawBars(ctx, frequencyData);
65
- break;
66
- case "bricks":
67
- this.drawBricks(ctx, frequencyData);
68
- break;
69
- case "line":
70
- this.drawLine(ctx, byteTimeData);
71
- break;
72
- case "curve":
73
- this.drawCurve(ctx, byteTimeData);
74
- break;
75
- case "pixel":
76
- this.drawPixel(ctx, frequencyData);
77
- break;
78
- case "wave":
79
- this.drawWave(ctx, frequencyData);
80
- break;
81
- case "spikes":
82
- this.drawSpikes(ctx, frequencyData);
83
- break;
84
- case "roundBars":
85
- this.drawRoundBars(ctx, frequencyData);
86
- break;
87
- }
88
- ctx.restore();
89
- }
90
- });
91
- }
92
- render() {
93
- return html`<canvas ${ref(this.canvasRef)}></canvas>`;
94
- }
95
- connectedCallback() {
96
- super.connectedCallback();
97
- try {
98
- if (this.targetElement) {
99
- new CrossUpdateController(this.targetElement, this);
100
- }
101
- } catch (e) {
102
- }
103
- this.resizeObserver = new ResizeObserver(() => {
104
- this.resizeCanvas();
105
- });
106
- this.resizeObserver.observe(this);
107
- this.mutationObserver = new MutationObserver((mutationsList) => {
108
- for (const mutation of mutationsList) {
109
- if (mutation.type === "attributes") {
110
- this.frameTask.run();
111
- }
112
- }
113
- });
114
- this.mutationObserver.observe(this, { attributes: true });
115
- if (!EF_RENDERING()) {
116
- this.styleObserver = new CSSStyleObserver(["color"], () => {
117
- this.frameTask.run();
118
- });
119
- this.styleObserver.attach(this);
120
- }
121
- }
122
- disconnectedCallback() {
123
- super.disconnectedCallback();
124
- this.resizeObserver?.disconnect();
125
- this.mutationObserver?.disconnect();
126
- this.styleObserver?.detach();
127
- }
128
- resizeCanvas() {
129
- this.ctx = this.initCanvas();
130
- if (this.ctx) {
131
- this.frameTask.run();
132
- }
133
- }
134
- initCanvas() {
135
- const canvas = this.canvasRef.value;
136
- if (!canvas) return null;
137
- const rect = {
138
- width: this.offsetWidth,
139
- height: this.offsetHeight
140
- };
141
- const dpr = window.devicePixelRatio;
142
- canvas.style.width = `${rect.width}px`;
143
- canvas.style.height = `${rect.height}px`;
144
- canvas.width = rect.width * dpr;
145
- canvas.height = rect.height * dpr;
146
- const ctx = canvas.getContext("2d");
147
- if (!ctx) return null;
148
- ctx.reset();
149
- return ctx;
150
- }
151
- drawBars(ctx, frequencyData) {
152
- const canvas = ctx.canvas;
153
- const waveWidth = canvas.width;
154
- const waveHeight = canvas.height;
155
- const totalBars = frequencyData.length;
156
- const paddingInner = this.barSpacing;
157
- const paddingOuter = 0.01;
158
- const availableWidth = waveWidth;
159
- const barWidth = availableWidth / (totalBars + (totalBars - 1) * paddingInner);
160
- ctx.clearRect(0, 0, waveWidth, waveHeight);
161
- const path = new Path2D();
162
- frequencyData.forEach((value, i) => {
163
- const normalizedValue = value / 255;
164
- const barHeight = normalizedValue * waveHeight;
165
- const y = (waveHeight - barHeight) / 2;
166
- const x = waveWidth * paddingOuter + i * (barWidth * (1 + paddingInner));
167
- path.rect(x, y, barWidth, barHeight);
168
- });
169
- ctx.fill(path);
170
- }
171
- drawBricks(ctx, frequencyData) {
172
- const canvas = ctx.canvas;
173
- const waveWidth = canvas.width;
174
- const waveHeight = canvas.height;
175
- ctx.clearRect(0, 0, waveWidth, waveHeight);
176
- const path = new Path2D();
177
- const columnWidth = waveWidth / frequencyData.length;
178
- const boxSize = columnWidth * 0.9;
179
- const verticalGap = boxSize * 0.2;
180
- const maxBricks = Math.floor(waveHeight / (boxSize + verticalGap));
181
- frequencyData.forEach((value, i) => {
182
- const normalizedValue = value / 255;
183
- const brickCount = Math.floor(normalizedValue * maxBricks);
184
- for (let j = 0; j < brickCount; j++) {
185
- const x = columnWidth * i;
186
- const y = waveHeight - (j + 1) * (boxSize + verticalGap);
187
- path.rect(x, y, boxSize, boxSize);
188
- }
189
- });
190
- ctx.fill(path);
191
- }
192
- drawRoundBars(ctx, frequencyData) {
193
- const canvas = ctx.canvas;
194
- const waveWidth = canvas.width;
195
- const waveHeight = canvas.height;
196
- const totalBars = frequencyData.length;
197
- const paddingInner = this.barSpacing;
198
- const paddingOuter = 0.01;
199
- const availableWidth = waveWidth;
200
- const barWidth = availableWidth / (totalBars + (totalBars - 1) * paddingInner);
201
- ctx.clearRect(0, 0, waveWidth, waveHeight);
202
- const path = new Path2D();
203
- frequencyData.forEach((value, i) => {
204
- const normalizedValue = value / 255;
205
- const height = normalizedValue * waveHeight;
206
- const x = waveWidth * paddingOuter + i * (barWidth * (1 + paddingInner));
207
- const y = (waveHeight - height) / 2;
208
- path.roundRect(x, y, barWidth, height, barWidth / 2);
209
- });
210
- ctx.fill(path);
211
- }
212
- drawLine(ctx, frequencyData) {
213
- const canvas = ctx.canvas;
214
- const waveWidth = canvas.width;
215
- const waveHeight = canvas.height;
216
- ctx.clearRect(0, 0, waveWidth, waveHeight);
217
- const path = new Path2D();
218
- const sampleRate = 1;
219
- for (let i = 0; i < frequencyData.length; i += sampleRate) {
220
- const x = i / frequencyData.length * waveWidth;
221
- const y = (1 - (frequencyData[i] ?? 0) / 255) * waveHeight;
222
- if (i === 0) {
223
- path.moveTo(x, y);
224
- } else {
225
- path.lineTo(x, y);
226
- }
227
- }
228
- const lastX = waveWidth;
229
- const lastY = (1 - (frequencyData[frequencyData.length - 1] ?? 0) / 255) * waveHeight;
230
- path.lineTo(lastX, lastY);
231
- ctx.lineWidth = this.lineWidth;
232
- ctx.stroke(path);
233
- }
234
- drawCurve(ctx, frequencyData) {
235
- const canvas = ctx.canvas;
236
- const waveWidth = canvas.width;
237
- const waveHeight = canvas.height;
238
- ctx.clearRect(0, 0, waveWidth, waveHeight);
239
- const path = new Path2D();
240
- frequencyData.forEach((value, i) => {
241
- const x = i / frequencyData.length * waveWidth;
242
- const y = (1 - value / 255) * waveHeight;
243
- if (i === 0) {
244
- path.moveTo(x, y);
245
- } else {
246
- const prevX = (i - 1) / frequencyData.length * waveWidth;
247
- const prevY = (1 - (frequencyData[i - 1] ?? 0) / 255) * waveHeight;
248
- const xc = (prevX + x) / 2;
249
- const yc = (prevY + y) / 2;
250
- path.quadraticCurveTo(prevX, prevY, xc, yc);
251
- }
252
- });
253
- ctx.lineWidth = this.lineWidth;
254
- ctx.stroke(path);
255
- }
256
- drawPixel(ctx, frequencyData) {
257
- const canvas = ctx.canvas;
258
- const waveWidth = canvas.width;
259
- const waveHeight = canvas.height;
260
- const baseline = waveHeight / 2;
261
- const barWidth = waveWidth / frequencyData.length;
262
- ctx.clearRect(0, 0, waveWidth, waveHeight);
263
- const path = new Path2D();
264
- frequencyData.forEach((value, i) => {
265
- const normalizedValue = value / 255;
266
- const x = i * (waveWidth / frequencyData.length);
267
- const barHeight = normalizedValue * (waveHeight / 2);
268
- const y = baseline - barHeight;
269
- path.rect(x, y, barWidth, barHeight * 2);
270
- });
271
- ctx.fill(path);
272
- }
273
- drawWave(ctx, frequencyData) {
274
- const canvas = ctx.canvas;
275
- const waveWidth = canvas.width;
276
- const waveHeight = canvas.height;
277
- const paddingOuter = 0.01;
278
- const availableWidth = waveWidth * (1 - 2 * paddingOuter);
279
- const startX = waveWidth * paddingOuter;
280
- ctx.clearRect(0, 0, waveWidth, waveHeight);
281
- const path = new Path2D();
282
- const firstValue = Math.min((frequencyData[0] ?? 0) / 255 * 2, 1);
283
- const firstY = (waveHeight - firstValue * waveHeight) / 2;
284
- path.moveTo(startX, firstY);
285
- frequencyData.forEach((value, i) => {
286
- const normalizedValue = Math.min(value / 255 * 2, 1);
287
- const x = startX + i / (frequencyData.length - 1) * availableWidth;
288
- const barHeight = normalizedValue * waveHeight;
289
- const y = (waveHeight - barHeight) / 2;
290
- if (i === 0) {
291
- path.moveTo(x, y);
292
- } else {
293
- const prevX = startX + (i - 1) / (frequencyData.length - 1) * availableWidth;
294
- const prevValue = Math.min((frequencyData[i - 1] ?? 0) / 255 * 2, 1);
295
- const prevBarHeight = prevValue * waveHeight;
296
- const prevY = (waveHeight - prevBarHeight) / 2;
297
- const xc = (prevX + x) / 2;
298
- const yc = (prevY + y) / 2;
299
- path.quadraticCurveTo(prevX, prevY, xc, yc);
300
- }
301
- });
302
- for (let i = frequencyData.length - 1; i >= 0; i--) {
303
- const normalizedValue = Math.min((frequencyData[i] ?? 0) / 255 * 2, 1);
304
- const x = startX + i / (frequencyData.length - 1) * availableWidth;
305
- const barHeight = normalizedValue * waveHeight;
306
- const y = (waveHeight + barHeight) / 2;
307
- if (i === frequencyData.length - 1) {
308
- path.lineTo(x, y);
309
- } else {
310
- const nextX = startX + (i + 1) / (frequencyData.length - 1) * availableWidth;
311
- const nextValue = Math.min((frequencyData[i + 1] ?? 0) / 255 * 2, 1);
312
- const nextBarHeight = nextValue * waveHeight;
313
- const nextY = (waveHeight + nextBarHeight) / 2;
314
- const xc = (nextX + x) / 2;
315
- const yc = (nextY + y) / 2;
316
- path.quadraticCurveTo(nextX, nextY, xc, yc);
317
- }
318
- }
319
- const lastY = (waveHeight + firstValue * waveHeight) / 2;
320
- const controlX = startX;
321
- const controlY = (lastY + firstY) / 2;
322
- path.quadraticCurveTo(controlX, controlY, startX, firstY);
323
- ctx.fill(path);
324
- }
325
- drawSpikes(ctx, frequencyData) {
326
- const canvas = ctx.canvas;
327
- const waveWidth = canvas.width;
328
- const waveHeight = canvas.height;
329
- const paddingOuter = 0.01;
330
- const availableWidth = waveWidth * (1 - 2 * paddingOuter);
331
- const startX = waveWidth * paddingOuter;
332
- ctx.clearRect(0, 0, waveWidth, waveHeight);
333
- const path = new Path2D();
334
- const firstValue = (frequencyData[0] ?? 0) / 255;
335
- const firstY = (waveHeight - firstValue * waveHeight) / 2;
336
- path.moveTo(startX, firstY);
337
- frequencyData.forEach((value, i) => {
338
- const normalizedValue = Math.min(value / 255 * 2, 1);
339
- const x = startX + i / (frequencyData.length - 1) * availableWidth;
340
- const barHeight = normalizedValue * (waveHeight / 2);
341
- const y = (waveHeight - barHeight * 2) / 2;
342
- if (i === 0) {
343
- path.moveTo(x, y);
344
- } else {
345
- const prevX = startX + (i - 1) / (frequencyData.length - 1) * availableWidth;
346
- const prevValue = (frequencyData[i - 1] ?? 0) / 255;
347
- const prevBarHeight = prevValue * (waveHeight / 2);
348
- const prevY = (waveHeight - prevBarHeight * 2) / 2;
349
- const xc = (prevX + x) / 2;
350
- const yc = (prevY + y) / 2;
351
- path.quadraticCurveTo(prevX, prevY, xc, yc);
352
- }
353
- });
354
- for (let i = frequencyData.length - 1; i >= 0; i--) {
355
- const normalizedValue = Math.min((frequencyData[i] ?? 0) / 255 * 2, 1);
356
- const x = startX + i / (frequencyData.length - 1) * availableWidth;
357
- const barHeight = normalizedValue * (waveHeight / 2);
358
- const y = (waveHeight + barHeight * 2) / 2;
359
- if (i === frequencyData.length - 1) {
360
- path.lineTo(x, y);
361
- } else {
362
- const nextX = startX + (i + 1) / (frequencyData.length - 1) * availableWidth;
363
- const nextValue = (frequencyData[i + 1] ?? 0) / 255;
364
- const nextBarHeight = nextValue * (waveHeight / 2);
365
- const nextY = (waveHeight + nextBarHeight * 2) / 2;
366
- const xc = (nextX + x) / 2;
367
- const yc = (nextY + y) / 2;
368
- path.quadraticCurveTo(nextX, nextY, xc, yc);
369
- }
370
- }
371
- const lastY = (waveHeight + firstValue * waveHeight) / 2;
372
- const controlX = startX;
373
- const controlY = (lastY + firstY) / 2;
374
- path.quadraticCurveTo(controlX, controlY, startX, firstY);
375
- ctx.fill(path);
376
- }
377
- get durationMs() {
378
- if (!this.targetElement) return 0;
379
- return this.targetElement.durationMs;
380
- }
381
- updated(changedProperties) {
382
- super.updated(changedProperties);
383
- if (changedProperties.size > 0) {
384
- this.frameTask.run();
385
- }
386
- }
387
- };
388
- EFWaveform.styles = css`
4
+ import { TWMixin } from "../gui/TWMixin2.js";
5
+ import { CrossUpdateController } from "./CrossUpdateController.js";
6
+ import { EF_RENDERING } from "../EF_RENDERING.js";
7
+ import { Task } from "@lit/task";
8
+ import { LitElement, css, html } from "lit";
9
+ import { customElement, property, state } from "lit/decorators.js";
10
+ import _decorate from "@oxc-project/runtime/helpers/decorate";
11
+ import { createRef, ref } from "lit/directives/ref.js";
12
+ import { CSSStyleObserver } from "@bramus/style-observer";
13
+ let EFWaveform = class EFWaveform$1 extends EFTemporal(TWMixin(LitElement)) {
14
+ constructor(..._args) {
15
+ super(..._args);
16
+ this.canvasRef = createRef();
17
+ this.ctx = null;
18
+ this.styleObserver = null;
19
+ this.mode = "bars";
20
+ this.color = "currentColor";
21
+ this.target = "";
22
+ this.barSpacing = .5;
23
+ this.targetElement = null;
24
+ this.lineWidth = 4;
25
+ this.targetController = new TargetController(this);
26
+ this.frameTask = new Task(this, {
27
+ autoRun: EF_INTERACTIVE,
28
+ args: () => {
29
+ return [this.targetElement, this.targetElement?.frequencyDataTask.value];
30
+ },
31
+ task: async () => {
32
+ if (!this.targetElement) return;
33
+ await this.targetElement.frequencyDataTask.taskComplete;
34
+ this.ctx ||= this.initCanvas();
35
+ const ctx = this.ctx;
36
+ if (!ctx) return;
37
+ const frequencyData = this.targetElement.frequencyDataTask.value;
38
+ const byteTimeData = this.targetElement.byteTimeDomainTask.value;
39
+ if (!frequencyData || !byteTimeData) return;
40
+ ctx.save();
41
+ if (this.color === "currentColor") {
42
+ const computedStyle = getComputedStyle(this);
43
+ const currentColor = computedStyle.color;
44
+ ctx.strokeStyle = currentColor;
45
+ ctx.fillStyle = currentColor;
46
+ } else {
47
+ ctx.strokeStyle = this.color;
48
+ ctx.fillStyle = this.color;
49
+ }
50
+ switch (this.mode) {
51
+ case "bars":
52
+ this.drawBars(ctx, frequencyData);
53
+ break;
54
+ case "bricks":
55
+ this.drawBricks(ctx, frequencyData);
56
+ break;
57
+ case "line":
58
+ this.drawLine(ctx, byteTimeData);
59
+ break;
60
+ case "curve":
61
+ this.drawCurve(ctx, byteTimeData);
62
+ break;
63
+ case "pixel":
64
+ this.drawPixel(ctx, frequencyData);
65
+ break;
66
+ case "wave":
67
+ this.drawWave(ctx, frequencyData);
68
+ break;
69
+ case "spikes":
70
+ this.drawSpikes(ctx, frequencyData);
71
+ break;
72
+ case "roundBars":
73
+ this.drawRoundBars(ctx, frequencyData);
74
+ break;
75
+ }
76
+ ctx.restore();
77
+ }
78
+ });
79
+ }
80
+ static {
81
+ this.styles = css`
389
82
  :host {
390
83
  display: block;
391
84
  all: inherit;
@@ -400,30 +93,301 @@ EFWaveform.styles = css`
400
93
  height: 100%;
401
94
  }
402
95
  `;
403
- __decorateClass([
404
- property({
405
- type: String,
406
- attribute: "mode"
407
- })
408
- ], EFWaveform.prototype, "mode", 2);
409
- __decorateClass([
410
- property({ type: String })
411
- ], EFWaveform.prototype, "color", 2);
412
- __decorateClass([
413
- property({ type: String, reflect: true })
414
- ], EFWaveform.prototype, "target", 2);
415
- __decorateClass([
416
- property({ type: Number, attribute: "bar-spacing" })
417
- ], EFWaveform.prototype, "barSpacing", 2);
418
- __decorateClass([
419
- state()
420
- ], EFWaveform.prototype, "targetElement", 2);
421
- __decorateClass([
422
- property({ type: Number, attribute: "line-width" })
423
- ], EFWaveform.prototype, "lineWidth", 2);
424
- EFWaveform = __decorateClass([
425
- customElement("ef-waveform")
426
- ], EFWaveform);
427
- export {
428
- EFWaveform
96
+ }
97
+ render() {
98
+ return html`<canvas ${ref(this.canvasRef)}></canvas><div class="text-5xl inline-block bg-red-500">`;
99
+ }
100
+ connectedCallback() {
101
+ super.connectedCallback();
102
+ try {
103
+ if (this.targetElement) new CrossUpdateController(this.targetElement, this);
104
+ } catch (_error) {}
105
+ this.resizeObserver = new ResizeObserver(() => {
106
+ this.resizeCanvas();
107
+ });
108
+ this.resizeObserver.observe(this);
109
+ this.mutationObserver = new MutationObserver((mutationsList) => {
110
+ for (const mutation of mutationsList) if (mutation.type === "attributes") this.frameTask.run();
111
+ });
112
+ this.mutationObserver.observe(this, { attributes: true });
113
+ if (!EF_RENDERING()) {
114
+ this.styleObserver = new CSSStyleObserver(["color"], () => {
115
+ this.frameTask.run();
116
+ });
117
+ this.styleObserver.attach(this);
118
+ }
119
+ }
120
+ disconnectedCallback() {
121
+ super.disconnectedCallback();
122
+ this.resizeObserver?.disconnect();
123
+ this.mutationObserver?.disconnect();
124
+ this.styleObserver?.detach();
125
+ }
126
+ resizeCanvas() {
127
+ this.ctx = this.initCanvas();
128
+ if (this.ctx) this.frameTask.run();
129
+ }
130
+ initCanvas() {
131
+ const canvas = this.canvasRef.value;
132
+ if (!canvas) return null;
133
+ const rect = {
134
+ width: this.offsetWidth,
135
+ height: this.offsetHeight
136
+ };
137
+ const dpr = window.devicePixelRatio;
138
+ canvas.style.width = `${rect.width}px`;
139
+ canvas.style.height = `${rect.height}px`;
140
+ canvas.width = rect.width * dpr;
141
+ canvas.height = rect.height * dpr;
142
+ const ctx = canvas.getContext("2d");
143
+ if (!ctx) return null;
144
+ ctx.reset();
145
+ return ctx;
146
+ }
147
+ drawBars(ctx, frequencyData) {
148
+ const canvas = ctx.canvas;
149
+ const waveWidth = canvas.width;
150
+ const waveHeight = canvas.height;
151
+ const totalBars = frequencyData.length;
152
+ const paddingInner = this.barSpacing;
153
+ const paddingOuter = .01;
154
+ const availableWidth = waveWidth;
155
+ const barWidth = availableWidth / (totalBars + (totalBars - 1) * paddingInner);
156
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
157
+ const path = new Path2D();
158
+ frequencyData.forEach((value, i) => {
159
+ const normalizedValue = value / 255;
160
+ const barHeight = normalizedValue * waveHeight;
161
+ const y = (waveHeight - barHeight) / 2;
162
+ const x = waveWidth * paddingOuter + i * (barWidth * (1 + paddingInner));
163
+ path.rect(x, y, barWidth, barHeight);
164
+ });
165
+ ctx.fill(path);
166
+ }
167
+ drawBricks(ctx, frequencyData) {
168
+ const canvas = ctx.canvas;
169
+ const waveWidth = canvas.width;
170
+ const waveHeight = canvas.height;
171
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
172
+ const path = new Path2D();
173
+ const columnWidth = waveWidth / frequencyData.length;
174
+ const boxSize = columnWidth * .9;
175
+ const verticalGap = boxSize * .2;
176
+ const maxBricks = Math.floor(waveHeight / (boxSize + verticalGap));
177
+ frequencyData.forEach((value, i) => {
178
+ const normalizedValue = value / 255;
179
+ const brickCount = Math.floor(normalizedValue * maxBricks);
180
+ for (let j = 0; j < brickCount; j++) {
181
+ const x = columnWidth * i;
182
+ const y = waveHeight - (j + 1) * (boxSize + verticalGap);
183
+ path.rect(x, y, boxSize, boxSize);
184
+ }
185
+ });
186
+ ctx.fill(path);
187
+ }
188
+ drawRoundBars(ctx, frequencyData) {
189
+ const canvas = ctx.canvas;
190
+ const waveWidth = canvas.width;
191
+ const waveHeight = canvas.height;
192
+ const totalBars = frequencyData.length;
193
+ const paddingInner = this.barSpacing;
194
+ const paddingOuter = .01;
195
+ const availableWidth = waveWidth;
196
+ const barWidth = availableWidth / (totalBars + (totalBars - 1) * paddingInner);
197
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
198
+ const path = new Path2D();
199
+ frequencyData.forEach((value, i) => {
200
+ const normalizedValue = value / 255;
201
+ const height = normalizedValue * waveHeight;
202
+ const x = waveWidth * paddingOuter + i * (barWidth * (1 + paddingInner));
203
+ const y = (waveHeight - height) / 2;
204
+ path.roundRect(x, y, barWidth, height, barWidth / 2);
205
+ });
206
+ ctx.fill(path);
207
+ }
208
+ drawLine(ctx, frequencyData) {
209
+ const canvas = ctx.canvas;
210
+ const waveWidth = canvas.width;
211
+ const waveHeight = canvas.height;
212
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
213
+ const path = new Path2D();
214
+ const sampleRate = 1;
215
+ for (let i = 0; i < frequencyData.length; i += sampleRate) {
216
+ const x = i / frequencyData.length * waveWidth;
217
+ const y = (1 - (frequencyData[i] ?? 0) / 255) * waveHeight;
218
+ if (i === 0) path.moveTo(x, y);
219
+ else path.lineTo(x, y);
220
+ }
221
+ const lastX = waveWidth;
222
+ const lastY = (1 - (frequencyData[frequencyData.length - 1] ?? 0) / 255) * waveHeight;
223
+ path.lineTo(lastX, lastY);
224
+ ctx.lineWidth = this.lineWidth;
225
+ ctx.stroke(path);
226
+ }
227
+ drawCurve(ctx, frequencyData) {
228
+ const canvas = ctx.canvas;
229
+ const waveWidth = canvas.width;
230
+ const waveHeight = canvas.height;
231
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
232
+ const path = new Path2D();
233
+ frequencyData.forEach((value, i) => {
234
+ const x = i / frequencyData.length * waveWidth;
235
+ const y = (1 - value / 255) * waveHeight;
236
+ if (i === 0) path.moveTo(x, y);
237
+ else {
238
+ const prevX = (i - 1) / frequencyData.length * waveWidth;
239
+ const prevY = (1 - (frequencyData[i - 1] ?? 0) / 255) * waveHeight;
240
+ const xc = (prevX + x) / 2;
241
+ const yc = (prevY + y) / 2;
242
+ path.quadraticCurveTo(prevX, prevY, xc, yc);
243
+ }
244
+ });
245
+ ctx.lineWidth = this.lineWidth;
246
+ ctx.stroke(path);
247
+ }
248
+ drawPixel(ctx, frequencyData) {
249
+ const canvas = ctx.canvas;
250
+ const waveWidth = canvas.width;
251
+ const waveHeight = canvas.height;
252
+ const baseline = waveHeight / 2;
253
+ const barWidth = waveWidth / frequencyData.length;
254
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
255
+ const path = new Path2D();
256
+ frequencyData.forEach((value, i) => {
257
+ const normalizedValue = value / 255;
258
+ const x = i * (waveWidth / frequencyData.length);
259
+ const barHeight = normalizedValue * (waveHeight / 2);
260
+ const y = baseline - barHeight;
261
+ path.rect(x, y, barWidth, barHeight * 2);
262
+ });
263
+ ctx.fill(path);
264
+ }
265
+ drawWave(ctx, frequencyData) {
266
+ const canvas = ctx.canvas;
267
+ const waveWidth = canvas.width;
268
+ const waveHeight = canvas.height;
269
+ const paddingOuter = .01;
270
+ const availableWidth = waveWidth * (1 - 2 * paddingOuter);
271
+ const startX = waveWidth * paddingOuter;
272
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
273
+ const path = new Path2D();
274
+ const firstValue = Math.min((frequencyData[0] ?? 0) / 255 * 2, 1);
275
+ const firstY = (waveHeight - firstValue * waveHeight) / 2;
276
+ path.moveTo(startX, firstY);
277
+ frequencyData.forEach((value, i) => {
278
+ const normalizedValue = Math.min(value / 255 * 2, 1);
279
+ const x = startX + i / (frequencyData.length - 1) * availableWidth;
280
+ const barHeight = normalizedValue * waveHeight;
281
+ const y = (waveHeight - barHeight) / 2;
282
+ if (i === 0) path.moveTo(x, y);
283
+ else {
284
+ const prevX = startX + (i - 1) / (frequencyData.length - 1) * availableWidth;
285
+ const prevValue = Math.min((frequencyData[i - 1] ?? 0) / 255 * 2, 1);
286
+ const prevBarHeight = prevValue * waveHeight;
287
+ const prevY = (waveHeight - prevBarHeight) / 2;
288
+ const xc = (prevX + x) / 2;
289
+ const yc = (prevY + y) / 2;
290
+ path.quadraticCurveTo(prevX, prevY, xc, yc);
291
+ }
292
+ });
293
+ for (let i = frequencyData.length - 1; i >= 0; i--) {
294
+ const normalizedValue = Math.min((frequencyData[i] ?? 0) / 255 * 2, 1);
295
+ const x = startX + i / (frequencyData.length - 1) * availableWidth;
296
+ const barHeight = normalizedValue * waveHeight;
297
+ const y = (waveHeight + barHeight) / 2;
298
+ if (i === frequencyData.length - 1) path.lineTo(x, y);
299
+ else {
300
+ const nextX = startX + (i + 1) / (frequencyData.length - 1) * availableWidth;
301
+ const nextValue = Math.min((frequencyData[i + 1] ?? 0) / 255 * 2, 1);
302
+ const nextBarHeight = nextValue * waveHeight;
303
+ const nextY = (waveHeight + nextBarHeight) / 2;
304
+ const xc = (nextX + x) / 2;
305
+ const yc = (nextY + y) / 2;
306
+ path.quadraticCurveTo(nextX, nextY, xc, yc);
307
+ }
308
+ }
309
+ const lastY = (waveHeight + firstValue * waveHeight) / 2;
310
+ const controlX = startX;
311
+ const controlY = (lastY + firstY) / 2;
312
+ path.quadraticCurveTo(controlX, controlY, startX, firstY);
313
+ ctx.fill(path);
314
+ }
315
+ drawSpikes(ctx, frequencyData) {
316
+ const canvas = ctx.canvas;
317
+ const waveWidth = canvas.width;
318
+ const waveHeight = canvas.height;
319
+ const paddingOuter = .01;
320
+ const availableWidth = waveWidth * (1 - 2 * paddingOuter);
321
+ const startX = waveWidth * paddingOuter;
322
+ ctx.clearRect(0, 0, waveWidth, waveHeight);
323
+ const path = new Path2D();
324
+ const firstValue = (frequencyData[0] ?? 0) / 255;
325
+ const firstY = (waveHeight - firstValue * waveHeight) / 2;
326
+ path.moveTo(startX, firstY);
327
+ frequencyData.forEach((value, i) => {
328
+ const normalizedValue = Math.min(value / 255 * 2, 1);
329
+ const x = startX + i / (frequencyData.length - 1) * availableWidth;
330
+ const barHeight = normalizedValue * (waveHeight / 2);
331
+ const y = (waveHeight - barHeight * 2) / 2;
332
+ if (i === 0) path.moveTo(x, y);
333
+ else {
334
+ const prevX = startX + (i - 1) / (frequencyData.length - 1) * availableWidth;
335
+ const prevValue = (frequencyData[i - 1] ?? 0) / 255;
336
+ const prevBarHeight = prevValue * (waveHeight / 2);
337
+ const prevY = (waveHeight - prevBarHeight * 2) / 2;
338
+ const xc = (prevX + x) / 2;
339
+ const yc = (prevY + y) / 2;
340
+ path.quadraticCurveTo(prevX, prevY, xc, yc);
341
+ }
342
+ });
343
+ for (let i = frequencyData.length - 1; i >= 0; i--) {
344
+ const normalizedValue = Math.min((frequencyData[i] ?? 0) / 255 * 2, 1);
345
+ const x = startX + i / (frequencyData.length - 1) * availableWidth;
346
+ const barHeight = normalizedValue * (waveHeight / 2);
347
+ const y = (waveHeight + barHeight * 2) / 2;
348
+ if (i === frequencyData.length - 1) path.lineTo(x, y);
349
+ else {
350
+ const nextX = startX + (i + 1) / (frequencyData.length - 1) * availableWidth;
351
+ const nextValue = (frequencyData[i + 1] ?? 0) / 255;
352
+ const nextBarHeight = nextValue * (waveHeight / 2);
353
+ const nextY = (waveHeight + nextBarHeight * 2) / 2;
354
+ const xc = (nextX + x) / 2;
355
+ const yc = (nextY + y) / 2;
356
+ path.quadraticCurveTo(nextX, nextY, xc, yc);
357
+ }
358
+ }
359
+ const lastY = (waveHeight + firstValue * waveHeight) / 2;
360
+ const controlX = startX;
361
+ const controlY = (lastY + firstY) / 2;
362
+ path.quadraticCurveTo(controlX, controlY, startX, firstY);
363
+ ctx.fill(path);
364
+ }
365
+ get durationMs() {
366
+ if (!this.targetElement) return 0;
367
+ return this.targetElement.durationMs;
368
+ }
369
+ updated(changedProperties) {
370
+ super.updated(changedProperties);
371
+ if (changedProperties.size > 0) this.frameTask.run();
372
+ }
429
373
  };
374
+ _decorate([property({
375
+ type: String,
376
+ attribute: "mode"
377
+ })], EFWaveform.prototype, "mode", void 0);
378
+ _decorate([property({ type: String })], EFWaveform.prototype, "color", void 0);
379
+ _decorate([property({
380
+ type: String,
381
+ reflect: true
382
+ })], EFWaveform.prototype, "target", void 0);
383
+ _decorate([property({
384
+ type: Number,
385
+ attribute: "bar-spacing"
386
+ })], EFWaveform.prototype, "barSpacing", void 0);
387
+ _decorate([state()], EFWaveform.prototype, "targetElement", void 0);
388
+ _decorate([property({
389
+ type: Number,
390
+ attribute: "line-width"
391
+ })], EFWaveform.prototype, "lineWidth", void 0);
392
+ EFWaveform = _decorate([customElement("ef-waveform")], EFWaveform);
393
+ export { EFWaveform };