fractalkitty looking for math? fractalkitty.com

codepen challenges

minimalLeaves

#random#trig#classes#image#interactive#animation#math#symmetry#plants#codepen-challenge

treeleafLeaves

let tree;
let leaf;
let L = [];
let n = 0,
	t = 0;
dt = 100;
function preload() {
	tree = loadImage("/assets/tree3.png");
	leaf = loadImage("/assets/leaf1.png");
}
function setup() {
	cnv = createCanvas(windowWidth, windowHeight);
	stroke(255, 255, 255, 100);
	strokeWeight(3);
	noFill();
}
function draw() {
	background(255);
	t = frameCount / dt;
	w = min(width, height);
	h = w;
	translate(width / 2, height / 2);
	image(tree, -w / 4, 0 - h / 3, w / 2, h / 3);
	push();
	scale(1, -1);
	image(tree, -w / 4, 0 - h / 3, w / 2, h / 3);
	pop();
	for (let i = 0; i < n; i++) {
		push();
		translate(L[i].driftX, L[i].driftY);
		L[i].display();
		L[i].move();
		pop();
	}
}

function mousePressed() {
	if (mouseY - height / 2 < 0) {
		L.push(new Leaves(mouseX - width / 2, mouseY - height / 2));
		n++;
	}
}

class Leaves {
	constructor(x, y) {
		this.x1 = x;
		this.y1 = y;
		this.r = random(-PI, PI);
		this.x = x;
		this.y = y;
		this.d = 0;
		this.offset = frameCount / dt;
		this.driftX = 0;
		this.driftY = 0;
		this.driftV = random(-0.5, 0.5);
		this.falling = 0;
	}
	display() {
		push();
		translate(this.x, this.y);
		rotate(this.r);
		image(leaf, 0, 0, 10, 10);
		pop();
		push();
		scale(1, -1);
		translate(this.x, this.y);
		rotate(this.r);
		image(leaf, 0, 0, 10, 10);
		pop();
	}
	move() {
		if (this.falling === 0 && this.y < 0) {
			this.x =
				this.x1 * cos(t - this.offset) +
				(this.r * sin(t - this.offset) * cos(t - this.offset)) / 2;
			this.y = this.y + 0.2;
		} else {
			this.falling = 1;
			if (this.d <= width) {
				arc(this.x, this.y, this.d, this.d / 2, 0, PI);
				this.d = this.d + 1;
				this.driftX += this.driftV;
				this.driftY += abs(this.driftV);
			}
		}
	}
}