HTML5 Canvas Font Size Based on Canvas Size


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';
}

Updated on: 25-Jun-2020

499 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements