improve hydra audio detection initialization

This commit is contained in:
Ámbar Tenorio Fornés 2023-11-22 17:39:16 +01:00
parent 0a280e9399
commit e0e896d4b5

View file

@ -1,27 +1,33 @@
import { getDrawContext } from '@strudel.cycles/core'; import { getDrawContext } from '@strudel.cycles/core';
let options = ''; let audio = false;
let hydra;
export async function initHydra(config) { function appendCanvas(c){
//load and init hydra
if (!document.getElementById('hydra-canvas')) {
const { canvas: testCanvas } = getDrawContext(); const { canvas: testCanvas } = getDrawContext();
await import('https://unpkg.com/hydra-synth'); c.canvas.id = 'hydra-canvas';
h = new Hydra({ detectAudio: config?.audio }); c.canvas.style.position = 'absolute';
h.canvas.id = 'hydra-canvas'; c.canvas.style.top = '0px';
h.canvas.style.position = 'absolute'; testCanvas.after(c.canvas);
h.canvas.style.top = '0px';
testCanvas.after(h.canvas);
} }
// update options export async function initHydra(config) {
if (options != JSON.stringify(config)) { audio = config?.audio || false;
options = JSON.stringify(config); //load and init hydra
if (!document.getElementById('hydra-canvas')) {
await import('https://unpkg.com/hydra-synth');
hydra = new Hydra({ detectAudio: audio });
appendCanvas(hydra);
}
new Hydra({ // if config.audio is true
canvas: document.getElementById('hydra-canvas'), // and current canvas des not detect audio
detectAudio: config?.audio, if (config?.audio && !hydra.detectAudio ){
}); //remove previous canvas without audio detection
document.getElementById('hydra-canvas').remove()
// create and append a new audio responsive canvas
hydra = new Hydra({ detectAudio: audio});
appendCanvas(hydra)
} }
} }