public update()

in libs/core/sim/neopixel.ts [189:238]


        public update(colors: number[][]) {
            if (!colors || colors.length <= 0)
                return;

            if (this.pixels.length == 0 && this.cols > 1) {
                // first time, so redo width of canvas
                let rows = Math.ceil(colors.length / this.cols);
                let rt = CANVAS_HEIGHT / rows;
                let width = this.cols * rt;
                this.canvas.setAttributeNS(null, "width", `${width}px`)
                this.updateViewBox(0, 0, width, CANVAS_HEIGHT);
            }

            for (let i = 0; i < colors.length; i++) {
                let pixel = this.pixels[i];
                if (!pixel) {
                    let cxy: Coord = [0, CANVAS_VIEW_PADDING + i * PIXEL_SPACING];
                    if (this.cols > 1) {
                        const row = Math.floor(i / this.cols);
                        const col = i - row * this.cols;
                        cxy  = [(col + 1) * PIXEL_SPACING,  (row + 1) * PIXEL_SPACING]
                    }
                    pixel = this.pixels[i] = new NeoPixel(cxy, this.cols);
                    svg.hydrate(pixel.el, { title: `offset: ${i}` });
                    this.canvas.appendChild(pixel.el);
                }
                pixel.setRgb(colors[i] as [number, number, number]);
            }

            //show the canvas if it's hidden
            pxsim.U.removeClass(this.background, "hidden");

            // resize
            let [first, last] = [this.pixels[0], this.pixels[this.pixels.length - 1]]
            let yDiff = last.cy - first.cy;
            let newH = yDiff + CANVAS_VIEW_PADDING * 2;
            let [oldX, oldY, oldW, oldH] = this.viewBox;
            if (newH > oldH) {
                let scalar = newH / oldH;
                let newW = oldW * scalar;
                if (this.cols > 1) {
                    // different computation for matrix
                    let rows = Math.ceil(colors.length / this.cols);
                    newH = PIXEL_SPACING * (rows + 1);
                    newW = PIXEL_SPACING * (this.cols + 1);
                    this.updateViewBox(0, oldY, newW, newH);
                } else
                    this.updateViewBox(-newW / 2, oldY, newW, newH);
            }
        }