Skip to content

Instantly share code, notes, and snippets.

@ejfox

ejfox/101.coffee Secret

Created June 11, 2017 21:21
Show Gist options
  • Save ejfox/71c7649563e56067944bf03f25cec2f4 to your computer and use it in GitHub Desktop.
Save ejfox/71c7649563e56067944bf03f25cec2f4 to your computer and use it in GitHub Desktop.
fs = require 'fs'
d3 = require 'd3'
d3Node = require 'd3-node'
canvasModule = require 'canvas-prebuilt'
randGen = require 'random-seed'
d3n = new d3Node { canvasModule }
argv = require 'yargs'
.alias 's', 'seed'
.argv
run = ->
if argv.seed
seed = argv.seed
else
seed = Date.now()
makeArt seed
makeArt = (seed) ->
rand = new randGen()
rand.seed(seed)
console.log('seed', seed)
canvas = d3n.createCanvas 850,625
ctx = canvas.getContext '2d'
width = canvas.width
height = canvas.height
data = d3.range(420).map ->
{
x: rand(width)
y: rand(height)
}
data.forEach((d,i) ->
ctx.beginPath()
ctx.rect d.x, d.y, 2, 120
ctx.fillStyle = 'black'
ctx.fill()
ctx.closePath()
)
fileOutput = './dist/' + seed + '.png'
console.log('canvas output --> ' + fileOutput);
# Save image locally to /dist/
canvas.pngStream().pipe(fs.createWriteStream(fileOutput))
return canvas
module.exports = makeArt
if(require.main == module)
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment