如何在Canvas元素上渲染FontAwesome中的字形

Mic*_*est 23 javascript html5 canvas webfonts

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"; }
Run Code Online (Sandbox Code Playgroud)

我试图将图标从此字体渲染到画布元素,但无法看到如何访问这些字形.

ctx.font = "40px FontAwesome";
ctx.fillText("\f000",20,20); // no cigar
Run Code Online (Sandbox Code Playgroud)

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

Rom*_*sse 22

你应该使用String.fromCharCode:

ctx.fillText(String.fromCharCode("0xf000"), 20, 20);
Run Code Online (Sandbox Code Playgroud)

或者这种Unicode转义的语法:

ctx.fillText("\uf000", 20, 20);
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,使用这种格式:`createjs.Text('\ uf178','100px FontAwesome,'red')`.非常感谢. (2认同)