23

FontAwesome在其 CSS 文件中指定字形,如下所示:

/*  Font Awesome uses the Unicode Private Use Area (PUA) to ensure 
    screen readers do not read off random characters that represent icons */
.icon-glass:before                { content: "\f000"; }
.icon-music:before                { content: "\f001"; }

我正在尝试将此字体中的图标渲染到画布元素,但看不到如何访问这些字形。

ctx.font = "40px FontAwesome";
ctx.fillText("\f000",20,20); // no cigar

如何在 JavaScript 字符串中指定这些字形?

4

1 回答 1

24

您应该使用String.fromCharCode

ctx.fillText(String.fromCharCode("0xf000"), 20, 20);

或此 Unicode 转义语法:

ctx.fillText("\uf000", 20, 20);
于 2013-02-08T14:24:48.690 回答