Make the server work.

This commit is contained in:
lucaswadedavis 2016-12-15 12:20:41 -08:00
parent 919b151a21
commit dd83bf5917
3 changed files with 159 additions and 159 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
node_modules/ node_modules/
.DS_Store
images/*

0
images/.placeholder Normal file
View file

316
server.js
View file

@ -2,100 +2,100 @@ var davis = require('./js/davis.js');
var geo = require('./js/geo.js'); var geo = require('./js/geo.js');
var ngon = function (c) { var ngon = function (c) {
var n=c.n || 3; var n=c.n || 3;
var ctx= c.context || false; var ctx= c.context || false;
var x=c.x || 500; var x=c.x || 500;
var y=c.y || x; var y=c.y || x;
var r=c.r || 500; var r=c.r || 500;
if (n%2==0){ if (n%2==0){
var rotation=360/(n*2)*davis.random(n*2); var rotation=360/(n*2)*davis.random(n*2);
} }
else { else {
var rotation=90+(180*davis.random(2)); var rotation=90+(180*davis.random(2));
}; };
rotation=c.rotation || rotation; rotation=c.rotation || rotation;
var color=c.color || davis.randomColor("grey"); var color=c.color || davis.randomColor("grey");
var lineWidth=c.lineWidth || 1; var lineWidth=c.lineWidth || 1;
var fill=c.fill || davis.randcomColor(); var fill=c.fill || davis.randcomColor();
ctx.beginPath(); ctx.beginPath();
for (var i=0;i<n+2;i++){ for (var i=0;i<n+2;i++){
var nx=geo.getPoint(x,y,r,rotation+(i*360/n)).x2; var nx=geo.getPoint(x,y,r,rotation+(i*360/n)).x2;
var ny=geo.getPoint(x,y,r,rotation+(i*360/n)).y2; var ny=geo.getPoint(x,y,r,rotation+(i*360/n)).y2;
ctx.lineTo(nx,ny); ctx.lineTo(nx,ny);
} }
ctx.lineJoin='miter'; ctx.lineJoin='miter';
ctx.strokeStyle=color; ctx.strokeStyle=color;
ctx.lineWidth=lineWidth; ctx.lineWidth=lineWidth;
ctx.fillStyle=fill; ctx.fillStyle=fill;
ctx.fill(); ctx.fill();
ctx.stroke(); ctx.stroke();
return true; return true;
}; };
var textIcon = function (canvas, width, height) { var textIcon = function (canvas, width, height) {
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
ctx.beginPath(); ctx.beginPath();
var color=davis.randomColor();
var gradient=ctx.createLinearGradient(0,0,0,height);
gradient.addColorStop(0,color);
gradient.addColorStop(1,davis.pick([color,davis.randomColor(),"#000","#fff",davis.alpha(color,0)]));
ctx.fillStyle=gradient;
ctx.fillRect(0,0,width,height);
for (var j=0;j<davis.random(4);j++){
var r=davis.random(width/2);
var rf=davis.random(width/2);
var color=davis.randomColor(); var color=davis.randomColor();
davis.maybe(2,3,function(){color=davis.randomColor("grey");}); var gradient=ctx.createLinearGradient(0,0,0,height);
var lw=1+davis.random(width/20); gradient.addColorStop(0,color);
var steps=1+davis.random(30); gradient.addColorStop(1,davis.pick([color,davis.randomColor(),"#000","#fff",davis.alpha(color,0)]));
var incrementMod=davis.random(3)*90; ctx.fillStyle=gradient;
var n=davis.random(6); ctx.fillRect(0,0,width,height);
var rotation=90*davis.random(3);
var fadeColor=davis.pick(["rgba(0,0,0,0)","rgba(255,255,255,0)"]);
var fadeRadius=Math.random();
davis.maybe(1,5,function(){fadeRadius=0;});
davis.maybe(1,5,function(){fadeRadius=false;});
for (var i=0;i<steps;i++){
var increment=i*360/steps;
var x=geo.getPoint(width/2,height/2,rf,increment+incrementMod).x2;
var y=geo.getPoint(width/2,height/2,rf,increment+incrementMod).y2;
circle({
n:n, for (var j=0;j<davis.random(4);j++){
var r=davis.random(width/2);
var rf=davis.random(width/2);
var color=davis.randomColor();
davis.maybe(2,3,function(){color=davis.randomColor("grey");});
var lw=1+davis.random(width/20);
var steps=1+davis.random(30);
var incrementMod=davis.random(3)*90;
var n=davis.random(6);
var rotation=90*davis.random(3);
var fadeColor=davis.pick(["rgba(0,0,0,0)","rgba(255,255,255,0)"]);
var fadeRadius=Math.random();
davis.maybe(1,5,function(){fadeRadius=0;});
davis.maybe(1,5,function(){fadeRadius=false;});
for (var i=0;i<steps;i++){
var increment=i*360/steps;
var x=geo.getPoint(width/2,height/2,rf,increment+incrementMod).x2;
var y=geo.getPoint(width/2,height/2,rf,increment+incrementMod).y2;
circle({
n:n,
gradient:true,
context:ctx,
x:x,
y:y,
r:r,
rotation:rotation,
lineWidth:lw,
color:color,
fadeColor:fadeColor,
fadeRadius:fadeRadius
});
}
}
var radialCanvas = new Canvas(width, height);
var ctx2 = radialCanvas.getContext('2d');
radial(ctx2, width, height);
var pattern = ctx.createPattern(radialCanvas);
ctx.clearRect(0, 0, width, height);
ngon({
n:3+davis.random(4),
gradient:true, gradient:true,
context:ctx, context:ctx,
x:x, x:width/2,
y:y, y:height/2,
r:r, r:width/4+davis.random(width/4),
rotation:rotation, fill:pattern,
lineWidth:lw, lineWidth:davis.random(width/15)
color:color, });
fadeColor:fadeColor,
fadeRadius:fadeRadius
});
}
}
var radialCanvas = new Canvas(width, height);
var ctx2 = radialCanvas.getContext('2d');
radial(ctx2, width, height);
var pattern = ctx.createPattern(radialCanvas);
ctx.clearRect(0, 0, width, height);
ngon({
n:3+davis.random(4),
gradient:true,
context:ctx,
x:width/2,
y:height/2,
r:width/4+davis.random(width/4),
fill:pattern,
lineWidth:davis.random(width/15)
});
}; };
@ -103,102 +103,100 @@ var textIcon = function (canvas, width, height) {
var circle = function(c){ var circle = function(c){
var ctx= c.context || false; var ctx= c.context || false;
var x=c.x || 500; var x=c.x || 500;
var y=c.y || x; var y=c.y || x;
var r=c.r || 10; var r=c.r || 10;
var color=c.color || davis.randomColor("grey"); var color=c.color || davis.randomColor("grey");
var fadeColor=c.fadeColor || "rgba(0,0,0,0)"; var fadeColor=c.fadeColor || "rgba(0,0,0,0)";
var fadeRadius=c.fadeRadius || Math.random(); var fadeRadius=c.fadeRadius || Math.random();
var cr=ctx.canvas.width/2; var cr=ctx.canvas.width/2;
var gradient=ctx.createRadialGradient(cr,cr,(fadeRadius*cr),cr,cr,cr); var gradient=ctx.createRadialGradient(cr,cr,(fadeRadius*cr),cr,cr,cr);
gradient.addColorStop(0,color); gradient.addColorStop(0,color);
gradient.addColorStop(1,fadeColor); gradient.addColorStop(1,fadeColor);
var lineWidth=c.lineWidth || 1; var lineWidth=c.lineWidth || 1;
ctx.beginPath(); ctx.beginPath();
ctx.arc(x,y,r,0,2*Math.PI); ctx.arc(x,y,r,0,2*Math.PI);
ctx.strokeStyle=gradient; ctx.strokeStyle=gradient;
ctx.lineWidth=lineWidth; ctx.lineWidth=lineWidth;
ctx.stroke(); ctx.stroke();
return true; return true;
}; };
var radial = function(ctx,width,height){ var radial = function(ctx,width,height){
ctx.beginPath(); ctx.beginPath();
ctx.arc(width/2,width/2,davis.random(width/2),0,2*Math.PI); ctx.arc(width/2,width/2,davis.random(width/2),0,2*Math.PI);
ctx.strokeStyle=davis.randomColor(); ctx.strokeStyle=davis.randomColor();
ctx.lineWidth=1+davis.random(0.5*width); ctx.lineWidth=1+davis.random(0.5*width);
ctx.stroke(); ctx.stroke();
for (var j=0;j<davis.random(4);j++){ for (var j=0;j<davis.random(4);j++){
var r=davis.random(width/2); var r=davis.random(width/2);
var rf=davis.random(width/2); var rf=davis.random(width/2);
var color=davis.randomColor(); var color=davis.randomColor();
davis.maybe(2,3,function(){color=davis.randomColor("grey");}); davis.maybe(2,3,function(){color=davis.randomColor("grey");});
var lw=1+davis.random(width/10); var lw=1+davis.random(width/10);
var steps=1+davis.random(30); var steps=1+davis.random(30);
var incrementMod=davis.random(3)*90; var incrementMod=davis.random(3)*90;
var n=davis.random(6); var n=davis.random(6);
var rotation=90*davis.random(3); var rotation=90*davis.random(3);
var fadeColor=davis.pick(["rgba(0,0,0,0)","rgba(255,255,255,0)"]); var fadeColor=davis.pick(["rgba(0,0,0,0)","rgba(255,255,255,0)"]);
var fadeRadius=Math.random(); var fadeRadius=Math.random();
davis.maybe(1,5,function(){fadeRadius=0;}); davis.maybe(1,5,function(){fadeRadius=0;});
davis.maybe(1,5,function(){fadeRadius=false;}); davis.maybe(1,5,function(){fadeRadius=false;});
for (var i=0;i<steps;i++){ for (var i=0;i<steps;i++){
var increment=i*360/steps; var increment=i*360/steps;
var x=geo.getPoint(width/2,height/2,rf,increment+incrementMod).x2; var x=geo.getPoint(width/2,height/2,rf,increment+incrementMod).x2;
var y=geo.getPoint(width/2,height/2,rf,increment+incrementMod).y2; var y=geo.getPoint(width/2,height/2,rf,increment+incrementMod).y2;
circle({ circle({
n:n, n:n,
gradient:true, gradient:true,
context:ctx, context:ctx,
x:x, x:x,
y:y, y:y,
r:r, r:r,
rotation:rotation, rotation:rotation,
lineWidth:lw, lineWidth:lw,
color:color, color:color,
fadeColor:fadeColor, fadeColor:fadeColor,
fadeRadius:fadeRadius fadeRadius:fadeRadius
}); });
}
} }
}
}; };
var Canvas = require('canvas'); var Canvas = require('canvas');
var express = require('express'); var express = require('express');
var fs = require('fs');
var app = express(); var app = express();
app.use(express.static(__dirname + '/images'));
app.get('/:width', function(req, res) { app.get('/:width', function(req, res) {
var width = Math.min(1000, Math.max(0, parseInt(req.params.width) || 500)); var width = 200;
var height = width; var height = 200;
var Canvas = require('canvas');
var Image = Canvas.Image;
var canvas = new Canvas(width, height);
var ctx = canvas.getContext('2d');
textIcon(canvas, width, height);
var img = new Buffer(canvas.toDataURL(), 'base64');
var fileName = "fox" + Math.floor(Math.random() * 10000) + ".png";
fs.writeFile(__dirname + '/images/' + fileName, canvas.toBuffer(), function(err) {
console.log('error', err);
});
var Canvas = require('canvas') console.log(fileName);
, Image = Canvas.Image res.redirect(301, fileName);
, canvas = new Canvas(width, height)
, ctx = canvas.getContext('2d');
textIcon(canvas, width, height);
res.send(canvas.toDataURL());
/*
var img = new Buffer(canvas.toDataURL(), 'base64');
console.log(img.length);
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end();
*/
}); });