-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
162 lines (133 loc) · 3.87 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
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
var canvas = document.querySelector('canvas');
// Sets the canvas to cover the full browser screen
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// This gets the context of which inputs will be set
var c = canvas.getContext('2d');
//
// // This property is for defining the fill color of a drawn object.
// c.fillStyle = 'rgba(255, 0, 0, 0.5)';
// c.fillRect(100, 100, 100, 100);
//
// c.fillStyle = 'rgba(0, 255, 0, 0.5)';
// c.fillRect(400, 100, 100, 100);
//
// c.fillStyle = 'rgba(0, 0, 255, 0.5)';
// c.fillRect(300, 300, 100, 100);
//
// c.beginPath();
// // Starting point
// c.moveTo(50, 300);
// // ending point
// c.lineTo(300, 100);
// c.lineTo(400, 300);
// // This sets the color and it will take any css valid color values
// c.strokeStyle = "#fa34a3";
// c.stroke();
//
// // Arc/Circle
// // c.beginPath();
// // c.arc(300, 300, 30, 0, Math.PI * 2, false);
// // c.strokeStyle = 'blue';
// // c.stroke();
//
// function Circle(x, y)
// var colors = ['red', 'blue', 'green'];
// for (var i = 0; i < 500; i++) {
// // Using multiply by the size of the area to randomize location of drawn object
// var x = Math.random() * window.innerWidth;
// var y = Math.random() * window.innerHeight;
// c.beginPath();
// c.arc(x, y, 30, 0, Math.PI * 2, false);
// var randNum = (Math.random() * colors.length);
// c.strokeStyle = colors[Math.floor(randNum)];
// c.stroke();
// }
// var x = Math.random() * innerWidth;
// var y = Math.random() * innerHeight;
// var dy= (Math.random() - 0.5) * 8; // Can create negative speeds
// var dx = (Math.random() - 0.5) * 8;
// var radius = 30;
var mouse = {
x: undefined,
y: undefined
}
var maxRadius = 40;
var minRadius = 2;
var colorArray = [
'#263650',
'#E74E4E',
'#F7FBFC',
'#57CBFF',
'#2273AA'
];
console.log(colorArray);
window.addEventListener('mousemove',
function (event) {
mouse.x = event.x;
mouse.y = event.y
})
window.addEventListener('resize', function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
init();
})
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 saves original size state
this.color = colorArray[Math.floor(Math.random() * colorArray.length)];
this.draw = function () {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
// c.strokeStyle = 'blue';
// c.stroke();
c.fillStyle = this.color;
c.fill();
}
this.update = function() {
if (this.x + this.radius > innerWidth ||
this.x - this.radius < 0) { this.dx = -this.dx; }
if (this.y + this.radius > innerHeight ||
this.y - this.radius < 0) { this.dy = -this.dy; }
this.x += this.dx;
this.y += this.dy;
// Interactivity
if (mouse.x - this.x < 50 && mouse.x - this.x > (-50) &&
mouse.y - this.y < 50 && mouse.y - this.y > (-50)) {
// Size check
if (this.radius < maxRadius) {
this.radius += 1;
}
}
else if (this.radius > this.minRadius) {
this.radius -= 1;
}
// This will be the method called
this.draw();
}
}
var circleArray = [];
function init() {
circleArray = [];
for (var i = 0; i < 800; i++) {
var x = Math.random() * (innerWidth - (radius * 2 )) + radius; // subtracting the radius to prevent being spawned out of bounds
var y = Math.random() * (innerHeight - (radius * 2 )) + radius;
var dy= (Math.random() - 0.5) * 8; // Can create negative speeds
var dx = (Math.random() - 0.5) * 8;
var radius = (Math.random() * 3) + 1;
circleArray.push(new Circle( x, y, dx, dy, radius));
}
}
function animate() {
requestAnimationFrame(animate);
c.clearRect(0,0, innerWidth, innerHeight);
for (var i = 0; i < circleArray.length; i++) {
circleArray[i].update();
}
}
init();
animate();