fractalkitty looking for math? fractalkitty.com

codepen challenges

windy dreams

#random#trig#vectors#classes#interactive#animation#sky#weather#codepen-challenge

click to change

duneAsandAstarsnStarsduneDrawduneSandstarsM


let duneA = [];
let sandA = [];
let stars = [];
let cnt = 0;
let nStars;
let nD;
function setup() {
  duneA = [];
  sandA = [];
  nStars = int(random(100, 800));
  c = min(windowWidth * 0.9, 700);
  r1 = random(120, 250);
  g1 = random(100, 140);
  b1 = random(50, 150);
  createCanvas(c, c / 2);
  noFill();
  strokeWeight(4);
  angleMode(DEGREES);
  color1 = color(random(30), random(40), random(40, 50));
  nD = int(random(4, 6));
  for (let i = 0; i <= nD; i++) {
    duneA[i] = [];
    dune(i, true);
  }
  for (let i = 0; i < duneA[nD].length; i++) {
    sandA.push(new Sand(duneA[nD - 1][i].x, duneA[nD - 1][i].y));
  }
  for (let i = 0; i < duneA[nD].length; i += 2) {
    sandA.push(new Sand(duneA[nD - 2][i].x, duneA[nD - 2][i].y));
  }
  starsM();
}
function draw() {
  t = frameCount * 10;
  translate(width / 2, height / 2);
  push();
  background(color1);
  rotate(frameCount / 50);
  for (let i = 0; i < nStars; i++) {
    fill(255);
    noStroke();

    circle(stars[i].x, stars[i].y, 1);
  }
  pop();
  for (let i = 0; i < nD; i++) {
    duneDraw(i);
  }
  for (let i = 0; i < sandA.length; i++) {
    sandA[i].display();
  }
}

function duneDraw(n) {
  for (let j = 0; j < int(height * 1.2); j += 4.5) {
    stroke(r1 - j, g1 - j, b1 - j);
    beginShape();
    for (let i = 0; i < duneA[n].length; i++) {
      vertex(duneA[n][i].x, duneA[n][i].y + j / 1.5);
    }
    endShape();
  }
}

function dune(n) {
  a = random(5, 20);
  b = random(4, 10);
  c = random(20, 360);
  cnt = 0;
  for (let i = round(-width / 2); i < round(width / 2 + 1); i += 4) {
    oldy = random(1);
    if (n % 2 === 0) {
      y =
        -oldy -
        a * b * sin(i / (a + b) + c) * cos(-i / b) +
        b * cos(i / b + c) * sin(i / c) +
        n * 10;
    } else {
      y =
        oldy +
        a * b * sin(i / (a + b) + c) * cos(-i / b) -
        b * cos(i / b + c) * sin(i / c) +
        n * 10;
    }
    duneA[n][cnt] = createVector(i, y);
    cnt++;
  }
}

function mousePressed() {
  setup();
  draw();
}

class Sand {
  constructor(x, y) {
    this.x1 = x;
    this.y1 = y;
    this.x = x;
    this.y = y;
    this.s = random(2, 10);
    this.r = random(5, 10);
  }
  display() {
    let d = dist(this.x, this.y, this.x1, this.y1);

    this.x = this.x + this.s;
    this.y = this.y - this.s / (this.r * abs(sin(t / 100)) + 4);
    if (d > 255) {
      this.x = this.x1;
      this.y = this.y1;
    }
    push();
    strokeWeight(0.5);
    noStroke();
    fill(r1, g1, b1, 255 - this.d);
    circle(this.x, this.y, 0.5);
    circle(this.x - 100, this.y - 10, 0.3);
    circle(this.x - 140, this.y - this.r, 0.3);
    circle(this.x - 140 + this.s, this.y + this.r, 0.3);
    circle(this.x - 120, this.y - 10 * this.s, 0.2);
    circle(this.x - 220, this.y - 10 * this.s, 0.2);
    pop();
  }
}

function starsM() {
  for (let i = 0; i < nStars; i++) {
    stars[i] = createVector(random(-width, width), random(-height, height));
  }
}