Canvas API 2D Graphics
When to use
Use this skill when building high-performance 2D data visualizations, interactive image manipulation tools, mini-games, or pixel-level rendering engines.
Prerequisites
- Solid understanding of the HTML
<canvas>element and Cartesian coordinate systems. - Familiarity with JavaScript animation timing loops (
requestAnimationFrame).
Step-by-step workflow
- Acquire 2D context: Obtain the context via
canvas.getContext('2d')with appropriate alpha and performance options. - Handle High-DPI displays: Scale canvas backing store dimensions by
window.devicePixelRatioand adjust CSS style dimensions to prevent blurriness. - Execute draw routines: Use vector path methods (
beginPath,arc,lineTo,stroke,fill) inside a dedicated render function. - Implement animation loops: Drive smooth 60fps animations with
requestAnimationFrame, clearing dirty regions withclearRect. - Manage transform matrices: Wrap coordinate translations, rotations, and scaling between
ctx.save()andctx.restore().
Commands / code examples
export function initCanvas(canvas: HTMLCanvasElement) {
const ctx = canvas.getContext('2d');
if (!ctx) throw new Error('2D context not supported');
// Handle Retina / High-DPI screens
const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
ctx.scale(dpr, dpr);
let angle = 0;
function render() {
ctx.clearRect(0, 0, rect.width, rect.height);
ctx.save();
ctx.translate(rect.width / 2, rect.height / 2);
ctx.rotate(angle);
ctx.fillStyle = '#3b82f6';
ctx.fillRect(-40, -40, 80, 80);
ctx.restore();
angle += 0.02;
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
Common mistakes & pitfalls
- Failing to adjust for
window.devicePixelRatio, resulting in blurry rendering on modern displays. - Allocating objects or resizing canvas dimensions inside the
requestAnimationFrameloop, triggering garbage collection stutter. - Forgetting
ctx.save()/ctx.restore()around transformations, causing cumulative transformation bugs.
Verification checklist
- Canvas resolution is scaled by
devicePixelRatioto prevent Retina blur. - Animation loop runs via
requestAnimationFramewith frame delta calculation. - Context transformations are isolated between
ctx.save()andctx.restore(). - Render loop cleans up gracefully when component unmounts.
Related skills
- svg-craft-and-animation
- webgl-threejs-basics
- browser-dom-manipulation-events
- performance-code-review-complexity-allocations