xxxxxxxxxx
/******************
Code by Vamoss
Original code link:
https://openprocessing.org/sketch/2313346
Author links:
http://vamoss.com.br
http://twitter.com/vamoss
http://github.com/vamoss
******************/
const speed = 2
var prevPos = {}
var pos = {}
var dir = 1
var radius = 1
var angle = 0
function setup() {
createCanvas(600, 600);
background(255);
pos = {x: width/2, y: height/2}
prevPos = {x: pos.x, y: pos.y}
}
function draw() {
if(random() < 0.1) dir *= -1
if(random() < 0.1) radius = random(1, 5)
if(floor(frameCount/30)%2==0){
angle += 1/radius*dir
}
for(let i = 0; i < speed; i++){
pos.x += cos(angle) * radius
pos.y += sin(angle) * radius
}
line(prevPos.x, prevPos.y, pos.x, pos.y)
prevPos.x = pos.x
prevPos.y = pos.y
if(pos.x < 0 || pos.x > width || pos.y < 0 || pos.y > height){
dir *= -1
angle += PI * dir
for(let i = 0; i < speed*2; i++){
pos.x += cos(angle) * radius
pos.y += sin(angle) * radius
}
}
}