-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
176 lines (156 loc) · 4.72 KB
/
main.js
File metadata and controls
176 lines (156 loc) · 4.72 KB
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
167
168
169
170
171
172
173
174
/**
* INITIAL CONFIG
*/
const settings = {
radius: 80,
scanner: {
sTop: 0,
sLeft: 0,
width: 400,
height: 300
},
opacityTreeshold: 128,
line: {
width: 2,
maxLength: 45,
color: 'rgba(255,255,255,1)'
},
magnet: {
force: 10
},
offsetX: 50,
offsetY: -200,
endedTouch: false,
restart: false
}
const mouse = {
x: null,
y: null,
radius: settings.radius,
};
const canvas = document.getElementById("canvas1");
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - (window.innerWidth <= 400 ? 175 : 115);
canvas.style.letterSpacing = '2px';
let particles = [];
ctx.font = `${getTextSize()}px Verdana`;
ctx.fillStyle = 'white';
let textString = window.innerWidth < 800 ? 'C' : 'Code' ;
ctx.fillText(textString, 0, 30);
const {sTop, sLeft, width, height} = settings.scanner;
let textCoords = ctx.getImageData(sTop, sLeft, width, height);
textCoords.data = textCoords.data.filter( (_, idx) => (idx + 1) % 4 === 0 );
function getTextSize() {
if (window.innerWidth < 800) return 12;
if (window.innerWidth < 1200) return 18;
return 21;
}
function changeText() {
settings.restart = true;
particles = [];
const text = document.getElementById("textInp").value;
textString = window.innerWidth < 800 ? text[0] : text;
ctx.fillText(textString, 0, 30);
textCoords = ctx.getImageData(sTop, sLeft, width, height);
textCoords.data = textCoords.data.filter( (_, idx) => (idx + 1) % 4 === 0 );
init();
animate();
}
/**
* CLASS INITIALIZATION
*/
class Particle {
constructor(x, y, size = 3) {
this.x = x;
this.y = y;
this.size = size;
this.baseX = this.x;
this.baseY = this.y;
this.density = Math.random() * 30 + 1;
this.elasticity = Math.random() * 50 + 1;
this.maxDistance = mouse.radius + this.elasticity;
}
draw() {
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
}
update() {
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.maxDistance && !settings.endedTouch) {
let force = (this.maxDistance - distance) / this.maxDistance;
this.x -= (dx / distance) * force * this.density * 2;
this.y -= (dy / distance) * force * this.density * 2;
} else {
let magnetForce = settings.magnet.force;
this.x -= (this.x - this.baseX) / magnetForce;
this.y -= (this.y - this.baseY) / magnetForce;
}
}
}
// PROGRAM BODY
init();
animate();
function init() {
let i = 0;
for(let y=0; y < textCoords.height; y++ ) {
for(let x=0; x < textCoords.width; x++) {
if(textCoords.data[i] > settings.opacityTreeshold && x%4 === 0) {
let positionX = (x * 5) + settings.offsetX;
let positionY = (y * 5) + settings.offsetY;
particles.push(new Particle(positionX, positionY) );
}
i++;
}
}
settings.restart = false;
}
function animate() {
ctx.clearRect(0,0,canvas.width, canvas.height);
particles.forEach( particle => {
particle.draw();
particle.update();
})
connect();
if(!settings.restart) requestAnimationFrame(animate)
}
function connect() {
for(let a = 0; a < particles.length; a++ ) {
for(let b = a; b < particles.length; b++) {
let dx = particles[a].x - particles[b].x;
let dy = particles[a].y - particles[b].y;
let distance = Math.sqrt(dx * dx + dy * dy) ;
let color = settings.line.color;
let opacity = 1 - (distance/settings.line.maxLength);
if(distance < settings.line.maxLength) {
ctx.strokeStyle = color.replace('1', opacity );
ctx.lineWidth = settings.line.width;
ctx.beginPath()
ctx.moveTo(particles[a].x, particles[a].y);
ctx.lineTo(particles[b].x, particles[b].y);
ctx.stroke();
}
}
}
}
window.addEventListener('mousemove', event => {
mouse.x = event.x;
mouse.y = event.y;
//console.log(mouse.x, mouse.y)
});
window.addEventListener('touchmove', event => {
settings.endedTouch = false;
mouse.x = event.touches[0].clientX;
mouse.y = event.touches[0].clientY;
console.log(event)
});
window.addEventListener('touchend', event => {
//mouse.x = event.touches[0].clientX;
//mouse.y = event.touches[0].clientY;
settings.endedTouch = true;
});