-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
185 lines (161 loc) · 5.15 KB
/
Copy pathapp.js
File metadata and controls
185 lines (161 loc) · 5.15 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
175
176
177
178
179
180
181
182
183
184
const canvas = document.getElementById("canvas1");
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = windows.innerHeight;
let particlesArray;
// get mouse position
let mouse = {
x: null,
y: null,
radius: (canvas.height/80) * (canvas.width/80)
}
window.addEventListener('mousemove',
function(event){
mouse.x = event.x;
mouse.y = event.y;
}
);
//create particle
class Particle {
constructor(x, y, directionX, directionY, size, color) {
this.x = x;
this.y = y;
this.directionX = directionX;
this.directionY = directionY;
this.size = size;
this.color = color;
}
// method to draw individual particle
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
// ctx.fillStyle = '#8C5523';
ctx.fillStyle = '#28A072FF';
ctx.fill();
}
// check particle position, check mouse position, move the particle, draw the particle
update() {
// check if particle is stil wihin canvas
if (this.x > canvas.width || this.x < 0) {
this.directionX = -this.directionX;
}
if (this.y > canvas.height || this.y < 0) {
this.directionY = -this.directionY;
}
// check collission detection - mouse position / particle position
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx*dy + dy*dy);
if (distance < mouse.radius + this.size){
if (mouse.x < this.x && this.x < canvas.width - this.size * 10) {
this.x +=10;
}
if (mouse.x > this.x && this.x > this.size * 10) {
this.x -= 10;
}
if (mouse.y < this.y && this.y < canvas.height - this.size * 10){
this.y += 10;
}
if (mouse.y > this.y && this.y > this.size * 10){
this.y -=10;
}
}
// move particle
this.x += this.directionX;
this.y += this.directionY;
// draw particle
this.draw();
}
}
// create particle array
function init() {
particlesArray = [];
let numberOfParticles = (canvas.height * canvas.width) / 9000;
for (let i = 0; i < numberOfParticles; i++) {
let size = (Math.random() * 5) + 1;
let x = (Math.random() * ((innerWidth - size * 2) - (size * 2))+ size * 2);
let y = (Math.random() * ((innerHeight - size * 2) - (size * 2))+ size * 2);
let directionX = (Math.random() * 5) - 2.5;
let directionY = (Math.random() * 5) - 2.5;
let color = '#8C5523';
particlesArray.push(new particlesArray(x, y, directionY, size, color));
}
}
// check if particles are close enough to draw line between them
function connect(){
for (let a = 0; a < particlesArray.length; a++) {
for (let b = a; b < particlesArray.length; b++) {
let distance = ((particlesArray[a].x - particlesArray[b].x)
* (particlesArray[a].x - particlesArray[b].x))
+ ((particlesArray[a].y - particlesArray[b].y)
(particlesArray[a].y - particlesArray[b].y));
if (distance < (canvas.width/7) * (canvas.height/7)) {
ctx.strokeStyle = 'rgba(140,85,31,1)';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(particlesArray[a].x, particlesArray[a].y);
ctx.lineTo(particlesArray[b].x, particlesArray[b].y);
ctx.stroke();
}
}
}
}
//animation loop
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0,0,innerWidth, innerHeight);
for (let i = 0; i < particlesArray.length; i++) {
particlesArray[i].update();
}
connect();
}
// resize event
window.addEventListener('resize',
function(){
canvas.width = innerWidth;
canvas.height = innerHeight;
mouse.radius = ((canvas.height/80) * (canvas.height/80));
init();
}
)
init();
animate();
//FORM
// document.getElementById("contactForm").addEventListener("submit", function(event) {
// let isValid = true;
// let name = document.getElementById("name");
// let email = document.getElementById("email");
// let message = document.getElementById("message");
// let nameError = document.getElementById("nameError");
// let emailError = document.getElementById("emailError");
// let messageError = document.getElementById("messageError");
// // Reset errors
// nameError.style.display = "none";
// nameError.style.opacity = "0";
// emailError.style.display = "none";
// emailError.style.opacity = "0";
// messageError.style.display = "none";
// messageError.style.opacity = "0";
// if (name.value.trim() === "") {
// nameError.style.display = "block";
// setTimeout(() => nameError.style.opacity = "1", 10);
// isValid = false;
// }
// let emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// if (!emailPattern.test(email.value.trim())) {
// emailError.style.display = "block";
// setTimeout(() => emailError.style.opacity = "1", 10);
// isValid = false;
// }
// if (message.value.trim() === "") {
// messageError.style.display = "block";
// setTimeout(() => messageError.style.opacity = "1", 10);
// isValid = false;
// }
// if (!isValid) {
// event.preventDefault();
// } else {
// alert("Form submitted successfully!");
// document.getElementById("contactForm").reset();
// }
// });