-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the easy-to-use api and refactor font registration
- Loading branch information
Showing
8 changed files
with
152 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import {registerFont, unregisterFont} from '../src/api.js'; | ||
import {fileURLToPath} from 'url'; | ||
import fs from 'fs'; | ||
import fs from 'node:fs'; | ||
|
||
export function registerFontAsset(filename: string) { | ||
const path = new URL(filename, import.meta.url); | ||
const array = new Uint8Array(fs.readFileSync(path)); | ||
registerFont(array, fileURLToPath(path)); | ||
const array = fs.readFileSync(path).buffer; | ||
registerFont(array, path); | ||
} | ||
|
||
export function unregisterFontAsset(filename: string) { | ||
const path = new URL(filename, import.meta.url); | ||
unregisterFont(fileURLToPath(path)); | ||
unregisterFont(path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {h, renderToCanvas, registerFont} from '../src/api.js'; | ||
import {createCanvas} from 'canvas'; | ||
import fs from 'node:fs'; | ||
|
||
// Register fonts before layout. This is a required step. | ||
// It is only async when you don't pass an ArrayBuffer | ||
await registerFont(new URL('../assets/Roboto/Roboto-Regular.ttf', import.meta.url)); | ||
|
||
// Always create styles at the top-level of your module if you can | ||
const divStyle = { | ||
backgroundColor: {r: 28, g: 10, b: 0, a: 1}, | ||
color: {r: 179, g: 200, b: 144, a: 1} | ||
}; | ||
|
||
// Since we're creating styles directly, colors have to be defined numerically | ||
const spanStyle = { | ||
color: {r: 115, g: 169, b: 173, a: 1}, | ||
fontWeight: 700 | ||
}; | ||
|
||
// Create a DOM | ||
const rootElement = h('div', {style: divStyle}, [ | ||
'Hello, ', | ||
h('span', {style: spanStyle}, ['World!']) | ||
]); | ||
|
||
// Layout and paint into the entire canvas (see also renderToCanvasContext) | ||
const canvas = createCanvas(200, 50); | ||
renderToCanvas(rootElement, canvas, /* optional density: */ 2); | ||
|
||
// Save your image | ||
canvas.createPNGStream().pipe(fs.createWriteStream(new URL('hello.png', import.meta.url))); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters