aboutsummaryrefslogtreecommitdiffhomepage
path: root/starfield/stars.js
blob: f9e8a1282a52633034bc1e5b1e1bff61d275be74 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Starfield JS v1.0.1
Copyright 2018 Yaroslav de la Peña Smirnov
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

    * Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from this
    software without specific prior written permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

// ix and iy are the x,y coordinates in 'space', posz is how
// far away from the screen the particle/star is
function Particle(ix, iy, posz, rgb){
    this.ix = ix;
    this.iy = iy;
    this.posz = posz;
    this.rgb = rgb;

    // getX and getY get the projected coordinates, that is the real x, y
    // coordinate on 'screen' rather than 'space'
    this.getX = function(cWidth){
        return this.ix / this.posz * cWidth/8 + cWidth/2;
    }

    this.getY = function(cHeight){
        return this.iy / this.posz * cHeight/8 + cHeight/2;
    }
}

function warpLaunch(options={}){
    /* >>> Some parameters <<< */
    var blur = 1.0;
    var IBAH = 0;
    var max_particles =
        options.hasOwnProperty('max_particles') ? options['max_particles'] : 500;
    var speed =
        options.hasOwnProperty('speed') ? options['speed'] : 50;
    var step =
        options.hasOwnProperty('step') ? options['step'] : 2;
    var interactive = options['interactive'];
    var color = options['color'];
    var ox, oy;

    var canvas = document.getElementById("stars");
    if(!canvas){
        console.log("There is no '#stars' canvas to draw on! Aborting...");
        return;
    }
    var container = canvas.parentNode;
    var ctx = canvas.getContext("2d");

    // This will be populated with all the 'particles' (i.e. stars)
    // on the canvas
    var particles = [];
    var last = null; // previous timestamp
    var requestedFrame;

    function randomRange(min, max){
        return Math.floor(Math.random() * (max - min - 1)) + min;
    }

    function randomInt(max){
        return Math.floor(Math.random() * max);
    }

    function resize(){
        // On window/viewport resize, get the new size of the container
        // and accordingly resize the canvas, then reinitialize
        canvas.height = parseInt(container.offsetHeight);
        canvas.width = parseInt(container.offsetWidth);
        ox = canvas.width/3;
        oy = canvas.height/2;
        if (canvas.height/canvas.width < 0.25){
            oy = canvas.height*1.5;
        }
        else if (canvas.height/canvas.width < 0.5){
            oy = canvas.height;
        }
        try{ window.cancelAnimationFrame(requestedFrame); }
        catch(e) {}
        ctx.imageSmoothingEnabled = false;
        ctx.webkitImageSmoothingEnabled = false;
        ctx.mozImageSmoothingEnabled = false;
        ctx.save();
        initParticles();
    }

    function getRandomPos(n){
        let i = randomRange(-n, n);
        if (i == 0) return 1;
        return i;
    }

    function getNewParticle(){
        // Randomly assign them the needed parameters
        let ix = getRandomPos(ox)
        let iy = getRandomPos(oy);
        let posz = randomRange(0, 200);
        if (!color)
            return new Particle(ix, iy, posz, [255, 255, 255]);
        let b = randomRange(178, 255);
        if (b >= 180){
            let r = randomRange(100, b-50);
            let g = randomRange(100, b-20);
            return new Particle(ix, iy, posz, [r, g, b]);
        }
        b = randomRange(0, 150);
        let r = randomRange(180, 255);
        let g = randomRange(20, r-50);
        return new Particle(ix, iy, posz, [r, g, b]);
    }

    // Init all the stars, and place them randomly on screen
    function initParticles(){
        particles.length = 0;
        ctx.save();
        for (let i = 0; i < max_particles; ++i){
            let particle = getNewParticle();
            particles.push(particle);
            drawStar(particle);
        }
        requestedFrame = window.requestAnimationFrame(nextFrame);
    }

    function drawStar(particle){
        let alpha = (1 - particle.posz/200) + 0.1;
        let scale = alpha*2
        ctx.globalAlpha = alpha;
        ctx.translate(particle.getX(canvas.width), particle.getY(canvas.height));
        ctx.scale(scale, scale);
        ctx.fillStyle = "rgba("+particle.rgb[0]+
            ", "+particle.rgb[1]+
            ", "+particle.rgb[2]+", 0.8)";
        ctx.fillRect(0, 2, 5, 1);
        ctx.fillRect(2, 0, 1, 5);
        ctx.fillStyle = "rgba("+particle.rgb[0]+
            ", "+particle.rgb[1]+
            ", "+particle.rgb[2]+", 0.5)";
        ctx.fillRect(1, 1, 3, 3);
        ctx.restore();
        ctx.save();
    }

    function nextFrame(timestamp){
        if (!last) last = timestamp;
        let delta = timestamp - last;
        // If more than 250ms have passed since last frame, it is assumed
        // that the browser stopped calling back frames because the tab/page
        // was in the background, thus this frame is ignored, and the last
        // timestamp reset
        if (delta > 250){
            last = timestamp;
            requestedFrame = window.requestAnimationFrame(nextFrame);
            return;
        }
        //ctx.clearRect(0, 0, canvas.width, canvas.height);
        if (IBAH === 1) {
        	blur -= 0.02;
        	if (blur <= 0.0) {
        		IBAH = 2;
        	}
        } else if (IBAH === 2) {
        	blur += 0.03;
        	if (blur >= 1.0) {
        		IBAH = 0;
        		blur = 1.0;
        	}
        }
        ctx.fillStyle = "rgba(0, 0, 0," + blur + ")";
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        // Decrease the distance from the screen of the star based
        // based on the speed
        for(let i = 0; i < max_particles; i++){
            let particle = particles[i];
            particle.posz -= delta*speed/1000;
            if (particle.posz <= 0){
                particle = getNewParticle();
                particle.posz = 200;
                particles[i] = particle;
            }
            drawStar(particle);
        }
        last = timestamp;
        requestedFrame = window.requestAnimationFrame(nextFrame);
    }

    function updateSpeed(e){
        // If mouse wheel scrolls up, increase speed, if down, decrease
        e.preventDefault();
        e.stopPropagation();
        let new_speed = e.deltaY > 0 ? speed - step : speed + step;
        if (new_speed > 250){
            speed = 250;
            return
        }
        if (new_speed <= 0){
            speed = 2;
            return;
        }
        speed = new_speed;
        console.log(speed);
    }

    resize();

    if (interactive){
        console.log("Interactivity is on, move the mouse wheel to control speed");
        canvas.addEventListener("wheel", updateSpeed);
    	window.addEventListener('keypress', function(e){
        	if (e.code == 'KeyQ')
            	blur += 0.1;
        	if (e.code == 'KeyE')
            	blur -= 0.1;
        	if (e.code == 'KeyW') {
        		IBAH = 1;
        		blur = 0.95;
        	}
    	});
    }

    window.addEventListener("resize", resize);
}