节点画布文本和单词间距

时间:2018-01-15 20:23:00

标签: javascript node.js canvas

我尝试使用带有自定义字母/单词间距的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));

0 个答案:

没有答案