Canvas & Multimedia APIs
Master HTML5 Canvas, WebGL, and multimedia APIs for interactive graphics, games, and rich media experiences.
55 min•By Priygop Team•Last updated: Feb 2026
Canvas 2D API
- Canvas Element: <canvas width='800' height='600'> — pixel-based drawing surface. Get context: canvas.getContext('2d'). Unlike SVG, canvas is raster (bitmap)
- Drawing: fillRect, strokeRect, arc, lineTo, bezierCurveTo — draw shapes, lines, curves. Fill with colors, gradients, patterns. Transform with translate, rotate, scale
- Text: fillText('Hello', x, y) — render text on canvas. Set font, textAlign, textBaseline. Measure text width with measureText() for precise positioning
- Images: drawImage(img, x, y, w, h) — render images on canvas. Create image processing effects: grayscale, blur, brightness by manipulating pixel data with getImageData
- Animation: requestAnimationFrame(draw) — 60fps animation loop. Clear canvas, update positions, redraw. Foundation for HTML5 games and interactive visualizations
- Performance: Use OffscreenCanvas for Web Worker rendering. Layer static elements on separate canvases. Use requestAnimationFrame (never setInterval) for animation
Interactive APIs
- Drag & Drop: draggable='true' attribute + dragstart, dragover, drop events. Build sortable lists, file upload zones, kanban boards with native HTML API
- Geolocation: navigator.geolocation.getCurrentPosition() — get user's latitude/longitude (requires permission). Use for location-based features, maps, store locators
- Web Workers: Run JavaScript in background threads — computationally heavy tasks (image processing, data parsing) without freezing the UI. postMessage for communication
- Intersection Observer: Observe when elements enter/exit viewport — lazy-load images, infinite scrolling, scroll-triggered animations. More efficient than scroll event listeners
- Resize Observer: Watch element size changes — update Canvas dimensions, adjust layouts, resize charts. Essential for responsive components that can't use CSS alone
- Performance Observer: Monitor web performance metrics — LCP, FID, CLS values. Send to analytics for real-user monitoring (RUM). Core Web Vitals tracking