I'm trying to translate an iteration over two arrays into one that's 4 times the size of the combined arrays, to make an image,
example input => [[1][0]]
expected output=> [[255][255][255][255][0][0][0][255]]
the ((height*width+index)*4) (i have tried with and without the *4) is what I have understood to be the formula for translating that to the rgba of the ImageData().data;
I'm writing only the latest row, will slice/join/slice the ImageData.data so that I only write one line per draw.
the expected visual is a visualization of a 2-d cellular automata rule, drawn 1 line at a time.
the actual: weird circuit bend-y, pulsating patterns.
draw(){
if(this.height<this.lineNum){ //lineNum is the current y draw line
this.image.data = this.image.data.slice(this.width*4).join(this.image.data.slice(0,this.width*4)); //cut the begining, put it at the end, then write over it so it moves
}
this.nodes.forEach((node,x)=>{
const state = node.lastState();
const index = (((Math.min(this.lineNum,this.height))*x + (node.index))*4);
this.image.data[index+0]=state?255:0;
this.image.data[index+1]=state?255:0;
this.image.data[index+2]=state?255:0;
this.image.data[index+3]=255;
})
this.ctx.putImageData(this.image ,0,0);
}
here's the full code, but I am sure that I have this formula wrong somehow.
the snippet above is from line 61's draw function.
https://codepen.io/altruios/pen/QWEoYXz?editors=1010
Edit
const index = (((Math.min(node.state.length-1,this.height))*this.width + (node.index))*4);
produces something more 'stable' but still incorrect.
so I think that formula may be correct, and there is some state update logic not happening correctly.