remove old .draw, rename .onFrame to .draw

+ breaking: from, to are now called lookbehind, lookahead
+ migrate .pianoroll
This commit is contained in:
Felix Roos 2024-03-28 07:35:01 +01:00
parent eb8ac29136
commit ab015ff48a
2 changed files with 8 additions and 43 deletions

View File

@ -39,41 +39,9 @@ function stopAnimationFrame(id) {
function stopAllAnimations() {
Object.keys(animationFrames).forEach((id) => stopAnimationFrame(id));
}
Pattern.prototype.draw = function (callback, { id = 'std', from, to, onQuery, ctx } = {}) {
if (typeof window === 'undefined') {
return this;
}
stopAnimationFrame(id);
ctx = ctx || getDrawContext();
let cycle,
events = [];
const animate = (time) => {
const t = getTime();
if (from !== undefined && to !== undefined) {
const currentCycle = Math.floor(t);
if (cycle !== currentCycle) {
cycle = currentCycle;
const begin = currentCycle + from;
const end = currentCycle + to;
setTimeout(() => {
events = this.query(new State(new TimeSpan(begin, end)))
.filter(Boolean)
.filter((event) => event.part.begin.equals(event.whole.begin));
onQuery?.(events);
}, 0);
}
}
callback(ctx, events, t, time);
animationFrames[id] = requestAnimationFrame(animate);
};
requestAnimationFrame(animate);
return this;
};
// this is a more generic helper to get a rendering callback for the currently active haps
// maybe this could also use Drawer internally? might need to support setting a custom pattern in Drawer
let memory = {};
Pattern.prototype.onFrame = function (fn, options) {
Pattern.prototype.draw = function (fn, options) {
if (typeof window === 'undefined') {
return this;
}

View File

@ -36,26 +36,23 @@ const getValue = (e) => {
};
Pattern.prototype.pianoroll = function (options = {}) {
let { cycles = 4, playhead = 0.5, overscan = 1, hideNegative = false, ctx, id } = options;
let { cycles = 4, playhead = 0.5, overscan = 0, hideNegative = false, ctx = getDrawContext(), id = 1 } = options;
let from = -cycles * playhead;
let to = cycles * (1 - playhead);
const inFrame = (hap, t) => (!hideNegative || hap.whole.begin >= 0) && hap.isWithinTime(t + from, t + to);
this.draw(
(ctx, haps, t) => {
const inFrame = (event) =>
(!hideNegative || event.whole.begin >= 0) && event.whole.begin <= t + to && event.endClipped >= t + from;
(haps, time) => {
pianoroll({
...options,
time: t,
time,
ctx,
haps: haps.filter(inFrame),
haps: haps.filter((hap) => inFrame(hap, time)),
});
},
{
from: from - overscan,
to: to + overscan,
ctx,
lookbehind: from - overscan,
lookahead: to + overscan,
id,
},
);