fix: clear hydra on reset

This commit is contained in:
Felix Roos 2024-03-10 00:29:52 +01:00
parent 0875e805b4
commit 012b19c79e
2 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import { getDrawContext } from '@strudel/core';
import { getDrawContext, controls } from '@strudel/core';
let latestOptions;
@ -11,6 +11,7 @@ function appendCanvas(c) {
return testCanvas;
}
let hydra;
export async function initHydra(options = {}) {
// reset if options have changed since last init
if (latestOptions && JSON.stringify(latestOptions) !== JSON.stringify(options)) {
@ -19,7 +20,6 @@ export async function initHydra(options = {}) {
latestOptions = options;
//load and init hydra
if (!document.getElementById('hydra-canvas')) {
console.log('reinit..');
const {
src = 'https://unpkg.com/hydra-synth',
feedStrudel = false,
@ -27,7 +27,7 @@ export async function initHydra(options = {}) {
} = { detectAudio: false, ...options };
await import(/* @vite-ignore */ src);
const hydra = new Hydra(hydraConfig);
hydra = new Hydra(hydraConfig);
if (feedStrudel) {
const { canvas } = getDrawContext();
canvas.style.display = 'none';
@ -37,4 +37,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;

View File

@ -17,6 +17,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 {
@ -75,6 +76,11 @@ export function Repl({ embedded = false }) {
onUpdateState: (state) => {
setReplState({ ...state });
},
onToggle: (playing) => {
if (!playing) {
clearHydra();
}
},
afterEval: (all) => {
const { code } = all;
setLatestCode(code);
@ -165,6 +171,7 @@ export function Repl({ embedded = false }) {
const resetEditor = async () => {
resetGlobalEffects();
clearCanvas();
clearHydra();
resetLoadedSounds();
editorRef.current.repl.setCps(0.5);
await prebake(); // declare default samples
@ -189,6 +196,7 @@ export function Repl({ embedded = false }) {
setActivePattern(patternData.id);
setViewingPatternData(patternData);
clearCanvas();
clearHydra();
resetLoadedSounds();
resetGlobalEffects();
await prebake(); // declare default samples