SVG
Playground

Learn SVG

The Coordinate System

SVG uses a coordinate system where (0, 0) is the top-left corner. The viewBox attribute defines the visible area in user-space units.

  • viewBox="0 0 800 600" — 800×600 user units
  • preserveAspectRatio — controls scaling and alignment

Unlike canvas, SVG is retained-mode — every element lives in the DOM and can be styled, animated, and inspected individually.

Shapes & Paths

SVG provides six shape primitives:

  • <rect>, <circle>, <ellipse>
  • <line>, <polyline>, <polygon>

The <path> element is the most powerful — its d attribute uses commands:

  • M move, L line, C cubic bézier
  • Q quadratic bézier, A arc, Z close

Lowercase commands use relative coordinates.

Fill, Stroke & Paint

fill sets interior color; stroke sets the outline.

  • stroke-width — line thickness
  • stroke-dasharray — dash pattern
  • stroke-linecap — butt, round, square
  • stroke-linejoin — miter, round, bevel

Gradients and patterns are defined in <defs> and referenced via fill="url(#id)".

Transforms

Transform functions applied via the transform attribute:

  • translate(x, y) — move
  • rotate(angle) — spin around origin
  • scale(sx, sy) — resize
  • skewX(a), skewY(a) — shear
  • matrix(a,b,c,d,e,f) — full affine

Transforms compose right-to-left. Use <g> to group children under a shared transform.

Filters & Effects

SVG filters are pixel-processing pipelines defined in <filter>:

  • <feGaussianBlur> — blur
  • <feColorMatrix> — hue/saturation/grayscale
  • <feTurbulence> — Perlin noise
  • <feDisplacementMap> — warp
  • <feComposite>, <feMerge> — combine layers

Primitives connect via result, in, and in2 attributes.

Animation

Three approaches to SVG animation:

  1. SMIL — <animate>, <animateTransform>, <animateMotion>. Declarative, self-contained.
  2. CSS — @keyframes + animation. Familiar and composable.
  3. JavaScript — requestAnimationFrame + DOM manipulation. Full control for physics and simulations.
Reuse & Structure
  • <defs> — define non-rendered resources
  • <symbol> — reusable component with its own viewBox
  • <use> — instantiate a symbol or element
  • <clipPath> — hard clipping boundary
  • <mask> — soft alpha compositing
  • <marker> — line endpoint decorations
SVG vs Canvas

SVG is retained-mode — each element is a DOM node you can style, animate, and attach events to. It scales to any resolution.

Canvas is immediate-mode — you draw pixels and they're gone. Better for thousands of sprites or pixel manipulation.

SVG excels at diagrams, charts, icons, and moderate element counts. Canvas wins for particle-heavy or pixel-level rendering.

Specs & Resources

SVG 2 Spec (W3C) MDN SVG Tutorial MDN Path Reference MDN Filter Effects CSS-Tricks SVG Guide
SVG Source
christianwilcox.com All explorations About this exploration