Canvas and SVG
HTML provides two ways to draw graphics — <canvas> for pixel-based drawing with JavaScript, and <svg> for vector shapes that scale perfectly to any size.
Canvas — Pixel Drawing
<canvas id="c" width="300" height="130"></canvas> <script> var ctx = document.getElementById('c').getContext('2d'); // Filled rectangle ctx.fillStyle = '#e8573a'; ctx.fillRect(20, 20, 110, 60); // Circle ctx.beginPath(); ctx.arc(220, 65, 45, 0, Math.PI * 2); ctx.fillStyle = '#60a5fa'; ctx.fill(); // Text ctx.fillStyle = '#a6e3a1'; ctx.font = 'bold 12px monospace'; ctx.fillText('Canvas!', 20, 110); </script>
SVG — Scalable Vectors
<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg"> <!-- Rectangle with rounded corners --> <rect x="10" y="10" width="110" height="70" fill="#e8573a" rx="8"/> <!-- Circle --> <circle cx="220" cy="55" r="45" fill="#60a5fa"/> <!-- Text --> <text x="10" y="115" fill="#a6e3a1" font-size="13" font-family="monospace"> SVG scales perfectly! </text> </svg>
Canvas vs SVG
Canvas API Reference
Modern websites embed SVG icons directly in HTML instead of using icon font libraries. Inline SVG is faster to load, fully customizable with CSS variables, and perfectly accessible with title and desc tags.
Canvas was invented by Apple in 2004 for macOS Dashboard widgets. SVG was standardized by W3C in 2001 but became widely used only when browsers improved SVG rendering around 2010. Google Maps used Canvas heavily which drove browser optimization.