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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              <div class="wrapper"></div>
<script src="https://d3js.org/d3.v3.min.js"  charset="utf-8"></script>

            
          
!
            
              .wrapper {
  width: 100%;  
}

/* SVG styles */
.main {
  display: block;
  margin: auto;
  margin-top: 50px;
}


            
          
!
            
              var w = 500;
var h = 200;
var offsetX = 250;
var offsetY = 100;
var pi = Math.PI;

var fields = function() {
  var currentTime, hour, minute, second, data;
  
  currentTime = new Date();
  second = currentTime.getSeconds();  
  minute = currentTime.getMinutes() + second / 60; 
  hour = currentTime.getHours() + minute / 60; 
  
  // Good practice - keep values out of keys
  data = [
    {
      "unit": "seconds",
      "numeric": second
    },
    {
      "unit": "minutes",
      "numeric": minute
    },
    {
      "unit": "hours",
      "numeric": hour
    }
  ];
  return data;
}

var secondScale = d3.scale
               .linear()
               .domain([0, 59 + 999 / 1000])
               .range([0, 2 * pi]);

var minuteScale = d3.scale
               .linear()
               .domain([0, 59 + 59 / 60])
               .range([0, 2 * pi]);

var hourScale = d3.scale
               .linear()
               .domain([0, 11 + 59 / 60])
               .range([0, 2 * pi]);

var svg = d3.select(".wrapper")
            .append("svg")
            .attr("class", "main")
            .attr("width", w)
            .attr("height", h);

var clock = svg.append("g")
               .attr("transform", "translate(" + offsetX + "," + offsetY + ")");

clock.append("circle")
     .attr("class", "clock outercircle")
     .attr("r", 80)
     .attr("fill", "none")
     .attr("stroke", "black")
     .attr("stroke-width", 2);

clock.append("circle")
     .attr("class", "clock innercircle")
     .attr("r", 4)
     .attr("fill", "black");

var render = function(data) {
  var hourArc, minuteArc, secondArc;
  clock.selectAll(".clockhand").remove();
  
  secondArc = d3.svg.arc()
    .innerRadius(0)
    .outerRadius(70)
    .startAngle(function(d) {
    return secondScale(d.numeric);
  })
    .endAngle(function(d) {
    return secondScale(d.numeric);
  });

  minuteArc = d3.svg.arc()
    .innerRadius(0)
    .outerRadius(70)
    .startAngle(function(d) { return minuteScale(d.numeric); })
    .endAngle(function(d) { return minuteScale(d.numeric); });

  hourArc = d3.svg.arc()
    .innerRadius(0)
    .outerRadius(50)
    .startAngle(function(d) { return hourScale(d.numeric % 12); })
    .endAngle(function(d) { return hourScale(d.numeric % 12); });
  
  clock.selectAll(".clockhand")
    .data(data)
    .enter()
    .append("path")
    .attr("d", function(d) {
      if (d.unit === "seconds") {
        return secondArc(d);
      } else if (d.unit === "minutes") {
        return minuteArc(d);
      } else if (d.unit === "hours") {
        return hourArc(d);
      }
    })
    .attr("class", "clockhand")
    .attr("stroke", "black")
    .attr("stroke-width", function(d) {
      if (d.unit === "seconds") {
        return 2;
      } else if (d.unit === "minutes") {
        return 3;
      } else if (d.unit === "hours") {
        return 3;
      }
    })
    .attr("fill", "none");
}

setInterval(function() {
  var data = fields();
  return render(data);
}, 1000);



            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console