-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstars.js
63 lines (51 loc) · 1.28 KB
/
stars.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
// Setting the Canvas width and height to fill the web browser.
var canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var c = canvas.getContext('2d');
var numStars = 300;
var stars = []; //Empty array
var size = 1;
var fl = canvas.width;
var centerX = canvas.width/2;
var centerY = canvas.height/2;
var speed = 8;
for(var i=0; i<numStars; i++) {
stars[i] = new Star();
}
function Star() {
this.x = Math.random()*canvas.width; //x axis location
this.y = Math.random()*canvas.height; //y axis
this.z = Math.random()*canvas.width; //depth of star
this.move = function(){
this.z = this.z-speed;
if(this.z<=0) {
this.z = canvas.width;
}
}
this.show = function() {
var x, y, s; //x-axis, y-axis, size
x = (this.x - centerX) * (fl/this.z);
x = x + centerX;
y = (this.y - centerY ) * (fl/this.z);
y=y+centerY;
s = size * (fl/this.z);
c.beginPath();
c.fillStyle = 'white';
c.arc(x,y,s, 0, Math.PI*2);
c.fill();
}
}
function draw() {
c.fillStyle = 'black';
c.fillRect(0,0, canvas.width, canvas.height);
for(var i=0; i<numStars; i++) {
stars[i].show();
stars[i].move();
}
}
function update() {
draw();
window.requestAnimationFrame(update);
}
update();