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

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
.fixed100% width, fixed 400px height

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.

Responsive Canvas

Use .canvas-responsive to maintain aspect ratio while filling available width. The canvas is positioned absolutely inside the wrapper.

Custom Aspect Ratios

Override the default 16:9 ratio with inline aspect-ratio:

Canvas Sizing

Canvas has two independent sizes: the element size (CSS) and the drawing buffer size (width/height attributes). For crisp rendering on retina displays, set the buffer to a multiple of the display size.

High DPI / Retina

Accessibility

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

Fallback Content

Place content inside the canvas element. It's shown when canvas is unsupported and available to screen readers.

ARIA Labels

For simple informative graphics, use role="img" with aria-label:

Data Tables

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

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
  • Ensure interactive canvases are keyboard accessible
  • Respect prefers-reduced-motion for animated content

CSS Reference

SelectorProperties
canvasdisplay: block; max-inline-size: 100%;
canvas.fullinline-size: 100%; block-size: auto;
canvas.fixedinline-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