Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

javascript canvas api for your terminal!

branch: master
Octocat-spinner-32 examples clean up tab example July 21, 2012
Octocat-spinner-32 lib Release 0.0.5 July 21, 2012
Octocat-spinner-32 .gitignore Initial commit October 27, 2011
Octocat-spinner-32 .npmignore Initial commit October 27, 2011
Octocat-spinner-32 History.md Release 0.0.5 July 21, 2012
Octocat-spinner-32 Makefile Initial commit October 27, 2011
Octocat-spinner-32 Readme.md grid December 05, 2011
Octocat-spinner-32 index.js Initial commit October 27, 2011
Octocat-spinner-32 package.json remove engines July 21, 2012
Readme.md

term-canvas

experimental html canvas API for the terminal written with node.js.

terminal canvas chart

terminal canvas grid

Installation

$ npm install term-canvas

Examples

States

state

Source:

var Canvas = require('term-canvas');

var canvas = new Canvas(50, 100)
  , ctx = canvas.getContext('2d');

ctx.fillStyle = 'red';
ctx.fillRect(5, 5, 20, 10);
ctx.save();

ctx.fillStyle = 'blue';
ctx.fillRect(6, 6, 18, 8);
ctx.save();

ctx.fillStyle = 'yellow';
ctx.fillRect(7, 7, 16, 6);

ctx.restore();
ctx.fillRect(8, 8, 14, 4);

ctx.restore();
ctx.fillRect(9, 9, 12, 2);

console.log('\n\n\n\n\n');
ctx.resetState();

Fill styles

Static colored rects with no draw loop:

rects

Source:

var Canvas = require('term-canvas');

var canvas = new Canvas(50, 100)
  , ctx = canvas.getContext('2d');

ctx.fillStyle = 'red';
ctx.fillRect(5, 5, 20, 10);

ctx.fillStyle = 'blue';
ctx.fillRect(27, 5, 20, 10);

ctx.fillStyle = 'yellow';
ctx.fillRect(49, 5, 20, 10);

console.log('\n\n\n');
ctx.resetState();

Animation

animated rects

Source:

var Canvas = require('term-canvas')
  , size = process.stdout.getWindowSize();

process.on('SIGINT', function(){
  ctx.reset();
  process.nextTick(function(){
    process.exit();
  });
});

process.on('SIGWINCH', function(){
  size = process.stdout.getWindowSize();
  canvas.width = size[0];
  canvas.height = size[1];
  x2 = x = 1;
  y2 = y = 1;
});

var canvas = new Canvas(size[0], size[1])
  , ctx = canvas.getContext('2d')
  , x = 1
  , y = 2
  , sx = 2
  , sy = 2
  , x2 = 1
  , y2 = 5
  , sx2 = 1
  , sy2 = 1;

ctx.hideCursor();
setInterval(function(){
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.strokeStyle = 'blue';
  ctx.strokeRect(1, 1, canvas.width - 1, canvas.height - 1);
  ctx.strokeStyle = 'green';
  ctx.strokeRect(x += sx, y += sy, 30, 5);
  ctx.fillStyle = 'yellow';
  ctx.fillRect(x2 += sx2, y2 += sy2, 12, 5);
  ctx.fillStyle = 'white';
  ctx.fillText('Rectangle', x2 + 1, y2 + 2);
  ctx.moveTo(0, 10);
  if (x + 30 >= canvas.width || x <= 1) sx = -sx;
  if (x2 + 10 >= canvas.width || x2 <= 1) sx2 = -sx2;
  if (y + 5 >= canvas.height || y <= 1) sy = -sy;
  if (y2 + 5 >= canvas.height || y2 <= 1) sy2 = -sy2;
}, 1000 / 20);

Tests

There are none! currently testing with OSX "Terminal".

License

(The MIT License)

Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.