gpt4 book ai didi

javascript - PixiJS : Load custom font

转载 作者:行者123 更新时间:2023-11-30 11:31:03 24 4
gpt4 key购买 nike

我目前正在学习 Pixijs。我看了这个人的教程:github.com/kittykatattack/learningPixi顺便说一句,不错。

环境:Xampp、Firefox、Pixijs、HTML、CSS

现在我尝试加载自定义字体。第一次加载我的 Pixijs 元素应该运行的页面时,字体没有显示,控制台显示一些错误消息......第二次加载页面(不删除缓存)时,文本显示为给定的字体!

为什么?!

以下是加载字体的片段:

index.html:

<head>
<meta charset='UTF-8' />
<style>
@font-face{
font-family: "FFFForward";
src: url("assets/fonts/fffforward.TTF");
}

* {padding: 0; margin: 0}
</style>

app.js:

function DrawText(){

PointsTopText = new Text(
"P1: " + PointsTop,
{fontFamily: 'FFFForward, Arial', fontSize: 32, fill: 'white'}
);
PointsBotText = new Text(
"Cpu: " + PointsBot,
{fontFamily: 'FFFForward, Arial', fontSize: 32, fill: 'white'}
);

PointsTopText.position.set(0, Renderer.height / 2 - 32 * 2);
PointsBotText.position.set(0, Renderer.height / 2);

World.addChild(PointsTopText);
World.addChild(PointsBotText);
}

这里是控制台的错误日志:

控制台:

downloadable font: bad search range (font-family: "FFFForward" style:normal weight:normal stretch:normal src index:0) source: [path]/assets/fonts/fffforward.TTF  PixiJs:6:14
downloadable font: bad range shift (font-family: "FFFForward" style:normal weight:normal stretch:normal src index:0) source: [path]/assets/fonts/fffforward.TTF PixiJs:6:14
downloadable font: cmap: bad id_range_offset (font-family: "FFFForward" style:normal weight:normal stretch:normal src index:0) source: [path]/assets/fonts/fffforward.TTF PixiJs:6:14
downloadable font: hdmx: the table should not be present when bit 2 and 4 of the head->flags are not set (font-family: "FFFForward" style:normal weight:normal stretch:normal src index:0) source: [path]/assets/fonts/fffforward.TTF PixiJs:6:14
downloadable font: hdmx: Table discarded (font-family: "FFFForward" style:normal weight:normal stretch:normal src index:0) source: [path]/assets/fonts/fffforward.TTF PixiJs:6:14

正如我所说,此消息仅在我第一次加载我的页面时出现。第二次一切正常。

我该怎么做才能避免这种情况?

为什么会这样?

这是什么意思?

最佳答案

使用 webfontloader - https://github.com/typekit/webfontloader - 在 PixiJS 中进行任何绘图之前同步加载字体。这也适用于其他基于 Canvas 的应用。

将以下内容放入 HTML 页面的 部分:

<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
<script>
WebFont.load({
google: {
families: ['Press Start 2P']
},
active:e=>{
console.log("font loaded!");
// now start setting up your PixiJS (or canvas) stuff!
}
});
</script>

关于javascript - PixiJS : Load custom font,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46218500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com