-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-starfield.js
97 lines (66 loc) · 1.79 KB
/
canvas-starfield.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
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
var canvas = document.querySelector('canvas');
canvas.width = 400 ;
canvas.height = 400 ;
var context = canvas.getContext('2d');
context.translate(canvas.width/2 , canvas.height/2);
mouseX = undefined;
var speed ;
canvas.addEventListener('mousemove', function(event){
mouseX= event.clientX;
})
function Star(){
this.x = (Math.random()*canvas.width)- canvas.width/2;
this.y = (Math.random()*canvas.height)- canvas.height/2;
this.z = (Math.random()* canvas.width);
this.sx;
this.sy;
this.pz = this.z;
this.update = function(){
if(speed){
this.z -= speed;
if (this.z<1) {
this.z = canvas.width;
this.x = (Math.random()*canvas.width* 1.5)- canvas.width/2;
this.y = (Math.random()*canvas.height*1.5)- canvas.height/2;
this.pz = this.z;
}
}
}
this.show = function(){
var sx = (this.x / this.z)* canvas.width;
var sy = (this.y / this.z)* canvas.height;
this.radius = (canvas.width/this.z) * 2;
context.beginPath();
context.arc(sx, sy , this.radius, 0, Math.PI * 2, false);
context.fillStyle = "#ffffff";
context.fill();
context.closePath();
var px = (this.x / this.pz)* canvas.width;
var py = (this.y / this.pz)* canvas.height;
context.beginPath();
context.moveTo(sx, sy);
context.lineTo(px, py);
context.strokeStyle = '#ffffff';
context.stroke();
this.pz = this.z;
}
}
var starField = []
function init(){
for (var i = 0; i < 800; i++) {
var radius = 2;
starField.push(new Star());
}
}
function animate(){
window.requestAnimationFrame(animate);
speed = (mouseX/canvas.width)*50;
context.fillStyle = "#000000";
context.fillRect(-(canvas.width/2),-(canvas.height/2),canvas.width,canvas.height);
for(var i = 0; i< starField.length; i++){
starField[i].show();
starField[i].update();
}
}
init();
animate();