SVG & Canvas
๐ Covered in this chapter:
Scalable Vector Graphics (SVG) ยท Inline SVG Shapes ยท SVG Accessibility ยท HTML5 <canvas> Element ยท 2D Rendering Context ยท Canvas vs SVG Comparison
Welcome to Phase 5 (Chapter 13): SVG & Canvas! HTML5 provides two native graphic rendering engines: Scalable Vector Graphics (SVG) for XML-based vector shapes, and the
<canvas> element for high-performance 2D/3D pixel rendering via JavaScript.
1Inline SVG Vector Shapes
HTML โ Inline SVG Circles & Rectangles
โถ Run in HTML Editor
<svg width="100" height="100" viewBox="0 0 100 100" role="img" aria-label="Orange Circle Icon">
<circle cx="50" cy="50" r="40" fill="#f97316" stroke="#ffffff" stroke-width="4" />
</svg>
2HTML5 <canvas> 2D Context
HTML + JS โ Canvas 2D Rendering
โถ Run in HTML Editor
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #f97316;"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#f97316';
ctx.fillRect(10, 10, 150, 80);
</script>
โ Frequently Asked Questions (FAQ)
Q1: SVG vs Canvas โ which is better?
SVG is DOM-based, scalable to any resolution without loss, and ideal for icons, charts, and UI graphics. Canvas is pixel-based, fast, and ideal for high-frame-rate web games and animations.