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,pathText:
text,tspan,textPathContainers:
g,svg,defs,symbol,useGradients:
linearGradient,radialGradient,stopFilters:
filter,feBlend,feColorMatrix,feGaussianBlur, and moreAnimation:
animate,animateTransform,animateMotion
Important Notes
Always register
SVGAnalyzerbefore using SVG bindingsSVG attributes are case-sensitive (e.g.,
viewBox, notviewbox)Use
xlink:hreffor older SVG references, or justhreffor modern browsersFor better performance with many SVG elements, consider using
one-timebindings for static values
Related Documentation
Attribute Binding - Basic binding syntax
Class and Style Binding - Dynamic styling
Repeat Rendering - Iterating over data
Last updated
Was this helpful?