Browse Source

Play, Pause and Stop button logic moved into sync.js under different callbacks.

refactor/modularize
RUSHIL AMBARISH KADU 6 months ago
parent
commit
b404da4d44
  1. 29
      steps/src/main.js
  2. 27
      steps/src/sync.js

29
steps/src/main.js

@ -26,7 +26,13 @@ import {
updateLoadingModal, updateLoadingModal,
showLoadingModal, showLoadingModal,
} from "./modal.js"; // Modify this import } from "./modal.js"; // Modify this import
import { animationLoop, videoFrameCallback } from "./sync.js";
import {
animationLoop,
videoFrameCallback,
startPlayback,
pausePlayback,
stopPlayback,
} from "./sync.js";
import { radarSketch } from "./p5/radarSketch.js"; import { radarSketch } from "./p5/radarSketch.js";
import { speedGraphSketch } from "./p5/speedGraphSketch.js"; import { speedGraphSketch } from "./p5/speedGraphSketch.js";
import { parseVisualizationJson, parseJsonWithOboe } from "./fileParsers.js"; import { parseVisualizationJson, parseJsonWithOboe } from "./fileParsers.js";
@ -668,32 +674,23 @@ applySnrBtn.addEventListener("click", () => {
// Event listener for play/pause button click. // Event listener for play/pause button click.
playPauseBtn.addEventListener("click", () => { playPauseBtn.addEventListener("click", () => {
if (!appState.vizData && !videoPlayer.src) return; if (!appState.vizData && !videoPlayer.src) return;
appState.isPlaying = !appState.isPlaying; appState.isPlaying = !appState.isPlaying;
playPauseBtn.textContent = appState.isPlaying ? "Pause" : "Play"; playPauseBtn.textContent = appState.isPlaying ? "Pause" : "Play";
if (appState.isPlaying) { if (appState.isPlaying) {
if (videoPlayer.src && videoPlayer.readyState > 1) {
appState.masterClockStart = performance.now();
appState.mediaTimeStart = videoPlayer.currentTime;
appState.lastSyncTime = appState.masterClockStart;
videoPlayer.play();
videoPlayer.requestVideoFrameCallback(videoFrameCallback); // Start the high-precision loop
}
requestAnimationFrame(animationLoop); // Keep rAF for non-video sync (e.g. scrubbing)
startPlayback();
} else { } else {
if (videoPlayer.src) videoPlayer.pause();
pausePlayback();
} }
}); });
// Event listener for stop button click. // Event listener for stop button click.
stopBtn.addEventListener("click", () => { stopBtn.addEventListener("click", () => {
videoPlayer.pause();
if (!appState.vizData && !videoPlayer.src) return;
stopPlayback();
appState.isPlaying = false; appState.isPlaying = false;
playPauseBtn.textContent = "Play"; playPauseBtn.textContent = "Play";
if (appState.vizData) {
updateFrame(0, true);
} else if (videoPlayer.src) {
videoPlayer.currentTime = 0;
}
if (appState.speedGraphInstance) appState.speedGraphInstance.redraw(); if (appState.speedGraphInstance) appState.speedGraphInstance.redraw();
}); });

27
steps/src/sync.js

@ -11,6 +11,33 @@ import {
} from "./dom.js"; } from "./dom.js";
import { findRadarFrameIndexForTime } from "./utils.js"; import { findRadarFrameIndexForTime } from "./utils.js";
// --- NEW Playback Control Functions ---
export function startPlayback() {
if (videoPlayer.src && videoPlayer.readyState > 1) {
appState.masterClockStart = performance.now();
appState.mediaTimeStart = videoPlayer.currentTime;
appState.lastSyncTime = appState.masterClockStart;
videoPlayer.play();
videoPlayer.requestVideoFrameCallback(videoFrameCallback); // Start the high-precision loop
}
requestAnimationFrame(animationLoop); // Keep rAF for non-video sync (e.g. scrubbing)
}
export function pausePlayback() {
if (videoPlayer.src) {
videoPlayer.pause();
}
}
export function stopPlayback() {
videoPlayer.pause();
if (appState.vizData) {
updateFrame(0, true);
} else if (videoPlayer.src) {
videoPlayer.currentTime = 0;
}
}
export function videoFrameCallback(now, metadata) { export function videoFrameCallback(now, metadata) {
// If the video is no longer playing, stop the callback loop. // If the video is no longer playing, stop the callback loop.

Loading…
Cancel
Save