forked from tecky708/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas.html
97 lines (81 loc) · 1.46 KB
/
Canvas.html
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
<!DOCTYPE html>
<html>
<head>
<title>Creative HTML</title>
<style>
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: black;
}
.canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.star {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
}
.star:nth-child(even) {
animation: twinkle 1s infinite;
}
.star:nth-child(odd) {
animation: twinkle 2s infinite;
}
@keyframes twinkle {
from {
opacity: 1;
}
to {
opacity: 0.5;
}
}
</style>
</head>
<body>
<canvas class="canvas"></canvas>
<script>
var canvas = document.querySelector(".canvas");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Create a bunch of stars
var stars = [];
for (var i = 0; i < 1000; i++) {
var star = {
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: Math.random() * 5
};
stars.push(star);
}
// Draw the stars
function drawStars() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < stars.length; i++) {
var star = stars[i];
ctx.beginPath();
ctx.arc(star.x, star.y, star.radius, 0, 2 * Math.PI);
ctx.fillStyle = "white";
ctx.fill();
}
}
// Animate the stars
function animate() {
drawStars();
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>