HTML5 Canvas Font Size Based on Canvas Size

JavascriptWeb DevelopmentFront End Scripts

To scale fonts, let us see an example.

Canvas: 800px Font Size: 60px

You need to use the following code for our example to scale font size based on canvas −

var fontBase = 800;
var fontSize = 60;

function getFont() {
   var ratio = fontSize / fontBase;
   var cSize = canvas.width * ratio;
   return (cSize |0) + 'px sans-serif';
Published on 06-Apr-2018 17:17:27