node-canvas模块使用

node-canvas模块是在node中实现canvas渲染的一种方式。可结合Echarts等应用于服务端生成图片流,进行图片处理等,如导出word或pdf时根据数据动态生成图片并嵌入文档中。下面将以Echarts为例,由服务端生成图片。

1. node-canvas安装

安装方式与npm包一致:

npm install canvas // or yarn add canvas

但是,node-canvas包依赖于Cairo等,需要先在系统中安装相关依赖。

OSCommand
OS Xbrew install pkg-config cairo libpng jpeg giflib
Ubuntusudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
Fedorasudo yum install cairo cairo-devel cairomm-devel libjpeg-turbo-devel pango pango-devel pangomm pangomm-devel giflib-devel
Solarispkgin install cairo pkg-config xproto renderproto kbproto xextproto

对于El Capitan用户而言,在安装相关依赖时可能会出错,还需要执行xcode-select --install。同时,使用brew安装时,会提示/usr/local/var无权限创建的问题,可以通过如下命令解决:

mkdir /usr/local/var
sudo chmod -R 777 /usr/local/var

Linux下安装时可能会出现由于gcc、g++版本过低而无法编译node-canvas的问题,此时需要升级gcc、g++,升级办法详见此处。GCC中国镜像可以使用中科院镜像

2. Echarts生成图片

首先,需要安装Echarts包:

npm install echarts // or yarn add echarts

由于node-canvas默认不支持中文字体,所以需要导入中文字体文件,以华文仿宋为例,下载对应的ttf文件至项目目录,以便后续导入canvas。

由于项目后台使用Egg.js框架,所以通过extend的方式扩展application,相应代码如下:

// app/extend/application.js
'use strict';

const path = require('path');
const Canvas = require('canvas');
const echarts = require('echarts');
const fs = require('fs');

module.exports = {
  generateImage (options, savePath, size) {
    return new Promise((resolve, reject) => {
      const canvas = new Canvas(parseInt(size.width,10), parseInt(size.height,10));
      const font = new Canvas.Font('华文仿宋', path.join(__dirname, '华文仿宋.ttf'));
      const ctx = canvas.getContext('2d');
      ctx.addFont(font);
      ctx.font = '12px 华文仿宋';

      echarts.setCanvasCreator(function () {
        return canvas;
      });
      const chart = echarts.init(canvas);
      options.animation = false;
      options.textStyle = {
        fontFamily: '华文仿宋',
        fontSize: 12,
      };
      chart.setOption(options);
      try {
        fs.writeFileSync(savePath, chart.getDom().toBuffer());
        console.log("Create Img:" + savePath);
      } catch (err){
        console.error("Error: Write File failed" + err.message);
      }
      resolve();
    })
  }
}

通过此种方式即可在savePath目录生成相应的图片文件。对于图片流(base64编码)而言,则只需要将chart.getDom().toBuffer()改成chart.getDom().toBuffer().toString('base64'),并直接resolve即可。

The complete Visual Behaviour Authoring framework for Unity, empowering you to create advanced AI Behaviours and Logic, including three separate, fully featured, yet seamlessly interchangeable modules for you to choose and easily add in your game: • Behaviour Trees • Hierarchical State Machines • Dialogue Trees NodeCanvas is a production ready framework used by many awesome games including Kim, Pamela, Hurtworld, Shroud of the Avatar, Kingdom and Ghost of a Tale. [Games Showcase] Feature Highlights ------------------------------------- • Choose the right tool for the task at hand. • Design reactive Behaviour Trees and Hierarchical State Machines within an intuitive, designer friendly visual node editor: (Full Undo/Redo, Zoom In/Out, Multi-selection, Duplication, Copy/Pasting, JSON Import/Export, Groups, Comments and more) • Use local & global variables of any type, visually or in code, for creating reusable and actor-oriented, parametric behaviours, optionally saving and loading those variables between gaming sessions. • Data Bind variables with any component property of any type directly. • Sync variables automatically over the network using UNET, for creating multiplayer games. • Visually Debug behaviours with realtime, colorful and informative runtime debugging. • Live Edit everything while in play mode to perfectly understand your design goals and how to achieve them. • Utilize any existing code directly with advanced and extremely fast Reflection Tasks, automatically integrating Unity's and 3rd Party asset APIs. • Work with Lists/Arrays, Enums, Interfaces and pretty much ANY Variable Type you need out-of-the-box. • React to world changes and transfer data using the built-in Event System. • Reuse and Bind made behaviours among any number of different agents. • Organize your designs using Behaviour Sub-Trees and Sub-State Machines. • Extend NodeCanvas Framework to create your own Actions, Conditions, Nodes or even completely new modules with the e
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值