-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
132 lines (111 loc) · 3.14 KB
/
canvas.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
var canvas = document.querySelector('canvas'); // searches for canvas in html
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var c = canvas.getContext('2d'); // returning drawing context to variable c <- toolset to use on canvas
var color_Array = [
'#562135',
'#c3829e',
'#e9b1cd',
'#ffe7de',
'#fcd1d7',
'#5bc8b2',
'#7ed9ca',
'#a2e8dc',
'#d8fff8',
'#2fbcbc'
]
class Circle{
constructor(x = (Math.random() * (innerWidth - 60)) + 30, y = (Math.random() * (innerHeight - 60)) + 30, radius = Math.random() * 30){
this.x = x;
this.y = y;
this.min_Radius = radius + 1;
this.max_Radius = radius + 50;
this.radius = radius;
this.dx = (Math.random() - 0.5) * 4;
this.dy = (Math.random() - 0.5) * 4;
this.color = color_Array[Math.floor(Math.random() * color_Array.length)];
this.bouncing_x = false;
this.bouncing_y = false;
}
Draw(){
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.strokeStyle = '#999999';
c.stroke();
c.fillStyle = this.color;
c.fill();
}
Update(){
if(this.x < this.radius){
if(Math.abs(this.dx) > 0.4){
this.dx = Math.abs(this.dx) * 0.7;
}
else{
this.dx = 0;
}
this.x = this.radius;
}
else if(this.x > canvas.width - this.radius){
if(Math.abs(this.dx) > 0.4){
this.dx = -Math.abs(this.dx) * 0.7;
}
else{
this.dx = 0;
}
this.x = canvas.width - this.radius;
}
if(this.y < this.radius){
if(Math.abs(this.dy) > 0.4){
this.dy = Math.abs(this.dy) * 0.7;
}
else{
this.dy = 0;
}
this.y = this.radius;
}
else if(this.y > canvas.height - this.radius){
if(Math.abs(this.dy) > 0.4){
this.dy = -Math.abs(this.dy) * 0.7;
}
else{
this.dy = 0;
}
this.y = canvas.height - this.radius;
}
else{
//this.dy += 0.1;
}
this.x += this.dx;
this.y += this.dy;
if(Math.sqrt((mouse.x - this.x) ** 2 + (mouse.y - this.y) ** 2) < 100){
if(this.radius < this.max_Radius){
this.radius += 2.0;
}
}
else{
if(this.radius > this.min_Radius){
this.radius -= 2.0;
}
}
}
}
var mouse = {
x: undefined,
y: undefined
}
window.addEventListener('mousemove', function(event) { mouse.x = event.x; mouse.y = event.y; } )
var circles = [];
var num_Circles = 1500;
for(i = 0; i < num_Circles; i++){
circles.push(new Circle());
}
function animate(){
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
c.globalCompositeOperation = "darken";
for(i = 0; i < circles.length; i++){
circles[i].Update();
circles[i].Draw();
}
}
animate();