Module gamejs/surfacearray

Fast pixel access.

Example

// create array from display surface
  var srfArray = new SurfaceArray(display);
  // direct pixel access
  srfArray.set(50, 100, [255, 0, 0, 100]);
  console.log(srfArray.get(30, 50));
  // blit modified array back to display surface
  blitArray(display, srfArray);

Functions

Class SurfaceArray

Instance Methods

Instance Properties


SurfaceArray (surfaceOrDimensions)

The SurfaceArray can be constructed with a surface whose values are then used to initialize the pixel array.

The surface passed as argument is not modified by the SurfaceArray.

If an array is used to construct SurfaceArray, the array must describe the dimensions of the SurfaceArray [width, height].

Parameters

gamejs.Surface|Array surfaceOrDimensions

See

http://dev.w3.org/html5/2dcontext/#pixel-manipulation

SurfaceArray.prototype.get (x, y)

Get rgba value at position xy,

Parameters

Number x
Number y

Returns

Array [red, green, blue, alpha]

SurfaceArray.prototype.getSize ()


SurfaceArray.prototype.set (x, y, rgba)

Set rgba value at position x, y.

For performance reasons this function has only one signature being Number, Number, Array4.

Parameters

Number x x position of pixel
Number y y position of pixel
Array rgba [red, green, blue, alpha] values [255, 255, 255, 255] (alpha, the last argument defaults to 255)

Throws

Error if x, y out of range

SurfaceArray.prototype.surface

a new gamejs.Surface on every access, representing the current state of the SurfaceArray.


blitArray (surface, surfaceArray)

Directly copy values from an array into a Surface.

This is faster than blitting the surface property on a SurfaceArray

The array must be the same dimensions as the Surface and will completely replace all pixel values.

Parameters

gamejs.Surface surface
gamejs.surfacearray.SurfaceArray surfaceArray