<canvas>

A canvas for drawing bitmap graphics with JavaScript.

Display
inline (replaced element)
Closing tag
Required
Permitted content
Fallback content, visible when the canvas doesn’t work, for example with JavaScript turned off
On this page

Example

#
HTML
<canvas id="scene" width="320" height="150">
  A purple square, an orange circle, and a blue line.
</canvas>
<script>
  const ctx = document.getElementById('scene').getContext('2d')

  ctx.fillStyle = '#8b5cf6'
  ctx.fillRect(20, 20, 80, 80)

  ctx.beginPath()
  ctx.arc(165, 60, 40, 0, Math.PI * 2)
  ctx.fillStyle = '#f97316'
  ctx.fill()

  ctx.beginPath()
  ctx.moveTo(230, 100)
  ctx.lineTo(300, 20)
  ctx.strokeStyle = '#38bdf8'
  ctx.lineWidth = 6
  ctx.stroke()

  ctx.fillStyle = '#e5e7eb'
  ctx.font = '15px system-ui'
  ctx.fillText('Shapes drawn in JavaScript', 20, 135)
</script>
Preview

Definition and usage

#

The <canvas> element is a rectangular canvas on which you draw with JavaScript code: shapes, charts, animations, even whole games. HTML itself creates only an empty area, and drawing happens through the context obtained with the getContext("2d") method (or getContext("webgl") for 3D graphics).

Canvas draws pixels, not objects. Once you draw a circle, there is no “circle element” that you could style in CSS or click, so for interaction you have to work out yourself what the user hit. If you need a few scalable shapes, icons, or diagrams, <svg> is usually a better choice.

The size of the canvas is set by the width and height attributes (300 by 150 pixels by default). The content between the tags is fallback text: it is worth describing what the graphic shows, because the drawing itself is invisible to screen readers.

Syntax

#
Syntax
<canvas id="canvas" width="300" height="150"></canvas>

Attributes

#
  • width / height

    number of pixels (300 and 150 by default)

    The width and height of the canvas in pixels, that is, the resolution of the drawing. If you enlarge the canvas in CSS without changing these attributes, the image is stretched and blurred, as in the lower part of the example. Changing the attributes in a script clears the whole canvas.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

width / height

number of pixels (300 and 150 by default)

The width and height of the canvas in pixels, that is, the resolution of the drawing. If you enlarge the canvas in CSS without changing these attributes, the image is stretched and blurred, as in the lower part of the example. Changing the attributes in a script clears the whole canvas.

HTML
<style>
  canvas { display: block; margin-bottom: 8px; }
  .blurry { width: 240px; height: 60px; }
</style>
<canvas class="sharp" width="240" height="60"></canvas>
<canvas class="blurry" width="60" height="15"></canvas>
<script>
  document.querySelectorAll('canvas').forEach((canvas) => {
    const ctx = canvas.getContext('2d')
    ctx.fillStyle = '#a78bfa'
    ctx.font = canvas.height * 0.6 + 'px system-ui'
    ctx.fillText('CodeWorlds', 4, canvas.height * 0.7)
  })
</script>
Preview

More examples

#
A simple bar chart
HTML
<canvas id="chart" width="320" height="150">
  Progress chart: HTML 80%, CSS 60%, JavaScript 35%.
</canvas>
<script>
  const ctx = document.getElementById('chart').getContext('2d')
  const courses = [
    { name: 'HTML', percent: 80, color: '#f97316' },
    { name: 'CSS', percent: 60, color: '#38bdf8' },
    { name: 'JS', percent: 35, color: '#facc15' }
  ]

  ctx.font = '14px system-ui'
  courses.forEach((course, index) => {
    const y = 15 + index * 45
    const width = course.percent * 2.2
    ctx.fillStyle = course.color
    ctx.fillRect(50, y, width, 28)
    ctx.fillStyle = '#e5e7eb'
    ctx.fillText(course.name, 8, y + 19)
    ctx.fillText(course.percent + '%', 58 + width, y + 19)
  })
</script>
Preview

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • On high pixel density screens the drawing can look slightly blurry. Multiply width and height by window.devicePixelRatio, leave the CSS size unchanged, and scale the context with ctx.scale().
  • Describe the contents of the canvas with fallback text or an aria-label attribute with role="img", so the graphic isn’t an empty spot for a screen reader.
  • For animation, use requestAnimationFrame, not setInterval. The browser then matches the drawing rate to the screen refresh.

Related entries

#

Spotted a mistake or missing an example? Write to us.