xxxxxxxxxx
69
let audio;
let cols;
let rows;
let current;
let previous;
let amplitude;
let dampening = 0.99;
function preload(){
audio = loadSound('waterdrop.mp3');
}
function setup() {
pixelDensity(1);
createCanvas(600, 400);
cols = width;
rows = height;
current = new Array(cols).fill(220).map(n => new Array(rows).fill(0));
previous = new Array(cols).fill(220).map(n => new Array(rows).fill(0));
amplitude = new p5.Amplitude();
}
function mouseDragged (){
//previous[mouseX][mouseY] = 2500;
}
function mousePressed(){
audio.play();
}
function mouseReleased(){
audio.stop();
}
function draw() {
background(112, 146, 204);
//print(amplitude.getLevel());
if(amplitude.getLevel() > 0.06){
previous[mouseX][mouseY] = 2500;
}
loadPixels();
for (let i = 1; i < cols - 1; i++) {
for (let j = 1; j < rows - 1; j++) {
current[i][j] =
(previous[i - 1][j] +
previous[i + 1][j] +
previous[i][j - 1] +
previous[i][j + 1]) /
2 -
current[i][j];
current[i][j] = current[i][j] * dampening;
let index = (i + j * cols) * 4;
pixels[index + 0] = current[i][j];
pixels[index + 1] = current[i][j];
pixels[index + 2] = current[i][j];
}
}
updatePixels();
let temp = previous;
previous = current;
current = temp;
}
// https://thecodingtrain.com/CodingChallenges/102-2d-water-ripple.html