Verified Commit 34e87a93 authored by Thibaut Broggi's avatar Thibaut Broggi
Browse files

Render image (fail on svg import)

parent 605fc023
#!/usr/bin/env node
const fs = require('fs');
const express = require('express');
const Canvas = require('canvas');
const Image = Canvas.Image;
const app = express();
const config = require('./config.json');
app.set('x-powered-by', false);
app.use(express.static('./static'));
app.get('/generate', function (req, res) {
res.send('Hello World!');
// let bgColor = document.querySelector('.colorpicker input[type="radio"][name="bg-color"]:checked').value;
// let fgColor = document.querySelector('.colorpicker input[type="radio"][name="fg-color"]:checked').value;
// let fillText = document.getElementById('step2value').value.trim();
// let font = document.querySelector('#step3 input[type="radio"]:checked').value;
// let icon = document.querySelector('#step4 input[type="radio"]:checked').value;
// let signature = document.getElementById('signature').value.trim();
let bgColor = 'red';
let fgColor = 'yellow';
let fillText = 'shampooing';
let icon = 2;
let font = 'arial';
let signature = 'moi';
let width = 600;
let height = 413;
let fontSize = 44;
let canvas = new Canvas(width, height);
let ctx = canvas.getContext('2d');
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fill();
// Write text
ctx.font = fontSize + 'pt FiraSans';
ctx.fillStyle = 'white';
ctx.textBaseline = 'top';
ctx.fillText('Internet,', width * 0.08, height * 0.2);
ctx.fillText('et libertés.', width * 0.08, height * 0.2 + fontSize * 2.8);
ctx.font = fontSize + 'pt ' + font;
ctx.fillStyle = fgColor;
ctx.fillText(fillText, width * 0.08, height * 0.2 + fontSize * 1.4);
// Add footer
ctx.fillStyle = 'white';
ctx.fillRect(0, height * 0.85, width, height * 0.15);
ctx.fill();
ctx.font = '12pt FiraSans';
ctx.fillStyle = '#497ed5';
let offset = height * 0.05;
if (signature) {
ctx.fillText('Faites comme ', offset, height * 0.9);
offset += ctx.measureText('Faites comme ').width;
ctx.font = 'bold 12pt FiraSans';
ctx.fillText(signature, offset, height * 0.9);
offset += ctx.measureText(signature).width;
ctx.font = '12pt FiraSans';
ctx.fillText(', s', offset, height * 0.9);
offset += ctx.measureText(', s').width;
} else {
ctx.fillText('S', offset, height * 0.9);
offset+= ctx.measureText('S').width;
}
ctx.fillText('outenez ', offset, height * 0.9);
offset += ctx.measureText('outenez ').width;
ctx.font = 'bold 12pt FiraSans';
ctx.fillText('La Quadrature du Net !', offset, height * 0.9);
// Draw picto
fs.readFile('./static/imgs/' + icon + '.svg', {encoding: 'utf8'}, function(err, res) {
let img = new Image();
let imgXml = res.replace(/#FFD201/g, fgColor);
img.src = 'data:image/svg+xml;base64,' + Buffer(imgXml).toString('base64');
img.onload = function() {
ctx.drawImage(img, 0, 0);
let stream = canvas.pngStream();
stream.on('data', function(chunk) {
res.write(chunk);
});
stream.on('end', function() {
res.end();
});
};
});
})
app.listen(process.env.PORT || config.port || 80, function () {
console.log('Example app listening on port ' + config.port);
app.get('/', function(req, res) {
res.sendfile('./static/index.html');
})
app.listen(process.env.PORT || config.port || 80);
Supports Markdown
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