Skip to content

Commit 8890c4d

Browse files
authored
Merge pull request #146 from HashLips/dev
Added gif export
2 parents 4149f26 + 5f89702 commit 8890c4d

File tree

7 files changed

+102
-3
lines changed

7 files changed

+102
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ const pixelFormat = {
260260
};
261261
```
262262

263+
### Generate pixelated images from collection
264+
265+
In order to export gifs based on the layers created, you just need to set the export on the `gif` object in the `src/config.js` file to `true`. You can also play around with the `repeat`, `quality` and the `delay` of the exported gif.
266+
267+
Setting the `repeat: -1` will produce a one time render and `repeat: 0` will loop forever.
268+
269+
```js
270+
const gif = {
271+
export: true,
272+
repeat: 0,
273+
quality: 100,
274+
delay: 500,
275+
};
276+
```
277+
263278
### Printing rarity data (Experimental feature)
264279

265280
To see the percentages of each attribute across your collection, run:

modules/HashlipsGiffer.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const GifEncoder = require("gif-encoder-2");
2+
const { writeFile } = require("fs");
3+
4+
class HashLipsGiffer {
5+
constructor(_canvas, _ctx, _fileName, _repeat, _quality, _delay) {
6+
this.canvas = _canvas;
7+
this.ctx = _ctx;
8+
this.fileName = _fileName;
9+
this.repeat = _repeat;
10+
this.quality = _quality;
11+
this.delay = _delay;
12+
this.initGifEncoder();
13+
}
14+
15+
initGifEncoder = () => {
16+
this.gifEncoder = new GifEncoder(this.canvas.width, this.canvas.height);
17+
this.gifEncoder.setQuality(this.quality);
18+
this.gifEncoder.setRepeat(this.repeat);
19+
this.gifEncoder.setDelay(this.delay);
20+
};
21+
22+
start = () => {
23+
this.gifEncoder.start();
24+
};
25+
26+
add = () => {
27+
this.gifEncoder.addFrame(this.ctx);
28+
};
29+
30+
stop = () => {
31+
this.gifEncoder.finish();
32+
const buffer = this.gifEncoder.out.getData();
33+
writeFile(this.fileName, buffer, (error) => {});
34+
console.log(`Created gif at ${this.fileName}`);
35+
};
36+
}
37+
38+
module.exports = HashLipsGiffer;

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hashlips_art_engine",
3-
"version": "1.0.5",
3+
"version": "1.0.9",
44
"description": "HashLips Art Engine is a tool used to create multiple different instances of artworks based on provided layers.",
55
"main": "index.js",
66
"bin": "index.js",
@@ -23,6 +23,7 @@
2323
"license": "MIT",
2424
"dependencies": {
2525
"canvas": "^2.8.0",
26+
"gif-encoder-2": "^1.0.5",
2627
"sha1": "^1.1.1"
2728
}
2829
}

src/config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const description = "Remember to replace this description";
1414
const baseUri = "ipfs://NewUriToReplace";
1515

1616
const solanaMetadata = {
17-
symbol: "NOC",
17+
symbol: "YC",
1818
seller_fee_basis_points: 1000, // Define how much % you want from secondary market sales 1000 = 10%
1919
external_url: "https://www.youtube.com/c/hashlipsnft",
2020
creators: [
@@ -50,6 +50,13 @@ const format = {
5050
height: 512,
5151
};
5252

53+
const gif = {
54+
export: false,
55+
repeat: 0,
56+
quality: 100,
57+
delay: 500,
58+
};
59+
5360
const text = {
5461
only: false,
5562
color: "#ffffff",
@@ -104,4 +111,5 @@ module.exports = {
104111
namePrefix,
105112
network,
106113
solanaMetadata,
114+
gif,
107115
};

src/main.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ const {
2727
namePrefix,
2828
network,
2929
solanaMetadata,
30+
gif,
3031
} = require(path.join(basePath, "/src/config.js"));
3132
const canvas = createCanvas(format.width, format.height);
3233
const ctx = canvas.getContext("2d");
3334
var metadataList = [];
3435
var attributesList = [];
3536
var dnaList = new Set();
3637
const DNA_DELIMITER = "-";
38+
const HashlipsGiffer = require(path.join(
39+
basePath,
40+
"/modules/HashlipsGiffer.js"
41+
));
42+
43+
let hashlipsGiffer = null;
3744

3845
const buildSetup = () => {
3946
if (fs.existsSync(buildDir)) {
@@ -42,6 +49,9 @@ const buildSetup = () => {
4249
fs.mkdirSync(buildDir);
4350
fs.mkdirSync(path.join(buildDir, "/json"));
4451
fs.mkdirSync(path.join(buildDir, "/images"));
52+
if (gif.export) {
53+
fs.mkdirSync(path.join(buildDir, "/gifs"));
54+
}
4555
};
4656

4757
const getRarityWeight = (_str) => {
@@ -315,6 +325,17 @@ const startCreating = async () => {
315325
await Promise.all(loadedElements).then((renderObjectArray) => {
316326
debugLogs ? console.log("Clearing canvas") : null;
317327
ctx.clearRect(0, 0, format.width, format.height);
328+
if (gif.export) {
329+
hashlipsGiffer = new HashlipsGiffer(
330+
canvas,
331+
ctx,
332+
`${buildDir}/gifs/${abstractedIndexes[0]}.gif`,
333+
gif.repeat,
334+
gif.quality,
335+
gif.delay
336+
);
337+
hashlipsGiffer.start();
338+
}
318339
if (background.generate) {
319340
drawBackground();
320341
}
@@ -324,7 +345,13 @@ const startCreating = async () => {
324345
index,
325346
layerConfigurations[layerConfigIndex].layersOrder.length
326347
);
348+
if (gif.export) {
349+
hashlipsGiffer.add();
350+
}
327351
});
352+
if (gif.export) {
353+
hashlipsGiffer.stop();
354+
}
328355
debugLogs
329356
? console.log("Editions left to create: ", abstractedIndexes)
330357
: null;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ gauge@~2.7.3:
159159
strip-ansi "^3.0.1"
160160
wide-align "^1.1.0"
161161

162+
gif-encoder-2@^1.0.5:
163+
version "1.0.5"
164+
resolved "https://registry.yarnpkg.com/gif-encoder-2/-/gif-encoder-2-1.0.5.tgz#872ae04d8028de4a3417646ceb80dda88d6247b9"
165+
integrity sha512-fsRAKbZuUoZ7FYGjpFElmflTkKwsn/CzAmL/xDl4558aTAgysIDCUF6AXWO8dmai/ApfZACbPVAM+vPezJXlFg==
166+
162167
glob@^7.1.3:
163168
version "7.1.7"
164169
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90"

0 commit comments

Comments
 (0)