Schedule a callback for the next native frame. requestAnimationFrame and cancelAnimationFrame are native globals: they need no import and work at module scope, in component bodies, and in event handlers.
const handle = requestAnimationFrame((timestamp) => {
console.log("frame at", timestamp)
})
cancelAnimationFrame(handle)Morph reuses the browser names because the scheduling contract matches: callbacks run once at the start of the next frame, receive a millisecond timestamp, return a numeric handle, and can be cancelled before they run. Morph accepts a smaller callback surface than the browser and rejects the rest at check time, so the shared name stays honest under Name Follows Behavior.
requestAnimationFrame(callback: (timestamp: number) => void): number
cancelAnimationFrame(handle: number): void| API | Parameters | Returns | Behavior |
|---|---|---|---|
requestAnimationFrame |
callback | number |
Schedules a one-shot callback for the next frame and returns a positive handle |
cancelAnimationFrame |
handle from requestAnimationFrame |
— | Prevents a pending callback from running; unknown or finished handles are ignored |
Handles are positive integers. They identify one pending callback and are not reused for a different request.
A callback may declare zero parameters or one timestamp parameter. JavaScript ignores arguments a callback does not declare, so both forms are valid:
requestAnimationFrame(() => {
console.log("next frame")
})
requestAnimationFrame((timestamp: number) => {
console.log("next frame at", timestamp, "ms")
})The callback can be an inline arrow/function or a named callable:
function paint(timestamp: number) {
console.log("paint", timestamp)
}
const measure = (timestamp: number) => {
console.log("measure", timestamp)
}
requestAnimationFrame(paint)
requestAnimationFrame(measure)Named callbacks must declare their timestamp parameter with a numeric type. An unannotated parameter has no C++ type at module scope, so function paint(timestamp) is rejected; write timestamp: number. Inline callbacks may leave the parameter unannotated because the call site supplies the type.
Callbacks must be synchronous. An async callback returns a promise that the browser would resume through the microtask queue; Morph's frame scheduler invokes the callback directly, so async callbacks (including async function declarations passed by name) are rejected instead of silently dropping work after an await.
A function declaration used as a named callback must be declared at module scope. A function declaration nested inside another function captures stack locals by reference and would not survive until the next frame, so it is rejected; use an inline callback or a const arrow/function value instead.
To animate, request the next frame from inside the callback:
function spin(timestamp: number) {
console.log("spin at", timestamp)
requestAnimationFrame(spin)
}
requestAnimationFrame(spin)Store the handle returned by requestAnimationFrame and pass that handle directly to cancelAnimationFrame:
const handle = requestAnimationFrame(() => {
console.log("never runs")
})
cancelAnimationFrame(handle)A handle that has already run, or that was never scheduled by the current process, is ignored. Cancellation is safe at any time.
morph check only accepts handles that provably hold frame-handle values: a numeric literal or a variable initialized from requestAnimationFrame and never reassigned. Reassigning that variable makes it invalid as a handle, because the value may no longer identify a frame callback. Pass the returned value directly or keep it in a dedicated const.
Frames advance while the app event loop is running. The runtime pumps pending callbacks before each frame is committed, so the callback sees the state from the previous frame and its mutations are reflected in the frame that follows.
The scheduler is feature-gated: an app that never calls requestAnimationFrame does not link the frame scheduler. There is no background thread and no timer wakeup; callbacks run on the main loop's existing frame cadence.
morph check rejects anything outside the supported surface, so unsupported code never compiles to a different meaning:
| Form | Error |
|---|---|
requestAnimationFrame() |
mx-js-syntax |
requestAnimationFrame(cb, extra) |
mx-js-syntax |
requestAnimationFrame(...callbacks) |
mx-js-syntax |
requestAnimationFrame(42) |
mx-js-syntax |
requestAnimationFrame((a, b) => ...) |
mx-js-syntax |
requestAnimationFrame((t: string) => ...) |
mx-js-syntax |
requestAnimationFrame((t = 0) => ...) (default parameter) |
mx-js-syntax |
requestAnimationFrame(async (t) => ...) / an async function passed by name |
mx-js-syntax |
function tick(t) {} then requestAnimationFrame(tick) |
mx-js-syntax |
A function declaration nested in another function, then requestAnimationFrame(tick) |
mx-js-syntax |
const raf = requestAnimationFrame |
mx-js-member |
cancelAnimationFrame("1") |
mx-js-syntax |
cancelAnimationFrame(handle + 1) |
mx-js-syntax |
cancelAnimationFrame(1.5) (non-integer literal) |
mx-js-syntax |
let handle = requestAnimationFrame(...) then reassigning handle |
mx-js-syntax |
| Reassigning a named callback before passing it | mx-js-syntax |
window.requestAnimationFrame(...) |
mx-js-global |
The callback count is deliberately limited: JavaScript passes undefined for extra declared parameters, but Morph has no native representation for that call shape. Rejecting it keeps accepted callbacks exact.
Schedule work after the current event handler finishes. Locals captured by the callback stay alive until it runs:
import { morphState } from 'morph'
export const windowConfig = { title: "Deferred", width: 640, height: 480 }
export default function Deferred() {
const [status, setStatus] = morphState("idle")
const run = () => {
const label = "frame-callback"
requestAnimationFrame((timestamp: number) => {
console.log(label, timestamp)
setStatus("ran")
})
}
return (
<body>
<text>{status}</text>
<button onClick={run}>Run</button>
</body>
)
}