小编Jen*_*ens的帖子

在Fabric.js中使用远程Web字体初始化加载的文本

我正在使用Fabric JS开发一个大型自定义应用程序,我已经做得很好.但我使用webfont的init加载文本对象有问题.

只要该字体在客户端的计算机上是本地的,我工作正常,因为未加载webfont并且画布上的文本对象以默认的sans-serif字体系列呈现.

简而言之,就是我所做的(在这个例子中,我使用"allstar"作为我的webfont):

CSS: 在fabric.js之前,css被加载到头部的fonts.css中

@font-face{
    font-family:'allstar';
    src:
        url('/path-to-fonts/all_star-webfont.eot');
    src:
        url('/path-to-fonts/all_star-webfont.eot?#iefix') format('embedded-opentype'),
        url('/path-to-fonts/all_star-webfont.woff') format('woff'),
        url('/path-to-fonts/all_star-webfont.ttf') format('truetype'),
        url('/path-to-fonts/all_star-webfont.svg#allstarregular') format('svg');
    font-weight:normal;
    font-style:normal
}
Run Code Online (Sandbox Code Playgroud)

Javascript: 这是在$(document).ready(function(){})里面的页面底部加载的

var textSample = new fabric.Text(text, {
    fontFamily: "allstar",
});
canvas.add(textSample);
canvas.renderAll();
Run Code Online (Sandbox Code Playgroud)

如果我在页面上的其他地方使用该字体(例如:在带有点和加载字体的透明span标签中),它可以正常工作.但我不认为这是一种正确的编码方式.

我使用fabric.js版本1.3.0

javascript css fonts fabricjs

7
推荐指数
1
解决办法
4940
查看次数

标签 统计

css ×1

fabricjs ×1

fonts ×1

javascript ×1