nature-of-code/0. Randomness/7. Perlin Walker/sketch.js

38 lines
591 B
JavaScript

class Walker {
constructor(x = 0, y = 0) {
this.tx = 0
this.ty = 10000
this.x = x
this.y = y
}
step() {
let dx = map(noise(this.tx), 0, 1, -1, 1)
let dy = map(noise(this.ty), 0, 1, -1, 1)
this.x += dx
this.y += dy
this.tx += 0.01
this.ty += 0.01
}
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();
}