Core API
getContext("2d")
Canvas is a bitmap drawing surface controlled with JavaScript.
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#f7df1e";
ctx.fillRect(20, 20, 120, 80);
Graphics, Media & UI
Canvas is a bitmap drawing surface controlled with JavaScript.
Visual JavaScript should make state easier to understand. The code must stay connected to real data, user intent and accessibility.
A polished interface is not only animation. It is timing, feedback, labels, fallback states and predictable interaction.
getContext("2d")
Use motion or graphics to explain state, not to distract from it.
Keep text, labels and keyboard alternatives available.
Avoid layout-heavy loops and unnecessary repaints.
Examples
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#f7df1e";
ctx.fillRect(20, 20, 120, 80);
canvas.width = 300; // Drawing code is missing, so the canvas stays blank.
Code patterns
Use these examples when turning data, state and interaction into something visible.
A direct example of the topic.
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#f7df1e";
ctx.fillRect(20, 20, 120, 80);
A shortcut that makes the interface fragile.
canvas.width = 300; // Drawing code is missing, so the canvas stays blank.
Users may ask the browser for less animation.
const prefersReducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
if (!prefersReducedMotion) {
element.animate(keyframes, options);
}
Visuals should not be the only source of meaning.
<figure> <canvas aria-label="Monthly revenue chart"></canvas> <figcaption>Revenue increased over four months.</figcaption> </figure>
Rules that matter
Motion, canvas and drag/drop can improve a product, but only when they stay usable and understandable.
Do not hard-code charts or status when data should drive them.
Canvas, charts and drag/drop need labels or fallback content.
JavaScript should orchestrate, CSS can animate state.
Repeated measurements and writes can hurt performance.
Reduced motion and keyboard use are part of production design.
Mouse, keyboard and touch can behave differently.
Production thinking
Graphics and interaction are where users feel quality immediately. Small implementation choices decide whether the UI feels smooth or chaotic.
Visual UI needs semantic labels, focus management, reduced motion support and alternatives for pointer-only behavior.
Production visual code should be measured, tested on real devices and kept separate from business logic where possible.
Important text should not exist only inside canvas pixels or animation frames.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these before moving to the next graphics and UI lesson.
Senior audit upgrade