forked from Abhash12/College-Club-Website
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (69 loc) · 1.95 KB
/
index.js
File metadata and controls
77 lines (69 loc) · 1.95 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
const canvas = document.getElementById("particle");
const ctx = canvas.getContext("2d");
canvas.Width = window.innerWidth;
canvas.Height = window.innerHeight;
let particleArray;
//mouseposition
let mouse = {
x: null,
y: null,
radius: (canvas.Width / 80) * (canvas.Height / 80),
};
window.addEventListener("mousemove", function (event) {
mouse.x = event.x;
mouse.y = event.y;
});
// particle
// 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.fill();
}
// check particle position, check mouse position, move the
//particle, draw the particle
update() {
//check if particle is still winthin canvas
if (this.x > canvas.width || this.x < 0) {
this.directionX = -this.directionX;
}
if (this.y > canvas.height || this.y < 0) {
this.direction = -this.directionY;
}
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx * dx + dy * dy);
//check collision detection - mouse position / particle position
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();
}
//check collision detection - mouse position / particle position
}