Skip to content

Instantly share code, notes, and snippets.

@TheOdd
Created April 6, 2018 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheOdd/fa4a658b391ec6ba69767a8a141cb0ba to your computer and use it in GitHub Desktop.
Save TheOdd/fa4a658b391ec6ba69767a8a141cb0ba to your computer and use it in GitHub Desktop.
Tilemap monochrome deviation generator
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)
}
@TheOdd
Copy link
Author

TheOdd commented Apr 6, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment