node-canvas 文字显示出错

var http = require('http');
var fs = require('fs');
var url = require('url');
var path = require('path')
var querystring = require("querystring");
const { createCanvas } = require('canvas');

var createCavas = function(data,fileName){
   
   const canvas = createCanvas(240, 600);
   const ctx = canvas.getContext('2d');
   var rectH=20;
   var rectW=60;
   ctx.lineWidth = 1;
   ctx.strokeStyle = "#ccc";
   ctx.font = "12px Impact";
   if(data.length>31){
      data = data.slice(0,30)
   }
   for(var i= 0;i<data.length;i++){
      ctx.fillText(i,30,rectH*i-5); 
      ctx.fillText(data[i].nickName,90,rectH*i-5); 
      ctx.fillText(data[i].userWinLoseMoney,150,rectH*i-5);
      ctx.fillText(data[i].balance,210,rectH*i-5);

      ctx.moveTo(rectW*i,0);
      ctx.lineTo(rectW*i,canvas.height);

      ctx.moveTo(0,rectH*i);
      ctx.lineTo(canvas.width,rectH*i);
      ctx.textAlign = "center";
   }
   ctx.stroke();
   canvas.createPNGStream().pipe(fs.createWriteStream(path.join(__dirname, fileName + '.png')))
}

// 创建服务器
http.createServer( function (request, response) {  
   // 解析请求,包括文件名
   var pathname = url.parse(request.url).pathname;
   var postData = "";
   // 数据块接收中
    request.on("data", function (postDataChunk) {
        postData += postDataChunk;
    });
    request.on("end", function () {
         console.log('数据接收完毕');
         var params = JSON.parse(postData);//querystring.parse(postData)
         var data = params.data;

         var fileName = 'userscore__'+params.table+'__'+params.ShoeNo+'__'+params.GameNo+'__'+params.groupId; 
         createCavas(data,fileName)
   });

   // 从文件系统中读取请求的文件内容
   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
      }else{             
         response.write(data);        
      }
      //  发送响应数据
      response.end();
   });   
}).listen(8080);
 
console.log('Server running at http://127.0.0.1:8080/');

图片描述

阅读 2k
1 个回答

docker容器内没安装字体导致

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题