Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The applying order of SVG transforms

Tags:

In W3C's spec, it says:

The value of the ‘transform’ attribute is a <transform-list>, which is defined as a list of transform definitions, which are applied in the order provided.

...

If a list of transforms is provided, then the net effect is as if each transform had been specified separately in the order provided

When I tried out the follow markups in firefox, chrome and IE10, all three rendered by doing translate first, then following by rotate! See the codepen snippet. What have I missed here? Or the implementations from the 3 browsers are not W3C compliant?

<svg width="180" height="200"
  xmlns="http://www.w3.org/2000/svg" 
  xmlns:xlink="http://www.w3.org/1999/xlink">

  <!-- This is the element before translation and rotation are applied -->
  <rect x="0" y="0" height="100" width="100" style="stroke:#000; fill: #0086B2" fill-opacity=0.2 stroke-opacity=0.2></rect>

  <!-- Now we add a text element and apply rotate and translate to both -->
  <rect x="0" y="0" height="100" width="100" style="stroke:#000; fill: #0086B2" transform="rotate(45 100 50) translate(50)"></rect>
</svg>
like image 689
cateyes Avatar asked Sep 03 '13 01:09

cateyes


People also ask

What is transform in SVG?

The transform attribute defines a list of transform definitions that are applied to an element and the element's children. Note: As of SVG2, transform is a presentation attribute, meaning it can be used as a CSS property.

How do I rotate SVG in HTML?

Inline you would do it like this: x-axis flip : transform="scale(-1 1)" y-axis flip : transform="scale(1 -1)"

Where is the SVG origin?

For HTML elements, this coordinate system originates at the 50% 50% point of the element. For SVG elements, the origin is, assuming we have no transform applied on the element itself or any of its ancestors inside the <svg> element, at the 0 0 point of the SVG canvas.


1 Answers

The specification is pretty clear about the order which is that the rightmost transform is applied first.

If a list of transforms is provided, then the net effect is as if each transform had been specified separately in the order provided.

I.e,

<g transform="translate(-10,-20) scale(2) rotate(45) translate(5,10)">
  <!-- graphics elements go here -->
</g>

is functionally equivalent to:

<g transform="translate(-10,-20)">
  <g transform="scale(2)">
    <g transform="rotate(45)">
      <g transform="translate(5,10)">
        <!-- graphics elements go here -->
      </g>
    </g>
  </g>
</g>
like image 85
Robert Longson Avatar answered Oct 05 '22 12:10

Robert Longson