I have an interactive SVG diagram that is almost completely unlike this simple demo…
Demo: http://jsfiddle.net/CphxH/6/
…except insofar as it:
- has a bunch of
<text>
elements, - dynamically adjusts the
viewBox
, and - I'd like the font size of the text seen on screen to be the same regardless of how big the viewBox is.
Ideally I'd like the equivalent of vector-effect:non-scaling-stroke
(at least on Webkit), but for font-size
. Does such a thing exist? If not, is my best bet to calculate the viewBox size, combine it with the aspectRatio
to determine the larger axis, and then use JS to manipulate CSS rules for the font-size
property?
No, there is not any automatic way to keep the text size constant if you change the viewBox. You would have to adjust the text size with Javascript as you thought.
Here is a JavaScript-based solution. You call
recordSVGFontSizes()
once at the beginning, passing it an SVG element and a StyleSheet. It calculates the viewport for the SVG, and runs through the stylesheet and finds all rules that mentionfont-size
and record the original font size.Then, when your viewBox changes, pass the recorded values to
normalizeSVGFontSizes()
. It will re-calculate the viewport and update all font sizes appropriately.Note that since the viewport calculation does not currently work for Firefox, this script does not work there.
Demo: http://jsfiddle.net/CphxH/8/