block by fogleman d767c6ab36ed6c3a724aedcdafdb0485

Random Walkers

Full Screen

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>body {padding: 0; margin: 0;}</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/p5.min.js"></script>
<script>

function Point() {
    this.x = width / 2;
    this.y = height / 2;
}

Point.prototype.copy = function() {
    var point = new Point();
    point.x = this.x;
    point.y = this.y;
    return point;
}

Point.prototype.step = function() {
    var angle = random(-PI, PI);
    this.x += cos(angle) * 1;
    this.y += sin(angle) * 1;
}

Point.prototype.draw = function() {
    ellipse(this.x, this.y, 2);
}

var points = [];

function restart() {
    points = [];
    for (var i = 0; i < 1; i++) {
        points.push(new Point());
    }
}

function setup() {
    background(255);
    createCanvas(windowWidth, windowHeight);
    restart();
}

function windowResized() {
    background(255);
    resizeCanvas(windowWidth, windowHeight);
    restart();
}

function draw() {
    noStroke();
    fill(0, 0, 0, 8);
    for (var t = 0; t < 50; t++) {
        for (var i = 0; i < points.length; i++) {
            points[i].step();
            points[i].draw();
        }
        if (points.length < 100 && random() < 0.001) {
            points.push(points[Math.floor(random(points.length))].copy());
        }
    }
}

</script>
</body>
</html>