项目作者: fl3xice

项目描述 :
Gif Canvas creator
高级语言: TypeScript
项目地址: git://github.com/fl3xice/gifcanvas.git
创建时间: 2021-08-07T21:16:14Z
项目社区:https://github.com/fl3xice/gifcanvas

开源协议:GNU General Public License v3.0

下载


GifCanvas

Gif Canvas creator

npm
node-current
GitHub Repo stars

How to install

  1. npm i gif-gifcanvas
  1. yarn add gif-gifcanvas

Constructor

Param Type Default
canvas Canvas
options? delay: number
fps: number
repeat: number
0,60,0
Method Returns
addFrame() GifCanvas
end() Buffer
animation() GifCanvas

Example

Example gif

  1. const { GifCanvas } = require('gif-gifcanvas');
  2. const { createCanvas } = require("canvas");
  3. const { writeFile } = require('fs')
  4. const path = require("path");
  5. const canvas = createCanvas(250, 250);
  6. const gifcanvas = new GifCanvas(canvas);
  7. for (let i = 0; i < gifcanvas.canvas.width - 50; i+=3) {
  8. gifcanvas.addFrame(ctx => {
  9. ctx.fillStyle = '#050505';
  10. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  11. ctx.fillStyle = '#ffffff';
  12. ctx.fillRect(i, 0, 50, 50);
  13. });
  14. }
  15. for (let i = 0; i < gifcanvas.canvas.height - 50; i+=3) {
  16. gifcanvas.addFrame(ctx => {
  17. ctx.fillStyle = '#050505';
  18. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  19. ctx.fillStyle = '#ffffff';
  20. ctx.fillRect(200, i, 50, 50);
  21. });
  22. }
  23. for (let i = 200; i > 0; i-=3) {
  24. gifcanvas.addFrame(ctx => {
  25. ctx.fillStyle = '#050505';
  26. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  27. ctx.fillStyle = '#ffffff';
  28. ctx.fillRect(i, 200, 50, 50);
  29. });
  30. }
  31. for (let i = 200; i > 0; i-=3) {
  32. gifcanvas.addFrame(ctx => {
  33. ctx.fillStyle = '#050505';
  34. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  35. ctx.fillStyle = '#ffffff';
  36. ctx.fillRect(0, i, 50, 50);
  37. });
  38. }
  39. writeFile(path.join(__dirname, './example.gif'), gifcanvas.end(), (err) => {
  40. if (err) {
  41. console.error(err);
  42. }
  43. });