ndarray-canvas

0.0.0 • Public • Published

ndarray-canvas

Renders a 2D ndarray to a canvas element.

Installation

npm install ndarray-canvas

Usage

require('ndarray-canvas')(canvas, ndarray)

Renders a single ndarray as a greyscale image, returning the canvas element. If a canvas element is not supplied, a new one will be created. Values in the array are expected to range between 0 and 255.

require('ndarray-canvas')(canvas, red, green, blue)

If you supply three ndarrays to the function (one representing each channel), you can get an RGB canvas back instead!

Take the following example:

var cave = require('cave-automata-2d')
  , fill = require('ndarray-fill')
  , zeros = require('zeros')
  , render = require('./')

function amplify(array) {
  return fill(array, function(x, y) {
    return array.get(x, y) * 255
  })
}

function generate() {
  var array = zeros([250, 250])
  cave(array)(20)
  amplify(array)
  return array
}

var greyscale = render(null, generate())
  , rgb = render(null, generate(), generate(), generate())

document.body.appendChild(greyscale)
document.body.appendChild(rgb)

And you should get back something like this:

example

Dependents (0)

Package Sidebar

Install

npm i ndarray-canvas

Weekly Downloads

5

Version

0.0.0

License

MIT

Last publish

Collaborators

  • hughsk