Javascript Strarfield

Just copy this as .html file in your computer and open with browser.
You can check it as online version in here.

<html>
<head>
<title>Web Demo!</title>
<script type="text/javascript">
var LENS = 256
var CENTX = 400
var CENTY = 300
var StarX = []
var StarY = []
var StarZ = []
var ZV = []
var rnd = Math.random()
var t
var dist
var NX
var NY

function SETPIX(X, Y, ID){
var COL = Math.round(StarZ[ID])
document.write('<span style="color: rgb('+COL+','+COL+','+COL+'); position:absolute; top:'+Y+'; left:'+X+';" id='+ID+'>*</span>')
}
function MOVEPIX(X, Y, ID){
document.getElementById(ID).style.top = Y
document.getElementById(ID).style.left = X
}

function UpdateField() {
for (var i = 1; i <= 100; i++){
StarX[i] = StarX[i] - ZV[i]
if (StarX[i] < -400) {StarX[i] = 400}
dist=LENS-StarZ[i]
if (dist != 0) {
NX = CENTX + (LENS*StarX[i]/dist)
NY = CENTY - (LENS*StarY[i]/dist)
MOVEPIX(NX, NY, i)
}

}
t=setTimeout("UpdateField()", 100)
}
</script>
<head>
<body bgcolor="#000000">
<script type="text/javascript">
for (var i = 1; i <= 100; i++){
rnd = Math.random()
StarX[i] = (rnd*800)-400
rnd = Math.random()
StarY[i] = (rnd*600)-300
rnd = Math.random()
StarZ[i] = rnd*255
rnd = Math.random()
ZV[i] = (rnd*(10-5))+5
}
for (var i = 1; i <= 100; i++){
dist=LENS-StarZ[i]
if (dist != 0) {
NX = CENTX + (LENS*StarX[i]/dist)
NY = CENTY - (LENS*StarY[i]/dist)
SETPIX(NX, NY, i)
}

}

UpdateField()
</script>
</body>
</html>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.