Skip to content

Commit ce125be

Browse files
add canvas's animations
1 parent dcb6a10 commit ce125be

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

assets/js/anime.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const canvas2 = document.getElementById('anime');
2+
const ctx2 = canvas.getContext('2d');
3+
canvas.style.position = 'fixed';
4+
canvas.style.top = 0;
5+
canvas.style.left = 0;
6+
canvas.style.zIndex = '-1';
7+
canvas.style.width = '100%';
8+
canvas.style.height = '100%';
9+
canvas.style.background = '#0f1720';
10+
11+
let snowflakes = [];
12+
const numFlakes = 100;
13+
14+
function resizeCanvas(){
15+
canvas.width = window.innerWidth;
16+
canvas.height = window.innerHeight;
17+
}
18+
window.addEventListener('resize', resizeCanvas);
19+
resizeCanvas();
20+
21+
class Snowflake {
22+
constructor(){
23+
this.reset();
24+
}
25+
reset(){
26+
this.x = Math.random() * canvas.width;
27+
this.y = Math.random() * canvas.height;
28+
this.radius = Math.random() * 3 + 1;
29+
this.speedY = Math.random() * 1 + 0.5;
30+
this.speedX = Math.random() * 0.5 - 0.25;
31+
}
32+
update(){
33+
this.y += this.speedY;
34+
this.x += this.speedX;
35+
if(this.y > canvas.height){
36+
this.y = -this.radius;
37+
this.x = Math.random() * canvas.width;
38+
}
39+
}
40+
draw(){
41+
ctx.beginPath();
42+
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
43+
ctx.fillStyle = "rgba(255,255,255,0.9)";
44+
ctx.fill();
45+
}
46+
}
47+
48+
for(let i=0;i<numFlakes;i++) snowflakes.push(new Snowflake());
49+
50+
function animate(){
51+
ctx.clearRect(0,0,canvas.width,canvas.height);
52+
for(let flake of snowflakes){
53+
flake.update();
54+
flake.draw();
55+
}
56+
requestAnimationFrame(animate);
57+
}
58+
animate();

assets/js/bg.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const canvas = document.getElementById('bgCanvas');
2+
const ctx = canvas.getContext('2d');
3+
canvas.style.position = 'fixed';
4+
canvas.style.top = 0;
5+
canvas.style.left = 0;
6+
canvas.style.zIndex = '-1';
7+
canvas.style.width = '100%';
8+
canvas.style.height = '100%';
9+
canvas.style.background = '#0f1720';
10+
11+
let stars = [];
12+
const numStars = 150;
13+
14+
// Resize
15+
function resizeCanvas(){
16+
canvas.width = window.innerWidth;
17+
canvas.height = window.innerHeight;
18+
}
19+
window.addEventListener('resize', resizeCanvas);
20+
resizeCanvas();
21+
22+
// Star class
23+
class Star {
24+
constructor(){
25+
this.reset();
26+
}
27+
reset(){
28+
this.x = Math.random() * canvas.width;
29+
this.y = Math.random() * canvas.height;
30+
this.size = Math.random() * 1.5;
31+
this.alpha = Math.random();
32+
this.speed = 0.002 + Math.random() * 0.02;
33+
}
34+
twinkle(){
35+
this.alpha += this.speed;
36+
if(this.alpha >= 1 || this.alpha <= 0){
37+
this.speed *= -1;
38+
}
39+
}
40+
draw(){
41+
ctx.beginPath();
42+
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
43+
ctx.fillStyle = `rgba(255,255,255,${this.alpha})`;
44+
ctx.fill();
45+
}
46+
}
47+
48+
// Create stars
49+
for(let i=0;i<numStars;i++) stars.push(new Star());
50+
51+
// Animate
52+
function animate(){
53+
ctx.clearRect(0,0,canvas.width,canvas.height);
54+
for(let s of stars){
55+
s.twinkle();
56+
s.draw();
57+
}
58+
requestAnimationFrame(animate);
59+
}
60+
animate();

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ <h1 id="modal-title"></h1>
4646
</div>
4747
</div>
4848

49+
<canvas id="bgCanvas"></canvas>
50+
<canvas id="anime"></canvas>
51+
4952
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
5053
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js"></script>
5154

5255
<script src="./assets/js/main.js"></script>
56+
<script src="./assets/js/anime.js"></script>
57+
<script src="./assets/js/bg.js"></script>
5358
</body>
5459
</html>

0 commit comments

Comments
 (0)