Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="info">
		<h1>3d Perspective Spheres</h1>
		<p>Move mouse left/right to rotate. Click the right button to decrease fov (field of view), and the left button to increase.</p>
		<p>It may be interesting to note that this doesn't use WebGL, all projection and rendering is done with the 2d canvas context.</p>
</div>
<canvas id="canvas"></canvas>
              
            
!

CSS

              
                html, body {
  margin: 0;
}

body {
  background-color: rgb(0,0,0);
  color: #eee;
  font-family: Arial;
}

canvas {
  display: block;
  margin-left: auto;
  margin-right: auto;
}


@keyframes info-box-animation {
  0% {
    opacity: 1;
    z-index: 1000;
  }
  100% {
    opacity: 0;
    z-index: -1;
  }
}

.info {
  position: absolute;
  top: 10%;
  left: 25%;
  width: 50%;
  background-color: rgba(0,0,0,0.8);
  
  padding: 1em;

  animation: info-box-animation 2s ease 10s 1 forwards;
}
              
            
!

JS

              
                //////////////////////////////////////
// Utility Object for mouse tracking
///////////////////////////////////////
var mouse=function(){var b={leftButton:!1,middleButton:!1,rightButton:!1,x:0,y:0};document.addEventListener("contextmenu",function(a){a.preventDefault()});document.addEventListener("mousedown",function(a){0==a.button&&(b.leftButton=!0);1==a.button&&(b.middleButton=!0);2==a.button&&(b.rightButton=!0)});document.addEventListener("mouseup",function(a){0==a.button&&(b.leftButton=!1);1==a.button&&(b.middleButton=!1);2==a.button&&(b.rightButton=!1)});document.addEventListener("mousemove",function(a){b.x=
a.clientX;b.y=a.clientY});document.addEventListener("mouseenter",function(a){b.inWindow=!0});document.addEventListener("mouseleave",function(a){b.inWindow=!1});return b}();
 
 

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var W = canvas.width = window.innerWidth;
var H = canvas.height = window.innerHeight;


var points = [];

var createSphere = function(originX, originY, originZ, scale) {
  for( var i = 0; i < scale*50; i++ ) {
    // Make points randomly distributed across the sphere's
    // surface. We start with Spherical coordinates then
    // convert to x y and z
    var theta = Math.PI*2*Math.random();
    var phi = Math.PI*Math.random();
    var radius = scale;

    var x = radius * Math.sin(theta) * Math.cos(phi);
    var y = radius * Math.sin(theta) * Math.sin(phi);
    var z = radius * Math.cos(theta);

    points.push({x: x+originX, y: y+originY, z: z+originZ});
  }
}

createSphere(0, 0, 0, 100);
createSphere(100, 150, 150, 20);
createSphere(-100, 150, -150, 30);
createSphere(100, -150, -150, 40);
createSphere(-100, -150, 150, 10);


// Field of View
var fov = 550;
var fovSpeed = -5;

var turnAngle = 0;
var turnSpeed = 0.01;

var render = function() {
  ctx.clearRect(0,0,W,H);
    
  if(  mouse.inWindow ) turnAngle = ((2*Math.PI)/W)*mouse.x*2;
  if( !mouse.inWindow ) turnAngle = (turnAngle + turnSpeed) % (2*Math.PI);
  var sinAngle = Math.sin(turnAngle);
  var cosAngle = Math.cos(turnAngle)

  if( mouse.leftButton ) fov += 10;
  if( mouse.rightButton ) fov -= 10;
  
  if( !mouse.inWindow ) fov += fovSpeed;
  if( fov >  1000 ) fovSpeed = -5;
  if( fov < -1000 ) fovSpeed = 5;

  // Writing pixels directly is far more efficent then using
  // Draw commands.
  var imageData = ctx.getImageData(0,0,W,H);

  for( var i = 0; i < points.length; i++ ) {    
    var point = points[i];

    var rotatedX =  cosAngle*point.x + sinAngle*point.z;
    var rotatedZ = -sinAngle*point.x + cosAngle*point.z;

    var scale = fov / (fov + rotatedZ);
    var x2d = rotatedX * scale + W/2;
    var y2d = point.y * scale + H/2;

    // This isn't really "normalized", but it's close enough for our purposes.
    var normalizedZ = (-rotatedZ + 150) / 300;

    // Get the index in the image data array that corrisponds to the
    // desired pixel
    var c = Math.round(y2d)*imageData.width + Math.round(x2d);
    // Each pixel has four values - red green blue and alpha
    c *= 4;

    imageData.data[c]   = 0;   // Red
    imageData.data[c+1] = (normalizedZ*150) + 100; // Green
    imageData.data[c+2] = (normalizedZ*30) + 30;  // Blue
    imageData.data[c+3] = (normalizedZ*150) + 100; // Alpha

  }

  ctx.putImageData(imageData, 0, 0);

  window.requestAnimationFrame(render);
};

render();
        

              
            
!
999px

Console