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');
};
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 = () => {