Canvas

Struct Canvas 

Source
pub struct Canvas;
Expand description

The <canvas> element - provides a bitmap drawing surface for graphics via JavaScript.

§Purpose

The <canvas> element provides a resolution-dependent bitmap canvas for drawing graphics via JavaScript and the Canvas API. It can be used for rendering graphs, game graphics, animations, photo composition, real-time video processing, and other visual images on the fly.

§Content Categories

  • Flow Content
  • Phrasing Content
  • Embedded Content
  • Palpable Content

§Permitted Content Model

  • Transparent content (fallback for browsers without canvas support)

§Common Use Cases

  • 2D games and interactive animations
  • Data visualizations and charts
  • Image editing and manipulation
  • Real-time video effects and filters
  • Drawing tools and diagramming applications

§Key Attributes

  • width: Width of the canvas in CSS pixels (default: 300)
  • height: Height of the canvas in CSS pixels (default: 150)

§Example

<!-- Basic canvas -->
<canvas id="myCanvas" width="800" height="600">
  Your browser doesn't support the canvas element.
</canvas>
<script>
  const ctx = document.getElementById('myCanvas').getContext('2d');
  ctx.fillStyle = 'blue';
  ctx.fillRect(10, 10, 100, 100);
</script>

<!-- Canvas for charts -->
<canvas id="chart" width="600" height="400" aria-label="Sales data chart">
  <p>Sales data: Q1: $100k, Q2: $150k, Q3: $175k, Q4: $200k</p>
</canvas>

<!-- Game canvas -->
<canvas id="gameCanvas" width="1024" height="768">
  <p>This game requires a browser with canvas support.</p>
</canvas>

<!-- High DPI canvas -->
<canvas id="hdCanvas" width="1600" height="1200" style="width: 800px; height: 600px;">
  Fallback content for accessibility.
</canvas>

§Accessibility

  • Provide meaningful fallback content describing what the canvas shows
  • Use aria-label or aria-labelledby to describe the canvas purpose
  • For interactive canvases, ensure keyboard accessibility
  • Consider providing alternative text-based representations of visual data
  • Use ARIA live regions to announce dynamic changes
  • Ensure canvas content has sufficient color contrast

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Canvas

Source§

const TAG: &'static str = "canvas"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl<T: FlowContent> CanContain<T> for Canvas

Source§

impl EmbeddedContent for Canvas

Source§

impl FlowContent for Canvas

Source§

impl PalpableContent for Canvas

Source§

impl PhrasingContent for Canvas

Auto Trait Implementations§

§

impl Freeze for Canvas

§

impl RefUnwindSafe for Canvas

§

impl Send for Canvas

§

impl Sync for Canvas

§

impl Unpin for Canvas

§

impl UnwindSafe for Canvas

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.