Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

canvas

The canvas element provides a drawing surface for rendering graphics, charts, games, and dynamic visual content via JavaScript.

Description

The <canvas> element provides a bitmap drawing surface for JavaScript-driven graphics. VB styles canvases as block-level and responsive by default, with variants for borders, loading states, interactive cursors, and drawing modes.

When to Use

  • Charts and graphs: Data visualization (Chart.js, D3, etc.)
  • Games: 2D and 3D game rendering
  • Image manipulation: Filters, cropping, compositing
  • Animations: Complex animations beyond CSS capabilities
  • Drawing tools: Signatures, sketches, annotation

When to Use Other Elements

  • Use <svg> for scalable, accessible vector graphics
  • Use <img> for static images
  • Use CSS animations for simple, declarative motion
  • Use <video> for pre-recorded animated content

Attributes

AttributeDefaultEffect
width300Drawing buffer width in pixels, and the element's intrinsic width
height150Drawing buffer height in pixels, and the element's intrinsic height

Without the attributes a canvas is a 300×150 bitmap. Anything drawn outside the buffer is clipped, and CSS that sizes the element differently scales the bitmap rather than adding pixels (see Canvas Sizing).

Variants

VB provides variant classes for borders, loading placeholders, and cursor modes. The interactive demo below includes a coordinate tracker (hover) and a freehand drawing canvas (click and drag).

ClassEffect
(default)Block display, max-width 100%
.borderedBorder + border-radius to define the canvas boundary
.loadingStriped background pattern (placeholder before JS renders)
.interactiveCrosshair cursor for click/hover interactions
.drawingCrosshair cursor + touch-action: none for freehand drawing
.full100% container width, height from the aspect ratio of the buffer
.fixedFixed height: 100% width, 400px tall

Sizing classes stretch the bitmap. .full, .fixed and .canvas-responsive only change the CSS box; the drawing buffer keeps its width/height. Unless your script resizes the buffer to match (ResizeObserver + devicePixelRatio), drawings are scaled and look blurry or distorted.

Drawing Examples

Canvas requires JavaScript to render. These demos show shapes, a working bar chart with data labels, and a responsive canvas that reports its own dimensions.

Never hard-code text or line colours such as #333 on a transparent canvas — they vanish on a dark surface. Read the theme tokens and redraw when the colour scheme changes, as above.

Responsive Canvas

Use .canvas-responsive to maintain aspect ratio while filling available width. The canvas is positioned absolutely inside the wrapper. Like the other sizing classes it sizes the box only — sync the drawing buffer in script (see Canvas Sizing).

Custom Aspect Ratios

Override the default 16:9 ratio with a class in your stylesheet:

Canvas Sizing

Canvas has two independent sizes: the element size (CSS) and the drawing buffer size (width/height attributes). For crisp rendering on high-density displays, set the buffer to the display size times devicePixelRatio.

Keeping the buffer in sync

When CSS sizes the canvas (.full, .fixed, .canvas-responsive), watch the box and resize the buffer:

Accessibility

Canvas content is not accessible to screen readers by default. You must provide alternatives.

Fallback Content

Place content inside the canvas element. It renders when canvas is unsupported or scripting is disabled, and browsers expose it to screen readers even while the canvas is painting. Use it for data that deserves structure, like the values behind a chart.

ARIA Labels

For simple informative graphics, use role="img" with aria-label. Pick one approach per canvas: role="img" makes the canvas a single image, so its children become presentational and any fallback content is no longer exposed to assistive technology. Use fallback content or role="img" + aria-label, not both.

Data Tables

For complex data visualizations, provide an accessible data table nearby:

Interactive Canvases

A canvas has no built-in keyboard support. For pointer-driven canvases, give the element tabindex="0", handle arrow keys (with Shift for larger steps) in a keydown listener, and mirror the state in real text, such as an <output> referenced by aria-describedby. The .interactive demo above uses this pattern. Freehand drawing cannot be done from a keyboard, so offer an alternative (a typed signature, an upload) where it matters.

Best Practices

  • Always provide text alternatives for informative canvas content
  • Use role="img" and aria-label for simple graphics
  • Provide data tables for charts and graphs
  • Do not put role="img" on interactive canvases — it tells assistive technology the canvas is a static picture
  • Make interactive canvases keyboard operable
  • Respect prefers-reduced-motion for animated content

CSS Reference

SelectorProperties
canvasdisplay: block; max-inline-size: 100%;
canvas.fullinline-size: 100%; block-size: auto;
canvas.fixed (fixed height)inline-size: 100%; block-size: 400px;
canvas.interactivecursor: crosshair;
canvas.drawingcursor: crosshair; touch-action: none;
canvas.borderedborder; border-radius;
canvas.loadingDiagonal stripe background pattern
.canvas-responsiveposition: relative; aspect-ratio: 16 / 9;

Related

  • <svg> — Scalable vector graphics (often better for accessibility and resolution independence)
  • <img> — Static images
  • <figure> — Wrap canvas with captions for charts
  • <audio-visualizer> — VB's canvas-based audio visualization component
↑