Canvas2D Font Test

Testing font rendering with Vanilla JS Canvas2D

by Akurn

HTML

<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
<canvas id="test" width="600" height="400"></canvas>

JavaScript

WebFont.load({
  google: {
    families: [ 'Andika' ]
  },
  active: function () {
		const canvas = document.getElementById("test")
    const ctx = canvas.getContext("2d")
    
    const renderCanvas = document.createElement('canvas')
    renderCanvas.width = 600
    renderCanvas.height = 400
    const renderCtx = renderCanvas.getContext("2d")

    renderCtx.font = "normal 14px Andika"
    renderCtx.fillText("Here is some long text that should render with a custom font.", 10, 10)

    renderCtx.font = "normal 20px Andika"
    renderCtx.fillText("Here is some longer and bigger text that should render with a custom font.\nAlso has a new line.", 10, 40)
    
    renderCtx.font = "normal 34px Andika"
    renderCtx.fillText("Here is the largest text with a custom font.", 10, 100)
    
    ctx.drawImage(renderCanvas, 0, 0, 600, 400)
  }
})