-
Notifications
You must be signed in to change notification settings - Fork 2
/
starfield.js
79 lines (69 loc) · 2.3 KB
/
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
var Star = function(x,y) {
this.x;
this.y;
this.state = false;
this.angle = Math.random() * 360;
this.alpha = 0.0;
this.velocity = 1.0;
this.counter = 0; // life counter
}
/* starParticle processor */
var STARFIELD_STARS_MAX = 1000;
var starParticle = new Array();
var starParticle_idx = 0;
$(document).ready(function() {
for (var i = 0; i < STARFIELD_STARS_MAX; i++)
starParticle[i] = new Star(mobileWidth/2, mobileHeight/2);
});
function add_star(x,y ) {
starParticle_idx++;
if (starParticle_idx >= STARFIELD_STARS_MAX)
starParticle_idx = 0;
starParticle[starParticle_idx].x = x;
starParticle[starParticle_idx].y = y;
starParticle[starParticle_idx].state = true;
starParticle[starParticle_idx].angle = Math.random() * 360;
}
function proc_starfield() {
for (var i = 0; i < STARFIELD_STARS_MAX; i++) {
if (starParticle[i].state == true) {
// starParticle[i].segment.x += starParticle[i].segment.vecx * 0.25;
var data = ang2vec(starParticle[i].angle);
var dirx = data[0];
var diry = data[1];
starParticle[i].x += dirx*0.05*starParticle[i].velocity;
starParticle[i].y += diry*0.05*starParticle[i].velocity;
starParticle[i].velocity += 0.04;
starParticle[i].alpha += 0.0015;
// evaporate...
// starParticle[i].y -= 0.05;
// if (starParticle[i].alpha > 0.0)
// starParticle[i].alpha -= 0.001;
// apply gravity
//starParticle[i].segment.vecy += 0.3;
starParticle[i].counter++;
if (starParticle[i].counter % 100 == 0)
{
// var s = starParticle[i].segment.normal();
// var n = s.unit();
// add_starParticleet( s );
}
if (starParticle[i].counter > 1000) {
starParticle[i].counter = 0;
starParticle[i].velocity = 1.0;
starParticle[i].alpha = 0.0;
starParticle[i].state = false;
}
}
}
}
function draw_starfield() {
for (var i = 0; i < STARFIELD_STARS_MAX; i++) {
if (starParticle[i].state == true) {
gfx.globalAlpha = starParticle[i].alpha;
star.rotscale(starParticle[i].x, starParticle[i].y, 1, 1, starParticle[i].angle);
gfx.globalAlpha = 1;
}
}
// gfx.globalAlpha = 1;
}