Typescript library written to draw BitmapFont into node-canvas and html canvas.
npm install node-pixel-font
npm install --save node-pixel-font
`Node example
Include the code
`typescript
import { NodePixelFont } from 'node-pixel-font/lib/node-pixel-font'
const font = new NodePixelFont()
await font.loadFont('path/to/png-file.png', 'path/to/xml-file.xml')
font.draw(canvas, 'Hello World !', 10, 20, '#00FF00')
`HTML example
Include the code
`typescript
import { HTMLPixelFont } from 'node-pixel-font/lib/html-pixel-font'
const font = new HTMLPixelFont()
await font.loadFont('path/to/png-file.png', 'path/to/xml-file.xml')
font.draw(canvas, 'Hello World !', 10, 20, '#00FF00')
`API
loadFont()
`typescript
loadFont(pngFilePath: string, xmlFilePath: string): Promise
`load the font in memory.
draw() needs to be called after awaiting on loadFont()draw()
`typescript
draw(canvas: Canvas, text: string, x: number | HorizontalAlignment, y: number | VerticalAlignment, color: string = '#FFFFFF', scale: number = 1, rect?: Rect): void
`Draw the specified text into the provided canvas. Can specify the destination X and Y of the text, as well as color and scale factor. The last args rect is to align within smaller boundaries inside the canvas.
Alignment
You can align the text manually, by providing x, y as pixel number, or by providing either (or both) x and y as alignment using HorizontalAlignment and VerticalAlignment`typescript
import { NodePixelFont, HorizontalAlignment, VerticalAlignment } from 'node-pixel-font'
import { createCanvas } from 'canvas'
const canvas = createCanvas(64, 32)
const pixelFont = new NodePixelFont()
pixelFont.loadFont('path/to/file.png', 'path/to/file.xml')
pixelFont.draw(canvas, 'some text', HorizontalAlignment.Center, VerticalAlignment.Middle, '#FF0000')
``