Skip to content

Instantly share code, notes, and snippets.

@Sashkan
Created September 12, 2019 12:07
Show Gist options
  • Save Sashkan/92ee88303b520cad6d68bc1391016702 to your computer and use it in GitHub Desktop.
Save Sashkan/92ee88303b520cad6d68bc1391016702 to your computer and use it in GitHub Desktop.
snap.svg autoscaling text to box
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