mirror of
https://git.sleeping.town/mirrors/foxy-moxy
synced 2024-12-22 14:25:31 +00:00
Add some endpoints and the UUID thing.
This commit is contained in:
parent
320babd04e
commit
af5f06f0d9
2 changed files with 42 additions and 14 deletions
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
53
server.js
53
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 => '<img src="/' + 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('<img src="/' + fileName + '"/>');
|
||||
});
|
||||
|
||||
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('<img src="/' + fileName + '"/>');
|
||||
});
|
||||
|
||||
app.listen(process.env.PORT || 3000);
|
||||
console.log('listening on http://localhost:3000');
|
||||
|
|
Loading…
Reference in a new issue