Change font size of Canvas without knowing font family

前端 未结 5 987
遥遥无期
遥遥无期 2020-12-30 00:06

Is there a way to only change the font size of a canvas context without having to know/write the font family.

 var ctx = document.getElementById(\"canvas\").         


        
5条回答
  •  伪装坚强ぢ
    2020-12-30 00:17

    Here is an easier and cleaner way of changing the font size that will work regardless if you are using font-variant or font-weight or not.

    Assuming your new font size is 12px

    ctx.font = ctx.font.replace(/\d+px/, "12px");
    

    Or a nice one liner if you want to increase by 2 points:

    ctx.font = ctx.font.replace(/\d+px/, (parseInt(ctx.font.match(/\d+px/)) + 2) + "px");
    

提交回复
热议问题