我尝试使用带有自定义字母/单词间距的node-canvas渲染一些文本失败了。有人设法做到了吗?
let fs = require("fs"),
path = require("path"),
{ createCanvas, registerFont } = require("canvas");
async function loadFont() {
await registerFont(
path.join(__dirname, "assets", "fonts", "Grottel_Light.ttf"),
{
family: "Grottel Light"
}
);
}
function generateText(text, size, width, height) {
let canvas = createCanvas(),
ctx = canvas.getContext("2d"),
stream = canvas.createPNGStream();
ctx.font = `${size}px "Grottel Light"`;
ctx.canvas.width = width;
ctx.canvas.height = height;
ctx.fillText(text, 0, size);
stream.pipe(fs.createWriteStream(path.join(__dirname, "text.png")));
}
loadFont()
.then(() => {
generateText("Hello canvas text", 60, 1920, 1080);
})
.catch(err => console.log(err));