I'm working on a big custom application with Fabric JS and I already did a great job. But I have a problem with init loaded text object that uses a webfont.
As long as that font is local on the client's puter, I works fine, ELSE the webfont is NOT loaded and the text object on the canvas is rendered in a default sans-serif font-family.
Here is, in short, what I do (in this example I use "allstar" as my webfont):
CSS: The css is loaded inside fonts.css in the head before 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 loaded at the bottom of the page inside $(document).ready(function(){})
var textSample = new fabric.Text(text, {
fontFamily: "allstar",
});
canvas.add(textSample);
canvas.renderAll();
If I use the font elsewhere on the page (e.g.: in a transparent span tag with a dot and the font loaded), it works fine. But I don't think that's a proper way to code.
I Use fabric.js version 1.3.0
I'm working on a big custom application with Fabric JS and I already did a great job. But I have a problem with init loaded text object that uses a webfont.
As long as that font is local on the client's puter, I works fine, ELSE the webfont is NOT loaded and the text object on the canvas is rendered in a default sans-serif font-family.
Here is, in short, what I do (in this example I use "allstar" as my webfont):
CSS: The css is loaded inside fonts.css in the head before 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 loaded at the bottom of the page inside $(document).ready(function(){})
var textSample = new fabric.Text(text, {
fontFamily: "allstar",
});
canvas.add(textSample);
canvas.renderAll();
If I use the font elsewhere on the page (e.g.: in a transparent span tag with a dot and the font loaded), it works fine. But I don't think that's a proper way to code.
I Use fabric.js version 1.3.0
Share Improve this question edited Nov 14, 2013 at 23:57 kangax 39.2k13 gold badges100 silver badges135 bronze badges asked Nov 12, 2013 at 11:01 JensJens 1112 silver badges8 bronze badges6 Answers
Reset to default 4The issue:
- Canvas renders immediately
- Google loads webfonts
- Canvas has no idea until the next event that re renders it
The solution:
- Canvas renders immediately
- Google loads webfonts with a callback
- You force render
- Canvas now has the
http://jsfiddle/vvL6f/6/
WebFontConfig = {
google: { families: [ 'Ribeye::latin', 'Lora::latin', 'Croissant+One::latin', 'Emblema+One::latin', 'Graduate::latin', 'Hammersmith+One::latin', 'Oswald::latin', 'Oxygen::latin', 'Krona+One::latin', 'Indie+Flower::latin', 'Courgette::latin', 'Ranchers::latin' ] }
};
(function() {
var src = ('https:' === document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis./ajax/libs/webfont/1/webfont.js';
$.getScript(src, function(data) {
// Not sure why I need to run this twice but it makes it load the font and then reposition the font to it's correct coords.
canvas.renderAll();
canvas.renderAll();
});
})();
Short way:
<script src="//ajax.googleapis./ajax/libs/webfont/1.4.7/webfont.js"></script>
<script>
WebFont.load({
google: {
families: [ 'Abel', 'Aclonica']
},
fontinactive: function(familyName, fvd) {
console.log("Sorry " + familyName + " font family can't be loaded at the moment. Retry later.");
},
active: function() {
// do some stuff with font
$('#stuff').attr('style', "font-family:'Abel'");
var text = new fabric.Text("Text Here", {
left: 200,
top: 30,
fontFamily: 'Abel',
fill: '#000',
fontSize: 60
});
canvas.add(text);
}
});
</script>
Long way When do web-fonts load and can you pre-load them?
More on web font loader https://github./typekit/webfontloader .
fabric.js specific https://groups.google./forum/#!topic/fabricjs/sV_9xanu6Bg
Using fabric.js and Google Web Fonts, the following code works for me:
Fiddle: http://jsfiddle/DanBrown180/vvL6f/
CSS
<Style>
@font-face {
font-family: 'Jolly Lodger';
font-style: normal;
font-weight: 400;
src: local('Jolly Lodger'), local('JollyLodger'),
url(http://themes.googleusercontent./static/fonts/jollylodger/v1/RX8HnkBgaEKQSHQyP9itiaRDOzjiPcYnFooOUGCOsRk.woff) format('woff');
}
<style>
Javascript
<script type = "text/javascript">
canvas = new fabric.Canvas('c');
var text = new fabric.Text("Web Font Example", {
left: 200,
top: 30,
fontFamily: 'Jolly Lodger',
fill: '#000',
fontSize: 60
});
canvas.add(text);
</script>
HTML
<canvas id="c" height="200px" width="400px"></canvas>
Looks like it's because the Google Web Fonts css code uses:
src: local('Jolly Lodger'), local('JollyLodger')
in the CSS
I had the same problem. Font renders correctly after some event is fired.. So after creating an object we can set it active:
var textSample = new fabric.Text(text, {
fontFamily: "allstar",
});
canvas.add(textSample);
canvas.setActiveObject(textSample); // Selects the object
canvas.renderAll();
The best method is using setTimeout();
setTimeout(function(){
var activeObject = canvas.getActiveObject();
activeObject.set({
fontFamily: font,
useNative: true
});
canvas.renderAll();},500);
Check the jsfiddle
http://jsfiddle/muthupandiant/x3ptsgex/1/
As of 2017, it is easily possible to use web fonts within Fabric.js in Firefox.
You only have to include them via @import
(in your stylesheet) or via <link href="...">
(in <head>
). Afterwards you can select any font you want using fabric.Text's attribute 'fontFamily'.
Concerning your initial problem, calling canvas.renderAll();
should redraw the canvas and thus display all loaded web fonts correctly.
See a simple example on jsFiddle.