Pure SVG way to fit text to a box

后端 未结 4 685
小鲜肉
小鲜肉 2020-12-07 13:21

Box size known. Text string length unknown. Fit text to box without ruining its aspect ratio.

\"enter

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 13:41

    I didn't find a way to do it directly without Javascript, but I found a JS quite easy solution, without for loops and without modify the font-size and fits well in all dimensions, that is, the text grows until the limit of the shortest side.

    Basically, I use the transform property, calculating the right proportion between the desired size and the current one.

    This is the code:

    
    
     MY UGLY TEXT
     
    

    In the previous example the text grows until the width == 500, but if I use a box size of width = 500 and height = 30, then the text grows until height == 30.

提交回复
热议问题