prendy
Version:
Make games with prerendered backdrops using babylonjs and repond
54 lines (53 loc) • 2.17 kB
JavaScript
import { getState, startNewItemEffect, stopNewEffect } from "repond";
export function doWhenStateVidStateChanges(stateVidId, checkShouldRun, callback, checkInitial = true) {
const initialVidState = getState().stateVids[stateVidId].vidState;
if (checkInitial && checkShouldRun(initialVidState)) {
callback();
return null;
}
const effectId = "doWhenStateVidStateChanges" + Math.random();
startNewItemEffect({
id: effectId,
run: ({ newValue: newVidState }) => {
if (!checkShouldRun(newVidState))
return;
stopNewEffect(effectId);
callback();
},
check: { type: "stateVids", prop: "vidState", id: stateVidId },
atStepEnd: true,
step: "stateVidStateUpdates",
});
return effectId;
}
export function doWhenStateVidStateSeeked(stateVidId, callback) {
const effectId = "doWhenStateVidStateSeeked" + Math.random();
startNewItemEffect({
id: effectId,
run: ({ newValue: newVidState }) => {
stopNewEffect(effectId);
callback();
},
check: { type: "stateVids", prop: "doneSeekingTime", id: stateVidId },
atStepEnd: true,
step: "stateVidStateUpdates",
});
return effectId;
}
export function doWhenStateVidStateReady(stateVidId, vidStateToCheck, callback, checkInitial = true) {
return doWhenStateVidStateChanges(stateVidId, (newState) => newState === vidStateToCheck, callback, checkInitial);
}
export function doWhenStateVidPlayOrPause(stateVidId, callback, checkInitial = true) {
return doWhenStateVidStateChanges(stateVidId, (newState) => newState === "play" || newState === "pause", callback, checkInitial);
}
export function makeVideoElementFromPath(filepath) {
const videoElement = document.createElement("video");
videoElement.controls = false;
videoElement.muted = true; // allow playing without interaction
videoElement.playsInline = true;
videoElement.loop = true;
videoElement.src = filepath;
const randomVideoId = Math.random().toString();
videoElement.id = randomVideoId;
return videoElement;
}