Skip to content

Composition canvas 2D context is missing setTransform and createImageData #55

Closed

Description

@shinyobjectz

From #13's run of snapcn's free components through raea: in a composition, a <canvas>'s 2D context lacks part of the API.

  • ctx.setTransform(...) is not a function. This crashes dot-bloom at frame 15.
  • ctx.createImageData(w, h) is not a function. This crashes orb-swarm at frame 80.

@raea/ui's canvas context (sdk/ui/src/canvas/context.ts), the one an app page uses, has both, along with measureText and getImageData. A composition's canvas is a different, smaller one. Same root as #54: a composition's elements aren't the app DOM.

Activity

  1. Razepriv commented on Nov 11, 2026

    @Razepriv

    While reading the composition path on main (0ea4eab) for this and #56, I found something that may bear on the runtime decision.

    A composition's <canvas> ref is bevy-react 0.8.1's canvas element. Its CanvasContext (bevy-react/src/canvas.ts) records commands and sends them as {op: "update", kind: "canvas", props: {drawAppend}} (bridge.ts:314). I can't find anything in raea that reads drawAppend. page-ops paints a canvas only from __canvas2d (systems/page-ops/src/lib.rs:243, systems/canvas/src/lib.rs:49), the prop that sdk/ui's context sends. If that's right, adding setTransform and createImageData to bevy-react's context would stop the two crashes, but dot-bloom and orb-swarm would still draw an empty canvas. I haven't confirmed this with a render yet.

    If you'd rather not wait on the runtime decision, here is a small change that could be deleted later:

    • Composition canvas 2D context is missing setTransform and createImageData #55: sdk/ui/src/canvas/context.ts sends everything through one send() (:61-70, wire.set(id, "__canvas2d", …)). It would get an optional sink, and getContext("2d") on a composition's canvas would return that context with a sink that sends __canvas2d through bevy-react's channel. Compositions would then get the full context (transforms, text, measureText, image data) with no Rust change. getImageData would still read zeros there, because canvas.read is only registered for app pages. context.ts is at 479 of the 500 lines tools/size.sh allows, so the sink would be a few lines.
    • Compositions lack requestAnimationFrame and Image #56: a raea-owned sdk/raea/src/frame-globals.ts, installed only where a global is missing (as pure.ts pins Math.random and Date). It would add requestAnimationFrame, with callbacks run once per frame step from setF (runtime/bundle/src/entry.rs:92) at f * 1000 / fps so they stay deterministic, and a small Image that resolves /media/ paths the way <img> does. pulsing-border would still fail after this, since @paper-design/shaders-react needs WebGL2.

    Would you take that as a PR, or wait for the runtime decision? Either is fine. I'm working with Claude Code on a source build.

  2. shinyobjectz commented on Nov 12, 2026

    @shinyobjectz
    OwnerAuthor

    Fixed on main in 645b085; it ships in 0.1.3. @Razepriv, your reading was right: bevy-react's canvas context records an API nothing in raea draws. I went the way you sketched, so no PR is needed. Thank you for working it out.

    In a composition, ref.current.getContext("2d") is now @raea/ui's CanvasRenderingContext2D, the same context an app page has: setTransform, createImageData, paths, text, gradients and measureText (estimated in a composition). Its display list goes to the element as __canvas2d, which the page already drew for app pages. canvas.width and canvas.height read the element's props (300 x 150 when unset). Setting them resizes and clears the bitmap, as in a browser.

    One limit: getImageData reads zeros in a composition, since there's nothing to read pixels back from.

    Checked: red drawn, then lime under setTransform(1, 0, 0, 1, 50, 50), both land where Chrome puts them, across a 10-frame MP4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions