From 1558fd9519301a7679b1c0ea5d9d3da3d8d2b004 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Sat, 12 Mar 2022 20:38:06 +0100 Subject: [PATCH] fix draw return + optional querying --- repl/src/draw.mjs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/repl/src/draw.mjs b/repl/src/draw.mjs index aa8a6d2a..b8abca91 100644 --- a/repl/src/draw.mjs +++ b/repl/src/draw.mjs @@ -14,25 +14,29 @@ export const getDrawContext = (id = 'test-canvas') => { return canvas.getContext('2d'); }; -Pattern.prototype.draw = function (callback, duration) { +Pattern.prototype.draw = function (callback, queryDuration) { if (window.strudelAnimation) { cancelAnimationFrame(window.strudelAnimation); } const ctx = getDrawContext(); - let cycle, events; + let cycle, + events = []; const animate = (time) => { const t = Tone.getTransport().seconds; - const currentCycle = Math.floor(t / duration); - if (cycle !== currentCycle) { - cycle = currentCycle; - const begin = currentCycle * duration; - const end = (currentCycle + 2) * duration; - events = this.add(0).query(new State(new TimeSpan(begin, end))); + if (queryDuration) { + const currentCycle = Math.floor(t / queryDuration); + if (cycle !== currentCycle) { + cycle = currentCycle; + const begin = currentCycle * queryDuration; + const end = (currentCycle + 2) * queryDuration; + events = this.add(0).query(new State(new TimeSpan(begin, end))); + } } callback(ctx, events, t, time); window.strudelAnimation = requestAnimationFrame(animate); }; requestAnimationFrame(animate); + return this; }; export const cleanup = () => {