canvas
The canvas element provides a drawing surface for rendering graphics, charts, games, and dynamic visual content via JavaScript.
Description
The <canvas> element is a container for scriptable graphics. It provides a bitmap drawing surface that you can manipulate with JavaScript using either the 2D rendering context or WebGL for 3D graphics.
Vanilla Breeze styles canvas elements to be block-level and responsive by default, with variants for borders, loading states, and interactive cursors.
When to Use
- Charts and graphs - Data visualization (often via libraries like Chart.js)
- Games - 2D and 3D game rendering
- Image manipulation - Filters, cropping, compositing
- Animations - Complex animations beyond CSS capabilities
- Drawing applications - User-interactive drawing tools
- Signatures - Capture user signatures
When to Use Other Elements
Variants
Default
Basic canvas without styling.
.bordered
Adds a subtle border to define the canvas boundary.
.loading
Striped pattern indicates loading or placeholder state before content is rendered.
.interactive
Crosshair cursor for interactive canvases where users can click or draw.
.drawing
Crosshair cursor with touch-action: none for drawing applications that handle touch events manually.
Click and drag to draw
.full
Full container width.
.fixed
Full width with fixed height (400px).
Responsive Canvas
Use the .canvas-responsive wrapper to maintain aspect ratio while filling available width.
The wrapper maintains a 16:9 aspect ratio by default. The canvas is positioned absolutely to fill the container.
Custom Aspect Ratios
Canvas Sizing
Canvas has two size concepts: the element size (CSS) and the drawing buffer size (attributes).
High DPI / Retina Support
Live Examples
Basic Drawing
Chart Placeholder
Accessibility
Canvas content is not accessible to screen readers by default. You must provide alternatives.
Fallback Content
Place fallback content inside the canvas element for browsers that don't support canvas or for screen readers.
ARIA Labels
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"andaria-labelfor simple graphics - Provide data tables for charts and graphs
- Ensure interactive canvases are keyboard accessible
- Respect
prefers-reduced-motionfor animated content
Related Elements
CSS Reference
Styles defined in /src/native-elements/canvas/styles.css
| Selector | Properties |
|---|---|
canvas |
display: block; max-inline-size: 100%; |
canvas.full |
inline-size: 100%; block-size: auto; |
canvas.fixed |
inline-size: 100%; block-size: 400px; |
canvas.interactive |
cursor: crosshair; |
canvas.drawing |
cursor: crosshair; touch-action: none; |
canvas.bordered |
border; border-radius; |
canvas.loading |
background with diagonal stripes; |
.canvas-responsive |
position: relative; aspect-ratio: 16 / 9; |