Skip to content

Commit 0bcc481

Browse files
authored
Merge pull request #152 from markh182/preview-gif
added gif preview image
2 parents 5f89702 + 27f262e commit 0bcc481

3 files changed

Lines changed: 106 additions & 1 deletion

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"rarity": "node utils/rarity.js",
1818
"preview": "node utils/preview.js",
1919
"pixelate": "node utils/pixelate.js",
20-
"update_info": "node utils/update_info.js"
20+
"update_info": "node utils/update_info.js",
21+
"preview_gif": "node utils/preview_gif.js"
2122
},
2223
"author": "Daniel Eugene Botha (HashLips)",
2324
"license": "MIT",

src/config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ const preview = {
9494
imageName: "preview.png",
9595
};
9696

97+
const preview_gif = {
98+
numberOfImages: 5,
99+
order: 'ASC', // ASC, DESC, MIXED
100+
repeat: 0,
101+
quality: 100,
102+
delay: 500,
103+
imageName: "preview.gif",
104+
};
105+
97106
module.exports = {
98107
format,
99108
baseUri,
@@ -112,4 +121,5 @@ module.exports = {
112121
network,
113122
solanaMetadata,
114123
gif,
124+
preview_gif,
115125
};

utils/preview_gif.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"use strict";
2+
3+
const isLocal = typeof process.pkg === "undefined";
4+
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
5+
const fs = require("fs");
6+
const path = require("path");
7+
const { createCanvas, loadImage } = require("canvas");
8+
const buildDir = `${basePath}/build`;
9+
const imageDir = path.join(buildDir, "/images");
10+
const { format, preview_gif, } = require(path.join(basePath, "/src/config.js"));
11+
const canvas = createCanvas(format.width, format.height);
12+
const ctx = canvas.getContext("2d");
13+
14+
const HashlipsGiffer = require(path.join(
15+
basePath,
16+
"/modules/HashlipsGiffer.js"
17+
));
18+
let hashlipsGiffer = null;
19+
20+
const loadImg = async (_img) => {
21+
return new Promise(async (resolve) => {
22+
const loadedImage = await loadImage(`${_img}`);
23+
resolve({ loadedImage: loadedImage });
24+
});
25+
};
26+
27+
// read image paths
28+
const imageList = [];
29+
const rawdata = fs.readdirSync(imageDir).forEach(file => {
30+
imageList.push(loadImg(`${imageDir}/${file}`));
31+
});
32+
33+
const saveProjectPreviewGIF = async (_data) => {
34+
// Extract from preview config
35+
const { numberOfImages, order, repeat, quality, delay, imageName } = preview_gif;
36+
// Extract from format config
37+
const { width, height } = format;
38+
// Prepare canvas
39+
const previewCanvasWidth = width;
40+
const previewCanvasHeight = height;
41+
42+
if(_data.length<numberOfImages) {
43+
console.log(
44+
`You do not have enough images to create a gif with ${numberOfImages} images.`
45+
);
46+
}
47+
else {
48+
// Shout from the mountain tops
49+
console.log(
50+
`Preparing a ${previewCanvasWidth}x${previewCanvasHeight} project preview with ${_data.length} images.`
51+
);
52+
const previewPath = `${buildDir}/${imageName}`;
53+
54+
ctx.clearRect(0, 0, width, height);
55+
56+
hashlipsGiffer = new HashlipsGiffer(
57+
canvas,
58+
ctx,
59+
`${previewPath}`,
60+
repeat,
61+
quality,
62+
delay
63+
);
64+
hashlipsGiffer.start();
65+
66+
await Promise.all(_data).then((renderObjectArray) => {
67+
// Determin the order of the Images before creating the gif
68+
if(order == 'ASC') {
69+
// Do nothing
70+
}
71+
else if(order == 'DESC') {
72+
renderObjectArray.reverse();
73+
}
74+
else if(order == 'MIXED') {
75+
renderObjectArray = renderObjectArray.sort(() => Math.random() - 0.5);
76+
}
77+
78+
// Reduce the size of the array of Images to the desired amount
79+
if(parseInt(numberOfImages)>0) {
80+
renderObjectArray = renderObjectArray.slice(0, numberOfImages);
81+
}
82+
83+
renderObjectArray.forEach((renderObject, index) => {
84+
ctx.globalAlpha = 1;
85+
ctx.globalCompositeOperation = 'source-over';
86+
ctx.drawImage(renderObject.loadedImage, 0, 0, previewCanvasWidth, previewCanvasHeight);
87+
hashlipsGiffer.add();
88+
});
89+
});
90+
hashlipsGiffer.stop();
91+
}
92+
};
93+
94+
saveProjectPreviewGIF(imageList);

0 commit comments

Comments
 (0)