node-canvas

时间:2023-03-08 16:45:35
node-canvas

1、使用之前需要预先安装  Cairo

  本人安装遇到各种各样的坑,可以参考这里来填坑:https://github.com/Automattic/node-canvas/wiki/Installation---OSX

2、npm install canvas

  安装过程遇到问题,参照第一条填坑的方式继续。。。

3、安装中文字体包,我用的是微软雅黑的字体

  linux 与 mac 字体包的安装方式不同,具体可搜索一下使用方式

4、终于可以愉快编码了

var Canvas = require('canvas')
, Image = Canvas.Image
, canvas = new Canvas(500, 200)
,fs = require("fs")
, ctx = canvas.getContext('2d'); ctx.font = '30px "Microsoft YaHei"';
ctx.rotate(.1);
ctx.fillText("我写程序生成100万个!", 50, 100);
ctx.fillText("怕不怕", 50, 150); var te = ctx.measureText('Awesome!');
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
ctx.beginPath();
ctx.lineTo(50, 102);
ctx.lineTo(50 + te.width, 102);
ctx.stroke(); fs.writeFile('out.png', canvas.toBuffer());

生成的图片:

node-canvas