mirror of
https://git.sleeping.town/mirrors/foxy-moxy
synced 2024-11-15 00:27:43 +00:00
Refactor
This commit is contained in:
parent
62fa7c55f1
commit
76d4ad0d1a
1 changed files with 14 additions and 13 deletions
27
server.js
27
server.js
|
@ -8,6 +8,7 @@ var Fox = require('./js/fox.js');
|
||||||
var renderFox = require('./js/render-fox.js');
|
var renderFox = require('./js/render-fox.js');
|
||||||
|
|
||||||
function composeImage(width, height, seed) {
|
function composeImage(width, height, seed) {
|
||||||
|
seed = seed || uuid();
|
||||||
var fox = Fox(width, height, seed);
|
var fox = Fox(width, height, seed);
|
||||||
var canvas = new Canvas(width, height);
|
var canvas = new Canvas(width, height);
|
||||||
var ctx = canvas.getContext('2d');
|
var ctx = canvas.getContext('2d');
|
||||||
|
@ -15,10 +16,8 @@ function composeImage(width, height, seed) {
|
||||||
return canvas;
|
return canvas;
|
||||||
};
|
};
|
||||||
|
|
||||||
function writeFoxToDisk (width, height, seed) {
|
function writeFoxToDisk (canvas, nameSuffix) {
|
||||||
if (seed === undefined) seed = uuid();
|
var fileName = "fox-" + nameSuffix + ".png";
|
||||||
var canvas = composeImage(width, height, seed);
|
|
||||||
var fileName = "fox-" + seed + ".png";
|
|
||||||
var filePath = __dirname + '/images/' + fileName;
|
var filePath = __dirname + '/images/' + fileName;
|
||||||
|
|
||||||
fs.writeFile(filePath, canvas.toBuffer(), function(err) {
|
fs.writeFile(filePath, canvas.toBuffer(), function(err) {
|
||||||
|
@ -31,7 +30,9 @@ function writeFoxToDisk (width, height, seed) {
|
||||||
function writeFoxesToDisk (width, height, n=10) {
|
function writeFoxesToDisk (width, height, n=10) {
|
||||||
var fileNames = [];
|
var fileNames = [];
|
||||||
for (var i = 0; i < n; i++) {
|
for (var i = 0; i < n; i++) {
|
||||||
fileNames.push(writeFoxToDisk(width, height));
|
var seed = uuid();
|
||||||
|
var canvas = composeImage(width, height, seed);
|
||||||
|
fileNames.push(writeFoxToDisk(canvas, seed));
|
||||||
}
|
}
|
||||||
return fileNames;
|
return fileNames;
|
||||||
};
|
};
|
||||||
|
@ -47,18 +48,18 @@ app.get('/', function(req, res) {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/:width', function(req, res) {
|
app.get('/:width', function(req, res) {
|
||||||
var width = parseInt(req.params.width);
|
var width = parseInt(req.params.width) || 400;
|
||||||
if (!width) width = 400;
|
var seed = uuid();
|
||||||
var fileName = writeFoxToDisk(width, width);
|
var canvas = composeImage(width, width, seed);
|
||||||
|
var fileName = writeFoxToDisk(canvas, seed);
|
||||||
res.send('<img src="/' + fileName + '"/>');
|
res.send('<img src="/' + fileName + '"/>');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/:width/:seed', function(req, res) {
|
app.get('/:width/:seed', function(req, res) {
|
||||||
var width = parseInt(req.params.width);
|
var width = parseInt(req.params.width) || 400;
|
||||||
var seed = sanitize(req.params.seed);
|
var seed = sanitize(req.params.seed) || uuid();
|
||||||
if (width === undefined) width = 400;
|
var canvas = composeImage(width, width, seed);
|
||||||
if (!seed) seed = uuid();
|
var fileName = writeFoxToDisk(canvas, seed);
|
||||||
var fileName = writeFoxToDisk(width, width, seed);
|
|
||||||
res.send('<img src="/' + fileName + '"/>');
|
res.send('<img src="/' + fileName + '"/>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue