-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainGradient.js
137 lines (109 loc) · 3.47 KB
/
mainGradient.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
const COLORS = [
{ r: 255, g: 90, b: 90 }, // red
{ r: 45, g: 74, b: 227 }, // blue
{ r: 250, g: 255, b: 89 }, // yellow
{ r: 255, g: 104, b: 248 }, // purple
{ r: 44, g: 209, b: 252 }, // skyblue
{ r:0, g:105, b:255},//lightblue
];
// class는 constructor만드는 신문법이다.
class Canvas {
constructor() {
this.canvas = document.createElement("canvas"); //this는 새로 생성되는 object를 뜻함 즉 this.canvas는 뭐냐 새로생성되는 object의 canvas라는 속성에는 여기적힌 canvas란 값을 대입해 주세요
this.canvas.style.position = "absolute"; //this 는 새로 생성되는 object를 뜻한다. this.age = 15 이렇게 새로 생성되는 object에 값 부여 가능
this.canvas.style.width = 1200+"px";
this.canvas.style.height = 700+"px";
this.canvas.style.backgroundColor = '#686ae3';
this.ctx = this.canvas.getContext("2d");
document.body.appendChild(this.canvas);
this.pixelRatio = document.body.PixelRatio > 1 ? 2 : 1;
this.totalParticles = 6;
this.particles = [];
this.maxRadius = 1300;
this.minRadius = 1300;
window.addEventListener("resize", this.resize.bind(this), false);
this.resize();
window.requestAnimationFrame(this.animate.bind(this));
}
resize() {
this.stageWidth = window.innerWidth;
this.stageHeight = window.innerHeight;
this.canvas.width = this.stageWidth * this.pixelRatio;
this.canvas.height = this.stageHeight * this.pixelRatio;
this.ctx.globalCompositeOperation = `saturation`;
this.createParticles();
}
createParticles() {
let curColor = 0;
this.particles = [];
for (let i = 0; i < this.totalParticles; i++) {
const item = new GlowParticle(
Math.random() * this.stageWidth,
Math.random() * this.stageHeight,
Math.random() * (this.maxRadius - this.minRadius) + this.minRadius,
COLORS[curColor]
);
if (++curColor >= COLORS.length) {
curColor = 0;
}
this.particles[i] = item;
}
}
animate() {
window.requestAnimationFrame(this.animate.bind(this));
this.ctx.clearRect(0, 0, this.stageWidth, this.stageHeight);
for (let i = 0; i < this.totalParticles; i++) {
const item = this.particles[i];
item.animate(this.ctx, this.stageWidth, this.stageHeight);
}
}
}
window.onload = () => {
new Canvas();
};
const PI2 = Math.PI * 2;
class GlowParticle {
constructor(x, y, radius, rgb) {
this.x = x;
this.y = y;
this.radius = radius;
this.rgb = rgb;
this.vx = Math.random() * 3;
this.vy = Math.random() * 3;
this.sinValue = Math.random();
}
animate(ctx, stageWidth, stageHeight) {
this.sinValue += 1;
this.radius += Math.sin(this.sinValue);
this.x += this.vx;
this.y += this.vy;
if (this.x < 0) {
this.vx *= -1;
this.x += 10;
} else if (this.x > stageWidth) {
this.vx *= -1;
this.x -= 10;
}
if (this.y < 0) {
this.vy *= -1;
this.y += 10;
} else if (this.y > stageHeight) {
this.vy *= -1;
this.y -= 10;
}
ctx.beginPath();
const g = ctx.createRadialGradient(
this.x,
this.y,
this.radius * 0.01,
this.x,
this.y,
this.radius
);
g.addColorStop(0, `rgba(${this.rgb.r}, ${this.rgb.g}, ${this.rgb.b}, 1)`);
g.addColorStop(1, `rgba(${this.rgb.r}, ${this.rgb.g}, ${this.rgb.b}, 0)`);
ctx.fillStyle = g;
ctx.arc(this.x, this.y, this.radius, 0, PI2, false);
ctx.fill();
}
}