Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
Z

zfont

> 编程语言
Open source

Text plugin for Zdog - works with any .ttf font!

194 stars0 likes1 views
WebsiteGitHub

About

Text plugin for Zdog - works with any .ttf font!

Zfont

A text plugin for the Zdog 3D engine! Renders TrueType fonts via Typr.js | jaames.github.io/zfont

Features | Caveats | Demo | Installation | Usage | API | Zdog.Font | Zdog.Text | Zdog.TextGroup | Todo | Building

Features

  • Built on top of Typr.js, which supports a wide range of .ttf and .otf fonts with speed and grace
  • Less than 14kB minified and gzipped
  • No need to worry about waiting for fonts to load; text automatically pops into existence once the font is ready
  • Includes support for multiline text
  • Update font, text, color, alignment, etc at any time
  • Bonus utilities for measuring text, waiting for font load & more!

Caveats

  • You have to provide a .ttf to use yourself; it isn't possible to use system fonts
  • Character range is limited to whichever glyphs are supported by your chosen font, and font stacks/fallbacks aren't supported yet

Demo

A live demo can be found here, there's also some more in-depth examples on Codepen!

Installation

Install with NPM

bash
$ npm install zfont --save

If you are using a module bundler like Webpack or Rollup, import Zfont into your project:

javascript
// Using ES6 module syntax
import Zfont from 'zfont';

// Using CommonJS modules
const Zfont = require('zfont');

Using the jsDelivr CDN

xml

When manually including the library like this, it will be globally available on window.Zfont

Download and Host Yourself

Development version

Uncompressed at around 75kB, with source comments included

Production version

Minified to 45kB

Then add it to the `` of your page with a `


## Usage

### Register Plugin

After both Zdog and Zfont have been imported/downloaded, we need to initialize the Zfont plugin. Once it's initialized, the `Zdog.Font`, `Zdog.Text` and `Zdog.TextGroup` classes will be available:

```js
Zfont.init(Zdog);

Hello World

(Pssst! If you prefer to dive in, check out the basic demo over on Codepen)

To draw some text in a Zdog scene, first we need to set up a new Zdog.Font object with the .ttf url for our desired font, then we can create a new Zdog.Text object and add it to the illustration like any other shape:

javascript
// Initialize Zfont
Zfont.init(Zdog);

// Create a Zdog illustration
let illo = new Zdog.Illustration({
  element: '.zdog-canvas'
});

// Set up a font to use
let myFont = new Zdog.Font({
  src: './path/to/font.ttf'
});

// Create a text object
// This is just a Zdog.Shape object with a couple of extra parameters!
new Zdog.Text({
  addTo: illo,
  font: myFont,
  value: 'Hey, Zdog!',
  fontSize: 64,
  color: '#fff'
});

// Animation loop
function animate() {
  illo.updateRenderGraph();
  requestAnimationFrame(animate);
}
animate();

Multiline Text

Both Zdog.Text and Zdog.TextGroup support multiline text, by inserting a newline character (\n) wherever you wish to add a line break:

javascript
new Zdog.Text({
  ...
  value: 'The quick brown fox\njumps over the\nlazy zdog',
});

For better readability you may prefer to use an array of strings for the value option. In this case, each string in the array will be treated as a seperate line of text:

javascript
new Zdog.Text({
  ...
  value: [
    'The quick brown fox'
    'jumps over the',
    'lazy zdog'
  ]
});

Waiting for Fonts to Load

In most cases you don't have to worry about waiting for fonts to load, as text objects will magically pop into existence once their font is ready to use. However, the plugin also provides a Zdog.waitForFonts() utility function if you need to delay anything until all the fonts in your scene have finished loading.

For example, let's modify the animation loop from the previous example so that it doesn't begin until the fonts are ready:

javascript
// Animation loop
function animate() {
  illo.updateRenderGraph();
  requestAnimationFrame(animate);
}
// Zdog.waitForFonts() returns a Promise which is resolved once all the fonts added to the scene so far have been loaded
Zdog.waitForFonts().then(() => {
  // Once the fonts are done, start the animation loop
  animate();
})

API

Zdog.Font

Represents a font that can be used by an instance of either Zdog.Text or Zdog.TextGroup.

javascript
let font = new Zdog.Font({
  src: './path/to/font.ttf'
})

…

js
new Zdog.Text({
  addTo: illo,
  font: font,
  value: 'Hey, Zdog!',
  textAlign: 'center',
  textBaseline: 'middle',
  color: '#5222ee',
  stroke: 1,
})

…

js
new Zdog.TextGroup({
  addTo: illo,
  font: font,
  value: 'Hey, Zdog!',
  textAlign: 'center',
  color: '#5222ee',
  stroke: 2,
})

…

js
Zdog.waitForFonts().then(function() {
  // Do something once the font is ready
}

Todo

  • Google Fonts & Typekit integration?
  • Support for different text directions, e.g. right-to-left
  • Support for fallback fonts
  • Support for color (SVG) fonts

Building

Install Dependencies with NPM

bash
$ npm install

Run Devserver

bash
$ npm start

Build production files

bash
$ npm run build

2019 James Daniel

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript3dcanvasfontsvg

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言