Created
April 6, 2018 16:49
-
-
Save TheOdd/fa4a658b391ec6ba69767a8a141cb0ba to your computer and use it in GitHub Desktop.
Tilemap monochrome deviation generator
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
const Canvas = require('canvas') | |
const fs = require('fs') | |
const dimensions = [15, 15] | |
const imageCount = 15 | |
// const color = [154, 131, 78] // poopy brown | |
// const color = [17, 124, 17] // plant stuff | |
const color = [121, 121, 121] // sad gray | |
const getRandomInt = (min, max) => Math.floor(Math.random() * (Math.floor(max) - Math.ceil(min))) + Math.ceil(min) | |
const setToRandomColor = (imgObj, base) => { | |
for (let i = 0; i < 3; i++) | |
imgObj[base + i] = color[i] + getRandomInt(-10, 11) | |
imgObj[base + 3] = 255 | |
} | |
const generateImage = out => { | |
const c = new Canvas(...dimensions) | |
const ctx = c.getContext('2d') | |
let imgData = ctx.createImageData(...dimensions) | |
for (let i = 0; i < imgData.data.length; i += 4) | |
setToRandomColor(imgData.data, i) | |
ctx.putImageData(imgData, 0, 0) | |
const stream = c.pngStream() | |
stream.on('data', chunk => { | |
out.write(chunk) | |
}) | |
stream.on('end', () => { | |
console.log('saved png') | |
}) | |
} | |
for (let z = 0; z < imageCount; z++) { | |
const out = fs.createWriteStream(__dirname + `/output/tilemap (${z + 1}).png`) | |
generateImage(out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This runs in Node.js and requires the NPM package canvas as a dependency. This also requires some local dependencies per machine that are detailed on their NPM page.