Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epsylon
GitHub Repository: epsylon/ufonet
Path: blob/master/core/js/stars.js
1212 views
1
function $i(id) { return document.getElementById(id); }
2
function $r(parent,child) { (document.getElementById(parent)).removeChild(document.getElementById(child)); }
3
function $t(name) { return document.getElementsByTagName(name); }
4
function $c(code) { return String.fromCharCode(code); }
5
function $h(value) { return ('0'+Math.max(0,Math.min(255,Math.round(value))).toString(16)).slice(-2); }
6
function _i(id,value) { $t('div')[id].innerHTML+=value; }
7
function _h(value) { return !hires?value:Math.round(value/2); }
8
9
function get_screen_size()
10
{
11
var w=document.documentElement.clientWidth;
12
var h=document.documentElement.clientHeight;
13
return Array(w,h);
14
}
15
16
var url=document.location.href;
17
18
var flag=true;
19
var test=true;
20
var n=parseInt((url.indexOf('n=')!=-1)?url.substring(url.indexOf('n=')+2,((url.substring(url.indexOf('n=')+2,url.length)).indexOf('&')!=-1)?url.indexOf('n=')+2+(url.substring(url.indexOf('n=')+2,url.length)).indexOf('&'):url.length):512);
21
var w=0;
22
var h=0;
23
var x=0;
24
var y=0;
25
var z=0;
26
var star_color_ratio=0;
27
var star_x_save,star_y_save;
28
var star_ratio=256;
29
var star_speed=1;
30
var star_speed_save=0;
31
var star=new Array(n);
32
var color;
33
var opacity=0.1;
34
35
var cursor_x=0;
36
var cursor_y=0;
37
var mouse_x=0;
38
var mouse_y=0;
39
40
var canvas_x=0;
41
var canvas_y=0;
42
var canvas_w=0;
43
var canvas_h=0;
44
var context;
45
46
var key;
47
var ctrl;
48
49
var timeout;
50
var fps=0;
51
52
function init()
53
{
54
var a=0;
55
for(var i=0;i<n;i++)
56
{
57
star[i]=new Array(5);
58
star[i][0]=Math.random()*w*2-x*2;
59
star[i][1]=Math.random()*h*2-y*2;
60
star[i][2]=Math.round(Math.random()*z);
61
star[i][3]=0;
62
star[i][4]=0;
63
}
64
var starfield=$i('starfield');
65
starfield.style.position='absolute';
66
starfield.width=w;
67
starfield.height=h;
68
context=starfield.getContext('2d');
69
context.fillStyle='rgb(0,0,0)';
70
context.strokeStyle='rgb(255,255,255)';
71
}
72
73
function anim()
74
{
75
mouse_x=cursor_x-x;
76
mouse_y=cursor_y-y;
77
context.fillRect(0,0,w,h);
78
for(var i=0;i<n;i++)
79
{
80
test=true;
81
star_x_save=star[i][3];
82
star_y_save=star[i][4];
83
star[i][0]+=mouse_x>>4; if(star[i][0]>x<<1) { star[i][0]-=w<<1; test=false; } if(star[i][0]<-x<<1) { star[i][0]+=w<<1; test=false; }
84
star[i][1]+=mouse_y>>4; if(star[i][1]>y<<1) { star[i][1]-=h<<1; test=false; } if(star[i][1]<-y<<1) { star[i][1]+=h<<1; test=false; }
85
star[i][2]-=star_speed; if(star[i][2]>z) { star[i][2]-=z; test=false; } if(star[i][2]<0) { star[i][2]+=z; test=false; }
86
star[i][3]=x+(star[i][0]/star[i][2])*star_ratio;
87
star[i][4]=y+(star[i][1]/star[i][2])*star_ratio;
88
if(star_x_save>0&&star_x_save<w&&star_y_save>0&&star_y_save<h&&test)
89
{
90
context.lineWidth=(1-star_color_ratio*star[i][2])*2;
91
context.beginPath();
92
context.moveTo(star_x_save,star_y_save);
93
context.lineTo(star[i][3],star[i][4]);
94
context.stroke();
95
context.closePath();
96
}
97
}
98
timeout=setTimeout('anim()',fps);
99
}
100
101
function release()
102
{
103
switch(key)
104
{
105
case 13:
106
context.fillStyle='rgb(0,0,0)';
107
break;
108
}
109
}
110
111
function start()
112
{
113
resize();
114
anim();
115
}
116
117
function resize()
118
{
119
w=parseInt((url.indexOf('w=')!=-1)?url.substring(url.indexOf('w=')+2,((url.substring(url.indexOf('w=')+2,url.length)).indexOf('&')!=-1)?url.indexOf('w=')+2+(url.substring(url.indexOf('w=')+2,url.length)).indexOf('&'):url.length):get_screen_size()[0]);
120
h=parseInt((url.indexOf('h=')!=-1)?url.substring(url.indexOf('h=')+2,((url.substring(url.indexOf('h=')+2,url.length)).indexOf('&')!=-1)?url.indexOf('h=')+2+(url.substring(url.indexOf('h=')+2,url.length)).indexOf('&'):url.length):get_screen_size()[1]);
121
x=Math.round(w/2);
122
y=Math.round(h/2);
123
z=(w+h)/2;
124
star_color_ratio=1/z;
125
cursor_x=x;
126
cursor_y=y;
127
init();
128
}
129
130