To create animations that depend on time (like pulsing or moving elements), use the afterSync method on an LedMatrixInstance. This method accepts a SyncHook callback that is automatically executed after every matrix sync.
Callback Signature:
type SyncHook = (matrix: LedMatrixInstance, dt: number, t: number) => void;
matrix: The current LedMatrixInstance.dt: Time elapsed since the previous sync (in milliseconds).t: Monotonically-increasing system clock time (in milliseconds).
CRITICAL: When calling matrix.sync() inside the afterSync callback to create a continuous animation loop, you must wrap it in a setTimeout(() => matrix.sync(), 0). Failing to do this will cause a stack overflow error because the calls will occur synchronously on the same stack.
This pattern is similar to requestAnimationFrame in web development.
// Example: Creating a continuous animation loop
matrix.afterSync((mat, dt, t) => {
// 1. Perform your drawing logic here
mat.fgColor(0xff0000).setPixel(0, 0);
// 2. Schedule the next sync using setTimeout to avoid stack overflow
setTimeout(() => mat.sync(), 0);
});
// Start the initial sync
matrix.sync();