Created
September 12, 2019 12:07
-
-
Save Sashkan/92ee88303b520cad6d68bc1391016702 to your computer and use it in GitHub Desktop.
snap.svg autoscaling text to box
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
const setAutoscalingText = (text) => { | |
// Select text | |
const textObject = this.element.select(`#${clientCustomization.id}`) | |
if (textObject) { | |
// get coordinates | |
const textCoordinates = textObject.getBBox() | |
const boundingBox = this.element.select('rect').getBBox() | |
// Set new text | |
textObject.attr({ | |
text, | |
}) | |
// Check if it goes out of the box. If so, reduce font-size to fit | |
if (textCoordinates.x + textCoordinates.w > boundingBox.x + boundingBox.w) { | |
const newFontSize = Math.floor( | |
textCoordinates.w / textObject.attr('text').split('').length, | |
).toString() | |
textObject.attr({ | |
fontSize: newFontSize, | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment