-
-
Save lorkel/33515186edb72d8b8638c478a10b3bdc to your computer and use it in GitHub Desktop.
Fabric.js Textbox resize according to specified height
This file contains hidden or 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
// load canvas with id 'c' | |
const canvas = new fabric.Canvas('c'); | |
// use canvas height as the height limit | |
var limit = canvas.height; | |
var text = new fabric.Textbox('Some very long text'); | |
// set initial values | |
text.set({ | |
top: margin, | |
width: canvas.width, | |
textAlign: 'center', | |
fontWeight: 'bold', | |
fontSize: 12 | |
}); | |
while (text.width > canvas.width) { | |
text.setWidth(text.width -= 10); | |
} | |
while (text.height > limit) { | |
text.set({fontSize: text.fontSize-1}); | |
} | |
canvas.add(text); | |
text.centerH(); | |
canvas.renderAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment