binary-pixel-map
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

binary-pixel-map

A library written in TypeScript for creating and editing a binary 2D pixel map, made for usage with small monochrome displays, for example on a Raspberry Pi or Arduino.

This library is based on oled-js and oled-font-5x7 by noopkat.

Installation

yarn add binary-pixel-map

Usage

import { PixelMap, Pixel } from 'binary-pixel-map';
 
// Create a new pixel map with a width of 54, height of 18.
const map = new PixelMap(54, 18);
 
// Draw "Hello, world!" on the map.
map.text(1, 1, 'Hello,');
map.text(1, 10, 'world!');
 
// Enable the top left and bottom right pixel.
map.set(0, 0, Pixel.ON);
map.set(53, 17, Pixel.ON);
 
console.log(map.toString());

The example above will output the following:

PixelMap { x - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
           - x - - - x - - - - - - - - - - - - x x - - - - - - x x - - - - - - - - - - - - - - - - - - - - - - - - - -
           - x - - - x - - - - - - - - - - - - - x - - - - - - - x - - - - - - - - - - - - - - - - - - - - - - - - - -
           - x - - - x - - - - x x x - - - - - - x - - - - - - - x - - - - - - x x x - - - - - - - - - - - - - - - - -
           - x x x x x - - - x - - - x - - - - - x - - - - - - - x - - - - - x - - - x - - - - - - - - - - - - - - - -
           - x - - - x - - - x x x x x - - - - - x - - - - - - - x - - - - - x - - - x - - - - x x - - - - - - - - - -
           - x - - - x - - - x - - - - - - - - - x - - - - - - - x - - - - - x - - - x - - - - - x - - - - - - - - - -
           - x - - - x - - - - x x x - - - - - x x x - - - - - x x x - - - - - x x x - - - - - x - - - - - - - - - - -
           - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
           - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
           - - - - - - - - - - - - - - - - - - - - - - - - - - x x - - - - - - - - - x - - - - - x - - - - - - - - - -
           - - - - - - - - - - - - - - - - - - - - - - - - - - - x - - - - - - - - - x - - - - - x - - - - - - - - - -
           - x - - - x - - - - x x x - - - - x - x x - - - - - - x - - - - - - x x - x - - - - - x - - - - - - - - - -
           - x - - - x - - - x - - - x - - - x x - - x - - - - - x - - - - - x - - x x - - - - - x - - - - - - - - - -
           - x - x - x - - - x - - - x - - - x - - - - - - - - - x - - - - - x - - - x - - - - - x - - - - - - - - - -
           - x - x - x - - - x - - - x - - - x - - - - - - - - - x - - - - - x - - - x - - - - - - - - - - - - - - - -
           - - x - x - - - - - x x x - - - - x - - - - - - - - x x x - - - - - x x x x - - - - - x - - - - - - - - - -
           - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x }

API

PixelMap

new PixelMap(widthnumber, heightnumber);

get

get(xnumber, ynumber)PixelStatus;

Get the value of the pixel at [x, y].

getAll

getAll()PixelStatus[][];

Get all pixels.

set

set(xnumber, ynumber, statusPixelStatus)void;

Set the value of the pixel at [x, y];

clear

clear()void;

Clear the whole map.

fill

fill(statusPixelStatus)void;

Fill the map with enabled (ON) or disabled (OFF) pixels.

line

line(x0number, y0number, x1number, y1number, statusPixelStatus = Pixel.ON)void;

Draw a line from [x0, y0] to [x1, y1] using Bresenham's line algorithm.

rectangle

rectangle(xnumber, ynumber, widthnumber, heightnumber, outlineboolean = false, statusPixelStatus = Pixel.ON)void;

Draw a rectangle at [x, y] with the size [width, height]. If outline is set to true, only an outline will be drawn. Otherwise the rectangle will be filled.

text

text(xnumber, ynumber, contentstring, sizenumber = 1, spacingnumber = 2, wrapboolean = true)void;

Draw text at [x, y] with size size. The standard size (size = 1) for letters is 5 x 7.

copy

copy(fromPixelMap, xnumber = 0, ynumber = 0)void;

Copy another pixel map onto the current pixel map at [x, y].

image

async image(pathstring | Buffer, xnumber = 0, ynumber = 0, maxWidth?: number, maxHeight?: number)Promise<void>;

Copy an image onto the pixel map. path can be a file path or image buffer. Supported file types are: PNG, JPEG, WebP, GIF, SVG and TIFF.

toArray

toArray()PixelStatus[];

Get the map as sequence of all rows. Can be useful for displaying the map on a display.

toString

toString()string;

Get the map as string. Can be useful for debugging.

Pixel

import { Pixel } from 'binary-pixel-map';

PixelStatus

type PixelStatus = boolean;

Used as alternative to booleans. You can still use regular booleans if you prefer.

Pixel.ON

Alternative way of writing true.

Pixel.OFF

Alternative way of writing false.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.2.1
    1
    • latest

Version History

Package Sidebar

Install

npm i binary-pixel-map

Weekly Downloads

5

Version

1.2.1

License

MIT

Unpacked Size

128 kB

Total Files

33

Last publish

Collaborators

  • mrten