<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <img id="outputGif" src="" /> <img src="./ani/1.png" alt="" /> </body> </html>
<script src="./gif.js"></script>
<script> const images = [ ];
for (let index = 1; index <= 1; index++) { images.push(`./ani/${index}.png`); }
const fps = 10;
const loadImages = images.map((src) => { return new Promise((resolve, reject) => { const img = new Image(); img.onload = () => resolve(img); img.onerror = reject; img.src = src; }); });
Promise.all(loadImages) .then((images) => { const gif = new GIF({ workers: 2, quality: 10, background: "0x0000", transparent: "0x0000", });
images.forEach((img, index) => { const canvas = document.createElement("canvas"); const context = canvas.getContext("2d"); canvas.width = img.width; canvas.height = img.height;
context.drawImage(img, 0, 0);
const imageData = context.getImageData( 0, 0, canvas.width, canvas.height ); const data = imageData.data;
const mixRed = 79; const mixGreen = 83; const mixBlue = 215;
for (let i = 0; i < data.length; i += 4) { let alpha = data[i + 3] / 255; if (alpha > 0 && alpha < 1) { data[i] = data[i] * alpha + mixRed * (1 - alpha); data[i + 1] = data[i + 1] * alpha + mixGreen * (1 - alpha); data[i + 2] = data[i + 2] * alpha + mixBlue * (1 - alpha); data[i + 3] = 255; } }
context.putImageData(imageData, 0, 0);
gif.addFrame(canvas, { delay: 1000 / fps }); });
gif.on("finished", function (blob) { document.getElementById("outputGif").src = URL.createObjectURL(blob); });
gif.render(); }) .catch((error) => { console.error("Error loading images:", error); }); </script>
|