HTML5 Canvas filltext and font-face

后端 未结 2 872
渐次进展
渐次进展 2021-01-12 19:28

Been looking around stackoverflow and google for ways to solve this issue im having but havent had much luck with a solution.

What is happening is my font-face font

相关标签:
2条回答
  • 2021-01-12 20:08

    Use this trick and bind an onerror event to an Image element.

    Demo here: http://jsfiddle.net/g6LyK/ — works on the latest Chrome.

    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.type = 'text/css';
    link.href = 'http://fonts.googleapis.com/css?family=Vast+Shadow';
    document.getElementsByTagName('head')[0].appendChild(link);
    
    // Trick from https://stackoverflow.com/questions/2635814/
    var image = new Image;
    image.src = link.href;
    image.onerror = function() {
        ctx.font = '50px "Vast Shadow"';
        ctx.textBaseline = 'top';
        ctx.fillText('Hello!', 20, 10);
    };
    
    0 讨论(0)
  • 2021-01-12 20:12

    According to an answer to this question, you might need to listen to the onreadystatechange event, checking if document.readystate === 'complete'.

    0 讨论(0)
提交回复
热议问题