A simple clock made with D3
Last active
March 24, 2025 05:13
-
-
Save tomgp/6475678 to your computer and use it in GitHub Desktop.
Simple D3 clock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <style> | |
| body { | |
| background: #fff; | |
| } | |
| svg{ | |
| stroke: #000; | |
| } | |
| #rim { | |
| fill: none; | |
| stroke: #999; | |
| stroke-width: 3px; | |
| } | |
| .second-hand{ | |
| stroke-width:3; | |
| } | |
| .minute-hand{ | |
| stroke-width:8; | |
| stroke-linecap:round; | |
| } | |
| .hour-hand{ | |
| stroke-width:12; | |
| stroke-linecap:round; | |
| } | |
| </style> | |
| <body> | |
| <script src="http://d3js.org/d3.v3.min.js"></script> | |
| <script src="http://d3js.org/d3.geo.projection.v0.min.js"></script> | |
| <script src="http://d3js.org/topojson.v1.min.js"></script> | |
| <script> | |
| var clockRadius = 200, margin = 50 | |
| width = (clockRadius+margin)*2, | |
| height = (clockRadius+margin)*2 | |
| hourHandLength = 2*clockRadius/3 | |
| minuteHandLength = clockRadius | |
| secondHandLength = clockRadius; | |
| var hourScale = d3.scale.linear() | |
| .range([0,354]) | |
| .domain([0,11]); | |
| var minuteScale = secondScale = d3.scale.linear() | |
| .range([0,354]) | |
| .domain([0,59]); | |
| var handData = [ | |
| { | |
| type:'hour', | |
| value:0, | |
| length:-hourHandLength, | |
| scale:hourScale | |
| }, | |
| { | |
| type:'minute', | |
| value:0, | |
| length:-minuteHandLength, | |
| scale:minuteScale | |
| }, | |
| { | |
| type:'second', | |
| value:0, | |
| length:-secondHandLength, | |
| scale:secondScale | |
| } | |
| ]; | |
| function drawClock(){ //create all the clock elements | |
| updateData(); //draw them in the correct starting position | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width) | |
| .attr("height", height); | |
| var face = svg.append('g') | |
| .attr('id','clock-face') | |
| .attr('transform','translate(' + (clockRadius + margin) + ',' + (clockRadius + margin) + ')'); | |
| face.append('circle') | |
| .attr('id','rim') | |
| .attr('x',0) | |
| .attr('y',0) | |
| .attr('r',clockRadius); | |
| var hands = face.append('g').attr('id','clock-hands'); | |
| hands.selectAll('line') | |
| .data(handData) | |
| .enter() | |
| .append('line') | |
| .attr('class', function(d){ | |
| return d.type + '-hand'; | |
| }) | |
| .attr('x1',0) | |
| .attr('y1',0) | |
| .attr('x2',0) | |
| .attr('y2',function(d){ | |
| return d.length; | |
| }) | |
| .attr('transform',function(d){ | |
| return 'rotate('+ d.scale(d.value) +')'; | |
| }); | |
| } | |
| function moveHands(){ | |
| d3.select('#clock-hands').selectAll('line') | |
| .data(handData) | |
| .transition() | |
| .attr('transform',function(d){ | |
| return 'rotate('+ d.scale(d.value) +')'; | |
| }); | |
| } | |
| function updateData(){ | |
| var t = new Date(); | |
| handData[0].value = (t.getHours() % 11) + t.getMinutes()/60 ; | |
| handData[1].value = t.getMinutes(); | |
| handData[2].value = t.getSeconds(); | |
| } | |
| drawClock(); | |
| setInterval(function(){ | |
| updateData(); | |
| moveHands(); | |
| }, 1000); | |
| d3.select(self.frameElement).style("height", height + "px"); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this: it was a fantastic foundation for my DST transition visualisation.