[SOLVED] OS-font vs external font at preloader SVG-file

I am currently trying to settle on a font-type in a preloader SVG-image. As of now I am trying to use a Google/Meriweather-font (or Markazi), that works perfect on my PC (fetches installed OS-font):
image

  • whereas my webhotel/remote SVG renders it like this on my laptop:
    image

… thus falling back an ugly Times New Roman-font.

I use the (quite standard) loading.js/Vanilla JS script with a little SVG-variation:


pc.script.createLoadingScreen(function (app) {
    var showSplash = function () {
        // splash wrapper
        var wrapper = document.createElement('div');
        wrapper.id = 'application-splash-wrapper';
        document.body.appendChild(wrapper);

        // splash
        var splash = document.createElement('div');
        splash.id = 'application-splash';
        wrapper.appendChild(splash);
        splash.style.display = 'none';
        
        var container = document.createElement('div');
        container.id = 'progress-bar-container';
        splash.appendChild(container);

        var bar = document.createElement('div');
        bar.id = 'progress-bar';
        container.appendChild(bar);

    };

    var hideSplash = function () {
        var splash = document.getElementById('application-splash-wrapper');
        splash.parentElement.removeChild(splash);
    };

    var setProgress = function (value) {
        var bar = document.getElementById('progress-bar');
        if(bar) {
            value = Math.min(1, Math.max(0, value));
            bar.style.width = value * 100 + '%';
        }
    };

    var createCss = function () {
        var css = [
            'body {',
            '    background-color: #283538;',
            '}',

            '#application-splash-wrapper {',
            '    position: absolute;',
            '    top: 0;',
            '    left: 0;',
            '    height: 100%;',
            '    width: 100%;',
            '    background-image: radial-gradient(#102030, #000000 , #101020);',
            '}',

              '#application-splash {',
            '    position: absolute;',
            '    top: calc(2% - 4px);',
            '    width: 86%;',
            '    left: 7%;',
            '}',

            '#application-splash img {',
            '    width: 86%;',
            '    margin: 0px auto;',
            '    display: block;',
            '}',

            '#progress-bar-container {',
            '    margin: 20px auto 0 auto;',
            '    height: 4px;',
            '    width: 100%;',
            '    background-image: linear-gradient(to left,#d0e3db, #f8f9e1 , #eef2c8);',
            '}',

            '#progress-bar {',
            '    width: 0%;',
            '    height: 100%;',
            '    background-color: #fff;',
            '}',
            '@media (max-width: 600px) {',
            '    #application-splash {',
            '        top: calc(2% - 4px);',
            '    width: 86%;',
            '    left: 7%;',
            '    }',
            '}'
        ].join("\n");

        var style = document.createElement('style');
        style.type = 'text/css';
        if (style.styleSheet) {
          style.styleSheet.cssText = css;
        } else {
          style.appendChild(document.createTextNode(css));
        }

        document.head.appendChild(style);
    };


    createCss();

    showSplash();
    
    // Use an image from the assets in the project via the asset registry
    // More information: http://developer.playcanvas.com/en/api/pc.AssetRegistry.html
    app.on('preload:start', function() {
        var splash = document.getElementById('application-splash');
        
        var logoAsset = app.assets.find('SplashSVG2.svg');
        
        if (logoAsset) {
            var logo = document.createElement('img');    
            logo.src = logoAsset.getFileUrl();

            // Insert DOM before the progress bar
            if (splash.childNodes.length > 0) {
                splash.insertBefore(logo, splash.childNodes[0]);
            } else {
                splash.appendChild(logo);
            }
            
            logo.onload = function () {
                splash.style.display = 'block';
            };
        }
    });
        
    app.on('preload:end', function () {
        app.off('preload:progress');
    });
    app.on('preload:progress', setProgress);
    app.on('start', hideSplash);
});

Q: Can I load an external google-font to be used within the SVG-file at browser DOM-execution, and prevent the fall-back?

PS: Have been trying to break the issue myself (A modern font loading strategy with the vanilla JS FontFaceSet.load() method | Go Make Things), but I am still too inexperienced in Vanilla JS.

Hope you can help (perhaps @Leonidas ? )

If you want to use a custom font in the loading screen of your application, you would need to assign it to the div element via CSS, which you inject with createCSS() function.

PS.
You should stop adding adjectives to “Javascript”, suggesting an existance of different Javascript variants. There is no “Vanilla” or “Standard” javascript. There is just javascript. Period. You may confuse people, who are not familiar with the language. There are so many good javascript learning tutorials out there. Grab one - you will be surprised how easy it is to use. Hope you take it as a critical advice, rather than anything.

Regarding other users: yes, and beginners might read our topics as well -> there are small but vital differences to JS in and outside of PC (just so new users of PC know … the usage of ‘this.’ makes, for instance, things easier in PC-JS within single ‘.js’-files, than by larger scope and pure HTML-JS usage {but be careful while reusing the same script on different entities}). PS: " … Here is a basic script. We can learn about the structure of a PlayCanvas script from it. …"
I actually started to get refences from the PC-team in regards to Vanilla [anyhow: I now agree that there are second to none/no differences between Vanilla and JS … new readers/beginners should not pay attention to https://gomakethings.com/a-modern-font-loading-strategy-with-the-vanilla-js-fontfaceset.load-method/ either].

Well. Back on topic-track: At the bottom of the createCSS() chunk I altered the code to:

  '@media (max-width: 600px) {',
            '    #application-splash {',
            '        top: calc(2% - 4px);',
            '    width: 86%;',
            '    left: 7%;',
            '    font-family: ’Merriweather’, sans-serif;',
            '    }',
            '}'
        ].join("\n");

        var style = document.createElement('style');
        style.type = 'text/css';
        style.href = 'https://fonts.googleapis.com/css?family=Merriweather&display=swap';
        style.rel = 'stylesheet';
        if (style.styleSheet) {
          style.styleSheet.cssText = css;
        } else {
          style.appendChild(document.createTextNode(css));
        }

        document.head.appendChild(style);
    };

At a raw first debug-approach, the code compiled without errors, in the sense that the SVG-image loaded (same effect with "-signs around “Merriweather” instead). It does not seem to work for the font alone though. The Times New Roman font still appears (browser falls back to nearest OS-font).

Ok, buddy, you win, I lost.

As for the css, you are not adding the stylesheet to the head correctly.

// createCss()
document.head.innerHTML += '<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Merriweather:wght@300&display=swap" />';

// and use double quotes in css
'#application-splash {',
'    font-family: "Merriweather", serif;',
'}'

Hey, man … I am still trying to keep it Lebowski-casual :slight_smile: … ‘The code wins’
Stylesheet: ok, will give it a look - and see if document.head.innerHTML fits the loading.js-template (otherwise try; to make it fit off course)

PS: Glad if the createCSS() structure keeps it within ’ and ". Would hate to dig into ` and ´ etc

If you have access to index.html, you could just add plain HTML + CSS there, without any javascript.

I am all in for the approach of embedding <script></script> clusters in HTML … when possible.
While still working within PC, the loading.js file is compiled for then to be exported (+ later upload).

Tried this:

 '#application-splash {',
            '    position: absolute;',
            '    top: calc(2% - 4px);',
            '    width: 86%;',
            '    left: 7%;',
            '    font-family: "Architects+Daughter", sans-serif;',
            '}',

            '#application-splash img {',
            '    width: 86%;',
            '    margin: 0px auto;',
            '    display: block;',
            '}',

            '#progress-bar-container {',
            '    margin: 20px auto 0 auto;',
            '    height: 4px;',
            '    width: 100%;',
            '    background-image: linear-gradient(to left,#d0e3db, #f8f9e1 , #eef2c8);',
            '}',

            '#progress-bar {',
            '    width: 0%;',
            '    height: 100%;',
            '    background-color: #fff;',
            '}',
            '@media (max-width: 600px) {',
            '    #application-splash {',
            '        top: calc(2% - 4px);',
            '    width: 86%;',
            '    left: 7%;',            
            '    }',
            '}'
        ].join("\n");
        
        document.head.innerHTML += '<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Architects+Daughter:wght@300&display=swap" />';
        
        var style = document.createElement('style');
        style.rel = 'stylesheet';
        if (style.styleSheet) {
          style.styleSheet.cssText = css;
        } else {
          style.appendChild(document.createTextNode(css));
        }

        document.head.appendChild(style);
    };
  • without luck (ps: exchanged the google font, to a font I haven’t installed on my PC either).

I have been googling “inkscape xml font” (I made the SVG in inkscape)

  • amongst hits; this looks interesting: https://stackoverflow.com/questions/10728892/svg-xml-font-family
    Within that example I am wondering if this metacode will float/the approach will make sense:

         '@media (max-width: 600px) {',
         '    #application-splash {',
         '        top: calc(2% - 4px);',
         '    width: 86%;',
         '    left: 7%;',            
         '    }',
         '<svg xmlns="http://www.w3.org/2000/svg">',
         '<text x="10" y="100"' font-family="'Architects+Daughter', serif" font-size="24">',
         '</text>',
         '</svg>',
         '}'
     ].join("\n");
     
     document.head.innerHTML += '<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Architects+Daughter:wght@300&display=swap" />';
     
     var style = document.createElement('style');

Have you tried embedding the font into the SVG? That looks like it would be your best bet.

Otherwise, this sounds like your problem: https://forums.tumult.com/t/using-a-custom-web-font-within-an-svg-file/7254

Hmmm … I was afraid it might end like this. Trouble is that I already have made the SVGs on PC, while having the Markazi (at first I thought Merriweather, but both), on my Fonts folder. Without paying notice then, I now know that the font falls back to Times New Roman on laptop (+ all other device without installed font).
I will remake the SVGs, but with your last link as reference, to secure use of Google family font at preload.