diff --git a/1. Vectors/1. Mouse Walker/index.html b/1. Vectors/1. Mouse Walker/index.html new file mode 100644 index 0000000..c390616 --- /dev/null +++ b/1. Vectors/1. Mouse Walker/index.html @@ -0,0 +1,15 @@ + + + + + + + +
+
+ + diff --git a/1. Vectors/1. Mouse Walker/sketch.js b/1. Vectors/1. Mouse Walker/sketch.js new file mode 100644 index 0000000..d7c09a6 --- /dev/null +++ b/1. Vectors/1. Mouse Walker/sketch.js @@ -0,0 +1,40 @@ +function clamp(x, min_x, max_x) { + return min(max(x, min_x), max_x) +} + +class Walker { + constructor(x = 0, y = 0) { + this.pos = createVector(x, y) + } + + step() { + let vel = createVector(0, 0) + if (random(1) < 0.5) { + vel.x = clamp(mouseX - this.pos.x, -1, 1) + vel.y = clamp(mouseY - this.pos.y, -1, 1) + } else { + vel.x = floor(random(3)) - 1 + vel.y = floor(random(3)) - 1 + } + + this.pos.add(vel) + } + + draw() { + stroke(0) + point(this.pos) + } +} + +let walker + +function setup() { + createCanvas(640, 240) + background(255) + walker = new Walker(320, 120) +} + +function draw() { + walker.step(); + walker.draw(); +}