Currently, I am working on a project using P5.js where I am saving values in an array and then creating a copy of that array to manipulate. However, I have encountered an issue where manipulating the second array also changes the original one, and I cannot determine the reason behind this unexpected behavior.
var particles = []
var particlesCopy = []
function calculateInitialPositions(){
for (var i = 0; i < pixels.length; i+=4) {
if (pixels[i] == 0){
var x_ = i % width
var y_ = i / width / 2
var coords_ = {x : x_ , y : y_}
particles.push(coords_)
}
};
}
function setup() {
loadPixels()
calculateInitialPositions();
particlesCopy = [...particles]
}
function draw() {
for (var i = 0; i < particlesCopy.length; i++) {
particlesCopy[0].x = 99
};
console.log(particles[0].x)
}
The console now prints 99