Canvas How to - Compare font from CSS and Canvas








Question

We would like to know how to compare font from CSS and Canvas.

Answer


<!--from ww w. j a  v a  2s. com-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
    var canvas = document.getElementById("a");
    var ctx = canvas.getContext('2d');
    ctx.font = "italic 40px Impact";
    ctx.fillText("Impact", 0, 40);
}//]]>  
</script>
</head>
<body>
  <div>CSS:</div>
  <div style="font-family: Impact; font-style: italic; font-size: 40px">Impact</div>
  <div>Canvas:</div>
  <canvas id="a" width="300" height="50"></canvas>
</body>
</html>

The code above is rendered as follows: