-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-thumbnails.js
More file actions
97 lines (75 loc) · 3 KB
/
generate-thumbnails.js
File metadata and controls
97 lines (75 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs');
const async = require('async');
const puppeteer = require('puppeteer');
const fileUrl = require('file-url');
module.exports = function (done) {
//make sure build/images/thumbnails folder exists
fs.mkdir("build/images", function (err) { if (err && err.code !== "EEXIST") console.log('ERROR CREATING FOLDER',err); });
fs.mkdir("build/images/thumbnails", function (err) { if (err && err.code !== "EEXIST") console.log('ERROR CREATING FOLDER',err); });
//load examples data
const examples = JSON.parse(fs.readFileSync('examples.json'));
//LOOP through shape types
async.eachOfLimit(examples, 1, (shapeArray, shape, finishedGeneratingShapeGroup) => {
console.log('> generating '+shape+'s ['+shapeArray.length+']');
//LOOP through each preset in shape group and render it
async.eachOfLimit(shapeArray, 1, (shapeData, index, renderedPreset) => {
//add object.shape specification if not added
if (!shapeData.object) shapeData.object = {};
shapeData.object.shape = shape;
//generate the thumbnail
generateThumbnail(shape+'-'+(index+1), shapeData, renderedPreset);
},
//done rendering all shapes in shape group
finishedGeneratingShapeGroup
);
},
//done with every shape type
(err) => {
console.log("done rendering all shapes");
done();
}
);
}
async function generateThumbnail (name, data, done) {
const browser = await puppeteer.launch({args: ['--disable-web-security']});
const page = await browser.newPage();
await page.setViewport({ width: 512, height: 512 });
//convert the data into url parameters
let urlParameters = generateUrlParametersFromData(data,'');
if (urlParameters.length < 3) urlParameters = '';
else urlParameters = '?' + urlParameters.substring(1); //replace first & with ?
console.log(' ',name,urlParameters);
//load the page
let generatedUrl = fileUrl('./build/example-generator.htm')+urlParameters;
await page.goto(generatedUrl).catch(err=>console.error('invalid url',generatedUrl));
//wait until the example is fully loaded
await page.waitForFunction('window.exampleLoaded === true');
//screenshot the page
await page.screenshot({
path: 'build/images/thumbnails/'+name+'.png',
omitBackground: true,
});
//success
await browser.close();
done();
}
//recursive function which goes through an object and adds the properties to a url parameter string
function generateUrlParametersFromData(data, path) {
let urlParameters = '';
for (var k in data) {
if (typeof data[k] == "object" && data[k] !== null)
urlParameters += generateUrlParametersFromData(data[k], (path?(path+'.'):'') +k);
else {
//console.log('adding to param',urlParameters,path,path.length, k);
let objPath = k;
if (path) objPath = '&' +path+'.' + objPath;
urlParameters += objPath +'='+ cleanData(data[k]);
}
}
return urlParameters;
}
//removes unnecessary stuff from data
function cleanData (data) {
if (typeof data == 'string') return data.replace('#','');
return data;
}