From a283520528bf05c42b3da6bd6798914acda9c1ab Mon Sep 17 00:00:00 2001 From: "Lu[ke] Wilson" Date: Sat, 1 Feb 2025 18:08:42 +0000 Subject: [PATCH] initCanvas --- src/hydra.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/hydra.js b/src/hydra.js index acdd20e..c4d6168 100644 --- a/src/hydra.js +++ b/src/hydra.js @@ -69,6 +69,26 @@ export class HydraSession { throw Error("'useStrudelCanvas(s0)' has been renamed to 's0.initStrudel'"); }; + const contexts = {}; + HydraSource.prototype.initCanvas = function (width = 1000, height = 1000) { + if (contexts[this.label] == undefined) { + const canvas = new OffscreenCanvas(width, height); + const ctx = canvas.getContext('2d'); + contexts[this.label] = ctx; + } + + const ctx = contexts[this.label]; + const canvas = ctx.canvas; + if (canvas.width !== width && canvas.height !== height) { + canvas.width = width; + canvas.height = height; + } else { + ctx.clearRect(0, 0, width, height); + } + this.init({ src: canvas }); + return ctx; + }; + const clamp = (num, min, max) => Math.min(Math.max(num, min), max); // Enables Hydra to use Strudel frequency data