xxxxxxxxxx
68
flakes = []
function setup() {
frameRate(144)
createCanvas(windowWidth, windowHeight)
}
function spawnFlake() {
x = random(0, width)
y = - 20
size = 15
flakes.push([
createVector(x,y),
size,
int(random(7,12))
])
}
function mouseClicked(){
spawnFlake()
}
function drawSnowFlake(location, size, arms) {
for (let i = 0; i < arms; i++) {
let angle = TWO_PI / arms * i;
let armEnd = p5.Vector.fromAngle(angle).mult(size).add(location);
drawSnowFlakeArm(location, armEnd, size / 3);
}
}
function drawSnowFlakeArm(start, end, size) {
line(start.x, start.y, end.x, end.y);
if (size > 10) {
let newSize = size / 3;
let angle = PI / 3;
let left = p5.Vector.sub(end, start).rotate(-angle).setMag(newSize).add(end);
let right = p5.Vector.sub(end, start).rotate(angle).setMag(newSize).add(end);
drawSnowFlakeArm(end, left, newSize);
drawSnowFlakeArm(end, right, newSize);
}
}
SPAWNS = [12,43,23,65,87,34,12,87,35]
SPAWN_I = 1
function draw() {
background(55,55,230);
push()
stroke('rgba(26,218,218,0.53)')
indices_to_remove = []
for (const [i,flake] of flakes.entries()){
[l, size, arms] = flake
drawSnowFlake(l, size, arms)
l.y +=1
if(l.y>height+size){
flakes.splice(0,1)
}
}
pop()
if (frameCount % SPAWNS[SPAWN_I]) {
spawnFlake()
SPAWN_I = (1+SPAWN_I) % SPAWNS.length
}
}