-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.js
More file actions
120 lines (107 loc) · 3.75 KB
/
canvas.js
File metadata and controls
120 lines (107 loc) · 3.75 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
const canvas = document.getElementById("canvas");
const banner = document.getElementById("header-canvas");
canvas.width = banner.offsetWidth;
canvas.height = banner.offsetHeight;
const ctx = canvas.getContext("2d");
// Give the circles random colors
let colorArray = ["#F25CA2", "#0433BF", "#032CA6", "#021859", "#0B9ED9"];
// Create a mouse object to detect the mouse position
let mouse = {
x: undefined,
y: undefined
};
// Add mouse event to the window object
window.addEventListener("mousemove", function(e) {
// initialize the mouse object by giving ot the current mouse position
mouse.x = e.x;
mouse.y = e.y;
});
// Write constructor Object to create many circles;
function Circle(x, y, dx, dy, radius) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.radius = radius;
this.minRadius = radius;
this.color = colorArray[Math.floor(Math.random() * colorArray.length)];
}
// create function to draw the circle on canvas
Circle.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.strokeStyle = "rgba(0, 255, 255, .3)";
ctx.stroke();
ctx.strokeStyle = this.color;
ctx.stroke();
ctx.fill();
};
// Update the circle to enable movement on each repaint of the canvas
Circle.prototype.update = function() {
this.draw();
if(this.x + this.radius > banner.offsetWidth || this.x - this.radius < 0) {
this.dx = -this.dx;
}
if(this.y + this.radius > banner.offsetHeight || this.y - this.radius < 0) {
this.dy = -this.dy;
}
this.x += this.dx;
this.y += this.dy;
// Create Mouse interactivity by judging position between mouse and circle.
if(mouse.x - this.x < 50 && mouse.x - this.x > -50 &&
mouse.y -this.y < 50 && mouse.y -this.y > -50) {
// Create maximum size for the circle growth
if(this.radius > 40) {
this.radius -= 1;
}
this.radius += 1;
} /* else if(this.radius > this.minRadius) {
this.radius -= 1;
} */
else {
this.radius = this.minRadius; // Return back to original radius;
}
};
let circleArray = [];
function init() {
for (let i = 0; i < 100; i++) {
//Generate random positions for x and y axis, as well as random velocities.
let radius = Math.random() * 10;
let x = Math.random() * (banner.offsetWidth - radius * 2) + radius; //To stop circle from getting stuck at the edge of the canvas
let y = Math.random() * (banner.offsetHeight - radius * 2) + radius;
let dy = Math.random() * 0.2;
let dx = Math.random() * 0.2;
// Push the new object circles into the array.
circleArray.push(new Circle(x, y, dx, dy, radius));
}
}
init();
function animate() {
ctx.clearRect(0, 0, banner.offsetWidth, banner.offsetHeight);
requestAnimationFrame(animate);
for(let i = 0; i < circleArray.length; i++) {
circleArray[i].update();
}
}
animate();
// Redestribute circles on window resize
window.addEventListener("resize", function() {
canvas.width = banner.offsetWidth;
canvas.height = banner.offsetHeight;
// Empty the array and refill it each time the window is resized.
circleArray = [];
if(banner.offsetWidth > 500)
for (let i = 0; i < 250; i++) {
//Generate random positions for x and y axis, as well as random velocities.
const radius = Math.random() * 10;
let x = Math.random() * (banner.offsetWidth - radius * 2) + radius; //To stop circle from getting stuck at the edge of the canvas
let y = Math.random() * (banner.offsetHeight - radius * 2) + radius;
let dy = Math.random() * 0.2;
let dx = Math.random() * 0.2;
let minRadius = radius;
// Push the new object circles into the array.
circleArray.push(new Circle(x, y, dx, dy, radius));
} else {
init();
}
});