xxxxxxxxxx
71
// Taking a line for a walk.
// Patterns worth noting:
// * Key latch for export
// * Perlin noise; noiseSeed
// * Pow bias
// Uses https://github.com/zenozeng/p5.js-svg to export SVG.
var bExportSvg = false;
function setup() {
createCanvas(600, 600, SVG);
noLoop();
}
function draw() {
background(230);
if (bExportSvg) clear();
strokeWeight(2);
stroke(0);
noFill();
var myBaseStepSize = 5;
var myHeading = random(TWO_PI);
var headingBias = 0.6;
var nIters = 5000;
var cx = width / 2;
var cy = height / 2;
var px = cx;
var py = cy;
beginShape();
for (var i = 0; i < nIters; i++) {
var myStepSize = myBaseStepSize + (noise(i / 20.0 + 10) - 0.5);
var dx = px - cx;
var dy = py - cy;
var dh01 = max(0.0001, sqrt(dx * dx + dy * dy) / 250);
myStepSize *= 1.0 - 0.9 * dh01;
myHeading += radians(20.0 * (noise(i / 10.0) - headingBias));
px += myStepSize * cos(myHeading);
py += myStepSize * sin(myHeading);
dh01 = pow(dh01, -0.3);
var qx = cx + dx * dh01;
var qy = cy + dy * dh01;
vertex(qx, qy);
}
endShape();
noLoop();
if (bExportSvg){
saveSVG("gl_linewalk.svg");
bExportSvg = false;
}
}
//---------------------------------
function keyPressed() {
if (key == "s") {
bExportSvg = true;
loop();
}
}
//---------------------------------
function mousePressed() {
noiseSeed(millis());
loop();
}