且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在HTML5 Canvas元素中使用自定义字体?

更新时间:2022-12-11 19:07:11

我把一个简单的演示在jsfiddle这里显示如何使用@ font-face: http:// jsfiddle .net / zMKge /

I've thrown together a simple demo on jsfiddle here showing how to do this with @font-face: http://jsfiddle.net/zMKge/

Opera还有一个关于使用< canvas> 包括文本API,但我不够酷,有两个超链接。 :)

Opera also has a simple tutorial on using <canvas>, including the text API, but I'm not cool enough to have two hyper-links. :)

CSS:

@font-face {
    font-family: 'KulminoituvaRegular';
    src: url('http://www.miketaylr.com/f/kulminoituva.ttf');
}

Javascript:

Javascript:

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 KulminoituvaRegular';
  ctx.fillStyle = 'orangered';
  ctx.textBaseline = 'top';
  ctx.fillText  ('Keyboard Cat', 0, 270);
};