最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I wait for first canvas-repaint until @font-face-font is loaded? - Stack Overflow

programmeradmin1浏览0评论

I have a HTML5-canvas and write text with context.fillText(...); using a @font-face-font.
Displaying the page with Firefox (3.6) I have the problem, that on the first paint of the canvas the font is not downloaded yet so the text will be displayed with a standard-font.
I found a 'solution' here but it does not work, because the font is only used in the canvas and the canvas is loaded and displayed with ajax using jQuery.
Is there any better solution than trying to use a timeout setTimeout(repaintCanvas, 500);?

I have a HTML5-canvas and write text with context.fillText(...); using a @font-face-font.
Displaying the page with Firefox (3.6) I have the problem, that on the first paint of the canvas the font is not downloaded yet so the text will be displayed with a standard-font.
I found a 'solution' here but it does not work, because the font is only used in the canvas and the canvas is loaded and displayed with ajax using jQuery.
Is there any better solution than trying to use a timeout setTimeout(repaintCanvas, 500);?

Share Improve this question asked Aug 3, 2010 at 7:32 dstdst 1,78613 silver badges27 bronze badges 1
  • See stackoverflow./questions/2756575/… – a paid nerd Commented Nov 22, 2011 at 7:50
Add a ment  | 

2 Answers 2

Reset to default 4

well, you could try this,

$.get('font/url.ttf',function(){
   // do canvas codes.... cause font is loaded...
});

I think I found a solution with the help of Reigel's answer:

$.get('font/url.ttf', function() {
    // do canvas codes.... cause font is loaded...
});

Additionally use the font via font-family: 'fontfacename'; for the canvas' parent-element.
Could be that the font is loaded twice, don't know. But without the second load it won't be displayed right.

发布评论

评论列表(0)

  1. 暂无评论