저는 며칠 동안 아무런 소용이 없었지만 단순한 것이어야합니다.Google 글꼴이 캔버스에 처음으로 표시되지 않습니다
처음으로 캔버스를 그릴 때 Google 글꼴을 올바르게 표시 할 수 없습니다. 글꼴의 후속 디스플레이는 OK입니다. 확실히 글꼴을로드하는 타이밍과 관련이있는 것 같습니다.
Drawing text to <canvas> with @font-face does not work at the first time에 나열된 제안 된 수정 프로그램을 대부분 시도했지만 성공하지 못했습니다. 당신이 jsfiddle를로드 할 때 http://jsfiddle.net/HatHead/GcxQ9/23/
, 캔버스 제목과 텍스트는 기본 글꼴입니다 볼 수 있습니다 :
다음은 위의 링크에서 수정의 대부분을 통합 내 jsfiddle입니다. 실행 버튼을 누르면 글꼴이 js 코드에 지정된 글꼴로 업데이트됩니다. 여기
위 jsfiddle의 코드입니다 :HTML :
<!-- you need to empty your browser cache and do a hard reload EVERYTIME to test this otherwise it will appear to working when, in fact, it isn't -->
<h1>Title Font</h1>
<p>Paragraph font...</p>
<canvas id="myCanvas" width="740" height="400"></canvas>
CSS :
@import url(http://fonts.googleapis.com/css?family=Architects+Daughter);
@import url(http://fonts.googleapis.com/css?family=Rock+Salt);
canvas {
font-family:'Rock Salt', 'Architects Daughter'
}
.wf-loading p {
font-family: serif
}
.wf-inactive p {
font-family: serif
}
.wf-active p {
font-family:'Architects Daughter', serif;
font-size: 24px;
font-weight: bold;
}
.wf-loading h1 {
font-family: serif;
font-weight: 400;
font-size: 42px
}
.wf-inactive h1 {
font-family: serif;
font-weight: 400;
font-size: 42px
}
.wf-active h1 {
font-family:'Rock Salt', serif;
font-weight: 400;
font-size: 42px;
}
JS :
// do the Google Font Loader stuff....
WebFontConfig = {
google: {
families: ['Architects Daughter', 'Rock Salt']
}
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
//play with the milliseconds delay to find the threshold - don't forget to empty your browser cache and do a hard reload!
setTimeout(WriteCanvasText, 0);
function WriteCanvasText() {
// write some text to the canvas
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.font = "normal" + " " + "normal" + " " + "bold" + " " + "42px" + " " + "Rock Salt";
context.fillStyle = "#d50";
context.fillText("Canvas Title", 5, 100);
context.font = "normal" + " " + "normal" + " " + "bold" + " " + "24px" + " " + "Architects Daughter";
context.fillText("Here is some text on the canvas...", 5, 180);
}
그것은 지연을 사용하여 작동하지만, 이것은 나쁜 해결책입니다.
이 문제를 해결할만한 아이디어가 있습니까? 누구든지 전에 때려? 굴복하고 첫 번째로드 텍스트 대신 이미지를 넣는 것을 싫어합니다.
많은 경우 stackoverflow'ers!
답변 : http://stackoverflow.com/questions/2756575/drawing-text-to-canvas-with-font-face-does-not-work-at-the-first-time – Cherniv
감사합니다. Chemiv하지만 불행히도, 내 질문에 메모로, 그리고 또한 실제 스레드 자체에서 철저하게 읽을 때, 해당 스레드에서 제안 된 수정 사항으로 문제를 해결할 수 없습니다. 내 질문에 제안 된 수정 사항이 포함되어 있지만 문제를 해결하지는 못합니다. 커뮤니티에 유용하다면 위의 코드를 데모로 게시 할 것입니다. – HatHead