This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
starfield.js
65 lines (53 loc) · 1.55 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
// based on: http://codentronix.com/2011/07/22/html5-canvas-3d-starfield/
var MAX_DEPTH = 32;
var canvas, ctx;
var stars = new Array(512);
var fs = require('fs');
var Canvas = require('canvas');
var Image = Canvas.Image;
var canvas = new Canvas(128, 16);
var panel = require('./index');
var mypanel;
if( canvas && canvas.getContext ) {
ctx = canvas.getContext("2d");
mypanel = new panel.Panel(ctx, 'ledpanel.lan', 1021);
initStars();
setInterval(loop,33);
}
/* Returns a random number in the range [minVal,maxVal] */
function randomRange(minVal,maxVal) {
return Math.floor(Math.random() * (maxVal - minVal - 1)) + minVal;
}
function initStars() {
for( var i = 0; i < stars.length; i++ ) {
stars[i] = {
x: randomRange(-25,25),
y: randomRange(-25,25),
z: randomRange(1,MAX_DEPTH)
}
}
}
function loop() {
var halfWidth = canvas.width / 2;
var halfHeight = canvas.height / 2;
ctx.fillStyle = "rgb(0,0,0)";
ctx.fillRect(0,0,canvas.width,canvas.height);
for( var i = 0; i < stars.length; i++ ) {
stars[i].z -= 0.2;
if( stars[i].z <= 0 ) {
stars[i].x = randomRange(-25,25);
stars[i].y = randomRange(-25,25);
stars[i].z = MAX_DEPTH;
}
var k = 10.0 / stars[i].z;
var px = Math.round(stars[i].x * k + halfWidth);
var py = Math.round(stars[i].y * k + halfHeight);
if( px >= 0 && px <= 128 && py >= 0 && py <= 128 ) {
var size = 1;//(1 - stars[i].z / 32.0) * 5;
var shade = parseInt((1 - stars[i].z / 32.0) * 255);
ctx.fillStyle = "rgb(0," +shade+"," + shade + ")";
ctx.fillRect(px,py,size,size);
}
}
mypanel.draw();
}