Center (proportional font) text in an HTML5 canvas

后端 未结 5 1298
别那么骄傲
别那么骄傲 2020-12-18 19:41

I would like to be able to center single lines of text within rectangular areas I can calculate. The one thing I have expected to do in 2D geometry on a canvas is to center

5条回答
  •  既然无缘
    2020-12-18 20:09

    You can use ctx.textAlign = "center"; to align item to center

    var canva = document.getElementById("canvas");
    ctx.textAlign = "center"; // To Align Center
    ctx.font = "40px Arial"; // To change font size and type
    ctx.fillStyle = '#313439'; // To give font 
    ctx.fillText("Text Center", 150 ,80);
    

提交回复
热议问题