forked from Lab-Lab-Lab/CPR-Music
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackClipCanvas.jsx
More file actions
679 lines (575 loc) · 23.4 KB
/
TrackClipCanvas.jsx
File metadata and controls
679 lines (575 loc) · 23.4 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
'use client';
import { useEffect, useMemo, useRef, useState } from 'react';
import { useMultitrack } from './MultitrackContext';
import waveformCache from '../components/audio/DAW/Multitrack/WaveformCache';
export default function TrackClipCanvas({ track, zoomLevel = 100, height = 100, logOperation = null }) {
const {
currentTime,
duration,
selectedTrackId,
setSelectedTrackId,
selectedClipId,
setSelectedClipId,
selectedClipIds,
setSelectedClipIds,
editorTool,
snapEnabled,
gridSizeSec,
setTracks,
} = useMultitrack();
const canvasRef = useRef(null);
const dragRef = useRef({ op: null, clipIndex: -1, startX: 0, pxPerSecCSS: 1, orig: null, sourceDuration: null });
// selectionBoxRef removed - selection box now handled by SelectionOverlay component
const [peaksCache, setPeaksCache] = useState(new Map()); // clip.id -> peaks
const clips = Array.isArray(track?.clips) ? track.clips : [];
// In select mode, all tracks are clickable (for cross-track selection)
// In other modes, only the selected track is interactive
const interactive = editorTool === 'select'
? (editorTool === 'select')
: ((editorTool === 'clip' || editorTool === 'cut') && selectedTrackId === track.id);
const MIN_DUR = 0.02; // 20ms
const HANDLE_W = 8; // CSS px
// Load peaks for all clips
useEffect(() => {
const loadPeaks = async () => {
const newPeaksCache = new Map();
for (const clip of clips) {
if (!clip.src) continue;
try {
// Calculate pixels-per-second to match timeline
const pixelsPerSecond = zoomLevel; // 100 zoom = 100 pixels/second
const clipWidthPx = Math.max(1, (clip.duration || 0) * pixelsPerSecond);
const peaks = await waveformCache.getPeaksForClip(
clip.src,
clip.offset || 0,
clip.duration || 0,
clipWidthPx,
zoomLevel
);
newPeaksCache.set(clip.id, peaks);
} catch (err) {
console.warn(`Failed to load peaks for clip ${clip.id}:`, err);
}
}
setPeaksCache(newPeaksCache);
};
loadPeaks();
}, [clips, duration, zoomLevel]);
const resizeToCSS = (canvas) => {
const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
const w = Math.max(1, Math.floor(rect.width * dpr));
const h = Math.max(1, Math.floor(rect.height * dpr));
if (canvas.width !== w) canvas.width = w;
if (canvas.height !== h) canvas.height = h;
return { dpr, width: canvas.width, height: canvas.height, cssWidth: rect.width, cssHeight: rect.height };
};
const clipRects = useMemo(() => {
// Calculate pixels-per-second to match timeline
// Use the same formula as MultitrackEditor for consistency
const pixelsPerSecond = zoomLevel; // 100 zoom = 100 pixels/second
return (dpr) => {
// Return positions in physical canvas pixels (CSS pixels * dpr)
return clips.map((c) => ({
id: c.id,
start: c.start || 0,
duration: c.duration || 0,
color: c.color || track?.color || '#7bafd4',
x: Math.max(0, Math.floor((c.start || 0) * pixelsPerSecond * dpr)),
w: Math.max(1, Math.floor((c.duration || 0) * pixelsPerSecond * dpr)),
}));
};
}, [clips, duration, zoomLevel, track?.color]);
// Draw loading state for clips that are still processing
const drawLoadingState = (ctx, clip, rect, dpr) => {
const clipH = rect.h;
const centerY = rect.y + clipH / 2;
ctx.save();
// Set up clipping region
ctx.beginPath();
ctx.rect(rect.x, rect.y, rect.w, clipH);
ctx.clip();
// Draw loading background
ctx.fillStyle = hexToRgba(rect.color, 0.1);
ctx.fillRect(rect.x, rect.y, rect.w, clipH);
// Draw animated loading bars or pulse
const time = Date.now() / 1000;
const animOffset = (Math.sin(time * 2) + 1) * 0.5; // 0-1 sine wave
if (clip.loadingState === 'reading' || clip.loadingState === 'decoding') {
// Animated bars for heavy processing
ctx.fillStyle = hexToRgba(rect.color, 0.3 + animOffset * 0.2);
const barCount = 8;
const barWidth = rect.w / barCount;
for (let i = 0; i < barCount; i++) {
const phase = (time * 3 + i * 0.5) % (Math.PI * 2);
const barHeight = (Math.sin(phase) + 1) * 0.3 + 0.1;
const barY = centerY - (clipH * barHeight) / 2;
ctx.fillRect(
rect.x + i * barWidth + barWidth * 0.1,
barY,
barWidth * 0.8,
clipH * barHeight
);
}
} else if (clip.loadingState === 'generating-waveform') {
// Smooth pulse for waveform generation
ctx.fillStyle = hexToRgba(rect.color, 0.2 + animOffset * 0.3);
const pulseHeight = clipH * (0.3 + animOffset * 0.4);
ctx.fillRect(rect.x, centerY - pulseHeight/2, rect.w, pulseHeight);
}
ctx.restore();
};
// Draw waveform for a clip
const drawWaveform = (ctx, clip, rect, dpr) => {
// If clip is loading, show loading state instead
if (clip.isLoading) {
drawLoadingState(ctx, clip, rect, dpr);
return;
}
const peaks = peaksCache.get(clip.id);
if (!peaks || peaks.length === 0) return;
const clipH = rect.h;
const centerY = rect.y + clipH / 2;
const amplitude = (clipH - 12 * dpr) / 2; // Leave some padding
// Calculate how many peaks to draw per pixel
const peaksPerPixel = peaks.length / rect.w;
ctx.save();
// Set up clipping region to contain waveform within clip bounds
ctx.beginPath();
ctx.rect(rect.x, rect.y, rect.w, clipH);
ctx.clip();
// Draw waveform
ctx.strokeStyle = hexToRgba(rect.color, 0.7);
ctx.fillStyle = hexToRgba(rect.color, 0.3);
ctx.lineWidth = Math.max(1, dpr);
// If we have more peaks than pixels, aggregate them
if (peaksPerPixel > 1) {
for (let x = 0; x < rect.w; x++) {
const peakStart = Math.floor(x * peaksPerPixel);
const peakEnd = Math.floor((x + 1) * peaksPerPixel);
let min = 1.0;
let max = -1.0;
// Find min/max in this pixel's range
for (let i = peakStart; i < peakEnd && i < peaks.length; i++) {
if (peaks[i][0] < min) min = peaks[i][0];
if (peaks[i][1] > max) max = peaks[i][1];
}
const yMin = centerY - max * amplitude;
const yMax = centerY - min * amplitude;
// Draw vertical line for this pixel
ctx.beginPath();
ctx.moveTo(rect.x + x, yMin);
ctx.lineTo(rect.x + x, yMax);
ctx.stroke();
}
} else {
// We have fewer peaks than pixels, so interpolate
ctx.beginPath();
// Top line (max values)
for (let i = 0; i < peaks.length; i++) {
const x = rect.x + (i / (peaks.length - 1)) * rect.w;
const y = centerY - peaks[i][1] * amplitude;
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
// Bottom line (min values, reversed)
for (let i = peaks.length - 1; i >= 0; i--) {
const x = rect.x + (i / (peaks.length - 1)) * rect.w;
const y = centerY - peaks[i][0] * amplitude;
ctx.lineTo(x, y);
}
ctx.closePath();
ctx.fill();
ctx.stroke();
}
// Draw center line
ctx.strokeStyle = hexToRgba(rect.color, 0.2);
ctx.lineWidth = dpr;
ctx.beginPath();
ctx.moveTo(rect.x, centerY);
ctx.lineTo(rect.x + rect.w, centerY);
ctx.stroke();
ctx.restore();
};
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ro = new ResizeObserver(() => draw());
ro.observe(canvas);
window.addEventListener('resize', draw);
// Set up animation for loading clips
let animationId = null;
const hasLoadingClips = clips.some(clip => clip.isLoading);
if (hasLoadingClips) {
const animate = () => {
draw();
animationId = requestAnimationFrame(animate);
};
animationId = requestAnimationFrame(animate);
}
function draw() {
const { dpr, width: W, height: H } = resizeToCSS(canvas);
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, W, H);
// Use consistent pixels-per-second calculation
const pixelsPerSecond = zoomLevel; // 100 zoom = 100 pixels/second (CSS pixels)
const pxPerSec = pixelsPerSecond * dpr; // Physical pixels for grid drawing
const projectDur = Math.max(0.001, duration || 0); // Local variable for grid
// Get clip rectangles (in physical canvas pixels)
const rects = clipRects(dpr);
// Draw each clip
for (let i = 0; i < rects.length; i++) {
const r = rects[i];
const clip = clips[i];
// Allow clips to show as selected even if track is not active (for cross-track selection)
const isSel = (r.id === selectedClipId || selectedClipIds.includes(r.id));
// Clip background
ctx.fillStyle = hexToRgba(r.color, isSel ? 0.2 : 0.12);
ctx.fillRect(r.x, Math.floor(6 * dpr), r.w, H - Math.floor(12 * dpr));
// Draw waveform
drawWaveform(ctx, clip, {
x: r.x,
y: Math.floor(6 * dpr),
w: r.w,
h: H - Math.floor(12 * dpr),
color: r.color
}, dpr);
// Clip border
ctx.lineWidth = Math.max(1, Math.floor((isSel ? 2 : 1.5) * dpr));
ctx.strokeStyle = hexToRgba(r.color, isSel ? 0.9 : 0.45);
ctx.strokeRect(r.x + 0.5, Math.floor(6 * dpr) + 0.5, r.w - 1, H - Math.floor(12 * dpr) - 1);
// Resize handles (only if selected)
if (isSel) {
const handleW = Math.max(4, Math.floor(HANDLE_W * dpr));
ctx.fillStyle = hexToRgba('#ffffff', 0.7);
ctx.fillRect(r.x, 0, handleW, H);
ctx.fillRect(r.x + r.w - handleW, 0, handleW, H);
}
// Clip label (optional) - Enhanced for loading states
if (r.w > 50 * dpr) {
ctx.fillStyle = hexToRgba('#ffffff', 0.8);
ctx.font = `${Math.floor(11 * dpr)}px Inter, system-ui, sans-serif`;
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
let label = clip.name || `Clip ${i + 1}`;
// Show loading state in label
if (clip.isLoading) {
const loadingStates = {
reading: '📖 Reading...',
decoding: '🔧 Decoding...',
'generating-waveform': '🌊 Generating waveform...',
'generating-peaks': '🌊 Generating peaks...'
};
const loadingText = loadingStates[clip.loadingState] || '⏳ Processing...';
label = `${label} - ${loadingText}`;
} else if (clip.hasError) {
label = `${label} - ❌ Error`;
ctx.fillStyle = hexToRgba('#ff6b6b', 0.9);
} else if (clip.processingMethod) {
// Show processing method for completed clips (subtle indicator)
const methodIcon = clip.processingMethod === 'worker' ? '🚀' : '🔄';
label = `${methodIcon} ${label}`;
}
ctx.fillText(label, r.x + 6 * dpr, Math.floor(10 * dpr));
}
}
// Playhead
// const projectDur = Math.max(1e-6, duration || 0);
// const scale = Math.max(0.01, zoomLevel / 100);
// const pxPerSec = (W * scale) / projectDur;
// const phX = Math.floor((currentTime || 0) * pxPerSec);
// ctx.fillStyle = '#ff3030';
// ctx.fillRect(phX, 0, Math.max(1, Math.floor(2 * dpr)), H);
// Draw grid lines if snap is enabled
if (snapEnabled && gridSizeSec > 0 && projectDur > 0) {
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = dpr;
ctx.setLineDash([4 * dpr, 4 * dpr]);
for (let t = 0; t < projectDur; t += gridSizeSec) {
const x = Math.floor(t * pxPerSec);
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, H);
ctx.stroke();
}
ctx.setLineDash([]);
}
// Selection box drawing removed - now handled by SelectionOverlay component
}
// Pointer handlers remain the same as original
function quantize(sec) {
if (!snapEnabled) return sec;
const gs = Math.max(0.001, Number(gridSizeSec) || 0.1);
return Math.round(sec / gs) * gs;
}
function hitTest(clientX) {
const rect = canvas.getBoundingClientRect();
const x = clientX - rect.left;
// Use consistent pixels-per-second calculation
const pixelsPerSecond = zoomLevel; // 100 zoom = 100 pixels/second (CSS pixels)
// Iterate in reverse order (top-most clip first)
for (let i = clips.length - 1; i >= 0; i--) {
const c = clips[i];
// Calculate position in CSS pixels
const x0 = (c.start || 0) * pixelsPerSecond;
const w = (c.duration || 0) * pixelsPerSecond;
if (x >= x0 && x <= x0 + w) {
const nearL = x - x0 <= HANDLE_W;
const nearR = x0 + w - x <= HANDLE_W;
return { index: i, edge: nearL ? 'L' : nearR ? 'R' : null, pxPerSecCSS: pixelsPerSecond };
}
}
return { index: -1, edge: null, pxPerSecCSS: pixelsPerSecond };
}
function onPointerDown(e) {
if (!interactive) {
// Even if not interactive, allow shift-clicking clips for cross-track selection
if (editorTool === 'select' && (e.shiftKey || e.ctrlKey || e.metaKey)) {
const hit = hitTest(e.clientX);
if (hit.index >= 0) {
const c = clips[hit.index];
console.log('🔶 TrackClipCanvas: Cross-track shift-click', { trackId: track.id, clipId: c.id });
// Add to or toggle from selection
if (selectedClipIds.includes(c.id)) {
setSelectedClipIds(selectedClipIds.filter(id => id !== c.id));
} else {
setSelectedClipIds([...selectedClipIds, c.id]);
}
// Stop propagation so SelectionOverlay doesn't interfere
e.stopPropagation();
return;
}
}
return;
}
canvas.setPointerCapture(e.pointerId);
// Only change selected track if not shift-clicking (to allow cross-track selection)
const isModifierKey = e.shiftKey || e.ctrlKey || e.metaKey;
if (!isModifierKey) {
setSelectedTrackId(track.id);
}
const hit = hitTest(e.clientX);
dragRef.current.pxPerSecCSS = hit.pxPerSecCSS;
// Handle select tool
if (editorTool === 'select') {
if (hit.index >= 0) {
// Clicked on a clip - handle selection
const c = clips[hit.index];
const isShift = e.shiftKey;
const isCtrl = e.ctrlKey || e.metaKey;
if (isShift || isCtrl) {
// Add to or toggle from selection
if (selectedClipIds.includes(c.id)) {
setSelectedClipIds(selectedClipIds.filter(id => id !== c.id));
} else {
setSelectedClipIds([...selectedClipIds, c.id]);
}
} else {
// Single select (replace selection)
setSelectedClipId(c.id);
setSelectedClipIds([c.id]);
setSelectedTrackId(track.id);
}
// Initialize drag so the clip can be moved in one click-drag motion
const op = hit.edge === 'L' ? 'resizeL' : hit.edge === 'R' ? 'resizeR' : 'move';
dragRef.current.op = op;
dragRef.current.clipIndex = hit.index;
dragRef.current.startX = e.clientX;
dragRef.current.orig = { start: c.start || 0, duration: c.duration || 0, offset: c.offset || 0 };
dragRef.current.sourceDuration = c.sourceDuration || null;
// Stop propagation so SelectionOverlay doesn't interfere
e.stopPropagation();
return;
}
// Empty space click — let SelectionOverlay handle it
return;
}
// Handle cut tool
if (editorTool === 'cut' && hit.index >= 0) {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const pixelsPerSecond = zoomLevel;
const clickTime = x / pixelsPerSecond;
// Split the clip at the click position
const c = clips[hit.index];
const clipStart = c.start || 0;
const clipEnd = clipStart + (c.duration || 0);
// Only split if click is inside the clip (not on edges)
if (clickTime > clipStart + 0.01 && clickTime < clipEnd - 0.01) {
const leftDuration = clickTime - clipStart;
const rightDuration = clipEnd - clickTime;
const leftClip = {
...c,
duration: leftDuration,
};
const rightClip = {
...c,
id: `${c.id}-split-${Date.now()}`,
start: clickTime,
duration: rightDuration,
offset: (c.offset || 0) + leftDuration,
};
// Update the track with the split clips
setTracks((prev) => prev.map((t) => {
if (t.id !== track.id || !Array.isArray(t.clips)) return t;
const nextClips = [...t.clips];
nextClips.splice(hit.index, 1, leftClip, rightClip);
return { ...t, clips: nextClips };
}));
}
return;
}
// Handle clip tool (original behavior)
if (hit.index >= 0) {
const c = clips[hit.index];
setSelectedClipId(c.id);
const op = hit.edge === 'L' ? 'resizeL' : hit.edge === 'R' ? 'resizeR' : 'move';
dragRef.current.op = op;
dragRef.current.clipIndex = hit.index;
dragRef.current.startX = e.clientX;
dragRef.current.orig = { start: c.start || 0, duration: c.duration || 0, offset: c.offset || 0 };
dragRef.current.sourceDuration = c.sourceDuration || null;
} else {
dragRef.current.op = null;
dragRef.current.clipIndex = -1;
}
}
function onPointerMove(e) {
const hit = hitTest(e.clientX);
// Selection box dragging removed - now handled by SelectionOverlay component
if (!dragRef.current.op) {
// Set cursor based on tool and hover state
if (editorTool === 'select') {
canvas.style.cursor = 'default';
} else if (editorTool === 'cut') {
canvas.style.cursor = hit.index >= 0 ? 'crosshair' : 'default';
} else if (hit.index >= 0) {
if (hit.edge) canvas.style.cursor = 'ew-resize';
else canvas.style.cursor = 'grab';
} else {
canvas.style.cursor = 'default';
}
}
if (!interactive) return;
if (!dragRef.current.op) return;
const dxCss = e.clientX - dragRef.current.startX;
const dxSecRaw = dxCss / dragRef.current.pxPerSecCSS;
const dxSec = snapEnabled ? quantize(dxSecRaw) : dxSecRaw;
const { start, duration: dur, offset } = dragRef.current.orig;
const srcDur = dragRef.current.sourceDuration; // total audio buffer length
const op = dragRef.current.op;
let newStart = start;
let newDur = dur;
let newOffset = offset;
if (op === 'move') {
newStart = Math.max(0, start + dxSec);
} else if (op === 'resizeL') {
// Trim from left: advance offset into the buffer, can't go past offset 0
newStart = Math.max(0, start + dxSec);
const delta = newStart - start;
newOffset = Math.max(0, (offset || 0) + delta);
newDur = Math.max(MIN_DUR, dur - delta);
// Clamp: can't reveal audio before the buffer start
if (newOffset < 0) {
const correction = -newOffset;
newOffset = 0;
newStart += correction;
newDur -= correction;
}
} else if (op === 'resizeR') {
newDur = Math.max(MIN_DUR, dur + dxSec);
// Clamp: can't extend past the end of the source audio buffer
if (srcDur != null) {
const maxDur = srcDur - (newOffset || offset || 0);
newDur = Math.min(newDur, maxDur);
}
}
draw();
const { dpr, width: W, height: H } = resizeToCSS(canvas);
const ctx = canvas.getContext('2d');
// Use consistent pixels-per-second calculation
const pixelsPerSecond = zoomLevel; // 100 zoom = 100 pixels/second
// Convert newStart and newDur to physical canvas pixels
const x0CSS = newStart * pixelsPerSecond;
const wCSS = newDur * pixelsPerSecond;
const x0 = Math.floor(x0CSS * dpr);
const w = Math.max(1, Math.floor(wCSS * dpr));
ctx.save();
ctx.globalCompositeOperation = 'source-over';
ctx.fillStyle = 'rgba(255,255,255,0.08)';
ctx.fillRect(x0, 0, w, H);
ctx.strokeStyle = 'rgba(255,255,255,0.6)';
ctx.lineWidth = Math.max(1, Math.floor(2 * dpr));
ctx.strokeRect(x0 + 0.5, 0.5, w - 1, H - 1);
ctx.restore();
dragRef.current.preview = { start: newStart, duration: newDur, offset: newOffset };
}
function onPointerUp(e) {
canvas.releasePointerCapture?.(e.pointerId);
// Selection box completion removed - now handled by SelectionOverlay component
if (!interactive) { draw(); return; }
if (!dragRef.current.op || dragRef.current.clipIndex < 0) { draw(); return; }
const idx = dragRef.current.clipIndex;
const p = dragRef.current.preview;
const op = dragRef.current.op;
dragRef.current.op = null;
dragRef.current.clipIndex = -1;
dragRef.current.preview = null;
if (!p) { draw(); return; }
setTracks((prev) => prev.map((t) => {
if (t.id !== track.id || !Array.isArray(t.clips)) return t;
const nextClips = t.clips.map((c, i) => i === idx ? { ...c, start: p.start, duration: p.duration, offset: p.offset } : c);
return { ...t, clips: nextClips };
}));
// Log for study protocol (Activity 3) - only for move operations
if (op === 'move' && logOperation) {
logOperation('clip_move', { trackId: track.id, clipIndex: idx, newStart: p.start });
}
draw();
}
canvas.addEventListener('pointerdown', onPointerDown);
canvas.addEventListener('pointermove', onPointerMove);
window.addEventListener('pointerup', onPointerUp);
draw();
return () => {
try { ro.disconnect(); } catch {}
window.removeEventListener('resize', draw);
canvas.removeEventListener('pointerdown', onPointerDown);
canvas.removeEventListener('pointermove', onPointerMove);
window.removeEventListener('pointerup', onPointerUp);
// Clean up animation
if (animationId) {
cancelAnimationFrame(animationId);
}
};
}, [clipRects, currentTime, duration, zoomLevel, interactive, selectedClipId, selectedClipIds,
selectedTrackId, snapEnabled, gridSizeSec, setSelectedTrackId, setSelectedClipId,
setSelectedClipIds, setTracks, track?.id, peaksCache, clips, editorTool, logOperation]);
return (
<canvas
ref={canvasRef}
style={{
position: 'absolute',
inset: 0,
width: '100%',
height: `${height}px`,
pointerEvents: interactive ? 'auto' : 'none',
cursor: 'default',
background: 'transparent',
}}
/>
);
}
function hexToRgba(hex, alpha = 1) {
if (!hex) return `rgba(123,175,212,${alpha})`;
let c = hex.replace('#', '');
if (c.length === 3) c = c.split('').map((x) => x + x).join('');
const num = parseInt(c, 16);
const r = (num >> 16) & 255;
const g = (num >> 8) & 255;
const b = num & 255;
return `rgba(${r},${g},${b},${alpha})`;
}