本文介绍了如何在HTML5 Canvas元素中使用自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过像Cufon和typeface.js这样的东西,但是它们似乎是SIFR的替代品,不允许你设置自由形式的坐标和绘制自定义类型到< canvas>

I've looked at things like Cufon and typeface.js but they seem to be SIFR alternatives and don't allow you to set freeform coordinates and draw custom type onto a <canvas>

任何人都有任何想法?

推荐答案

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

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

这篇关于如何在HTML5 Canvas元素中使用自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:25