-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
97 lines (74 loc) · 2.31 KB
/
index.js
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
var webpage = require('webpage');
function takeScreenshot(url, screenshotsDir, layer, done) {
var page = webpage.create();
var output = screenshotsDir + '/' + layer.type + '.png';
page.viewportSize = { width: layer.viewport.width, height: layer.viewport.height };
page.clipRect = { top: 0, left: 0, width: page.viewportSize.width, height: page.viewportSize.height };
console.info('Now loading: layer ' + layer.type + '...');
page.open(url, function(status) {
if (status !== 'success') console.error('Unable to load the address for layer ' + layer.type);
window.setTimeout(function() {
console.info('Generated: ' + output);
page.render(output);
done();
}, 1000);
});
}
function renderMockup(path, output, metadata) {
var page = webpage.create();
page.viewportSize = {
width: metadata.mockup.width,
height: metadata.mockup.height
};
page.clipRect = {
top: 0,
left: 0,
width: metadata.mockup.width,
height: metadata.mockup.height
};
page.open(path, function(status) {
if (status !== 'success') console.error('Unable to load mockup!');
page.onConsoleMessage = function(msg) {
console.warn('[render] ' + msg);
};
window.setTimeout(function() {
console.info('Saved responsive mockup to: ' + output);
page.render(output);
phantom.exit();
}, 500);
});
}
function create(options) {
['output', 'template', 'url'].forEach(function(requiredOption) {
if (!options[requiredOption]) throw(requiredOption + ' option missing');
});
var output = options.output;
var template = options.template;
var url = options.url;
var metadata = require('./templates/' + template + '/metadata');
var mockupPath = './templates/' + template + '/render.html';
var screenshotsDir = './screenshots/' + template;
var tasks = [];
function next() {
var task = tasks.shift();
if (task) {
task();
} else {
phantom.exit();
}
}
metadata.layers.forEach(function(layer, index) {
var newUrl;
if (Array.isArray(url)) {
newUrl = url[index];
} else {
newUrl = url;
}
tasks.push(function() { takeScreenshot(newUrl, screenshotsDir, layer, next); });
});
tasks.push(function() { renderMockup(mockupPath, output, metadata); });
next();
}
module.exports = {
create: create
};