Created
September 14, 2015 23:14
-
-
Save ianhirschfeld/ad718c114354253a6114 to your computer and use it in GitHub Desktop.
Dynamically adjust a SVG text box so that the font fits a certain width.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fitTextToWidth = function(node, width) { | |
var nodeWidth = node.getBBox().width; | |
if (nodeWidth > width) { | |
var $node = $(node); | |
var fontSize = $node.css('font-size'); | |
fontSize = parseInt(fontSize.substring(0, fontSize.length - 2)); | |
fontSize--; | |
if (fontSize > 0) { | |
$node.css('font-size', fontSize + 'px'); | |
fitTextToWidth(node, width); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment