-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 1.74 KB
/
index.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var fs = require('fs');
var Canvas = require('canvas');
var obelisk = require('obelisk.js')(Canvas);
var itermDraw = require('iterm2-image');
var lego = require('lego-colors');
var canvas = new Canvas(600,450);
var ctx = canvas.getContext('2d');
var content = fs.readFileSync('./figure');
var figure = content.toString().split('---\n');
figure = figure.map(function (layer) {
return layer.split('\n').map(function (row) {
return row.split('');
});
});
var point = new obelisk.Point(250, 300);
var pixelView = new obelisk.PixelView(canvas, point);
function toColor(x) {
return new obelisk.CubeColor().getByHorizontalColor(x.rgbHexAsNumber);
}
var colors = {
R: toColor(lego.colors.BRIGHT_RED),
Y: toColor(lego.colors.BRIGHT_YELLOW),
K: toColor(lego.colors.BLACK),
G: toColor(lego.colors.DARK_GREEN),
B: toColor(lego.colors.BRIGHT_BLUE),
};
// build cubes
for (var i = 0; i < figure.length; i++) {
var figureSquare = figure[i].reverse();
for (var j = 0; j < figureSquare.length; j++) {
var figureRow = figureSquare[j].reverse();
for (var k = 0; k < figureRow.length; k++) {
var p3d = new obelisk.Point3D(20 * j, 20 * k, (32 + 1) * i);
var cubeDms = new obelisk.CubeDimension(20, 20, 32);
var figureCell = figureRow[k];
if (colors[figureCell]) {
var cubeColor = colors[figureCell];
} else {
var cubeColor = new obelisk.CubeColor().getByHorizontalColor(0xF2F3F2);
}
if (figureCell !== ' ') {
var cube = new obelisk.Cube(cubeDms, cubeColor, false);
pixelView.renderObject(cube, p3d);
}
}
}
}
if (process.env.TERM_PROGRAM === 'iTerm.app') {
itermDraw(canvas.createPNGStream());
} else {
canvas.createPNGStream().pipe(fs.createWriteStream('./figure.png'));
}