Module gamejs/surfacearray

Fast pixel access.


// 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);


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].


gamejs.Surface|Array surfaceOrDimensions


SurfaceArray.prototype.get (x, y)

Get rgba value at position xy,


Number x
Number y


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.


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)


Error if x, y out of range


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.


gamejs.Surface surface
gamejs.surfacearray.SurfaceArray surfaceArray