forked from yangyang5214/asciicast2mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
129 lines (102 loc) · 2.99 KB
/
renderer.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env phantomjs
var system = require('system');
var pageUrl = system.args[1];
var width = parseInt(system.args[2], 10);
var height = parseInt(system.args[3], 10);
var theme = system.args[4];
var scale = parseInt(system.args[5], 10);
var page = require('webpage').create();
page.viewportSize = { width: 9999, height: 9999 };
page.zoomFactor = scale;
function logDebug(message) {
if (system.env['DEBUG'] === '1') {
console.log(message);
}
}
function logError(message) {
console.log("\u001b[31m==> \u001b[0m" + message);
}
function exit(code) {
phantom.exit(code === undefined ? 0 : code);
}
page.onConsoleMessage = function(msg) {
logDebug('console.log: ' + msg);
};
page.onError = function(msg, trace) {
logError('Script error: ' + msg);
exit(1);
};
page.onResourceError = function(resourceError) {
logError('Unable to load resource (#' + resourceError.id + ', URL:' + resourceError.url + ')');
logError('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
exit(1);
};
page.onCallback = function(data) {
var rect = data.rect;
if (!rect) {
logError("Couldn't get geometry of requested DOM element");
exit(1);
return;
}
logDebug('Setting clipRect...');
page.clipRect = {
left: rect.left * scale,
top: rect.top * scale,
width: rect.width * scale,
height: rect.height * scale
};
logDebug('Reading update from stdin...');
var line = system.stdin.readLine();
while (line !== '') {
var screen = JSON.parse(line);
var imagePath = system.stdin.readLine();
if (imagePath == '') {
logError('Error: imagePath empty');
exit(1);
}
logDebug('Calling updateTerminal...');
page.evaluate(function(screen) {
window.updateTerminal(screen);
}, screen);
logDebug('Saving screenshot to ' + imagePath + '...');
page.render(imagePath);
logDebug('Reading update from stdin...');
line = system.stdin.readLine();
}
logDebug('Rendering success');
exit(0);
};
logDebug('Loading page...');
page.open(pageUrl, function(status) {
if (status !== "success") {
logError("Failed to load " + url);
exit(1);
}
page.evaluate(function(width, height, theme) {
function initTerminal() {
var opts = {
width: width,
height: height,
theme: theme
};
window.updateTerminal = asciinema.gif.page.InitTerminal('player', opts);
setTimeout(function() { // let Powerline font render
var elements = document.querySelectorAll('.asciinema-player');
if (elements.length > 0) {
window.callPhantom({ rect: elements[0].getBoundingClientRect() });
} else {
window.callPhantom({ rect: undefined });
}
}, 10);
}
FontFaceOnload("Powerline Symbols", {
success: initTerminal,
error: function() {
console.log('Failed to pre-load Powerline Symbols font');
initTerminal();
},
timeout: 1000
});
}, width, height, theme);
});
// vim: ft=javascript