new MatrixBuilder(baseColorOrMatrix)
The MatrixBuilder eases the creation of LED matrix definitions for your L8 Smartlight.
It provides a couple of convenience functions to create and manipulate matrix arrays.
The constructor either takes a baseColor for the whole matrix, or a complete matrix as a starting point for manipulations.
The baseColorOrMatrix
argument is optional. If none is supplied an LED
off-state is chosen as default for every LED in the matrix.
Name | Type | Description |
---|---|---|
baseColorOrMatrix |
Object | Array | optional |
Example
var l8 = new L8();
//... Initialize L8...
var builder = new MatrixBuilder();
var matrix = builder.rectangle(
{r: 0, g: 0, b: 15},
2, 2,
5, 5,
false
)
.row({r: 15, g: 0, b: 0}, 3)
.row({r: 15, g: 0, b: 0}, 4)
.column({r: 15, g: 0, b: 0}, 3}),
.column({r: 15, g: 0, b: 0}, 4)
.toMatrix();
l8.setMatrix(matrix);
The given examples draws a blue border with a red crosshair on the L8
Members
-
privateinitialMatrix_Array
-
The initial matrix all operations will be applied against.
-
privateoperations_Array
-
List of all transformation operations to be applied to the initial matrix upon calls to
toMatrix
.
Methods
-
staticMatrixBuilder.map(matrix, fn)
-
Execute a
map
like operation on a matrix taking into account its special nature.The operation is mostly equivalent to a normal
array.map
call. However special considerations about the matrix will be taken into account, like lines and columns.The callback is executed with the following arguments:
- color
- column (x-coordinate)
- row (y-coordinate)
- index
The matrix will be modified in place
The following example will remove the red component of any pixel inside the given matrix.
Name Type Description matrix
Array fn
function Example
var matrix = // some matrix MatrixBuilder.map(matrix, function(originalColor, row, column, index) { return {r: 0, g: originalColor.g, b: originalColor.b}; }); l8.setMatrix(matrix, ...);
-
column(color, x, y0, y1){MatrixBuilder}
-
Draw pixels inside a certain column of the grid.
Optionally a start and end row may be provided. If none are given the start and end of the column are assumed.
Name Type Description color
Object x
Number y0
Number y1
Number -
createArrayWithSize(size){Array}
-
Create an Array with a given size and values at every index position.
This array may easily be used to execute map/reduce operations to fill/create a new array structure.
Name Type Description size
Number -
operation(fn){MatrixBuilder}
-
Add a transformation operation to the MatrixBuilder
Operations are functions, which will be called in order of registration upon the matrix. They are supposed to transform or manipulate it in any way relevant to the creation process.
Operations are provided with an optional parameters object, which may be supplied during the call to
toMatrix
, which allows to introduce variables into the building pipeline.Name Type Description fn
-
rectangle(color, x0, y0, x1, y1, filled){MatrixBuilder}
-
Draw a rectangle to the pixel grid
The grid is defined by specifying its upper left and lower right coordinates.
Optionally it may be specified if the grid should be filled or not. By default the grid will be filled.
Name Type Description color
Object x0
Number y0
Number x1
Number y1
Number filled
Boolean optional -
row(color, y, x0, x1){MatrixBuilder}
-
Draw pixels inside a certain row of the grid.
Optionally a start and end column may be provided. If none are given the start and end of the line are assumed.
Name Type Description color
Object y
Number x0
Number x1
Number -
toMatrix(variables){Array}
-
Generate the matrix using all defined operations as well as the variables
The given variables will be provided to each operation. Operations will be executed in the defined order.
Name Type Description variables
Object optional -
privatevalidateColors_(colors)
-
Validate colors are valid r, g, b objects
If one of the colors is invalid an exception will be thrown.
Name Type Description colors
Object[] Name Type Description r
Number g
Number b
Number -
privatevalidateCoordinates_(coordinates)
-
Validate coordinates are valid numbers between 0-7
If one of the coordinates is invalid an exception will be thrown.
Name Type Description coordinates
Number[]