@hozo/canvas•Canvas 2D · React Native Skia

One scene, drawn by both platforms

Describe a drawing once as JSX — rectangles, lines, paths, text, groups and clips — and Hozo draws the same retained scene with the browser's Canvas 2D API on the Web and with React Native Skia on iOS and Android. Pointer and touch hit-testing come from the scene's geometry, and every surface has to say what it means to a screen reader.

One series, drawn and read

Point at a day, hold it on a touch screen, or Tab to it: each candle is a shape with its own hit area and its own hidden button.

Daily price, Sep 1 to Sep 26: $182 to $228
DateOpenHighLowCloseVolume
Sep 1$182$188$175$1778,948
Sep 2$177$184$174$18016,529
Sep 3$180$187$176$18314,903
Sep 4$183$189$180$18715,911
Sep 5$187$190$183$18912,551
Sep 8$189$199$187$19413,183
Sep 9$194$203$190$19914,084
Sep 10$199$210$196$20410,695
Sep 11$204$211$202$20810,167
Sep 12$208$210$202$20813,803
Sep 15$208$216$207$2119,829
Sep 16$211$216$208$2128,701
Sep 17$212$217$209$21610,818
Sep 18$216$224$212$2188,318
Sep 19$218$220$214$21711,935
Sep 22$217$221$212$2169,265
Sep 23$216$223$211$22014,892
Sep 24$220$226$216$22215,646
Sep 25$222$230$217$2279,516
Sep 26$227$231$224$22811,480

A screen reader is given the table below instead of the pixels. It is in the page either way; this only makes it visible.

Pixels are not content, so the type says what is

A canvas is one element; what is drawn on it is invisible to a keyboard and a screen reader. Every <Canvas> therefore has to choose one of three meanings, and leaving all three out is a type error.

accessibilityLabel

A named image

The surface is exposed as one image with that name. Right for a sparkline whose point fits in a sentence.

accessibleFallback

A real alternative

The pixels leave the accessibility tree and your fallback — a table, a list — is placed beside them, visually hidden. You build it from the same data as the drawing, as the demo above does.

decorative

Nothing to say

Ornament. The canvas and its surface are hidden from assistive technology completely.

A shape you can press is a control

Give a shape onPress or href and an accessibilityLabel, and Hozo puts a real button or link for it in a hidden layer, in drawing order. Focusing it indicates the shape the way hovering does, so the demo's tooltip follows the keyboard too. Shapes drawn in several parts share one control through accessibilityControlId. A pressable shape with no name stays pointer-only and says so in development.

Canvas or SVG?

Hozo has both, and they are different tools rather than two speeds of one. Choose by how much is drawn and by what each mark has to be.

SVG (@hozo/svg)

  • Icons, logos and other marks that are design
  • Charts of hundreds of marks, each a DOM element you can style and hover with CSS
  • Filter effects such as blur, which @hozo/canvas does not apply to shapes
  • Emits real <svg> on the Web

Canvas (@hozo/canvas)

  • Thousands of marks, where one DOM node each would be the cost
  • Scenes redrawn often, and textured or vertex-coloured triangle meshes
  • Hit-testing from geometry rather than from one element per mark
  • One surface; meaning comes from a label or a fallback you provide

Writing one

The shapes are properties of Canvas. The fallback is built from the same rows as the bars, so the two cannot drift apart.

Canvas authoring: Revenue.tsx
import { Canvas } from '@hozo/canvas'export function Revenue({ quarters }) {  return (    <Canvas      viewBox={[0, 0, 400, 200]}      accessibilityLabel="Revenue by quarter"      accessibleFallback={<RevenueTable quarters={quarters} />}    >      {quarters.map((q, i) => (        <Canvas.Rect          key={q.name}          x={i * 100 + 20} y={200 - q.value} width={60} height={q.value}          fill="#c8a882"          accessibilityLabel={q.name}          onPress={() => select(q)}        />      ))}    </Canvas>  )}