Skip to content

Instantly share code, notes, and snippets.

@vivanov1410
Created August 5, 2012 10:42
Show Gist options
  • Select an option

  • Save vivanov1410/3263832 to your computer and use it in GitHub Desktop.

Select an option

Save vivanov1410/3263832 to your computer and use it in GitHub Desktop.
Star Sky (or Snow...)
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
canvas {
border: dotted 3px black;
background-color: #00003B;
}
</style>
<script>
window.onload = function() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 580;
canvas.height = 500;
MAX_STARS = 1000;
var stars = [];
function init() {
for(var i = 0; i < MAX_STARS; i++) {
var star = {};
star.x = Math.floor(Math.random() * canvas.width);
star.y = Math.floor(Math.random() * canvas.height);
star.size = Math.floor(Math.random() * 5 + 1);
stars.push(star);
}
}
function create_star(ctx, x, y, size) {
ctx.fillStyle = "white";
ctx.fillRect(x, y, size, size);
}
function draw_loop() {
canvas.width = canvas.width;
for(var i = 0; i < MAX_STARS; i++) {
star = stars[i];
create_star(ctx, star.x, star.y, star.size);
speed = star.size;
//star.x += speed;
star.y += speed;
star.x += Math.floor(Math.random() * 5 - 1)
if (star.x >= canvas.width) {
star.y = Math.floor(Math.random() * canvas.height);
star.x = 0;
}
if (star.y >= canvas.height) {
star.x = Math.floor(Math.random() * canvas.width);
star.y = 0;
}
}
setTimeout(draw_loop, 33)
}
init();
draw_loop(ctx);
};
</script>
</head>
<body>
<canvas id="canvas">
WOW! Back off! This browser doesn't support canvas
</canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment