foxy-moxy/server.js

202 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-12-16 18:05:14 +00:00
var fs = require('fs');
var express = require('express');
var Canvas = require('canvas');
2016-12-15 21:23:38 +00:00
var Fox = require('./js/fox.js');
2016-12-15 19:24:22 +00:00
2016-12-15 21:23:38 +00:00
var renderFox = function (canvas, opts) {
var width = opts.canvas.width;
var height = opts.canvas.height;
2016-12-15 20:20:41 +00:00
var ctx = canvas.getContext('2d');
2016-12-15 19:24:22 +00:00
ctx.fillStyle = opts.canvas.color;
ctx.fillRect(0, 0, width, height);
2016-12-15 22:21:30 +00:00
// draw ears
renderEars(ctx, opts.ears);
2016-12-15 22:42:11 +00:00
// draw head
renderHead(ctx, opts.head);
// draw eyes
renderEyes(ctx, opts.eyes);
2016-12-15 22:21:30 +00:00
// draw cheeks
// draw eyes
// draw nose
2016-12-15 23:21:12 +00:00
renderNose(ctx, opts.nose);
2016-12-15 22:21:30 +00:00
// draw mouth
2016-12-16 02:34:42 +00:00
renderMouth(ctx, opts.mouth);
2016-12-15 19:24:22 +00:00
};
2016-12-15 22:21:30 +00:00
function renderHead(ctx, opts) {
2016-12-15 23:52:19 +00:00
2016-12-15 22:21:30 +00:00
ctx.save();
ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
ctx.rotate(Math.PI / 4);
2016-12-16 00:30:59 +00:00
drawEllipseByCenter(ctx, 0, 0, opts.width, opts.height, opts.color, null, opts.kappa);
2016-12-15 22:21:30 +00:00
ctx.restore();
2016-12-16 00:30:59 +00:00
ctx.clip();
drawEllipseByCenter(ctx, ctx.canvas.width / 2, ctx.canvas.height, opts.maskWidth, opts.maskHeight, '#fff', '#fff', 0.5);
2016-12-15 22:21:30 +00:00
}
function renderEars(ctx, opts) {
var offset = {
x: ctx.canvas.width/2,
y: ctx.canvas.height/2
}
ctx.save();
ctx.translate(offset.x, offset.y);
ctx.rotate(-opts.left.angle);
2016-12-15 23:21:12 +00:00
drawEllipseByCenter(ctx, opts.left.x - offset.x, opts.left.y - offset.y, opts.left.width, opts.left.height, opts.color, null, opts.kappa);
2016-12-15 22:21:30 +00:00
ctx.restore();
ctx.save();
ctx.translate(offset.x, offset.y);
ctx.rotate(-opts.right.angle);
2016-12-15 23:21:12 +00:00
drawEllipseByCenter(ctx, opts.right.x - offset.x, opts.right.y - offset.y, opts.right.width, opts.right.height, opts.color, null, opts.kappa);
2016-12-15 22:21:30 +00:00
ctx.restore();
}
2016-12-15 22:42:11 +00:00
function renderEyes(ctx, opts) {
2016-12-16 02:17:29 +00:00
switch (opts.style) {
case "ellipse":
drawEllipseByCenter(ctx, opts.left.x, opts.left.y, opts.width, opts.height, "black", null, 0.5);
drawEllipseByCenter(ctx, opts.right.x, opts.right.y, opts.width, opts.height, "black", null, 0.5);
break;
case "smiley":
ctx.strokeStyle = "black";
ctx.beginPath();
ctx.moveTo(opts.left.x - opts.width, opts.left.y + opts.height);
ctx.bezierCurveTo(opts.left.x - opts.width, opts.left.y + opts.height, opts.left.x, opts.left.y, opts.left.x + opts.width, opts.left.y + opts.height);
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.moveTo(opts.right.x - opts.width, opts.right.y + opts.height);
ctx.bezierCurveTo(opts.right.x - opts.width, opts.right.y + opts.height, opts.right.x, opts.right.y, opts.right.x + opts.width, opts.right.y + opts.height);
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
break;
2016-12-16 04:20:51 +00:00
case "none":
break;
2016-12-16 02:17:29 +00:00
}
2016-12-15 23:21:12 +00:00
}
function renderNose(ctx, opts) {
ctx.strokeStyle = "black";
ctx.beginPath();
ctx.moveTo(opts.x - opts.width/2, opts.y - opts.height/2);
ctx.bezierCurveTo(opts.x - opts.width/2, opts.y - opts.height/2, opts.x, opts.y - opts.height, opts.x + opts.width/2, opts.y - opts.height/2);
ctx.bezierCurveTo(opts.x + opts.width/2, opts.y - opts.height/2, opts.x + opts.width/2, opts.y + opts.height/2, opts.x, opts.y + opts.height/2);
ctx.bezierCurveTo(opts.x, opts.y + opts.height/2, opts.x - opts.width/2, opts.y + opts.height/2, opts.x - opts.width/2, opts.y - opts.height/2);
2016-12-16 02:17:29 +00:00
ctx.fillStyle = "black";
ctx.fill();
2016-12-16 02:34:42 +00:00
ctx.stroke();
}
function renderMouth(ctx, opts) {
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.beginPath();
switch (opts.style) {
case "smirk":
ctx.moveTo(opts.x - opts.width/2, opts.y - opts.height/2);
ctx.bezierCurveTo(opts.x - opts.width/2, opts.y - opts.height/2,
opts.x - opts.width/2, opts.y + opts.height/2,
opts.x + opts.width/2, opts.y
)
break;
case "cat":
ctx.moveTo(opts.x - opts.width/2, opts.y + opts.height/2);
ctx.lineTo(opts.x, opts.y - opts.height/2);
ctx.lineTo(opts.x + opts.width/2, opts.y + opts.height/2);
break;
}
ctx.stroke();
2016-12-15 22:42:11 +00:00
}
function drawEllipseByCenter(ctx, cx, cy, w, h, color, fillColor, kappa) {
2016-12-15 22:21:30 +00:00
console.log("ellipse coords", cx, cy, w, h);
2016-12-15 22:42:11 +00:00
drawEllipse(ctx, cx - w/2.0, cy - h/2.0, w, h, color, fillColor, kappa);
2016-12-15 21:39:02 +00:00
}
2016-12-15 22:42:11 +00:00
function drawEllipse(ctx, x, y, w, h, color, fillColor, kappa) {
var kappa = kappa || 0.3,
2016-12-15 21:39:02 +00:00
ox = (w / 2) * kappa, // control point offset horizontal
oy = (h / 2) * kappa, // control point offset vertical
xe = x + w, // x-end
ye = y + h, // y-end
xm = x + w / 2, // x-middle
ym = y + h / 2; // y-middle
2016-12-15 22:42:11 +00:00
if (color) {
ctx.strokeStyle = color;
}
2016-12-15 21:39:02 +00:00
ctx.beginPath();
ctx.moveTo(x, ym);
ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
ctx.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
2016-12-15 22:42:11 +00:00
var fillColor = fillColor || color;
if (fillColor) {
ctx.fillStyle = fillColor;
ctx.fill();
}
2016-12-15 21:39:02 +00:00
//ctx.closePath(); // not used correctly, see comments (use to close off open path)
ctx.stroke();
}
2016-12-15 19:24:22 +00:00
var circle = function(c){
2016-12-15 20:20:41 +00:00
var ctx= c.context || false;
var x=c.x || 500;
var y=c.y || x;
var r=c.r || 10;
var cr=ctx.canvas.width/2;
var lineWidth=c.lineWidth || 1;
ctx.beginPath();
ctx.arc(x,y,r,0,2*Math.PI);
ctx.lineWidth=lineWidth;
2016-12-15 21:23:38 +00:00
ctx.fillStyle="red";
ctx.fill();
2016-12-15 20:20:41 +00:00
ctx.stroke();
return true;
2016-12-15 19:24:22 +00:00
};
function writeFoxesToDisk (width, height, n=10) {
var fileNames = [];
for (var i = 0; i < n; i++) {
var Image = Canvas.Image;
var canvas = new Canvas(width, height);
var ctx = canvas.getContext('2d');
var fox = Fox(width, height);
renderFox(canvas, fox);
var img = new Buffer(canvas.toDataURL(), 'base64');
var fileName = "fox" + Math.floor(Math.random() * 10000) + ".png";
var filePath = __dirname + '/images/' + fileName;
fs.writeFile(filePath, canvas.toBuffer(), function(err) {
if (err) console.log('error', err);
});
fileNames.push(fileName);
}
return fileNames;
};
2016-12-15 19:24:22 +00:00
var app = express();
2016-12-15 20:20:41 +00:00
app.use(express.static(__dirname + '/images'));
2016-12-15 19:24:22 +00:00
2016-12-15 21:23:38 +00:00
app.get('/', function(req, res) {
var fileNames = writeFoxesToDisk(300, 300, 30);
var images = fileNames.map(fileName => '<img src="/' + fileName + '"/>');
res.send(images.join(''));
2016-12-15 19:24:22 +00:00
});
app.listen(process.env.PORT || 3000);
console.log('listening on http://localhost:3000');