# API reference

Import `createPixelCurrent` from `pixel-current.mjs`. The core has no runtime dependencies. Type declarations live beside the module.

## Constructor

`createPixelCurrent(canvas, {from, to, recipe?, motion?, onProgress?})`

Use a dedicated HTML canvas with an explicit CSS width and height. The instance owns its drawing context, backing resolution, event listeners and observers. DPR is capped at 1.5. Await `app.ready` before controlling it. Handle rejection and leave your HTML fallback usable. Do not start a view after its owner has unmounted.

`onProgress(number)` runs when progress changes. Keep it inexpensive. Do not feed its values straight back into `setProgress` during playback.

## Sources

Text: `{kind:'text', value:'IDEA'}`. One to 42 visible Unicode code points, trimmed; controls and empty strings reject. System font metrics vary by OS.

Image: `{kind:'image', pixels:{width,height,data}}`. `data` is RGBA bytes in a Uint8Array or Uint8ClampedArray; dimensions must be valid and no greater than 4096 per side. Completely transparent input rejects. Transparent marks preserve their visible silhouette; an opaque photograph remains a rectangle.

`loadLocalImage(file)` from `image-input.mjs` checks PNG/JPEG/WebP file headers, rejects files over 5 MB or dimensions over 4096 before decoding, and resamples to at most 900 pixels per side. It returns `{pixels,dataURL,...}`. The caller owns URL/CORS safety for any separately decoded remote image. This adapter does not request remote URLs.

## Controls

| Method | Result and responsibility |
| --- | --- |
| `ready` | Promise of initial source readiness. Catch source errors. |
| `play({to:1,duration:2400})` | Promise resolving to `{status,progress}`. Destination is normalized; duration must be 100–30000 ms. `to:0` reverses. |
| `setProgress(0.4)` | Seek immediately; finite values clamp to 0–1. Ends current finite playback. |
| `pause()` / `resume()` | Hold and resume a finite passage. Hidden/offscreen time does not advance it. |
| `setSources(from,to)` | Promise; validates and replaces both masks together. Invalid replacements retain previous masks. Superseded/disposed replacements resolve false. |
| `configure(recipe)` | Promise; validates a closed recipe. Count/seed changes resample. Catch invalid settings. |
| `setView({yaw,pitch})` | Adjust projection; angles are radians and must be finite. It does not create or reconstruct 3D geometry. |
| `setMotionAllowed(false)` | Application preference; OS reduced motion still takes precedence. Playback settles without ongoing animation. |
| `bindScroll({target,start:0.82,end:0.18})` | Returns an idempotent detach function. Requires a positive-size element and valid anchors. Detach on owner cleanup. |
| `inspect()` | Version, readiness, disposal, progress, count, playing/paused state, RAF, visibility, CSS dimensions, DPR, frames, last CPU draw time and scroll-binding count. Not a GPU/FPS benchmark. |
| `dispose()` | Idempotent teardown: cancel owned RAFs, observers/listeners/bindings and release particle arrays. Recreate for a new view. |

Playback completion can be `finished`, `reduced-motion`, `motion-disabled`, `seek`, `superseded`, `sources-changed`, `scroll` or `disposed`. An interruption is a resolved completion reason, not necessarily an error. Inspect it if your application distinguishes completion from cancellation. Invalid API input can throw/reject.

## Scroll ownership

Progress starts at zero when the target's top meets 82% of viewport height and reaches one when its bottom meets 18%. Make the section tall enough; use a sticky canvas if it should remain visible. Use one progress owner: a scroll binding, finite playback, or your application's router/gesture state. An active scroll update supersedes finite playback.

## React

`PixelCurrentCanvas` from `react-adapter.mjs` creates and disposes a runtime instance. Pass stable or memoized `from`, `to` and `recipe`; changing their identities recreates it. Pass normalized controlled `progress`, a host `motion` preference, `onError` and `onProgress` if needed. React is an optional peer supplied by your app. The provided JSX example keeps a readable heading and a keyboard-operable progress input. No cloud state or React installation is bundled.
