44 lines
738 B
JavaScript
44 lines
738 B
JavaScript
function clamp(x, min_x, max_x) {
|
|
return min(max(x, min_x), max_x)
|
|
}
|
|
|
|
class Walker {
|
|
constructor(x = 0, y = 0) {
|
|
this.x = x
|
|
this.y = y
|
|
}
|
|
|
|
step() {
|
|
let dx = 0
|
|
let dy = 0
|
|
if (random(1) < 0.5) {
|
|
dx = clamp(mouseX - this.x, -1, 1)
|
|
dy = clamp(mouseY - this.y, -1, 1)
|
|
} else {
|
|
dx = floor(random(3)) - 1
|
|
dy = floor(random(3)) - 1
|
|
}
|
|
|
|
this.x += dx
|
|
this.y += dy
|
|
}
|
|
|
|
draw() {
|
|
stroke(0)
|
|
point(this.x, this.y)
|
|
}
|
|
}
|
|
|
|
let walker
|
|
|
|
function setup() {
|
|
createCanvas(640, 240)
|
|
background(255)
|
|
walker = new Walker(320, 120)
|
|
}
|
|
|
|
function draw() {
|
|
walker.step();
|
|
walker.draw();
|
|
}
|