xxxxxxxxxx
136
let cols;
let rows;
let current;
let previous;
let dampening = 0.99; // Controls ripple dissipation
let cellSize = 4; // Size of each cell
let baseStrength = 5000; // Base intensity of interaction
let interactStrength = baseStrength; // Dynamic intensity
let autoRipples = false; // Automatic ripple generation
let mousePressDuration = 0; // Counter for how long the mouse is pressed
function setup() {
createCanvas(windowWidth, windowHeight);
initializeGrid();
textSize(16);
fill(255);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
initializeGrid();
}
function initializeGrid() {
cols = floor(width / cellSize);
rows = floor(height / cellSize);
current = new Array(cols).fill(0).map(() => new Array(rows).fill(0));
previous = new Array(cols).fill(0).map(() => new Array(rows).fill(0));
}
function mouseDragged() {
mousePressDuration++;
interactStrength = baseStrength + mousePressDuration * 50; // Increase ripple strength
addRipple(mouseX, mouseY);
}
function mousePressed() {
mousePressDuration++;
interactStrength = baseStrength + mousePressDuration * 50; // Increase ripple strength
addRipple(mouseX, mouseY);
}
function mouseReleased() {
mousePressDuration = 0; // Reset the counter when the mouse is released
interactStrength = baseStrength; // Reset ripple strength
}
function keyPressed() {
if (key === 'A' || key === 'a') {
autoRipples = !autoRipples; // Toggle automatic ripples
} else if (key === 'R' || key === 'r') {
initializeGrid(); // Reset the grid
} else if (key === 'W' || key === 'w') {
dampening = constrain(dampening + 0.01, 0.9, 1); // Increase dampening
} else if (key === 'S' || key === 's') {
dampening = constrain(dampening - 0.01, 0.9, 1); // Decrease dampening
} else if (key === '+' && cellSize < 20) {
cellSize += 1; // Increase cell size
initializeGrid();
} else if (key === '-' && cellSize > 2) {
cellSize -= 1; // Decrease cell size
initializeGrid();
}
}
function addRipple(x, y) {
let gridX = floor(x / cellSize);
let gridY = floor(y / cellSize);
if (gridX > 0 && gridX < cols && gridY > 0 && gridY < rows) {
previous[gridX][gridY] = interactStrength;
}
}
function draw() {
background(0);
noStroke();
// Display ripples
for (let i = 1; i < cols - 1; i++) {
for (let j = 1; j < rows - 1; j++) {
// Cellular automata ripple algorithm
current[i][j] =
(previous[i - 1][j] +
previous[i + 1][j] +
previous[i][j - 1] +
previous[i][j + 1]) /
2 -
current[i][j];
// Apply dampening to simulate energy dissipation
current[i][j] *= dampening;
// Map the current state to a color intensity
let intensity = map(current[i][j], -interactStrength, interactStrength, 0, 255);
// Render each cell as a circle with its intensity
fill(intensity, intensity * 0.8, 255); // Blue-tinted ripple effect
ellipse(i * cellSize, j * cellSize, cellSize, cellSize);
}
}
// Swap buffers
let temp = previous;
previous = current;
current = temp;
if (autoRipples && frameCount % 10 === 0) {
// Add a random ripple every 10 frames
addRipple(random(width), random(height));
}
// Display info text
displayInfoText();
}
function displayInfoText() {
fill(255);
noStroke();
textAlign(LEFT, TOP);
text(
`Controls:
A - Toggle auto ripples
R - Reset grid
W - Increase dampening (slower fade)
S - Decrease dampening (faster fade)
+ - Increase cell size
- - Decrease cell size
Click and drag to create ripples.`,
10,
10
);
}