xxxxxxxxxx
75
// "glitch art"
let pos_origin = {x:200, y:200};
let squiggliness = 0.5;
let pos = pos_origin;
let dir = 0; // number between 0 and 2*PI
let step = 5;
function setup() {
createCanvas(600, 600);
background(0);
stroke('rgba(100,50,255,0.6)');
strokeWeight(5);
squiggliness = PI/4;
}
function turn(angle) {
dir += angle;
while (dir > 2*PI) {
dir -= 2*PI;
}
while (dir < 0) {
dir += 2*PI;
}
}
function forward() {
new_x = pos.x + cos(dir)*step;
new_y = pos.y + sin(dir)*step;
// console.log("Next point: ")
return {x:new_x, y:new_y};
}
function draw() {
if(frameCount%20 == 0) // slow fadeout
fill("rgba(255,255,255, 0.1)");
circle(pos.x, pos.y, 100)
let p = Math.random();
let turn_factor = Math.random();
if(p < 0.5)
turn(squiggliness*turn_factor);
else //if(p < 0.8)
turn (-squiggliness*turn_factor);
//else
// {} // continue at prev heading
next_pos = forward();
line(pos.x, pos.y, next_pos.x, next_pos.y);
pos = next_pos;
if (pos.x > width || pos.y > height || pos.x < 0 || pos.y < 0) {
pos = pos_origin;
spawnNew(pos);
}
}
function spawnNew(pos) {
pos_origin = pos;
let red_pct = Math.random();
let blue_pct = Math.random();
if (red_pct < .1)
red_pct = .1;
if (blue_pct < .2)
blue_pct = .2;
stroke(red_pct*255,10,blue_pct*255,0.6*255);
}
function mousePressed() {
spawnNew({x:mouseX, y:mouseY});
}