Verified Commit 9651aaf7 authored by Thibaut Broggi's avatar Thibaut Broggi
Browse files

Fix image drawing on server

parent e2661d53
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
}, },
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"canvas": "2.0.0-alpha.1", "canvas": "2.0.0-alpha.5",
"express": "^4.16.1" "express": "^4.16.1"
} }
} }
...@@ -31,7 +31,6 @@ for (let i in fonts) { ...@@ -31,7 +31,6 @@ for (let i in fonts) {
} }
Canvas.registerFont('./static/fonts/FiraSans-Bold.otf', {family: 'FiraSans', weight: 'bold'}); Canvas.registerFont('./static/fonts/FiraSans-Bold.otf', {family: 'FiraSans', weight: 'bold'});
app.get('/generate.png', function (req, res) { app.get('/generate.png', function (req, res) {
let bgColor = req.query['bg-color'] || '#f54358'; let bgColor = req.query['bg-color'] || '#f54358';
let fgColor = req.query['fg-color'] || '#ffd201'; let fgColor = req.query['fg-color'] || '#ffd201';
...@@ -42,7 +41,7 @@ app.get('/generate.png', function (req, res) { ...@@ -42,7 +41,7 @@ app.get('/generate.png', function (req, res) {
let width = 600; let width = 600;
let height = 413; let height = 413;
let fontSize = 44; let fontSize = 44;
let canvas = new Canvas(width, height); let canvas = Canvas.createCanvas(width, height);
let ctx = canvas.getContext('2d'); let ctx = canvas.getContext('2d');
// Clear canvas // Clear canvas
...@@ -89,17 +88,14 @@ app.get('/generate.png', function (req, res) { ...@@ -89,17 +88,14 @@ app.get('/generate.png', function (req, res) {
ctx.fillText('La Quadrature du Net !', offset, height * 0.9); ctx.fillText('La Quadrature du Net !', offset, height * 0.9);
// Draw picto // Draw picto
fs.readFile('./static/imgs/' + icon + '.svg', {encoding: 'utf8'}, function(err, res) { let img = new Image();
let img = new Image(); let imgXml = fs.readFileSync('./static/imgs/' + icon + '.svg');
let imgXml = res.replace(/#FFD201/g, fgColor); imgXml = imgXml.toString('ascii').replace(/#FFD201/gi, fgColor);
img.onload = function() { img.src = 'data:image/svg+xml;base64,' + new Buffer(imgXml).toString('base64');
ctx.drawImage(img, 0, 0); ctx.drawImage(img, 0, 0);
console.log('loaded');
}
img.src = 'data:image/svg+xml;base64,' + Buffer(imgXml).toString('base64');
});
let stream = canvas.pngStream();
// Render image
let stream = canvas.pngStream();
stream.on('data', function(chunk) { stream.on('data', function(chunk) {
res.write(chunk); res.write(chunk);
}); });
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment