-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from sahil-067/patch-1
Create Canvas.html
- Loading branch information
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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> |