-
-
Save bevacqua/4a06f4f2332663bd1d46952e528578d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const path = require(`path`) | |
const glob = require(`glob`) | |
const ponytars = glob.sync(`.bin/semipublic/ponytars/*.png`) | |
// app.get('/ponytar/:hash.png', getPonytar) | |
function getPonytar(req, res) { | |
const { hash } = req.params | |
const seed = seedFromHash(hash) | |
const random = randomFromSeed(seed) | |
const index = Math.floor(random() * ponytars.length) | |
const ponytar = ponytars[index] | |
res.sendFile(path.resolve(ponytar)) | |
} | |
function randomFromSeed(seed) { | |
return () => { | |
const x = Math.sin(seed++) * 1000 | |
return x - Math.floor(x) | |
} | |
} | |
function seedFromHash(hash) { | |
const rnotation = /[.e+]/g | |
const integer = parseInt(hash, 16).toString() | |
const seed = integer.toString().replace(rnotation, ``) | |
return seed / 100000 | |
} | |
module.exports = getPonytar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment