12 Waves

Modified from tiled waves

Log in to post a comment.

// Shamelesss borrowed from https://turtletoy.net/turtle/65cb465053
Canvas.setpenopacity(0.33);

// Global code will be evaluated once.
const turtle = new Turtle();
class Noise {
	// http://mrl.nyu.edu/~perlin/noise/
	constructor(octaves = 1) {
		this.p = new Uint8Array(512);
		this.octaves = octaves;
		for (let i = 0; i < 512; ++i) {
			this.p[i] = Math.random() * 256*100;
		}
	}
	lerp(t, a, b) {
		return a + t * (b - a);
	}
	grad2d(i, x, y) {
		const v = (i & 1) === 0 ? x : y;
		return (i & 2) === 0 ? -v : v;
	}
	noise2d(x2d, y2d) {
		const X = Math.floor(x2d) & 255;
		const Y = Math.floor(y2d) & 255;
		const x = x2d - Math.floor(x2d);
		const y = y2d - Math.floor(y2d);
		const fx = (3 - 2 * x) * x * x;
		const fy = (3 - 2 * y) * y * y;
		const p0 = this.p[X] + Y;
		const p1 = this.p[X + 1] + Y;
		return this.lerp(
			fy,
			this.lerp(
				fx,
				this.grad2d(this.p[p0], x, y),
				this.grad2d(this.p[p1], x - 1, y)
			),
			this.lerp(
				fx,
				this.grad2d(this.p[p0 + 1], x, y - 1),
				this.grad2d(this.p[p1 + 1], x - 1, y - 1)
			)
		);
	}
	noise(x, y, scale=0.5) {
		let e = 1,
			k = 1,
			s = 0;
		for (let i = 0; i < this.octaves; ++i) {
			e *= scale; // This constant factor will adjust where the lines are drawn
			s += e * (1 + this.noise2d(k * x, k * y)) / 2;
			k *= 2;
		}
		return s;
	}
}

const perlin = new Noise(.23);

// You can play around with these to get cool results
const bars = [33, -33];
const gap = 8;
const size = 50;

// The walk function will be called until it returns false.
turtle.up();

function inRange(x, min, max) {
    return x >= min && x <= max;
}

function draw(j, i, scale) {
    const h = perlin.noise(100 + j * 0.01, 100 + i * 0.01, scale);
    const y = 0.1 * (i-1000) + h * 200 - 100;
    turtle.goto(j, y);
    const flooredY = y;
    const isInRange = bars.some((bar) => inRange(flooredY, bar - (gap / 2), bar + (gap / 2)));
    if (isInRange) {
        turtle.up();
    } else {
        turtle.down();
    }
}


function walk(i) {
    [[50, 0.1], [0, 0.3], [-50, 0.2], [-100, 0.1]].forEach(([num, scale], index) => {
        const amount = index === 0 ? size + 1 : size - gap;
        for (let j = num; j < num + amount; j+=0.5) {
            draw(j, i, scale);
        }
        turtle.up();
    })
    return i < 3000;
}