简体   繁体   English

Pixi.js中的自定义字体

[英]Custom font in Pixi.js

I try to load a custom font into Pixi.js (2D WebGL framework). 我尝试将自定义字体加载到Pixi.js(2D WebGL框架)中。

They have an example using .woff google fonts: 他们有一个使用.woff谷歌字体的例子:

https://github.com/GoodBoyDigital/pixi.js/tree/master/examples/example%2010%20-%20Text https://github.com/GoodBoyDigital/pixi.js/tree/master/examples/example%2010%20-%20Text

I converted my .ttf to .woff and added in css: 我将.ttf转换为.woff并添加到css中:

@font-face
{
    font-family: "HU_Adrien";
    src: local('HU_Adrien'), url('HU_A0046.woff') format('woff');;
}

div.font{
    font-family: "HU_Adrien";
    color: white;
}

It shows in my div but not in my Pixi stage. 它显示在我的div中但不在我的Pixi阶段。

...
    // create a text object with a nice stroke
    var spinningText = new PIXI.Text("I'm fun!", {font: "160px HU_Adrien", fill: "#cc00ff", align: "center", stroke: "#FFFFFF", strokeThickness: 6});
    // setting the anchor point to 0.5 will center align the text... great for spinning!
    spinningText.anchor.x = spinningText.anchor.y = 0.5;
    spinningText.position.x = 620 / 2;
    spinningText.position.y = 400 / 2;
...

You need to convert HU_A0046.woff with this tool to HU_A0046.XML and add the file name to the preload list. 您需要将HU_A0046.woff此工具一起转换为HU_A0046.XML ,并将文件名添加到预加载列表中。 Then you should call PIXI.Bitmaptext 然后你应该调用PIXI.Bitmaptext

Example: 例:

...
var loader = new PIXI.AssetLoader(/*more media...*/, ["HU_A0046.xml"]); 
var spinningText = new PIXI.BitmapText("I'm fun!", { font: "35px FontName"}); //You can see the font name within the XML
spinningText.position.x = 620 / 2;
spinningText.position.y = 400 / 2;
...

In order to use a custom web font with PIXI you need to use the google web font api with it you can use web fonts from Google and other cdns as well as your own local web fonts you can read more at https://github.com/typekit/webfontloader 要使用PIXI自定义Web字体,您需要使用谷歌网络字体API,您可以使用谷歌和其他cdns的网络字体以及您自己的本地网络字体,您可以在https:// github上阅读更多内容。 COM / typekit / webfontloader

<script>
  WebFontConfig = {
    typekit: { id: 'xxxxxx' },
    google: {
        families: ['Droid Sans', 'Droid Serif']
    },
    custom: {
        families: ['My Font', 'My Other Font:n4,i4,n7'],
        urls: ['/fonts.css']
    },

    active: function() {
      // do something
      init();
    }
  },

  active: function() {
    // do something
    init();
  }
  };

  (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
              '://ajax.googleapis.com/ajax/libs/webfont/1.5.6/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
  })();
</script>

In this example, the fonts.css file might look something like this: 在此示例中,fonts.css文件可能如下所示:

@font-face {
  font-family: 'My Font';
  src: ...;
}
@font-face {
  font-family: 'My Other Font';
  font-style: normal;
  font-weight: normal; /* or 400 */
  src: ...;
}
@font-face {
  font-family: 'My Other Font';
  font-style: italic;
  font-weight: normal; /* or 400 */
  src: ...;
}
@font-face {
  font-family: 'My Other Font';
  font-style: normal;
  font-weight: bold; /* or 700 */
  src: ...;
}

*it's important to start using the font after they are rendered *在渲染后开始使用字体很重要

*convert your font with a tool like: http://www.font2web.com/ *使用以下工具转换您的字体: http//www.font2web.com/

*it will give you the needed web font files as well as the css code for your custom fonts *它将为您提供所需的Web字体文件以及自定义字体的css代码

It worked in my pixi 4 app after installing the .woff file (no conversion or bitmap text required) and adding 安装.woff文件(无需转换或位图文本)并添加后,它在我的pixi 4应用程序中工作

font: "24px Open Sans Regular"

to my pixi style definition. 我的pixi风格定义。

fonts.css fonts.css

@font-face {
    font-family: 'Open Sans Regular';
    src: url('/font/OpenSans-Regular.woff');
     /* format("woff"); // it never worked with a format */
}

app.js app.js

var WebFont = require("webfontloader");
let WebFontConfig = {
    custom: {
        families: ['Open Sans:300,400italic,500italic,600italic,700italic,400,500,600,700'],
        urls: ['/css/fonts.css']
      }
  };
  WebFont.load(WebFontConfig);

webpack.config.js webpack.config.js

module.exports = {
    entry: {
        main: "./src/app.ts",
        // vendor: [
        //     "webfontloader"
        // ]
    },
    output: {
        filename: "./bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        alias: {
            "webfontloader": path.resolve(__dirname, "./node_modules/webfontloader/webfontloader.js")
        },
        // Add '.ts' as resolvable extensions.
        extensions: [".ts", ".js"]
    }, …

Make sure to install 一定要安装

npm install webfontloader --save
npm i -D @types/webpack-env

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

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