diff --git a/package.json b/package.json
index 6a5cf0f..d619ed9 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,7 @@
"canvas": "^1.6.2",
"chance": "^1.0.4",
"express": "^4.14.0",
- "node-gyp": "^3.4.0"
+ "node-gyp": "^3.4.0",
+ "uuid": "^3.0.1"
}
}
diff --git a/server.js b/server.js
index 4428065..344884b 100644
--- a/server.js
+++ b/server.js
@@ -1,25 +1,36 @@
var fs = require('fs');
var express = require('express');
+var uuid = require('uuid/v4');
var Canvas = require('canvas');
var Fox = require('./js/fox.js');
var renderFox = require('./js/render-fox.js');
+function composeImage(width, height, seed) {
+ var fox = Fox(width, height, seed);
+ var canvas = new Canvas(width, height);
+ var ctx = canvas.getContext('2d');
+ renderFox(canvas, fox);
+ return canvas;
+};
+
+function writeFoxToDisk (width, height, seed) {
+ if (seed === undefined) seed = uuid();
+ var canvas = composeImage(width, height, seed);
+ var fileName = "fox-" + seed + ".png";
+ var filePath = __dirname + '/images/' + fileName;
+
+ fs.writeFile(filePath, canvas.toBuffer(), function(err) {
+ if (err) console.log('error', err);
+ });
+
+ return fileName;
+};
+
function writeFoxesToDisk (width, height, n=10) {
var fileNames = [];
for (var i = 0; i < n; i++) {
- var canvas = new Canvas(width, height);
- var ctx = canvas.getContext('2d');
- var fox = Fox(width, height);
- renderFox(canvas, fox);
- 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);
+ fileNames.push(writeFoxToDisk(width, height));
}
return fileNames;
};
@@ -29,10 +40,26 @@ var app = express();
app.use(express.static(__dirname + '/images'));
app.get('/', function(req, res) {
- var fileNames = writeFoxesToDisk(400, 400, 30);
+ var fileNames = writeFoxesToDisk(200, 200, 28);
var images = fileNames.map(fileName => '');
res.send(images.join(''));
});
+app.get('/:width', function(req, res) {
+ var width = parseInt(req.params.width);
+ if (!width) width = 400;
+ var fileName = writeFoxToDisk(width, width);
+ res.send('');
+});
+
+app.get('/:width/:seed', function(req, res) {
+ var width = parseInt(req.params.width);
+ var seed = req.params.seed;
+ if (width === undefined) width = 400;
+ if (seed === undefined) seed = uuid();
+ var fileName = writeFoxToDisk(width, width, seed);
+ res.send('');
+});
+
app.listen(process.env.PORT || 3000);
console.log('listening on http://localhost:3000');