-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
166 lines (143 loc) · 6.45 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
window.onload = () => {
const image = new Image()
image.src = './hestia-min.PNG';
image.addEventListener('load', () => {
let imgRatio = image.width / image.height;
console.log(`img: ${image.width} X ${image.height}, ratio: ${imgRatio}`);
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gradient1 = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient1.addColorStop(0.2, 'pink');
gradient1.addColorStop(0.3, 'red');
gradient1.addColorStop(0.8, 'orange');
gradient1.addColorStop(0.4, 'yellow');
gradient1.addColorStop(0.5, 'green');
gradient1.addColorStop(0.6, 'turquoise');
gradient1.addColorStop(0.7, 'violet');
const letters = ['H', 'E', 'S', 'T', 'I', 'A'];
let switcher = 1;
let counter = 0;
// setInterval(() => {
// counter ++;
// if(counter % 12 === 0) {
// switcher *= -1;
// }
// }, 1000);
ctx.drawImage(image, (canvas.width - Math.min(canvas.width, canvas.height) * imgRatio) / 2, 0, Math.min(canvas.width, canvas.height) * imgRatio, Math.min(canvas.width, canvas.height));
const pixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
ctx.clearRect(0, 0, canvas.width, canvas.height);
let particleArr = [];
const numOfParticle = 10000;
let mappedImage = [];
for(let y = 0; y < canvas.height; y ++) {
let row = [];
for(let x = 0; x < canvas.width; x ++) {
const red = pixels.data[y * 4 * pixels.width + x * 4];
const green = pixels.data[y * 4 * pixels.width + x * 4 + 1];
const blue = pixels.data[y * 4 * pixels.width + x * 4 + 2];
const brightness = calcRelativeBrightness(red, green, blue);
const cell = [
cellBrightness = brightness,
cellColor = `rgb(${red},${green},${blue})`
]
row.push(cell);
}
mappedImage.push(row);
// console.log(`${y}: `, mappedImage.length, mappedImage[y], mappedImage[y].length);
}
// console.log(mappedImage, mappedImage.length, mappedImage[0], mappedImage[0].length);
function calcRelativeBrightness(r, g, b) {
return Math.sqrt(r * r * .299 + g * g * .587 + b * b * .114) / 100
}
class Particle {
constructor() {
this.x = Math.random() * canvas.width;
this.y = 0;
this.speed = 0;
this.velocity = Math.random() * 6.5;
this.size = Math.random() * 1.5 + 1;
this.pos1 = Math.floor(this.y);
this.pos2 = Math.floor(this.x);
this.angle = 0;
this.letter = letters[Math.floor(Math.random() * letters.length)];
this.random = Math.random();
}
update() {
this.pos1 = Math.floor(this.y);
this.pos2 = Math.floor(this.x);
if (mappedImage[this.pos1] && mappedImage[this.pos1][this.pos2]) {
this.speed = mappedImage[this.pos1][this.pos2][0];
}
let movement = (2.5 - this.speed) + this.velocity;
this.angle += this.speed / 20;
this.size = this.speed;
// ctx.globalCompositeOperation = 'lighter'//'luminosity'//'soft-light';
// if (switcher === 1) {
// ctx.globalCompositeOperation = 'luminosity'//'luminosity'//'soft-light';
// } else {
// ctx.globalCompositeOperation = 'soft-light'//'luminosity'//'soft-light';
// }
// if (counter % 10 === 0) {
// this.x = Math.random() * canvas.width;
// this.y = 0;
// }
this.y -= movement + Math.cos(this.angle) * 3;
this.x += movement + Math.sin(this.angle) * 1;
if (this.y >= canvas.height) {
this.y = 0;
this.x = Math.random() * canvas.width;
}
if (this.y < 0) {
this.y = canvas.height;
this.x = Math.random() * canvas.width;
}
if (this.x >= canvas.width) {
this.x = 0;
this.y = Math.random() * canvas.height;
}
}
draw() {
ctx.beginPath();
if (mappedImage[this.pos1] && mappedImage[this.pos1][this.pos2]) {
ctx.fillStyle = mappedImage[this.pos1][this.pos2][1];
ctx.strokeStyle = mappedImage[this.pos1][this.pos2][1];
}
// ctx.fillStyle = gradient1;
// ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
// ctx.strokeRect(this.x, this.y, this.size * 2, this.size * 2);
if (this.random < 0.3) {
ctx.font = '10px Arial';
ctx.fillText(this.letter, this.x, this.y);
} else if (0.3 <= this.random && this.random < 0.6) {
ctx.strokeRect(this.x, this.y, this.size * 2, this.size * 2);
} else {
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
}
ctx.fill();
}
}
function init() {
for(let i = 0; i < numOfParticle; i ++) {
particleArr.push(new Particle);
}
}
init();
function animate() {
// ctx.drawImage(image, (canvas.width - Math.min(canvas.width, canvas.height) * imgRatio) / 2, 0, Math.min(canvas.width, canvas.height) * imgRatio, Math.min(canvas.width, canvas.height));
ctx.globalAlpha = 0.05;
ctx.fillStyle = 'rgb(0, 0, 0)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalAlpha = 0.25;
particleArr.forEach(particle => {
particle.update();
// ctx.globalAlpha = particle.speed * 0.25;
ctx.globalAlpha = 1;
particle.draw();
});
requestAnimationFrame(animate);
}
animate();
})
}