-
Notifications
You must be signed in to change notification settings - Fork 142
/
server.js
66 lines (53 loc) · 2.04 KB
/
server.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
var f = require('fabric'),
__fabric = global.fabric = f.fabric,
// Cufon = global.Cufon = modFabric.Cufon,
http = require('http'),
url = require('url'),
PORT = 8124;
// Fetch font definitions
// var fileNames = fs.readdirSync(RAILS_ROOT + '/public/javascripts/fonts');
// fileNames.forEach(function(fileName) {
// if (fileName.charAt(0) === '.') return;
// require(RAILS_ROOT + '/public/javascripts/fonts/' + fileName);
// });
// require('font_definitions.js');
// if (typeof __fontDefinitions !== 'undefined') {
// for (var fontName in __fontDefinitions) {
// if (Cufon.fonts[fontName.toLowerCase()]) {
// Cufon.fonts[fontName.toLowerCase()].offsetLeft = __fontDefinitions[fontName];
// }
// }
// }
// else {
// console.log('Failed to include font definitions file');
// }
var server = http.createServer(function (request, response) {
var params = url.parse(request.url, true);
serveImage(response, params.query.code, params.query.async);
});
server.listen(PORT);
console.log('Server listening on http://localhost:' + PORT);
__fabric.Object.prototype.originX = __fabric.Object.prototype.originY = 'center';
function serveImage(__response, __code, __async) {
var canvas = new __fabric.StaticCanvas(null, { width: 200, height: 200 });
var font = new __fabric.nodeCanvas.Font('Arial', __dirname + '/assets/fonts/Arial.ttf');
font.addFace(__dirname + '/assets/fonts/Arial Bold.ttf', 'bold');
font.addFace(__dirname + '/assets/fonts/Arial Italic.ttf', 'normal', 'italic');
font.addFace(__dirname + '/assets/fonts/Arial Bold Italic.ttf', 'bold', 'italic');
canvas.contextContainer.addFont(font); // when using createPNGStream or createJPEGStream
eval(__code);
__response.writeHead(200, { 'Content-Type': 'image/png' });
if (__async !== 'true') {
proceed();
}
function proceed() {
// canvas.renderAll();
var __stream = canvas.createPNGStream();
__stream.on('data', function(chunk) {
__response.write(chunk);
});
__stream.on('end', function() {
__response.end();
});
}
}