xxxxxxxxxx
let len = 0;
let prevX = 0;
let prevY = 0;
let prevEndX = 0;
let prevEndY = 0;
let spacing = 20;
function setup() {
createCanvas(600, 800); //canvas size
background(255); //background color
noLoop(); //protect the canvas from infinite looping
}
function draw() {
strokeWeight(2); //stroke weight
let x, y;
if (random(1) < 0.5) {
x = random(width); //random X
y = prevY; //draw after the previous Y line
} else {
x = prevX; //draw after the previous X line
y = random(height); //random Y
}
//Red Line
stroke(255, 0, 0); //red color
line(prevX, prevY + spacing, x, y + spacing);
//black line stroke weight
strokeWeight(5);
//Black Line
stroke(0);
line(prevX, prevY, x, y);
//blue line stroke weight
strokeWeight(2);
//blue line
let startX, startY, endX, endY;
if (prevEndX !== 0 && prevEndY !== 0) {
startX = prevEndX; //start from the end point of X
startY = prevEndY; //start from the end point of Y
if (random(1) < 0.5) {
endX = startX;
endY = random(height); //random Y
} else {
endX = random(width); //random X
endY = startY;
}
} else {
startX = random(width);
startY = random(height);
endX = random(width);
endY = startY; // use the starting y-coordinate to draw horizontal line
}
stroke(0, 0, 255); //blue color
line(startX, startY, endX, endY);
prevX = x;
prevY = y;
prevEndX = endX;
prevEndY = endY;
}
//mouse pressed function
function mousePressed() {
draw();
}