From 62d8955da985f36b3aef493a0a45c098590b80b2 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Fri, 1 Mar 2024 16:46:34 +0100 Subject: [PATCH] onFrame function --- packages/canvas/draw.mjs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/canvas/draw.mjs b/packages/canvas/draw.mjs index 9d0e0ac8..b03634c5 100644 --- a/packages/canvas/draw.mjs +++ b/packages/canvas/draw.mjs @@ -61,6 +61,25 @@ Pattern.prototype.draw = function (callback, { from, to, onQuery } = {}) { return this; }; +// this is a more generic helper to get a rendering callback for the currently active haps +// TODO: this misses events that are prolonged with clip or duration (would need state) +Pattern.prototype.onFrame = function (fn) { + if (typeof window === 'undefined') { + return this; + } + if (window.strudelAnimation) { + cancelAnimationFrame(window.strudelAnimation); + } + const animate = () => { + const t = getTime(); + const haps = this.queryArc(t, t); + fn(haps, t, this); + window.strudelAnimation = requestAnimationFrame(animate); + }; + requestAnimationFrame(animate); + return this; +}; + export const cleanupDraw = (clearScreen = true) => { const ctx = getDrawContext(); clearScreen && ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.width);