cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              body {
  height:100vh;
  background-color: #111;
  overflow: hidden;
}
canvas {
  background-image: radial-gradient(#011132, black);
  display: block;
  margin: 0 auto;
}
p {
  color: #777;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 18px;
  text-align: center;
  margin: 1em 0;
  font-weight: normal;
}

            
          
!
            
              var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var cw = (canvas.width = window.innerWidth), cx = cw / 2;
var ch = (canvas.height = window.innerHeight), cy = ch / 2;
ctx.strokeStyle = "#fff";

var requestId = null;
var balls = [];
var create = false;
var counter = 0;
var rad = Math.PI / 180;
var m = { x: 0, y: 0 };
var vp = { x: cx, y: cy }; //vanishing point
var fl = cx; // focal length

function Ball(x, y) {
  this.r = 15;
  this.R = x - cx;
  // 3D position
  this.pos = { x: x - cx, y: y - cy, z: 0 };
  // 2D position
  this.x = x;
  this.y = y;

  this.scale = { x: 1, y: 1 };

  this.rotateY = function(angle) {
    var cos = Math.cos(angle);
    var sin = Math.sin(angle);
    var x1 = this.pos.x * cos - this.pos.z * sin;
    var z1 = this.pos.z * cos + this.pos.x * sin;

    this.pos.x = x1;
    this.pos.z = z1;
  };

  this.draw2D = function() {
    ctx.save();
    ctx.fillStyle = oGrd(this, 210);
    ctx.translate(this.x, this.y);
    ctx.scale(this.scale.x, this.scale.y);
    ctx.beginPath();
    ctx.arc(0, 0, this.r, 0, 2 * Math.PI);
    ctx.fill();
    ctx.restore();
  };

  this.draw3D = function() {
    if (this.pos.z > -fl) {
      this.rotateY(0.02);

      var scale = fl / (fl - this.pos.z);
      this.scale = { x: scale, y: scale };
      this.x = vp.x + this.pos.x * scale;
      this.y = vp.y + this.pos.y * scale;
      this.visible = true;
    } else {
      this.visible = false;
    }
  };
}

var b = new Ball(cx + 100, cy);
balls.push(b);

canvas.addEventListener(
  "mousedown",
  function(evt) {
    create = true;
  },
  false
);

canvas.addEventListener(
  "mousemove",
  function(evt) {
    counter++;
    if (create && counter % 2 == 0) {
      m = oMousePos(canvas, evt);
      var b = new Ball(m.x, m.y);
      balls.push(b);
    }
  },
  false
);

canvas.addEventListener(
  "mouseup",
  function(evt) {
    create = false;
  },
  false
);

function Draw() {
  requestId = window.requestAnimationFrame(Draw);
  ctx.clearRect(0, 0, cw, ch);
  //console.log(b.scale.x)
  balls.map(function(b) {
    b.draw3D();
  });
  balls.sort(zSort);
  balls.map(function(b) {
    b.draw2D();
  });
}
//Draw();

function oMousePos(canvas, evt) {
  var ClientRect = canvas.getBoundingClientRect();
  return {
    //objeto
    x: Math.round(evt.clientX - ClientRect.left),
    y: Math.round(evt.clientY - ClientRect.top)
  };
}

function oGrd(o, h) {
  grd = ctx.createRadialGradient(
    -0.2 * o.r,
    -0.6 * o.r,
    0,
    -0.2 * o.r,
    -0.6 * o.r,
    o.r
  );

  grd.addColorStop(0, "hsla(" + h + ",95%,95%, 1)");
  grd.addColorStop(0.4, "hsla(" + h + ",95%,45%,.5)");
  grd.addColorStop(1, "hsla(" + h + ", 0%, 0%, 0)");

  return grd;
}

function zSort(a, b) {
  return a.pos.z - b.pos.z;
}

function Init() {
  if (requestId) {
    window.cancelAnimationFrame(requestId);
    requestId = null;
  }

  (cw = canvas.width = window.innerWidth), (cx = cw / 2);
  (ch = canvas.height = window.innerHeight), (cy = ch / 2);
  balls.length = 0;

  vp = { x: cx, y: cy }; //vanishing point
  fl = cx; // focal length
  Draw();
}

window.setTimeout(function() {
  Init();
  window.addEventListener("resize", Init, false);
}, 15);

            
          
!
999px
Loading ..................

Console