Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                body {
  background:silver;
  padding:0px;
  margin:0px;
}

#vw{
  position:absolute;
  top:15px;
  left:15px;
  width:700px;
  height:400px;
  border:solid gray 2px;
  background:black;
}


              
            
!

JS

              
                let vw,vwContext,lightBar;
let w,h;
let stars = [];
let nStarIndex = -1;
let vwWidth;
let vwHeight;
let nTotalStars = 0;
let starColors = ["#ffffff","#ffffff","#ffffff","#ffffff","#ffffff","#66ffff","#ffff99","#ccffcc","#ff80b3","#6699ff"];


function pageSetup() {
  vw = document.createElement("canvas"); 
  vw.id = "vw";
  document.body.appendChild(vw);
  vwContext = vw.getContext('2d');
  window.addEventListener('resize', pageResize);
  pageResize();
  genStarField();
} // end of function pageSetup()


function pageResize() {
  w = window.innerWidth;
	h = window.innerHeight;
  
  vwWidth = w - 15 * 2 - 5;
  vwHeight = h-15-15;
  vw.width=vwWidth;
  vw.height = vwHeight;
  vw.style.height = (vwHeight)+"px";
  vw.style.width = (vwWidth)+"px";
} // end of function pageResize()


function genStarField() {
  let nPossibleNewStars = rnd(5)+4;
  let n,star,x1,y1;

  for (n=0;n<nPossibleNewStars;n++) {
    if (rnd(200)<40) {
      genStar();
    } // end if
  } // next n
  
  let imgBuffer = document.createElement('canvas');
  imgBuffer.width = vwWidth;
  imgBuffer.height = vwHeight;
  let imgBufferContext = imgBuffer.getContext('2d');
  
  // draw the black void of space:
  imgBufferContext.fillStyle = "#000000";
  imgBufferContext.fillRect(0, 0, vwWidth, vwHeight);
  
  for (n=0;n<nTotalStars;n++){
    star = stars["star"+n];
    imgBufferContext.fillStyle = star.color;
    imgBufferContext.strokeStyle = star.color;
    imgBufferContext.beginPath();
    
    let nCenterX = Math.floor(vwWidth / 2);
    let nCenterY = Math.floor(vwHeight / 2);
    
    radians = degToRadians(star.travelAngle);
    x1 = Math.floor(star.sin * star.distanceTraveled)+ nCenterX; // + nCenterX;
    y1 = Math.floor(star.cos * star.distanceTraveled)+ nCenterY; //+ nCenterY;
    //debugger;

    if (x1>-1 && x1<vwWidth && y1>-1 && y1<vwHeight) {
      imgBufferContext.arc(x1, y1, star.size, 0, 2 * Math.PI);
      imgBufferContext.stroke();
      imgBufferContext.fill();
    } // end if
    
    star.distanceTraveled = star.distanceTraveled + star.speed;
    star.size = star.size  + .008;
    star.speed = star.speed + .1;
  } // next n
  
  vwContext.drawImage(imgBuffer, 0, 0);
  setTimeout("genStarField()",8);
} // end of function genStarField()



function genStar() {
  nStarIndex = nStarIndex + 1;

  if (nTotalStars<nStarIndex + 1) {
    nTotalStars = nStarIndex + 1;
  } // end if
  
  let star = {};
  
  star.travelAngle = rnd(360); // angle in degrees that star will be traveling in from the center
  let radians = degToRadians(star.travelAngle);
  star.sin = Math.sin(radians);
  star.cos = Math.cos(radians);
  star.distanceTraveled = rnd(3)+5; // starting distance traveled from the center of the view
  star.speed = 3;
  star.size = .1;
  star.color = starColors[rnd(starColors.length)];
  star.idxNum = nStarIndex;
  stars["star"+nStarIndex] = star;
  
  // at some point, start reusing star indexes:
  if (nStarIndex>10000) {
    nStarIndex=-1;
  } // end if
} // end of function genStar()


function rnd(nNum) {
  return Math.floor(Math.random() * nNum);
} // end of function rnd()


// converts degrees to radians
	function degToRadians(angleDegrees) {
		let pi = Math.PI;
		return angleDegrees * (pi/180);
	} // end of function degToRadians()

pageSetup();
              
            
!
999px

Console