Commit 695e90c7 authored by Luc Didry's avatar Luc Didry
Browse files

Backports new features to server

parent b53ff280
......@@ -13,57 +13,103 @@ const requestHandler = (request, response) => {
var canvas = new Canvas(800, 400);
var ctx = canvas.getContext('2d');
var w = (queryData.w !== undefined) ? queryData.w : 'WE MAKE';
var wx = (queryData.wx !== undefined) ? queryData.wx : '400';
var wy = (queryData.wy !== undefined) ? queryData.wy : '160';
var ws = (queryData.ws !== undefined) ? queryData.ws : '150';
var p = (queryData.p !== undefined) ? queryData.p : 'PORN';
var px = (queryData.px !== undefined) ? queryData.px : '400';
var py = (queryData.py !== undefined) ? queryData.py : '350';
var ps = (queryData.ps !== undefined) ? queryData.ps : '220';
var w = (queryData.w !== undefined) ? queryData.w : 'WE MAKE';
var wx = (queryData.wx !== undefined) ? queryData.wx : '400';
var wy = (queryData.wy !== undefined) ? queryData.wy : '160';
var ws = (queryData.ws !== undefined) ? queryData.ws : '150';
var wc = (queryData.wc !== undefined) ? queryData.wc : '#000000';
var p = (queryData.p !== undefined) ? queryData.p : 'PORN';
var px = (queryData.px !== undefined) ? queryData.px : '400';
var py = (queryData.py !== undefined) ? queryData.py : '350';
var ps = (queryData.ps !== undefined) ? queryData.ps : '220';
var pc = (queryData.pc !== undefined) ? queryData.pc : '#000000';
var co = (queryData.co !== undefined) ? queryData.co : '#FCD205';
var bgt = (queryData.bgt !== undefined) ? queryData.bgt : 'plain';
var rc = (queryData.rc !== undefined) ? queryData.rc : '#000000';
var bgr = (queryData.bgr !== undefined) ? queryData.bgr : '#FCD205';
var egr = (queryData.egr !== undefined) ? queryData.egr : '#FFFFFF';
var or = (queryData.or !== undefined) ? queryData.or : 'lr';
var cor = (queryData.cor !== undefined) ? queryData.cor : '20';
parse(request, {})
.then(data => {
console.log(data);
if (data.text !== undefined) {
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.end(JSON.stringify({response_type: 'in_channel', text: fullUrl(request)+'?p='+encodeURIComponent(data.text)}));
} else {
ctx.fillStyle = 'rgba(252, 210, 5, 1)';
roundRect(ctx, 0, 0, 800, 400, 20, true, false);
switch(bgt) {
case 'plain':
ctx.fillStyle = hexToRgbA(co);
break;
case 'gradient':
var grd;
switch(or) {
case 'lr':
grd = ctx.createLinearGradient(0, 200, canvas.width, 200);
break;
case 'rl':
grd = ctx.createLinearGradient(canvas.width, 200, 0, 200);
break;
case 'tb':
grd = ctx.createLinearGradient(400, 0, 400, canvas.height);
break;
case 'bt':
grd = ctx.createLinearGradient(400, canvas.height, 400, 0);
break;
case 'r':
grd = ctx.createRadialGradient(400, 200, 25, 400, 200, 400);
break;
}
grd.addColorStop(0, bgr);
grd.addColorStop(1, egr);
ctx.fillStyle = grd;
break;
case 'rainbow':
var grd = ctx.createLinearGradient(400, 0, 400, canvas.height);
grd.addColorStop(0, '#e50000');
grd.addColorStop(1 / 5, '#ff8d00');
grd.addColorStop(2 / 5, '#ffee00');
grd.addColorStop(3 / 5, '#008121');
grd.addColorStop(4 / 5, '#004cff');
grd.addColorStop(1, '#760188');
ctx.fillStyle = grd;
break;
}
roundRect(ctx, 0, 0, canvas.width, canvas.height, parseInt(cor), true, false);
// rounded rectangle
// Inside rounded rectangle
ctx.lineWidth = 20;
ctx.strokeStyle = 'rgb(0, 0, 0)';
ctx.strokeStyle = hexToRgbA(rc);
roundRect(ctx, 25, 25, 750, 350, 20, false);
ctx.textAlign="center";
// Write WE MAKE
ctx.font = 'bold '+parseInt(ws)+'px sans-serif';
ctx.fillStyle = 'rgba(0, 0, 0, 1)';
ctx.fillStyle = hexToRgbA(wc);
var i = 0;
while (ctx.measureText(w).width > 725) {
ctx.font = 'bold '+(parseInt(ws) - i++)+'px sans-serif';
}
ctx.fillText(w, parseInt(wx), parseInt(wy));
// Write new value
// Write PORN
ctx.font = 'bold '+parseInt(ps)+'px sans-serif';
ctx.fillStyle = 'rgba(0, 0, 0, 1)';
ctx.fillStyle = hexToRgbA(pc);
i = 0;
while (ctx.measureText(p).width > 725) {
ctx.font = 'bold '+(parseInt(ps) - i++)+'px sans-serif';
}
ctx.fillText(p, parseInt(px), parseInt(py));
// Get data
var data = canvas.toDataURL().replace('data:image/png;base64,','');
// Create image
var img = new Buffer(data, 'base64');
// Send image
response.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
......@@ -143,3 +189,19 @@ function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
function fullUrl(req) {
return req.headers['x-forwarded-proto']+'://'+req.headers['host']+'/WeMake.png';
}
function hexToRgbA(hex){
var c;
if (!/^#/.test(hex)) {
hex = '#'+hex;
}
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
c = hex.substring(1).split('');
if (c.length === 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = '0x'+c.join('');
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)';
}
throw new Error('Bad Hex');
}
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