-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_11.js
121 lines (99 loc) · 2.76 KB
/
day_11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// (c) 2021 Joseph HENRY
// This code is licensed under MIT license (see LICENSE for details)
let imageCounter = 0;
let img;
let time = 0;
const changeTime = 2000;
let particles = [];
/**
* Basic particle class
*/
class Particle {
/**
* @speed is a vector
*/
constructor(x, y, color, speed, initialLife) {
this.location = createVector(x, y);
this.color = color;
this.speed = speed;
this.initialLife = initialLife;
this.life = this.initialLife;
}
/**
* Return normalized age from 1 to 0
*/
getAge() {
return this.life / this.initialLife;
}
/**
* Add speed to location with extra rotation
* also decrease life
*/
update() {
const rotation = noise(this.location.x * 0.1, this.location.y * 0.1) * PI / 100 * (1 - this.getAge());
this.location.add(this.speed.rotate(rotation));
this.life -= 0.1;
}
display() {
const age = this.getAge();
stroke(red(this.color), green(this.color), blue(this.color), age * 20);
strokeWeight(age * 10);
point(this.location.x, this.location.y);
}
}
/**
* Create random particles from the image centered at 0
*/
function createRandomParticlesFromImage(img) {
img.loadPixels();
for (let i = 0; i < 200; i++) {
const rx = int(random(img.width));
const ry = int(random(img.height));
const diffX = rx - img.width / 2;
const diffY = ry - img.height / 2;
const speed = createVector(diffX, diffY).normalize().mult(random(0.01, 0.5));
const loc = 4 * (rx + ry * img.width);
const c = color(img.pixels[loc], img.pixels[loc + 1], img.pixels[loc + 2], 50);
particles.push(new Particle(diffX, diffY, c, speed, random(100, 500)));
}
}
/**
* Get random image from : https://picsum.photos/
*/
function randomImage(size) {
// Add random parameter to load a different image each time
return loadImage("https://picsum.photos/" + size + "?random=" + imageCounter++, (i) => {
// Callback when loaded, replace the current image
img = i;
createRandomParticlesFromImage(img);
});
}
function preload() {
randomImage(100);
}
function setup() {
createCanvas(500, 500);
background("#003344");
}
function draw() {
translate(width / 2, height / 2);
// Display the particles
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.display();
particle.update();
const plx = particle.location.x;
const ply = particle.location.y;
if (particle.life <= 0 || plx > width / 2 || plx < -width / 2 || ply > height / 2 || ply < -height / 2) {
particles.pop();
}
}
// Display the image
imageMode(CENTER);
image(img, 0, 0);
// Change image
if (millis() - time > changeTime) {
randomImage(100);
time = millis();
}
}