diff --git a/packages/hydra/hydra.mjs b/packages/hydra/hydra.mjs index e784228d..b25c7c24 100644 --- a/packages/hydra/hydra.mjs +++ b/packages/hydra/hydra.mjs @@ -1,6 +1,8 @@ import { getDrawContext } from '@strudel/draw'; +import { controls } from '@strudel/core'; let latestOptions; +let hydra; export async function initHydra(options = {}) { // reset if options have changed since last init @@ -25,7 +27,7 @@ export async function initHydra(options = {}) { hydraConfig.canvas = canvas; await import(/* @vite-ignore */ src); - const hydra = new Hydra(hydraConfig); + hydra = new Hydra(hydraConfig); if (feedStrudel) { const { canvas } = getDrawContext(); canvas.style.display = 'none'; @@ -34,4 +36,14 @@ export async function initHydra(options = {}) { } } +export function clearHydra() { + if (hydra) { + hydra.hush(); + } + globalThis.s0?.clear(); + document.getElementById('hydra-canvas')?.remove(); + globalThis.speed = controls.speed; + globalThis.shape = controls.shape; +} + export const H = (p) => () => p.queryArc(getTime(), getTime())[0].value; diff --git a/website/src/repl/Repl.jsx b/website/src/repl/Repl.jsx index dd164965..fb73b5cc 100644 --- a/website/src/repl/Repl.jsx +++ b/website/src/repl/Repl.jsx @@ -18,6 +18,7 @@ import { import { defaultAudioDeviceName } from '../settings.mjs'; import { getAudioDevices, setAudioDevice } from './util.mjs'; import { StrudelMirror, defaultSettings } from '@strudel/codemirror'; +import { clearHydra } from '@strudel/hydra'; import { useCallback, useEffect, useRef, useState } from 'react'; import { settingsMap, useSettings } from '../settings.mjs'; import { @@ -76,6 +77,11 @@ export function Repl({ embedded = false }) { onUpdateState: (state) => { setReplState({ ...state }); }, + onToggle: (playing) => { + if (!playing) { + clearHydra(); + } + }, afterEval: (all) => { const { code } = all; setLatestCode(code); @@ -166,6 +172,7 @@ export function Repl({ embedded = false }) { const resetEditor = async () => { resetGlobalEffects(); clearCanvas(); + clearHydra(); resetLoadedSounds(); editorRef.current.repl.setCps(0.5); await prebake(); // declare default samples @@ -190,6 +197,7 @@ export function Repl({ embedded = false }) { setActivePattern(patternData.id); setViewingPatternData(patternData); clearCanvas(); + clearHydra(); resetLoadedSounds(); resetGlobalEffects(); await prebake(); // declare default samples