The HTML <canvas> element adds a bitmap drawing surface to the page. You paint on it with JavaScript through the Canvas API—rectangles, paths, text, images, and frame-by-frame animation.
What does HTML provide?
HTML declares the surface: size (width / height), an id for script, and optional fallback text between the tags. Drawing itself is not markup—it is script calling methods on a 2D context.
How do you start drawing?
Select the element, then call getContext('2d'). That returns a CanvasRenderingContext2D object. Set styles (fillStyle, strokeStyle) and call methods like fillRect, arc, and fillText.
Canvas hub vs canvas tag page
This tutorial focuses on drawing graphics on an HTML page with the 2D Canvas API. Attribute-level detail for the element itself lives on HTML canvas Tag. For scalable vector markup, see HTML SVG.
Blank until script runs.
A canvas with no JavaScript is an empty rectangle. Always get the context and draw after the element exists in the DOM.
Size with attributes.
Set width and height on the element (defaults 300×150). Stretching only with CSS can blur or distort the bitmap.
2D vs WebGL.
This page uses getContext('2d'). The same element can also host WebGL for hardware-accelerated 3D—a separate API.
Syntax
HTML Syntax
Place a canvas on the page, then draw with a short script:
HTML
<canvas id="board" width="400" height="200">
Your browser does not support canvas.
</canvas>
<script>
const canvas = document.getElementById("board");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#22c55e";
ctx.fillRect(20, 20, 120, 80);
</script>
Reference
Core HTML Concepts
Pieces that make canvas drawing work on a page:
Piece
Role
<canvas>
Bitmap surface in the HTML document
width / height
Drawing buffer size in CSS pixels (not CSS layout alone)
getContext("2d")
2D drawing API (CanvasRenderingContext2D)
Fallback content
Text/HTML between tags for unsupported environments
Coordinates
Origin (0, 0) at top-left; X right, Y down
Common 2D drawing methods
Method / property
Use when
fillRect / strokeRect
Filled or outlined rectangles
beginPath + arc
Circles and arcs
fillStyle / strokeStyle
Colors before drawing
fillText / font
Text on the bitmap
clearRect
Erase before the next animation frame
requestAnimationFrame
Smooth redraw loops
Canvas vs SVG (for HTML authors)
Canvas
SVG
Model
Pixels (bitmap)
Shapes in the DOM (vector)
How you draw
JavaScript API
HTML/SVG markup (+ CSS)
Best for
Games, filters, many moving objects
Icons, logos, accessible diagrams
Hit-testing shapes
You track them yourself
Elements can receive events
Approach
How to Draw with Canvas in HTML
A practical workflow:
Add <canvas id="…" width="…" height="…"> with fallback text.
In script, document.getElementById(…) (or query after DOM ready).
Call getContext("2d") and keep the context in a variable.
Set styles (fillStyle, font, lineWidth).
Draw shapes, paths, or text; clear and redraw for animation.
For meaningful visuals, add aria-label or an HTML caption / data table.
Goal
Pattern
Static shape
fillRect / arc once after load
Labeled chart title
font + fillText
Moving sprite
clearRect + redraw in requestAnimationFrame
Photo on canvas
new Image() + drawImage in onload
Try it
Live Preview
Change the fill color—the same <canvas> redraws with fillRect and fillText.
#3b82f6
Uses getContext("2d") on a real HTML canvas element.
Preview
Code
HTML Examples
Six patterns: rectangle setup, circle, fill and stroke, text, animation, and a combined scene.
One context paints rectangle, text, and circle in order.
Later fills override fillStyle for the next shape.
HTML still only holds the blank surface + script.
Try It opens the full editable scene.
Edge Cases & Pitfalls
Most canvas bugs are sizing, timing, or assuming the bitmap is accessible like HTML.
size
CSS size ≠ buffer size
Changing only CSS width/height scales the bitmap and can look blurry. Prefer HTML width/height (or set them in JS).
timing
Script before the element
If getElementById runs too early, you get null. Put the script after the canvas or wait for DOMContentLoaded.
path
Forgetting beginPath
Without a new path, strokes and fills can connect to leftover geometry from earlier draws.
anim
Not clearing frames
Animation without clearRect leaves trails. Clear (or redraw a background) each frame.
a11y
Pixels are not semantic
Screen readers do not understand drawn shapes. Use fallback text, aria-label, or a data table beside charts.
limits
Huge canvases
Browsers cap canvas size (often around a few thousand pixels per side). Oversized surfaces may stop drawing.
Compatibility
Browser Support
The canvas element and Canvas 2D API are Baseline HTML/JavaScript. getContext('2d'), basic shapes, text, and requestAnimationFrame work in all modern browsers.
✓ Baseline · Widely available
HTML canvas
Bitmap drawing with the 2D context works across current browsers worldwide.
100%canvas + 2D API
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
HTML canvasUniversal
Bottom line: Add a sized canvas, getContext('2d'), draw with fillRect/arc/fillText, and clear between animation frames.
Remember
Key Takeaways
<canvas> is a blank bitmap; JavaScript paints it.
getContext("2d") is the starting API for 2D drawing.
Set width/height attributes so drawings stay sharp.
One line: Put a sized <canvas> in HTML, call getContext("2d"), then draw with the Canvas API.
Frequently Asked Questions
Canvas is an HTML element that provides a blank bitmap surface. You draw on it with JavaScript using a 2D context from getContext('2d')—shapes, text, images, and animations.
This tutorial teaches how to draw graphics on an HTML page (setup, shapes, styles, text, animation). The /html/tags/canvas page is the element reference for attributes, sizing, and fallback content.
Use canvas for games, particle effects, and pixel-heavy drawing. Use SVG for icons, logos, and diagrams that should stay sharp and stay in the DOM.
Nothing appears until script runs. Check that the element exists before getContext, that width/height are set, and that drawing calls actually run after the page loads.
CSS size and canvas buffer size must match. Prefer width and height attributes (or JS) over stretching with CSS alone, especially on high-DPI screens.
Drawn pixels are not semantic HTML. Put fallback text inside canvas, add aria-label when needed, and provide a real HTML alternative for important data.