fix draw return + optional querying

This commit is contained in:
Felix Roos 2022-03-12 20:38:06 +01:00
parent 984bd95cc0
commit 1558fd9519

View File

@ -14,25 +14,29 @@ export const getDrawContext = (id = 'test-canvas') => {
return canvas.getContext('2d'); return canvas.getContext('2d');
}; };
Pattern.prototype.draw = function (callback, duration) { Pattern.prototype.draw = function (callback, queryDuration) {
if (window.strudelAnimation) { if (window.strudelAnimation) {
cancelAnimationFrame(window.strudelAnimation); cancelAnimationFrame(window.strudelAnimation);
} }
const ctx = getDrawContext(); const ctx = getDrawContext();
let cycle, events; let cycle,
events = [];
const animate = (time) => { const animate = (time) => {
const t = Tone.getTransport().seconds; const t = Tone.getTransport().seconds;
const currentCycle = Math.floor(t / duration); if (queryDuration) {
if (cycle !== currentCycle) { const currentCycle = Math.floor(t / queryDuration);
cycle = currentCycle; if (cycle !== currentCycle) {
const begin = currentCycle * duration; cycle = currentCycle;
const end = (currentCycle + 2) * duration; const begin = currentCycle * queryDuration;
events = this.add(0).query(new State(new TimeSpan(begin, end))); const end = (currentCycle + 2) * queryDuration;
events = this.add(0).query(new State(new TimeSpan(begin, end)));
}
} }
callback(ctx, events, t, time); callback(ctx, events, t, time);
window.strudelAnimation = requestAnimationFrame(animate); window.strudelAnimation = requestAnimationFrame(animate);
}; };
requestAnimationFrame(animate); requestAnimationFrame(animate);
return this;
}; };
export const cleanup = () => { export const cleanup = () => {