SVG

A developer guide for enabling SVG binding in Aurelia 2.

Learn about enabling SVG binding in Aurelia templates.

Adding SVG Registration

By default, Aurelia won't work with SVG elements since SVG elements and their attributes require different parsing rules (SVG uses XML namespaces and has different attribute handling than HTML). To teach Aurelia how to handle SVG element bindings, register the SVGAnalyzer:

import { SVGAnalyzer } from '@aurelia/runtime-html';
import { Aurelia } from 'aurelia';

Aurelia
  .register(SVGAnalyzer) // <-- add this line
  .app(MyApp)
  .start();

After adding this registration, bindings on SVG elements will work as expected.

Basic SVG Bindings

Once SVGAnalyzer is registered, you can bind to SVG attributes just like HTML attributes:

<svg width="200" height="200">
  <!-- Bind to standard SVG attributes -->
  <circle cx.bind="circleX"
          cy.bind="circleY"
          r.bind="radius"
          fill.bind="fillColor" />

  <!-- Use interpolation for transform -->
  <rect x="10" y="10"
        width.bind="rectWidth"
        height.bind="rectHeight"
        transform="rotate(${rotation} 50 50)" />
</svg>

Dynamic SVG Charts

Create reactive charts with data binding:

SVG Path Animations

Bind to path data for dynamic shapes:

Interactive SVG Elements

Handle events on SVG elements:

SVG with CSS Classes

Apply dynamic classes to SVG elements:

SVG Gradients with Bindings

Create dynamic gradients:

Supported SVG Elements

The SVGAnalyzer supports all standard SVG elements including:

  • Basic shapes: circle, rect, ellipse, line, polyline, polygon, path

  • Text: text, tspan, textPath

  • Containers: g, svg, defs, symbol, use

  • Gradients: linearGradient, radialGradient, stop

  • Filters: filter, feBlend, feColorMatrix, feGaussianBlur, and more

  • Animation: animate, animateTransform, animateMotion

Important Notes

  • Always register SVGAnalyzer before using SVG bindings

  • SVG attributes are case-sensitive (e.g., viewBox, not viewbox)

  • Use xlink:href for older SVG references, or just href for modern browsers

  • For better performance with many SVG elements, consider using one-time bindings for static values

Last updated

Was this helpful?