123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const gifEncoder = require('gifencoder');
- const Canvas = require('canvas');
- const fs = require('fs');
- const gifInfo = require('./config/gifInfo');
- var task = 'sorry';
- var target = gifInfo[task];
- var ctxW = target.width;
- var ctxH = target.height;
- var encoder = new gifEncoder(ctxW, ctxH);
- var startTime = new Date().getTime()
- console.log(`正在生成gif...`);
- encoder.createReadStream().pipe(fs.createWriteStream(`dist/${task}.gif`));
- encoder.start();
- encoder.setRepeat(0);
- encoder.setDelay(1);
- encoder.setQuality(10);
- var canvas = new Canvas(ctxW, ctxH);
- var ctx = canvas.getContext('2d');
- function drawText(ctx, text, x, y) {
- ctx.fillStyle = "#ffffff";
- ctx.font = "16px Arial";
- ctx.textAlign = "center";
- ctx.fillText(text, x, y)
- }
- var imgObj = new Canvas.Image()
- for (let i = 0; i <= target.size; i++) {
- imgObj.src = `res/${task}/image-${i}.png`
- ctx.drawImage(imgObj, 0, 0, ctxW, ctxH)
- target.textArr.forEach(n => {
- if(i >= n.arr[0] && i < n.arr[1]) {
- drawText(ctx, n.text, ctxW / 2, ctxH - 6)
- }
- })
- // ctx.fillStyle = '#ff0000';
- // ctx.fillRect(0, 0, ctxW, ctxH);
- encoder.addFrame(ctx)
- }
- encoder.finish()
- var endTime = new Date().getTime() - startTime;
- console.log(`成功生成,耗时:${endTime}ms`);
|