build.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const gifEncoder = require('gifencoder');
  2. const Canvas = require('canvas');
  3. const fs = require('fs');
  4. const gifInfo = require('./config/gifInfo');
  5. var task = 'sorry';
  6. var target = gifInfo[task];
  7. var ctxW = target.width;
  8. var ctxH = target.height;
  9. var encoder = new gifEncoder(ctxW, ctxH);
  10. var startTime = new Date().getTime()
  11. console.log(`正在生成gif...`);
  12. encoder.createReadStream().pipe(fs.createWriteStream(`dist/${task}.gif`));
  13. encoder.start();
  14. encoder.setRepeat(0);
  15. encoder.setDelay(1);
  16. encoder.setQuality(10);
  17. var canvas = new Canvas(ctxW, ctxH);
  18. var ctx = canvas.getContext('2d');
  19. function drawText(ctx, text, x, y) {
  20. ctx.fillStyle = "#ffffff";
  21. ctx.font = "16px Arial";
  22. ctx.textAlign = "center";
  23. ctx.fillText(text, x, y)
  24. }
  25. var imgObj = new Canvas.Image()
  26. for (let i = 0; i <= target.size; i++) {
  27. imgObj.src = `res/${task}/image-${i}.png`
  28. ctx.drawImage(imgObj, 0, 0, ctxW, ctxH)
  29. target.textArr.forEach(n => {
  30. if(i >= n.arr[0] && i < n.arr[1]) {
  31. drawText(ctx, n.text, ctxW / 2, ctxH - 6)
  32. }
  33. })
  34. // ctx.fillStyle = '#ff0000';
  35. // ctx.fillRect(0, 0, ctxW, ctxH);
  36. encoder.addFrame(ctx)
  37. }
  38. encoder.finish()
  39. var endTime = new Date().getTime() - startTime;
  40. console.log(`成功生成,耗时:${endTime}ms`);