Init loaded text with remote web font in Fabric.js

风格不统一 提交于 2019-12-03 07:18:50

The issue:

  1. Canvas renders immediately
  2. Google loads webfonts
  3. Canvas has no idea until the next event that re renders it

The solution:

  1. Canvas renders immediately
  2. Google loads webfonts with a callback
  3. You force render
  4. Canvas now has the

http://jsfiddle.net/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.com/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();
    });
})();

Using fabric.js and Google Web Fonts, the following code works for me:

Fiddle: http://jsfiddle.net/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.com/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();
Ventoh

Short way:

<script src="//ajax.googleapis.com/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.com/typekit/webfontloader .

specific https://groups.google.com/forum/#!topic/fabricjs/sV_9xanu6Bg

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.net/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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!