Skip to content

Rendered images ​

A picture that takes time to make, such as a map drawn by a WebGL library from remote tiles, renders through <RenderedImage> (included in the contract-1 baseline):

tsx
import { RenderedImage } from 'imageflow';

<Element id="map" rect={rect}>
  <RenderedImage label="Map" inputs={{ style, longitude, latitude, zoom }} style={{ position: 'absolute', inset: 0 }}
    draw={async ({ width, height, pixelRatio, mode, signal }) => drawMap({ style, longitude, latitude, zoom }, { width, height, pixelRatio, signal })} />
</Element>
  • Drawing: ImageFlow calls draw when the element mounts and again when inputs (JSON-like data, everything draw depends on) changes. It passes the box's size in CSS pixels (96 per inch), pixelRatio (device pixels per CSS pixel), mode ('preview', 'check' or 'export') and an AbortSignal. Resolve to a canvas, an OffscreenCanvas or an image Blob; ImageFlow shows it as an image that fills the box. Nothing is saved in the book.
  • Output: output="vector" declares that draw returns SVG: it is drawn once for every view, at a pixelRatio of 1 (it does not depend on one), and returning pixels is an error. output="raster" declares pixels on purpose, such as satellite imagery or photographs. Without output, either is accepted, but a raster result is a rendered-image-raster Book check warning: a drawn picture such as a map or chart prints sharper and smaller as vectors, with the book's fonts. Prefer vectors for anything drawn.
  • Vector pictures: resolve to SVG markup (a string), an <svg> element or a Blob of type image/svg+xml, and ImageFlow places the SVG into the page itself, not as an <img>, so the PDF gets vector paths and real text instead of pixels. Its text uses the template's own @font-face fonts and stylesheet like any other text on the page, so name the template's bundled families in font-family and keep to characters those fonts contain: PDF preflight rejects text that falls back to a system font. Stroked text (an SVG stroke on <text>, such as a label halo) prints as a Type 3 font, which preflight also rejects; draw a halo as filled copies of the text shifted around it instead. Like other SVG text, it is not part of the Book check's text-size and safe-area rules. The SVG needs a viewBox, which is scaled to cover the box as raster pictures are (preserveAspectRatio="xMidYMid slice" unless the SVG sets its own); a convenient choice is the target's width and height in CSS pixels. Its ids are made unique on the page, <script>, <foreignObject> and on… attributes are removed, and there is no resolution check. Because the SVG is part of the page, the template's CSS styles it too: colours written as style="fill: var(--map-water, #cbd9d6)" follow variables the layout sets, such as a colour theme's, without drawing the picture again (presentation attributes like fill="…" do not accept var()). Keep vector pictures to what the PDF needs: tens of thousands of paths are fine, millions make a slow, large PDF. The box carries data-vector; the capability is rendered-vectors.
  • Resolution: previews draw at the screen's density (at most 2×; thumbnails and checks at 0.5×). Export draws at the format's target resolution ÷ 96, 300 ppi when the format sets none, so a picture is as sharp in the PDF as the format asks.
  • Waiting: the Book check and every export page wait for all pictures on the page before they are final. Each picture may draw for 60 s (120 s in export), counted from when it starts drawing, not while it waits for others; the check or export waits as long as pictures keep finishing. In the editor and the presentation, only time the window draws counts: a hidden or minimised window, or one on another Space, gets no animation frames, which map libraries need to finish. In the editor, pictures fill in as they finish, over a striped placeholder, and the Book check lists failures as they happen and shows while pictures are still drawing. A failed picture shows its message over the picture drawn there before, if any; layered pictures that fail together, such as shaded relief under a map's lines, stack their messages. At most two draw at once in the book view and one elsewhere, so a long book does not open a GPU context per page.
  • Drawing again: the picture is drawn again when inputs, the box's size or the output resolution changes. Each drawing uses the draw and values of the render that started it, so draw anything that must match the picture (such as a route over a map) from the same values as inputs.
  • Book view, thumbnails and presentation: while a book window stays open, identical pictures can be reused across page switches, saves and presentations, in the book view as in the page and layout thumbnails. Cached pictures are restored before a replacement frame is shown, so editing one page does not blank or redraw an unchanged map on another. New pictures still draw progressively. This memory cache, shared by all open books, includes the layout, picture position, draw's source, declared inputs, dimensions and pixel ratio. It is invalidated by changed network permissions, and by changed template code in the file that places the <RenderedImage> or in anything that file imports: an edit to a layout in index.tsx keeps a map placed from map.tsx cached, while an edit to map.tsx, a helper it imports or a package it uses draws the map again. Stylesheets are not counted, since vector pictures take the page's styles where they are placed; a raster picture that reads computed styles, or draws text in a template font, while drawing is not drawn again for a stylesheet edit. It holds an eighth of the computer's memory (1–4 GB), keeps each picture once, and stores raster pictures as WebP; a picture larger than 64 MB is drawn each time. The first view still needs to draw; checks, exports and printing always render afresh.
  • Layout previews: the Inspector's layout previews show what each layout would look like for the selected spread. Drawing a map for every layout cost about a second per selected spread, so only the spread's own layout draws its pictures as they are (usually from the book view's copy). Another layout shows the same picture if it was drawn already, and otherwise the picture last drawn in that layout for another spread, so its preview has a real map, if not this spread's; a layout the book has not drawn yet is drawn once. Pass standIn (such as a schematic map) to show instead of drawing such a layout, or layoutPreview="draw" for a picture that must always match the spread, such as one that tells layouts apart. A page that places no picture until it has its data, such as a map without its GPX file, can place one in layout previews anyway (ctx.view === 'layout-preview', Render function), so its preview shows a real map too.
  • Cancellation: signal is aborted when the element unmounts, a newer drawing replaces it, or the timeout passes; stop work and release GPU resources when it fires. A result after that is discarded, so an older drawing never replaces a newer one.
  • Too many pixels: in a check or export, a raster picture with more than 1.25× the pixels the output asks for is scaled down to what it asks for before it is placed, and the check reports rendered-image-oversized. Drawing more than the target resolution only makes drawing slower; the PDF never gets the extra pixels.
  • Resolution: a raster picture covers the box (object-fit: cover), so draw it at width × pixelRatio by height × pixelRatio pixels. A picture with fewer pixels on either axis fails with rendered-image-failed, since it would print blurred or stretched.
  • Failure: throw or reject with a message, such as Error('The map style could not be loaded.'). The box shows the message, and the Book check reports rendered-image-failed for the element, so export stops instead of printing the page without its picture. Decide readiness yourself: the starter's map resolves on MapLibre's idle event and rejects on any error event, so a style or tile that failed to load fails the picture.
  • Network: draw may reach the template's declared network origins once the user has allowed them; otherwise its requests are blocked and the picture fails. Its requests, WebGL contexts, workers and timers are not render-impure.
  • label names the picture in messages (default 'Picture'); alt, className and style apply to the box, whose class is if-rendered-image with data-rendered="pending" | "ready" | "failed".

For a reusable map helper and vector-map example, see SVG maps.