Currently, I am immersed in developing a large custom application using Fabric JS and thus far, my progress has been commendable. However, I have encountered an issue concerning the initialization of loaded text objects that utilize web fonts.
When the font is readily available on the client's device, everything functions as expected. However, if the webfont fails to load, the text object on the canvas defaults to a sans-serif font-family.
In essence, here is what I typically do (for illustration purposes, let's assume "allstar" is the webfont being utilized):
CSS: The CSS is incorporated within fonts.css, located within the head section preceding fabric.js
@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: This is called towards the end of the page, enclosed within $(document).ready(function(){})
var textSample = new fabric.Text(text, {
fontFamily: "allstar",
});
canvas.add(textSample);
canvas.renderAll();
While implementing the font elsewhere on the webpage (e.g., within a transparent span element with a dot and the font properly loaded) yields positive results, I believe this approach may not be the most efficient coding practice.
At the moment, I am utilizing fabric.js version 1.3.0