Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dalalibera/89dbc05cc50667f46a036f7f5f5be6f7 to your computer and use it in GitHub Desktop.
Save Dalalibera/89dbc05cc50667f46a036f7f5f5be6f7 to your computer and use it in GitHub Desktop.
Custom Font in Javascript Canvas
<span style="font-family: 'Lobster', cursive;">&nbsp;</span>
<canvas id=c width=500 height=500></canvas>
/*
sources:
http://stackoverflow.com/questions/2608022/how-can-i-use-custom-fonts-in-an-html5-canvas-element
http://stackoverflow.com/questions/2756575/drawing-text-to-canvas-with-font-face-does-not-work-at-the-first-time
*/
var ctx = document.getElementById('c').getContext('2d');
var kitty = new Image();
kitty.src = 'http://i954.photobucket.com/albums/ae30/rte148/891blog_keyboard_cat.gif';
kitty.onload = function(){
ctx.drawImage(this, 0,0,this.width, this.height);
ctx.font = '68px Lobster';
ctx.fillStyle = 'orangered';
ctx.textBaseline = 'top';
ctx.fillText ('Keyboard Cat', 0, 270);
};
@import url(https://fonts.googleapis.com/css?family=Lobster);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment