| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Subscription.RAF
Description
Overview
Miso.Subscription.RAF provides rAFSub, a subscription that hooks
into the browser's
requestAnimationFrame
loop. On each frame the browser calls back with a
DOMHighResTimeStamp
(milliseconds since page load, sub-millisecond precision), which is
forwarded to the component as an action.
This is the recommended driver for canvas-based animations and games because the browser throttles the callback to the display refresh rate (typically 60 fps) and pauses it automatically when the tab is hidden.
Quick start
import Miso import Miso.Subscription.RAF import Miso.Canvas data Action = Tick Double -- DOMHighResTimeStamp in ms myComponent = (componentmodel update view) {subs= [rAFSubTick ] ,events=defaultEvents} update :: Action ->Effectp props Model Action update (Tick t) = domodify(\m -> m { time = t })
Lifecycle
Internally rAFSub uses createSub:
- Acquire — schedules the first
requestAnimationFramecallback, which re-schedules itself on every invocation. - Release — calls
freeFunctionto cancel the callback and release the JS reference when the component unmounts.
See also
- Miso.Canvas — canvas drawing API driven by
rAFSubticks - Miso.Subscription.Util —
createSub - Miso.Subscription — re-export hub
Documentation
Arguments
| :: (Double -> action) | Callback fired each frame with a |
| -> Sub action |
A Sub for 60FPS animations when using requestAnimationFrame.
The Double returned is a DOMHighResTimeStamp expressed in milliseconds.
Arguments
| :: Double | Minimum interval between ticks in milliseconds (e.g. |
| -> action | Action to dispatch each time the interval elapses |
| -> Sub action |
Like rAFSub but fires action at most once per interval milliseconds.
Elapsed time is accumulated in IORefs inside the subscription so the
model is not touched between ticks — Miso only re-renders when a tick
actually fires, rather than on every animation frame.
app = defaultApp model update view
{ subs = [ rAFSubElapsed 175 Tick ] }