How to draw text in a given rectangle on canvas?

I create several images using canvas. I would like to be able to draw text in a given rectangle (i.e. Offset x, Offset y, Width and Height) on the canvas and so that the text is as large as possible, without overflow and, if possible, with word wrap. Is it possible?

+3
source share
1 answer

It is possible.

  • no-wrapping: You can use a method CanvasRenderingContext2D.measureText()that takes a string and returns an object {width: float}. You must find out the height by doing the math using the font size. Then just specify the font size in the binary search path. soon you will find the best size for the canvas.

  • : .

.

(: : ) div, . , .

+4

All Articles