xxxxxxxxxx
95
let startX, startY, endX, endY;
let x, y;
let xspeed, yspeed;
let randomStop;
let mx, my;
let going;
function setup() {
createCanvas(windowWidth, windowHeight);
randomSeed(0);
init();
}
function init() {
// Pick a starting side
let straw = random(4);
let rsX = random(width);
let rsY = random(height);
let reX = random(width);
let reY = random(height);
// Top and Bottom side
if(straw > 2) {
startX = rsX
startY = straw > 3 ? 0 : height;
endX = reX;
endY = straw > 3 ? height : 0;
}
// Left and Right side
else {
startX = straw > 1 ? 0 : width
startY = rsY;
endX = straw > 1 ? width : 0;
endY = reY;
}
setSpeed();
// Set random stop moment
let d = dist(startX, startY, endX, endY);
randomStop = d*random(1);
going = true;
x = startX;
y = startY;
// Set offscreen margin up to 10 seconds
let rs = random(10);
mx = 10; //xspeed*rs;
my = 10; //yspeed*rs;
}
function mousePressed() {
init();
}
function setSpeed() {
// Pick a random speed multiplier;
let scl = pow(2, random(7.5, 14));
// Set direction and speed
xspeed = (endX-startX)/scl;
yspeed = (endY-startY)/scl;
console.log(xspeed, yspeed);
}
function draw() {
background(255);
stroke(0);
strokeWeight(10);
//scl = map(mouseX, 0, width, 0, 10);
x += xspeed;
y += yspeed;
line(startX, startY, x, y);
//let d = dist(startX, startY, x, y);
// if(d > randomStop && going) {
// noLoop();
// going = false;
// setTimeout(function(){
// setSpeed();
// loop();
// }, random(1000, 10000));
// }
// Reinitialize
if((x < -mx || x > width + mx || y < -my || y > height + my)) {
init();
}
}
function keyPressed(){
init();
}