如何在 HTML5 Canvas 元素中使用自定义字体? [英] How can I use custom fonts in an HTML5 Canvas element?
本文介绍了如何在 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 做到这一点: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.
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 元素中使用自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文