简体   繁体   English

导入的字体在 Chrome 中有效,但在 Safari 中无效

[英]Imported Font Works In Chrome but not Safari

I have been testing my website in Chrome and have everything working as expected, but I just found out my font does not get imported in Safari and the default font is used instead of the one I'm supposed to be using.我一直在 Chrome 中测试我的网站,并且一切都按预期工作,但我刚刚发现我的字体没有在 Safari 中导入,并且使用了默认字体而不是我应该使用的字体。 I included a snippet showing how I'm importing and using the font - maybe I need to access the font in a different way to have it available to use in both browsers?我包含了一个片段,显示了我如何导入和使用字体 - 也许我需要以不同的方式访问字体才能让它在两个浏览器中都可用?

 const context = document.querySelector("canvas").getContext("2d"); context.width = document.body.clientWidth; context.height = document.body.clientHeight; context.beginPath(); context.font = "10vw Montserrat"; context.fillStyle = "red"; context.textAlign="center"; context.textBaseline = "middle"; context.fillText("XXXXXX", context.width/2, context.height/2); context.closePath();
 @import url('https://fonts.googleapis.com/css?family=Montserrat:900i&display=swap');
 <canvas class="game"></canvas>

A webfont will only be loaded if it's actually used in your DOM.只有在您的 DOM 中实际使用了 webfont 时才会加载它。
You can check this in chromes dev tool network tab.您可以在 chromes 开发工具网络选项卡中检查这一点。
(I assume it works for you in Chrome, as you may have "Montserrat" installed locally). (我认为它适用于 Chrome,因为您可能在本地安装了“Montserrat”)。

To ensure the font is loaded, you could load it via js FontFace() method.为确保字体已加载,您可以通过 js FontFace()方法加载它。

Working example工作示例

 const canvas = document.querySelector("canvas"); const context = canvas.getContext("2d"); context.width = document.body.clientWidth; context.height = document.body.clientHeight; let fontUrl = "https://fonts.gstatic.com/s/montserrat/v24/JTUFjIg1_i6t8kCHKm459Wx7xQYXK0vOoz6jqw16WXh0pg.woff2"; let fontFamily = "Montserrat"; let fontOptions = { weight: 900, style: "italic" }; loadFonts(fontFamily, fontUrl, fontOptions); async function loadFonts(fontFamily, fontUrl, fontOptions) { const font = new FontFace(fontFamily, `url(${fontUrl})`); font.weight = fontOptions.weight ? fontOptions.weight : 400; font.style = fontOptions.style ? fontOptions.style : "normal"; // wait for font to be loaded await font.load(); document.fonts.add(font); // apply font styles to canvas canvas.classList.add("fonts-loaded"); canvas.setAttribute( "style", `font-family:${fontFamily}; font-weight:${fontOptions.weight}; font-style:${fontOptions.style}` ); drawText(); } function drawText() { context.font = "48px Montserrat"; context.fontWeight = 900; context.fontStyle = "italic"; context.fillStyle = "red"; context.textAlign = "center"; context.textBaseline = "middle"; context.fillText("Hamburg", 150, 50); }
 canvas { height: 50vh; border: 1px solid red } .fonts-loaded { border: 1px solid green; }
 <canvas class="game" />

  1. We're loading the font file in an async function我们正在异步函数中加载字体文件
    (just open the google font css (eg https://fonts.googleapis.com/css?family=Montserrat:900i ) to get the actual font file url) (只需打开 google 字体 css(例如https://fonts.googleapis.com/css?family=Montserrat:900i )以获取实际的字体文件 url)
  2. Once the font is loaded we're applying the desired font style properties to the canvas element by setting an inline style (you could also use a class)加载字体后,我们通过设置内联样式将所需的字体样式属性应用于canvas元素(您也可以使用类)
  3. Now we can draw the text on canvas现在我们可以在画布上绘制文本

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM