Skip to content

Commit

Permalink
initCanvas
Browse files Browse the repository at this point in the history
  • Loading branch information
TodePond committed Feb 1, 2025
1 parent bfa7448 commit a283520
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/hydra.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a283520

Please sign in to comment.