├── .gitignore ├── package.json ├── README.md ├── figure ├── LICENSE.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | figure.png 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-obelisk-canvas-example", 3 | "version": "0.0.1", 4 | "description": "Example of how to run obelisk.js in Node.js", 5 | "dependencies": { 6 | "canvas": "^1.1.3", 7 | "iterm2-image": "^0.0.2", 8 | "lego-colors": "^0.0.2", 9 | "obelisk.js": "^1.0.2" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Obelisk.js in Node.js 2 | 3 | This example does the following: 4 | 5 | * Loads a file named `figure` that contains a 3d figure represented as layers: Each layer is separated by the `---` string and the cells of the layer may contain a letter that represents its color. If no letter is present nothing is drawn in that place. 6 | * Draws that figure inside a [node-canvas](https://github.com/learnboost/node-canvas). 7 | * If the script is run inside [iTerm2](iterm2.com) draws the image in the console. If not it's saved in a file named `figure.png` 8 | 9 | Take a look by yourself, an image is worth a thousand words! 10 | 11 | ![https://cloud.githubusercontent.com/assets/419703/2586880/6caee458-ba0d-11e3-9c87-e6013a7d0175.gif](https://cloud.githubusercontent.com/assets/419703/2586880/6caee458-ba0d-11e3-9c87-e6013a7d0175.gif) 12 | -------------------------------------------------------------------------------- /figure: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | R R 6 | R R 7 | 8 | 9 | R R 10 | R R 11 | --- 12 | 13 | 14 | 15 | 16 | BBBBB 17 | B K 18 | B K 19 | B K 20 | B K 21 | BKKKK 22 | --- 23 | 24 | 25 | 26 | 27 | WWWWW 28 | W W 29 | W W 30 | W W 31 | W W 32 | WWWWW 33 | --- 34 | 35 | 36 | 37 | 38 | KBBBB 39 | K B 40 | K B 41 | K B 42 | K B 43 | KKKKK 44 | --- 45 | 46 | 47 | 48 | RRR 49 | WRRRW 50 | W W 51 | W W 52 | W W 53 | W W 54 | WWWWW 55 | --- 56 | YYYYY 57 | Y G 58 | G G 59 | GRRRG 60 | GRRRG 61 | GYYYY 62 | 63 | 64 | 65 | 66 | --- 67 | YYYYY 68 | G Y 69 | G G 70 | GRRRG 71 | GRRRG 72 | YYYYG 73 | 74 | 75 | 76 | 77 | --- 78 | YKYKY 79 | Y Y 80 | Y Y 81 | Y Y 82 | Y Y 83 | YYYYY 84 | 85 | 86 | 87 | 88 | --- 89 | YYYYY 90 | Y Y 91 | B K 92 | B K 93 | B K 94 | BBKKK 95 | 96 | 97 | 98 | 99 | --- 100 | BBBBB 101 | B B 102 | B B 103 | B B 104 | W B 105 | WWWWW 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Alberto Pose 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | 3 | var Canvas = require('canvas'); 4 | 5 | var obelisk = require('obelisk.js')(Canvas); 6 | 7 | var itermDraw = require('iterm2-image'); 8 | var lego = require('lego-colors'); 9 | 10 | var canvas = new Canvas(600,450); 11 | var ctx = canvas.getContext('2d'); 12 | 13 | var content = fs.readFileSync('./figure'); 14 | var figure = content.toString().split('---\n'); 15 | 16 | figure = figure.map(function (layer) { 17 | return layer.split('\n').map(function (row) { 18 | return row.split(''); 19 | }); 20 | }); 21 | 22 | var point = new obelisk.Point(250, 300); 23 | 24 | var pixelView = new obelisk.PixelView(canvas, point); 25 | 26 | function toColor(x) { 27 | return new obelisk.CubeColor().getByHorizontalColor(x.rgbHexAsNumber); 28 | } 29 | 30 | var colors = { 31 | R: toColor(lego.colors.BRIGHT_RED), 32 | Y: toColor(lego.colors.BRIGHT_YELLOW), 33 | K: toColor(lego.colors.BLACK), 34 | G: toColor(lego.colors.DARK_GREEN), 35 | B: toColor(lego.colors.BRIGHT_BLUE), 36 | }; 37 | 38 | // build cubes 39 | for (var i = 0; i < figure.length; i++) { 40 | var figureSquare = figure[i].reverse(); 41 | for (var j = 0; j < figureSquare.length; j++) { 42 | var figureRow = figureSquare[j].reverse(); 43 | for (var k = 0; k < figureRow.length; k++) { 44 | var p3d = new obelisk.Point3D(20 * j, 20 * k, (32 + 1) * i); 45 | var cubeDms = new obelisk.CubeDimension(20, 20, 32); 46 | var figureCell = figureRow[k]; 47 | if (colors[figureCell]) { 48 | var cubeColor = colors[figureCell]; 49 | } else { 50 | var cubeColor = new obelisk.CubeColor().getByHorizontalColor(0xF2F3F2); 51 | } 52 | if (figureCell !== ' ') { 53 | var cube = new obelisk.Cube(cubeDms, cubeColor, false); 54 | pixelView.renderObject(cube, p3d); 55 | } 56 | } 57 | } 58 | } 59 | 60 | if (process.env.TERM_PROGRAM === 'iTerm.app') { 61 | itermDraw(canvas.createPNGStream()); 62 | } else { 63 | canvas.createPNGStream().pipe(fs.createWriteStream('./figure.png')); 64 | } 65 | --------------------------------------------------------------------------------