KineticJS
KineticJS copied to clipboard
Length of text on a text Path is unpredictable
I'm trying to get some text to fit onto a certain length path by scaling the font down until the text is small enough to fit into the available space. For plain text this is pretty trivial:
function CreateScaledText(txt, availableSpace)
{
var fontSize = 19;
var ruler = new Kinetic.Text({
text: txt,
fontSize: fontSize,
fontFamily: "Calibri",
fill: 'white',
});
while (ruler.getTextWidth() > availableSpace)
ruler.setFontSize(--fontSize);
}
This approach doesn't work at all for curved text though. If I use the above method to create some text, and then use the calculated fontSize for a textPath of a known length, the text gets cut off! Here is a jsfiddle demonstrating the problem