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. 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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <p>Please mouse over the canvas</p>
<canvas id="canvas"></canvas>
              
            
!

CSS

              
                body {
  background-color: #000;
}
canvas {
  background-color: hsl(60,100%,7%);
  display: block;
  margin: 0 auto;
}

p {
  color: #777;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 18px;
  text-align: center;
  margin: 3em 0 1em 0;
  font-weight: normal;
}
              
            
!

JS

              
                var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var cw = (canvas.width = 500), cx = cw / 2;
var ch = (canvas.height = 300), cy = ch / 2;

var m = {x:100,y:0};//mouse pos
var vp = { x: cx, y: cy }; //vanishing point
var fl = 250; // focal length
var speed =  -0.0001;

function Ball(x, y, z, w) {
  this.r = w/2;
  // 3D position
  this.pos = { x: x - cx, y: y - cy, z: z };
  // 2D position
  this.x = x;
  this.y = y;
  this.alp = 1;
  this.bool = false;
  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.translate(this.x, this.y - 3*w);
    ctx.scale(this.scale.x, this.scale.y);
    ctx.beginPath();
    ctx.arc(0, 0, .95*this.r, 0, 2 * Math.PI);
    ctx.fillStyle = "hsla(60,100%,45%,"+this.alp+")";
    ctx.fill();
    ctx.restore();
  };

  this.draw3D = function() {
    if (this.pos.z > -fl) { 
      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;
    } 
  };
}

function Clock(){
  this.w = 16;//circles diameter
  this.ledsPerRow = 40;
  this.angle = Math.PI/this.ledsPerRow;
  this.R = (this.w/2)/Math.sin(this.angle);// clock radius
  this.digitsRy = [
 ["1110","10","1110","1110","1000","1110","1110","1110","1110","1110","0000"], 
 ["1010","10","0010","0010","1000","1000","1000","0010","1010","1010","0100"], 
 ["1010","10","1110","1110","1110","1110","1110","0010","1110","1110","0000"],
 ["1010","10","1000","0010","0010","0010","1010","0010","1010","0010","0100"],
 ["1110","10","1110","1110","0010","1110","1110","0010","1110","0010","0000"], 
];
  this.pointsRy = [];
  
  this.whatTimeIsIt = function() {
  var d = new Date();
  var h = addZero(d.getHours());
  var m = addZero(d.getMinutes());
  var s = addZero(d.getSeconds());
  this.clock = h + ":" + m + ":" + s;//OK 
} // => this.clock 11:57:34
  
  this.buildClockString = function(j) {
  this.clockStr = "";
  for (var s = 0; s < this.clock.length; s++) {
    var n = this.clock[s] == ":" ? 10 : Number(this.clock[s]);
    this.clockStr += this.digitsRy[j][n];
  }
  while (this.clockStr.length < this.ledsPerRow) {
    this.clockStr += "0";
  }
} // => this.clockStr "101110000011101110000010001110000000"
 
  this.buildPointsRy = function(){
      for (var j = 0; j < this.digitsRy.length; j++) {
      this.buildClockString(j); // => this.clockStr "101110000011101110000010001110000000" 
      
    for (var i = 0; i < this.clockStr.length; i++) {
      ///////////////////////////////
        var n = this.ledsPerRow - i;// to reverse the clock
        var x = this.R*Math.cos(2*this.angle*n) 
        var y = j * this.w;
        var z = this.R*Math.sin(2*this.angle*n);
        var p = new Ball(cx+x,cy+y,z,this.w);
        p.bool = this.clockStr[i] == 1 ? true : false; 
        p.j=j;
        p.i=i;
      //////////////////////////////
      this.pointsRy.push(p)
    }
  } 
  }// => this.pointsRy
  
  this.updatePointsRy = function(){
    for (var j = 0; j < this.digitsRy.length; j++) {
      this.buildClockString(j); // => this.clockStr "101110000011101110000010001110000000" 
      
     for (var i = 0; i < this.clockStr.length; i++) {
       var p = this.pointsRy[j*this.clockStr.length + i];
       p.bool = this.clockStr[i] == 1 ? true : false; 
       p.alp = map(p.pos.z, this.R, -this.R, 1, .2)
     } 
    } 
  }
  
}

var clock = new Clock();
  clock.whatTimeIsIt(); // output: 16:26:42
  clock.buildPointsRy();

function Draw() {
  requestId = window.requestAnimationFrame(Draw);
  ctx.clearRect(0, 0, cw, ch);
  clock.whatTimeIsIt(); // output: 16:26:42
  clock.updatePointsRy();

  var angleY = (m.x - vp.x) * speed;
  clock.pointsRy.map(function(b){b.rotateY(angleY);b.draw3D();});
  clock.pointsRy.map(function(b){if(b.visible && b.bool){b.draw2D();};});
  
  textureOverlay();
}

Draw();

function textureOverlay(){
    for (var y = 0; y <= ch; y += 3) {
      ctx.beginPath();
      ctx.moveTo(0,y);
      ctx.lineTo(cw,y);
      ctx.stroke();
    }
}

canvas.addEventListener("mousemove",function(evt){
  m = oMousePos(canvas, evt);
},false
);

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

function addZero(i) {
  return i < 10 ? "0"+i : i;
}

function map(n, a, b, _a, _b) {
  var d = b - a;
  var _d = _b - _a;
  var u = _d / d;
  return _a + n * u;
}
              
            
!
999px

Console