forked from steadicat/swarmation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.js
28 lines (24 loc) · 772 Bytes
/
images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var fs = require('fs')
var Canvas = require('canvas')
var Image = Canvas.Image
var images = {}
var PIXEL = new Image
PIXEL.src = fs.readFileSync(__dirname + '/public/images/pixel.png')
var PIXEL_SIZE = 15
var PADDING = 15
images.getImage = function(formation, cb) {
var maxHeight = formation.map.length
var maxWidth = 0
formation.map.forEach(function(row) {
maxWidth = Math.max(maxWidth, row.length)
})
var canvas = new Canvas(maxWidth*PIXEL_SIZE + 2*PADDING, maxHeight*PIXEL_SIZE + 2*PADDING)
var ctx = canvas.getContext('2d')
formation.map.forEach(function(row, y) {
row.forEach(function(cell, x) {
if (cell) ctx.drawImage(PIXEL, PADDING + x*PIXEL_SIZE, PADDING + y*PIXEL_SIZE)
})
})
canvas.toBuffer(cb)
}
module.exports = images