Skip to content

Compositions can't measure text or layout: no document, no canvas measureText, no geometry on refs #54

Closed

Description

@shinyobjectz

From #13 (@kevinschaich): a composition can't measure its text or layout before it draws, and many motion components rely on that. We ran snapcn's 61 free components (MIT) through raea; 27 of them measure, in one of two ways, and both fail:

1. Canvas text measurement and fonts: there is no document in a composition.

  • document.createElement("canvas").getContext("2d").measureText(...) throws "Can't find variable: document". This crashes bloom-lockup, hello-zoom, sky-lockup, spec-sweep and title-sweep.
  • document.fonts.ready and document.fonts.load aren't there either.
  • Components that guard with typeof document fall back to guessed widths, so their layout drifts from Chrome. In word-gather, every word sits a little off.

2. Layout reads on refs: a ref's element has no geometry and no DOM API.

  • An element from useRef, read in useEffect, has offsetWidth, offsetHeight, offsetLeft and offsetTop undefined.
  • It has no getBoundingClientRect(), children, querySelectorAll() or style.
  • This crashes karaoke-captions and word-captions (getBoundingClientRect is not a function), text-reveal (NaN from offsetWidth), text-rewrite (querySelectorAll), type-morph ([...el.children]) and search-typing (el.style).

A minimal repro (after mount, in raea):

const ref = useRef<HTMLSpanElement>(null);
useEffect(() => console.log(ref.current?.offsetWidth, typeof ref.current?.getBoundingClientRect), []);
// <span ref={ref} style={{ fontSize: 64 }}>Hello world</span>

Chrome logs 335 function. raea logs undefined undefined, and document is undefined.

These components measure in an effect while the first frame is held (delayRender/continueRender), so they expect the read to be synchronous and the measured frame to be the one that's captured. An app page in raea (@raea/ui's DOM) already has all of these: geometry from the host's layout, children, querySelectorAll, and a canvas 2D context with measureText. A composition's elements come from bevy-react's bridge, which has none of them.

Activity

  1. shinyobjectz commented on Nov 13, 2026

    @shinyobjectz
    OwnerAuthor

    Fixed on main for 0.1.2. A composition's refs, document and window now answer from the same layout that draws the frame, synchronously: getBoundingClientRect (through transforms and perspective), offsetWidth/offsetHeight and the rest, getComputedStyle, children and selectors, document.createElement, createRange, document.fonts, ResizeObserver, a canvas's measureText with the real fonts, and svg text lengths. delayRender/continueRender hold a frame until a measure lands, and the reads work in live play too.

    Your repro (the probe in bench/layout-reads/probes) now puts "here" at x 342, where Chrome puts it.

    Measured against headless Chrome (same fonts, deviceScaleFactor 1, every text run's box and ink within 1 px at the first, middle and last frames) on snapcn's 33 measuring components:

    • before: 3 of 33 pass
    • now: 23 of 33 pass

    The 10 still off, each with its cause, are listed in docs/layout-reads.feature (LR10): mostly a few pixels of ink through transforms, and SF's tracking table on the canvas.

    Cost: a composition that reads nothing renders the same frames in the same time (P26 unchanged). Over 300 frames, the first read in a frame costs about 0.39 ms, and each further read in that frame about 0.01 ms.

    The manual's new guide, "Web components that measure" (docs/manual/guides/web-components.md), lists what answers and what doesn't yet (no innerHTML, a Range across wrapped lines is one rect, MutationObserver and IntersectionObserver never call back).

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