首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >第一次使用@ <canvas> -face将文本绘制为font时不起作用

第一次使用@ <canvas> -face将文本绘制为font时不起作用
EN

Stack Overflow用户
提问于 2010-05-03 15:01:06
回答 15查看 83K关注 0票数 108

当我使用通过@font-face加载的字体在画布中绘制文本时,文本不能正确显示。它根本不显示(在Chrome 13和Firefox 5中),或者字体错误(Opera 11)。这种类型的意外行为仅在具有该字体的第一个图形中发生。在此之后,一切都会正常工作。

这是标准行为还是什么?

谢谢。

PS:以下是测试用例的源代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>@font-face and &lt;canvas&gt;</title>
        <style id="css">
@font-face {
    font-family: 'Press Start 2P';
    src: url('fonts/PressStart2P.ttf');
}
        </style>
        <style>
canvas, pre {
    border: 1px solid black;
    padding: 0 1em;
}
        </style>
    </head>
    <body>
        <h1>@font-face and &lt;canvas&gt;</h1>
        <p>
            Description: click the button several times, and you will see the problem.
            The first line won't show at all, or with a wrong typeface even if it does.
            <strong>If you have visited this page before, you may have to refresh (or reload) it.</strong>
        </p>
        <p>
            <button id="draw">#draw</button>
        </p>
        <p>
            <canvas width="250" height="250">
                Your browser does not support the CANVAS element.
                Try the latest Firefox, Google Chrome, Safari or Opera.
            </canvas>
        </p>
        <h2>@font-face</h2>
        <pre id="view-css"></pre>
        <h2>Script</h2>
        <pre id="view-script"></pre>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script id="script">
var x = 30,
    y = 10;

$('#draw').click(function () {
    var canvas = $('canvas')[0],
        ctx = canvas.getContext('2d');
    ctx.font = '12px "Press Start 2P"';
    ctx.fillStyle = '#000';
    ctx.fillText('Hello, world!', x, y += 20);
    ctx.fillRect(x - 20, y - 10, 10, 10);
});
        </script>
        <script>
$('#view-css').text($('#css').text());
$('#view-script').text($('#script').text());
        </script>
    </body>
</html>
EN

回答 15

Stack Overflow用户

发布于 2011-11-22 15:50:18

使用this trick并将onerror事件绑定到Image元素。

演示here:适用于最新的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);
};
票数 26
EN

Stack Overflow用户

发布于 2016-03-27 22:09:19

在画布中使用FontFace API之前,您可以使用它加载字体:

const myFont = new FontFace('My Font', 'url(https://myfont.woff2)');

myFont.load().then((font) => {
  document.fonts.add(font);

  console.log('Font loaded');
});

首先下载字体资源myfont.woff2。下载完成后,字体将添加到文档的FontFaceSet中。

在撰写本文时,FontFace应用编程接口的规范是一个工作草案。See browser compatibility table here.

票数 20
EN

Stack Overflow用户

发布于 2011-09-03 06:41:13

问题的核心是,您正在尝试使用该字体,但浏览器尚未加载它,甚至可能还没有请求它。您需要的是加载字体,并在加载后给您一个回调;一旦您获得回调,您就知道可以使用该字体。

看看谷歌的WebFont Loader;它看起来像是一个“自定义”的提供程序,在加载后使用active回调就可以使其工作。

我以前从未使用过它,但快速浏览一下文档,您需要创建一个css文件fonts/pressstart2p.css,如下所示:

@font-face {
  font-family: 'Press Start 2P';
  font-style: normal;
  font-weight: normal;
  src: local('Press Start 2P'), url('http://lemon-factory.net/reproduce/fonts/Press Start 2P.ttf') format('ttf');
}

然后添加如下JS:

  WebFontConfig = {
    custom: { families: ['Press Start 2P'],
              urls: [ 'http://lemon-factory.net/reproduce/fonts/pressstart2p.css']},
    active: function() {
      /* code to execute once all font families are loaded */
      console.log(" I sure hope my font is loaded now. ");
    }
  };
  (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);
  })();
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2756575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档