Author Topic: Starfield in JavaScript...  (Read 7005 times)

0 Members and 1 Guest are viewing this topic.

Offline Rattrapmax6

  • Atari ST
  • ***
  • Posts: 131
  • Karma: 5
  • World Wild Web Stallion
    • View Profile
Starfield in JavaScript...
« on: August 09, 2006 »
Xteraco got to showing me some JavaScript demo's the other night, and being a JS coder myself, sparked some creative juices.. Well, I wipped up this lil effect just a few minutes ago just as a lil break from the other stuff I've been up to.... Pretty cool.. =)

Code: [Select]
<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>

 :D Enjoy!
« Last Edit: August 09, 2006 by Rattrapmax6 »
-Rattra (x.t.r.GRAPHICS)

Offline Dad1916

  • Atari ST
  • ***
  • Posts: 112
  • Karma: 3
    • View Profile
Re: Starfield in JavaScript...
« Reply #1 on: August 09, 2006 »
Indeed it works! Screenshot attached. Thanks for sharing !  O0 O0

Offline Rattrapmax6

  • Atari ST
  • ***
  • Posts: 131
  • Karma: 5
  • World Wild Web Stallion
    • View Profile
Re: Starfield in JavaScript...
« Reply #2 on: August 10, 2006 »
Hehe, welcome!

Link to where lurah hosted it on his ASCII site: http://www.ascii-world.com/demos/java_field.html
For those to lazy to make a HTML file...  ::)
-Rattra (x.t.r.GRAPHICS)

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Starfield in JavaScript...
« Reply #3 on: August 10, 2006 »
Welldone mate :)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17427
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Starfield in JavaScript...
« Reply #4 on: August 10, 2006 »
Really cool :-) Now do it in 3d!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rattrapmax6

  • Atari ST
  • ***
  • Posts: 131
  • Karma: 5
  • World Wild Web Stallion
    • View Profile
Re: Starfield in JavaScript...
« Reply #5 on: August 11, 2006 »
Like this?

Code: [Select]
<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){
var COL = Math.round(StarZ[ID])
document.getElementById(ID).style.top = Y
document.getElementById(ID).style.left = X
document.getElementById(ID).style.color = "rgb("+COL+","+COL+","+COL+")"
}

function UpdateField() {
for (var i = 1; i <= 100; i++){
StarZ[i] = StarZ[i] + ZV[i]
//if (StarX[i] < -400) {StarX[i] = 400}
if (StarZ[i] > 256){StarZ[i] = 0}
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>

I'd done that before, but updating colour last time was being a dog, I finally fond the right syntax, so.. there you have it..  :D .. Zooming star field! haha, and the scroll bars go insian!  ;D
« Last Edit: August 11, 2006 by Rattrapmax6 »
-Rattra (x.t.r.GRAPHICS)

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17427
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Starfield in JavaScript...
« Reply #6 on: August 11, 2006 »
Heheh, no point in posting another screen shot as it would look about the same but that's damn good Rattra :) Kudos.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline p01

  • Atari ST
  • ***
  • Posts: 158
  • Karma: 51
    • View Profile
    • www.p01.org
Re: Starfield in JavaScript...
« Reply #7 on: March 20, 2007 »
Nice, though a bit too big for my taste :p

You probably saw the 256b starfield I posted on Pouet. Here is a badly optimized version of it, in 209 bytes.

<body id=B text=tan bgColor=0><script>setInterval("m='';c=Math.cos;for(u=65;u;)m+='<p style=position:absolute;top:'+(50+(z=399/(9+(--U+u--&63)))*c(u*9))+'%;left:'+(50+z*c(u))+'%>·';B.innerHTML=m",U=9)</script>

Hope you like them.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Starfield in JavaScript...
« Reply #8 on: March 20, 2007 »
Cool one.

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Starfield in JavaScript...
« Reply #9 on: March 20, 2007 »
Cool one. Very well done  :goodpost:
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Starfield in JavaScript...
« Reply #10 on: March 20, 2007 »
:hi: aboard P01, nice starfield.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17427
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Starfield in JavaScript...
« Reply #11 on: March 20, 2007 »
Hey p01 I hadn't seen that one before, looks nice, good sense of depth which most broser starfields lack.. Thumb duely added and :hi:
Shockwave ^ Codigos
Challenge Trophies Won: