xxxxxxxxxx
92
//reference:
// Daniel Shiffman
// http://codingtra.in
// https://youtu.be/l__fEY1xanY
// https://thecodingtrain.com/CodingChallenges/052-random-walk.html
// chatgpt
let position, velocity, acceleration;
let oldPosition;
let colors;
function setup() {
createCanvas(400, 400);
position = createVector(width / 2, height / 2);
velocity = createVector(0, 0);
acceleration = createVector(0, 0);
oldPosition = position.copy();
background(0);
colors = [
"rgba(255, 0, 0, 0.8)",
"rgba(0, 255, 0, 0.8)",
"rgba(0, 0, 255, 0.8)",
"rgba(255, 255, 0, 0.8)",
"rgba(255, 0, 255, 0.8)",
"rgba(0, 255, 255, 0.8)",
];
}
function draw() {
// glow effect
stroke(random(colors));
strokeWeight(50);
glowEffect();
line(position.x, position.y, oldPosition.x, oldPosition.y);
// location
oldPosition.set(position);
let stepSize = random([80, -80]);
let moveVector = createVector(0, 0);
let r = floor(random(10));
switch (r) {
case 0:
moveVector.x = stepSize;
break;
case 1:
moveVector.x = -stepSize;
break;
case 2:
moveVector.y = stepSize;
break;
case 3:
moveVector.y = -stepSize;
break;
}
acceleration = moveVector;
if (acceleration.x !== 0) {
velocity.y = 0;
}
if (acceleration.y !== 0) {
velocity.x = 0;
}
velocity.add(acceleration);
velocity.limit(30);
position.add(velocity);
velocity.mult(0.95);
position.x = constrain(position.x, 0, width);
position.y = constrain(position.y, 0, height);
}
function glowEffect() {
for (let i = 10; i > 0; i--) {
strokeWeight(i);
let currentColor = random(colors);
let newAlpha = (i * 0.08).toFixed(2);
currentColor = currentColor.replace("0.8", newAlpha);
stroke(currentColor);
line(position.x, position.y, oldPosition.x, oldPosition.y);
}
}