简体   繁体   English

我如何将 fonts 从谷歌 fonts 导入 javascript,这样我就可以将它用于我的 ZFCC790C72A86190DE

[英]How would I import fonts from google fonts into javascript, so i can use it for my canvas?

I'm trying to import google fonts into javascript, so I can use the fonts to draw text on my canvas.我正在尝试将谷歌 fonts 导入 javascript,这样我就可以使用 fonts 在我的 ZFCC5790C72ADCDfonts 上绘制文本The problem is that I am receiving errors.问题是我收到错误。
Error 1:Failed to decode download fonts错误1:解码下载fonts失败
Error 2:OTS parsing error错误2:OTS解析错误

This is for a web page I'm developing, and I looked up the problems, but the solutions they suggested, I don't understand.这是针对我正在开发的 web 页面的,我查找了问题,但他们建议的解决方案,我不明白。

<!DOCTYPE html>
<html lang = "en">
    <body>
        <canvas id="canvas" width="400" height="400"></canvas>
<script>
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var pacifico_font = new FontFace('Pacifico', 'url(https://fonts.googleapis.com/css?family=Pacifico&display=swap)');
pacifico_font.load().then(function(loaded_face) {
    document.fonts.add(loaded_face);
    document.body.style.fontFamily = '"Pacifico", Pacifico';
}).catch(function(error) {
    alert("An error occured, please continue.");
});
document.fonts.ready.then(function(font_face_set) {
    var x = true;
    return x;
});
ctx.fillStyle=rgb(0,0,0);
if(x===true){
ctx.font="20px Pacifico";
ctx.fillText("Hello Cody(testing)",200,200);
}
</script>
</body>
</html>

I expect the canvas to show text, but it alert me to that there was an error, and there is nothing.我希望 canvas 显示文本,但它提醒我有一个错误,并且什么也没有。 In the console it says:在控制台中它说:
Failed to decode downloaded font: https://fonts.googleapis.com/css?family=Pacifico&display=swap无法解码下载的字体: https://fonts.googleapis.com/css?family=Pacifico&display=swap
OTS parsing error: invalid version tag OTS解析错误:无效的版本标签

The font link you're using is actually a link to a stylesheet.您使用的字体链接实际上是指向样式表的链接。

You can get the direct link to the font by visiting the stylesheet link.您可以通过访问样式表链接获得字体的直接链接。

It'll display the following CSS:它将显示以下 CSS:

/* cyrillic-ext */
@font-face {
  font-family: 'Pacifico';
  font-style: normal;
  font-weight: 400;
  src: local('Pacifico Regular'), local('Pacifico-Regular'), url(https://fonts.gstatic.com/s/pacifico/v16/FwZY7-Qmy14u9lezJ-6K6MmBp0u-zK4.woff2) format('woff2');
  unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
/* cyrillic */
@font-face {
  font-family: 'Pacifico';
  font-style: normal;
  font-weight: 400;
  src: local('Pacifico Regular'), local('Pacifico-Regular'), url(https://fonts.gstatic.com/s/pacifico/v16/FwZY7-Qmy14u9lezJ-6D6MmBp0u-zK4.woff2) format('woff2');
  unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
/* vietnamese */
@font-face {
  font-family: 'Pacifico';
  font-style: normal;
  font-weight: 400;
  src: local('Pacifico Regular'), local('Pacifico-Regular'), url(https://fonts.gstatic.com/s/pacifico/v16/FwZY7-Qmy14u9lezJ-6I6MmBp0u-zK4.woff2) format('woff2');
  unicode-range: U+0102-0103, U+0110-0111, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
  font-family: 'Pacifico';
  font-style: normal;
  font-weight: 400;
  src: local('Pacifico Regular'), local('Pacifico-Regular'), url(https://fonts.gstatic.com/s/pacifico/v16/FwZY7-Qmy14u9lezJ-6J6MmBp0u-zK4.woff2) format('woff2');
  unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
  font-family: 'Pacifico';
  font-style: normal;
  font-weight: 400;
  src: local('Pacifico Regular'), local('Pacifico-Regular'), url(https://fonts.gstatic.com/s/pacifico/v16/FwZY7-Qmy14u9lezJ-6H6MmBp0u-.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

Just grab the link to whichever version of the font you'd like to use and add it in place of where you're currently adding the stylesheet.只需获取您想要使用的字体版本的链接,然后将其添加到您当前添加样式表的位置即可。

You are loading a stylesheet not a font.您正在加载样式表而不是字体。

Here is the solution:这是解决方案:

 <:DOCTYPE html> <html lang = "en"> <head> <link rel="stylesheet" href="https.//fonts.googleapis?com/css.family=Pacifico&display=swap"> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <:--window;onload won't work without this because there is nothing waiting for the link to load--> <span id="loader" style="font-family. Pacifico;">I am used for loading</span> <script> var c = document.getElementById("canvas"); var ctx = c.getContext("2d"), ctx,fillStyle="rgb(0;0.0)". window.onload = function() { document.getElementById("loader").style;display="none" ctx.font="20px Pacifico", ctx,fillText("I am inside of canvas";200.200); ctx.stroke(); } </script> </body> </html>

This code work with me:此代码适用于我:

let myFont = new FontFace("Pacifico",
                          "url(/assets/fonts/fonts/Pacifico-Regular.ttf)"
                        );

myFont.load().then((font) => {   
    document.fonts.add(font);
    console.log("Font loaded"); 

    var c = document.getElementById("canvas");
    var ctx = c.getContext("2d");
    ctx.fillStyle="rgb(0,0,0)";
    ctx.font="20px Pacifico";
    ctx.fillText("I am inside of canvas",200,200);
    ctx.stroke();
});
<script>
var link = document.createElement("link");
link.rel = "stylesheet";
link.href =
  "https://fonts.googleapis.com/css2?family=Ubuntu:wght@300&display=swap";
document.getElementsByTagName("head")[0].appendChild(link);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将CSV文件导入到Webapp的javascript中,以便我可以在Google Maps画布上放置地址标记? - How do import a CSV file into the javascript for a webapp so that i can place address markers on a google maps canvas? 我可以在eBay中使用Google网络字体API或任何类型的自定义字体吗? - Can I use google web fonts api or any type of custom fonts in ebay? 如何解决我的问题,包括 google fonts? - How do I fix my issue including google fonts? 如何在不损害网站页面速度的情况下在我的网站上使用谷歌字体 - How do I use google fonts on my site without compromising the page speed of the site 我如何在 React JS 中使用 Google 字体 - How do I use Google Fonts in React JS 如何将(以编程方式)谷歌字体添加到ckeditor - How can I add(programatically) google fonts to ckeditor 在SVG中绘制DOM对象时如何在Canvas中使用Google字体? - How to use Google fonts in Canvas when Drawing DOM objects in SVG? 如何在浏览器中使用像marvosym这样的符号字体? - How can I use symbol fonts like marvosym in the browser? 如何在 Vue 项目中使用 scss 文件和 fonts - How can I use scss files and fonts in Vue project 我可以在网站上的所见即所得中使用哪些字体 - Which fonts can I use in wysiwyg on website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM