Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GIF图片分解和合成 #27

Open
hankliu62 opened this issue May 24, 2024 · 0 comments
Open

GIF图片分解和合成 #27

hankliu62 opened this issue May 24, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@hankliu62
Copy link
Owner

案例

分解GIF

import SuperGif from 'libgif'

var file = e.target.files[0];
 console.log(file.type.indexOf('image/gif'));
 load_gif(file);
function load_gif(gif_source) {
  var gifImg = document.getElementById('gifImg');
  // gif库需要img标签配置下面两个属性
  gifImg.setAttribute('rel:animated_src', URL.createObjectURL(gif_source))
  gifImg.setAttribute('rel:auto_play', '0')
  
  // 新建gif实例
  var rub = new SuperGif({ gif: gifImg } );
  
  var running = false;
  rub.load(function(){
  var img_list = [];
  var j = 0;
  console.log(rub.get_length());
  for (var i=1; i <= rub.get_length(); i++) {
  // 遍历gif实例的每一帧
  rub.move_to(i);
  
  var imgImage = new Image();
  //canvas生成base64图片数据
  imgImage.src = rub.get_canvas().toDataURL('image/jpeg',0.8);
  }
 }

图片合成

import GIF from 'gif.js'
var gif = new GIF({
  workers: 2,
  quality: 10,
  workerScript: 'js/gif.worker.js'
});
var j = 0;
  
var canvas= document.createElement("canvas");
var ctx = tCanvas.getContext('2d');
  
for (var i=1; i <= len; i++) {
  
 var imgImage = new Image();
 imgImage.src = img;
 imgImage.onload = function (e) {
  
 //Canvas绘制图片
 canvas.width = width;
 canvas.height = height;
 console.log(width,height)
 //铺底色
 ctx.fillStyle = "#fff";
 ctx.fillRect(0, 0, canvas.width, canvas.height);
 ctx.drawImage(imgImage, 0, 0, width, height);
  
  
 gif.addFrame(canvas,{copy:true,delay:50});
 j++;
 //图片
 if(j>=len) {
 gif.render()
 };
 }
}
gif.on('finished', function(blob) {
 //生成图片链接
  var url = URL.createObjectURL(blob);
}
@hankliu62 hankliu62 self-assigned this May 24, 2024
@hankliu62 hankliu62 added the enhancement New feature or request label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant