1.0.5 • Published 2 years ago

simple-image v1.0.5

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

SimpleImage

Simple image manipulation

GitHub release Build Status codecov Codacy Badge

Install

You can install via npm or yarn.

npm

npm install --save simple-image

yarn

yarn add simple-image

Documentation

This documentation is written in TypeScript, however this library works fine in vanilla JavaScript too.

Usage

Creating a SimpleImage

A SimpleImage instance is created asynchronously (beacause we have to wait on image.onload internally), therefore you must await the ready promise.

With dimensions

async function myFn(): void {
  const simpleImage = new SimpleImage(256, 256);

  await simpleImage.ready;

  // Do stuff
}

myFn();

or without async/await:

const simpleImage = new SimpleImage(256, 256);

simpleImage.ready.then(() => {
  // Do stuff
});

With an image element

async function myFn(): void {
  const image: HTMLImageElement = document.getElementById('my-image');

  const simpleImage = new SimpleImage(image);

  await simpleImage.ready;

  // Do stuff
}

myFn();

With a canvas element

async function myFn(): void {
  const canvas: HTMLCanvasElement = document.getElementById('my-canvas');

  const simpleImage = new SimpleImage(canvas);

  await simpleImage.ready;

  // Do stuff
}

myFn();

With a File

const input: HTMLInputElement = document.getElementById('my-input');

input.onchanges = () => {
  const simpleImage = new SimpleImage(input.files[0]);

  await simpleImage.ready;

  // Do stuff
};

With an existing SimpleImage instance

async function myFn(): void {
  const simpleImage = new SimpleImage(256, 256);

  await simpleImage.ready;

  // Do stuff

  const newSimpleImage = new SimpleImage(simpleImage);

  await newSimpleImage.ready;

  // Do stuff
}

myFn();

Methods

Get

There are 4 methods to get a colour at a given position, getRed, getGreen, getBlue, getAlpha.

ArgumentDescriptionType
xThe x coordinate of the pixel you want to the colour fornumber
yThe y coordinate of the pixel you want to the colour fornumber

Set

There are 4 methods to set a colour at a given position, getRed, getGreen, getBlue, getAlpha.

ArgumentDescriptionType
xThe x coordinate of the pixel you want to set the colour fornumber
yThe y coordinate of the pixel you want to set the colour fornumber
valueValue between 0 and 255 for the colournumber

getPixel

Gets a pixel at any given coordinate:

ArgumentDescriptionType
xThe x coordinate of the pixel you want to getnumber
yThe y coordinate of the pixel you want to getnumber

setPixel

Sets a pixel at any given coordinate to match a given pixel:

ArgumentDescriptionType
xThe x coordinate of the pixel you want to setnumber
yThe y coordinate of the pixel you want to setnumber
pixelThe pixel you want to match toSimplePixel

getPixels

Returns an array of SimplePixel of all the pixels in the image.

setSize

Sets the size of the SimpleImage to the dimensions provided.

ArgumentDescriptionType
widthThe new width for the imagenumber
heightThe new height for the imagenumber

draw

Draws the SimpleImage to the provided canvas.

ArgumentDescriptionType
canvasThe canvas to draw toHTMLCanvasElement

toDataUrl

Returns a data url for the SimpleImage.

SimplePixel

An instance of of a pixel within the SimpleImage.

Properties

PropertyDescriptionType
xThe x coordinate of the pixelnumber
yThe y coordinate of the pixelnumber
redGets/sets the red value for the pixel. Number between 0 and 255number
greenGets/sets the green value for the pixel. Number between 0 and 255number
blueGets/sets the blue value for the pixel. Number between 0 and 255number
alphaGets/sets the alpha value for the pixel. Number between 0 and 255number

Methods

setAllFrom

Sets all the colours to match those of a given SimplePixel

ArgumentDescriptionType
pixelThe pixel to matchSimplePixel
1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago