xxxxxxxxxx
const flakeCount = 350; // was 50
let flakeArray = [];
let tmpStg, flake, wind, wdnois;
function setup() {
createCanvas(windowWidth, windowHeight);
imageMode(CENTER);
for (let i = 0; i < flakeCount; i++) {
flakeArray.push(new Snowflake());
}
wind = 0;
wdnois = random();
}
function draw() {
background(0);
wind = noise(wdnois) * 4 - 2;
flakeArray.forEach(obj => { obj.display(); obj.update(); });
wdnois += 0.002;
}
class Snowflake {
constructor() {
this.flakeimg = setStg();
this.scl = random(.3, 1.7);
this.loc = createVector(random(0, width), random(0, height));
this.spd = random(.3, 1.5);
this.rotspd = random(30, 150);
if (random() < .5 ) { this.rotspd *= -1; };
this.holspd = random(-0.001,0.001);
this.holnoise = random(0,2000);
}
update() {
this.loc.x += (this.holspd+wind) * this.scl * 1.5;
this.loc.y += this.spd * this.scl;
if (this.loc.y - 50 * this.scl > height) {
this.scl = random(.3, 1.7);
this.loc.y = -50 * this.scl;
this.loc.x = random(0, width);
this.spd = random(.3, 1.5);
this.rotspd = random(30, 150);
if (random() < .5) { this.rotspd *= -1; };
}
if (this.loc.x - 50 * this.scl > width) {
this.loc.x = -50 * this.scl;
}
if (this.loc.x + 50 * this.scl < 0) {
this.loc.x = width + 50 * this.scl;
}
this.holspd += (noise(this.holnoise)-0.5)/200;
this.holnoise += 0.002;
}
display() {
push();
translate(this.loc.x, this.loc.y);
rotate(frameCount / this.rotspd);
scale(this.scl, this.scl);
image(this.flakeimg, 0, 0);
pop();
}
}
function setStg() {
let current;
flake = [];
tmpStg = createGraphics(100, 100);
tmpStg.colorMode(HSB);
tmpStg.translate(tmpStg.width / 2, tmpStg.height / 2);
tmpStg.rotate(PI / 6);
tmpStg.noFill();
let stcol = random(150, 330);
tmpStg.stroke(stcol, 60, 150, .5); // was (stcol, 60, 100, .5)
tmpStg.strokeWeight(1); // was 2
let isFinish = false;
while (!isFinish) {
current = new Particle(tmpStg.width / 2, random(2));
while (!current.finished() && !current.intersects()) {
current.update();
}
flake.push(current);
if (current.pos.x >= tmpStg.width / 2) {
for (let j = 0; j < 6; j++) {
tmpStg.rotate(PI / 3);
for (let i of flake) {
i.show();
}
tmpStg.push();
tmpStg.scale(1, -1);
for (let i of flake) {
i.show();
}
tmpStg.pop();
}
isFinish = true;
}
}
return tmpStg;
}
class Particle {
constructor(x, y) {
this.pos = createVector(x, y);
this.r = 1;
}
update() {
this.pos.x -= 1;
this.pos.y += random(-3, 3);
let angle = this.pos.heading();
angle = constrain(angle, 0, PI / 6);
let magnitude = this.pos.mag();
this.pos = p5.Vector.fromAngle(angle);
this.pos.setMag(magnitude);
}
show() {
tmpStg.point(this.pos.x, this.pos.y);
}
finished() {
return (this.pos.x < 1);
}
intersects() {
let result = false;
for (let i of flake) {
let d = dist(i.pos.x, i.pos.y, this.pos.x, this.pos.y);
if (d < this.r * 2) {
result = true;
break;
}
}
return result;
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}