UNPKG

led-canvas-matrix

Version:

Matrix Class providing basic data structures for led-canvas and led-canvas-text

164 lines (148 loc) 4.33 kB
"use strict"; var _slice = Array.prototype.slice; var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr); }; var _classProps = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; var getBounds = require("./getBounds"); var populate = require("./populate"); var sortLeds = require("./sortLeds"); var LedCanvasMatrix = (function () { var LedCanvasMatrix = function LedCanvasMatrix(x, y, width, height, leds) { if (x === undefined) x = 0; if (y === undefined) y = 0; if (width === undefined) width = 1; if (height === undefined) height = 1; if (leds === undefined) leds = []; if (Array.isArray(leds)) { this.leds = leds; } else { this.leds = populate(width, height, leds); } this.x = x; this.y = y; this.width = width; this.height = height; }; _classProps(LedCanvasMatrix, null, { get: { writable: true, value: function (x, y) { x = (x >= this.width) ? 0 : x; y = (y >= this.height) ? 0 : y; var led = this.leds[x + y * this.width]; var leds = led ? [led] : undefined; return led ? new LedCanvasMatrix(x, y, 1, 1, leds) : null; } }, index: { writable: true, value: function (idx) { var led = this.leds[idx]; return new LedCanvasMatrix(led.x, led.y, 1, 1, [led]); } }, prop: { writable: true, value: function (key, value) { var leds = this.leds.filter(function (led) { return led[key] == value; }); var bounds = getBounds(leds); return new LedCanvasMatrix(bounds.x, bounds.y, bounds.width, bounds.height, leds); } }, row: { writable: true, value: function (y) { return new LedCanvasMatrix(this.x, y, this.width, 1, this.leds.filter(function (led) { return led.y == y; })); } }, column: { writable: true, value: function (x) { return new LedCanvasMatrix(x, this.y, 1, this.height, this.leds.filter(function (led) { return led.x == x; })); } }, rect: { writable: true, value: function (x, y, width, height) { if (width === undefined) width = 1; if (height === undefined) height = 1; var xmax = x + width; var ymax = y + height; return new LedCanvasMatrix(x, y, width, height, this.leds.filter(function (led) { return led.x >= x && led.x < xmax && led.y >= y && led.y < ymax; })); } }, set: { writable: true, value: function (key, value) { if (typeof key !== "string") { this.leds.forEach(function (led) { led.enabled = typeof key === "undefined" ? true : key; }); } else { this.leds.forEach(function (led) { led.set(key, value); }); } return this; } }, toggle: { writable: true, value: function () { this.leds.forEach(function (led) { led.enabled = !led.enabled; }); return this; } }, render: { writable: true, value: function () { var args = _slice.call(arguments); this.leds.forEach(function (led) { led.render.apply(led, _toArray(args)); }); return this; } }, add: { writable: true, value: function (matrix) { var _this = this; var moved = matrix.leds.map(function (led) { led.x += _this.width; return led; }); var leds = this.leds.concat(moved).sort(sortLeds); var bounds = getBounds(leds); return new LedCanvasMatrix(bounds.x, bounds.y, bounds.width, bounds.height, leds); } }, join: { writable: true, value: function (matrix, x, y) { var _this2 = this; if (x === undefined) x = 0; if (y === undefined) y = 0; matrix.leds.forEach(function (led) { _this2.get(led.x + x, led.y + y).set(led.enabled); }); return this; } } }); return LedCanvasMatrix; })(); module.exports = LedCanvasMatrix;