Svg binding and limitations

Due to the valid markup of the binding syntax, aurelia is well suited to binding to svg. We’ve created some modular visualisation components using aurelia so have quite a bit of experience trying it out.

Firstly, we found out very quickly that binding to large path strings was very slow:

<path"humongousPathString" ... ></path>

Aurelia bindings should obviously not be used to pass huge primitive types, so it was considerably faster to simply set the attribute manually:

this.pathElement.setAttribute('d', newPath);

svg in component template causes a few issues

Aurelia svg components have to be wrapped in an svg element:

    ... component here

To get anything complex to display correctly, you’ll probably need to include the following CSS. This is because the default for the non-root svg is to hide the overflow:

svg {
  overflow: visible !important;

We spent ages trying to work out why the above approach was not working when we exported to png. It turns out you have to inline styles in every component for that to work. That gives us:

  <svg style="overflow: visible">


The nestled svg element also causes issues if we want to use a clipPath from a parent component as they don’t appear to work in the child svg. This means we currently have to copy the clip dimensions into some components and render the <clipPath> within it:

  <svg style="overflow: visible">
    <clipPath id="clip">
      <rect id="clip-rect" x.bind="rect.x" y.bind="rect.y" width.bind="rect.width" height.bind="rect.height"></rect>

    <line clip-path="url(#clip)" stroke="#000" ...


See below for a possible solution


The overflow issue seems like a browser bug. Did you test in multiple browsers?

The default in Chrome is to hide the overflow:

So we have no choice but to include it inline if we want it to display correctly when converting to png.

I’m guessing that the wrapping <svg> in the templates is so that the elements are created with the correct namespace?

It looks like it’s possible to remove the wrapping svg using a custom attribute which simply copies the elements out when it’s attached. This fixes all the above issues.

  <svg svg-remove>
    <rect x.bind="rect.x" y.bind="rect.y" width.bind="rect.width" height.bind="rect.height" style="background-color:red"></rect>
import {inject, customAttribute} from 'aurelia-framework';

export class SvgRemove {
    this.element = element;
    for(var i = 0; i< this.element.children.length; i++){

I’ve been trying out my svg-remove attribute with some of our more complicated visualisations.

Unfortunately, the DOM manipulation the attribute does on attached breaks aurelias binding to the elements and they’re not removed properly when the contents of a repeat.for change.

I’ve finally had some time to look into this and find a solution. The following code removes the wrapping <svg svg-remove> in the beforeCreate hook which seems to work.

My next step is to create a @containerlessSVG decorator which adds the existing containerless metadata and also applies the below hook without requiring the svg-remove attribute.

Does anybody know how I would apply this selectively from a decorator?

const viewResources = container.get(ViewResources);

  beforeCreate: (view, container, fragment) => {
    if(fragment.children.length === 1 && 
      fragment.children[0].tagName === 'svg' && 
    ) {
    const root = fragment.children[0];   
    for(var i = 0; i < root.children.length; i++){

Nice that you figured that out, but you shouldn’t be hooking into create phase unnecessarily, here is an example of how to do it in compile phase:

export class RemoveSvg {
  beforeCompile(fragment: DocumentFragment) {
    let svg = fragment.querySelector('svg');
    if (svg) {
      let remove = svg.hasAttribute('remove');
      if (remove) {
        while (svg.childElementCount) {
          (svg.parentNode as Element).insertBefore(svg.firstElementChild, svg);
        (svg.parentNode as Element).removeChild(svg);

And the html looks very similar to yours:

<template transform.bind='getTransform(x, y, w, A, B)'>
  <svg remove>
      <!-- ... -->

Thanks @bigopon.

I looked at beforeCompile but couldn’t get it working.

probably because you forgot to remove <svg-rect/> via containerness ? I saw you commented it out

I just noticed in your pythagoras code you have containerless commented out too so assumed that was the way to go.

If I use containerless, the <svg> never gets removed.


Yes, it’s there because it’s the svg of <svg-rect/> we want to remove, not svg inside <app/> so we need to declare view engine hooks for <svg-rect/>

Doh, I have to import it where it’s required! Thanks for the pointers.
I might try and do a docs PR because this this is kinda essential knowledge for doing anything complex with SVG and Aurelia.

By the way, that fractals demo is awesome!

1 Like

Please do :wink: