phunder
phunder

Reputation: 1703

Limiting length of text added to an HTML5 canvas

I am building a tree structure engine using an HTML5 canvas. Each tree node has a name that is read along with several other values from a database. Each node is represented by a block, the size of which is fixed. My problem is that the name of the block is often too long to fit onto the block. How can I determine how long (in width) a piece of text is going to be before adding it to the canvas, and how can I cut it off if it is going to be too long?

Any suggestions would be greatly appreciated.

Upvotes: 1

Views: 1133

Answers (1)

paul
paul

Reputation: 22001

context.measureText("some text").width;

Upvotes: 2

Related Questions