私は Fabric JS を使用して大きなカスタム アプリケーションに取り組んでおり、すでに素晴らしい仕事をしています。しかし、Web フォントを使用する init ロード テキスト オブジェクトに問題があります。
そのフォントがクライアントのコンピューターでローカルである限り、問題なく動作します。それ以外の場合、Web フォントは読み込まれず、キャンバス上のテキスト オブジェクトはデフォルトのサンセリフ フォント ファミリでレンダリングされます。
要するに、私がやっていることは次のとおりです(この例では、「allstar」をWebフォントとして使用しています):
CSS: css は、fabric.js の前の head の 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
}
Javascript: これは $(document).ready(function(){}) 内のページの下部にロードされます
var textSample = new fabric.Text(text, {
fontFamily: "allstar",
});
canvas.add(textSample);
canvas.renderAll();
ページの他の場所でフォントを使用すると (例: ドットとフォントがロードされた透明なスパン タグで)、正常に動作します。しかし、それは適切なコーディング方法ではないと思います。
fabric.js バージョン 1.3.0 を使用します