Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG, vector-effect="non-scaling-stroke" and IE

Tags:

svg

As I understand it, the SVG attribute vector-effect="non-scaling-stroke" should prevent strokes from being distorted (made thicker or thinner) if/when the SVG object is scaled or stretched.

For example - in this fiddle (http://jsfiddle.net/1cj0ovae/5/), I have two SVG objects; both are 5x wider than they are tall. Because the view box of both calls for a square, the SVGs are stretched and distorted.

In the upper SVG - the green path is distorted - it's much thicker than it's supposed to be (stroke-width="2").

In the lower SVG, however, the red path is displayed "correctly" - a 2px thick stroke - because it has the vector-effect="non-scaling-stroke" attribute set.

This seems to work in Chrome, Safari, and Firefox, but not in even recent versions of IE (e.g., IE10).

Is this an IE bug? Is there a workaround?

like image 355
mattstuehler Avatar asked Apr 22 '15 08:04

mattstuehler


1 Answers

vector-effects is part of SVG 1.2 tiny and the upcoming and as-yet-unfinished SVG 2 specifications.

IE has only targeted SVG 1.1 which does not have vector-effects. Other UAs have cherry picked parts of SVG 1.2 tiny such as vector-effects but no current UA implements it all (Opera 12 did I think).

To work around it figure out how wide the stroke should be using javascript. I.e. work out the difference between the transform that's applied to the shape and apply the inverse of that to the stroke-width. It's not straightforward as I found when I implemented this in Firefox.

like image 198
Robert Longson Avatar answered Sep 18 '22 21:09

Robert Longson