Skip to content

Instantly share code, notes, and snippets.

@DarwinSenior
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save DarwinSenior/53a709c016c45e515aa3 to your computer and use it in GitHub Desktop.

Select an option

Save DarwinSenior/53a709c016c45e515aa3 to your computer and use it in GitHub Desktop.
Clock experiment
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
</head>
<body>
<svg
height="200px" width="200px" background-color="grey" viewbox="-100 -100 200 200"
>
</svg>
<script>
var svg = d3.select('svg');
function init(){
svg
.append('circle').classed('frame', true)
.attr('cx', 0).attr('cy', 0)
.attr('r', 100)
.attr('fill', 'transparent')
.attr('stroke', 'black');
console.log(d3.range(12));
ir = 90;
or = 100;
svg.selectAll('line.marker')
.data(d3.range(12))
.enter()
.append('line').classed('marker',true)
.attr('x1', function(d){
return Math.cos(Math.PI/6*d)*ir;
})
.attr('y1', function(d){
return Math.sin(Math.PI/6*d)*ir;
})
.attr('x2', function(d){
return Math.cos(Math.PI/6*d)*or;
})
.attr('y2', function(d){
return Math.sin(Math.PI/6*d)*or;
})
.attr('stroke', 'black');
var tr = 80;
svg.selectAll('text.denote')
.data(d3.range(12)).enter()
.append('text').classed('denote', true)
.attr('x', function(d){return Math.sin(Math.PI/6*d)*tr})
.attr('y', function(d){return -Math.cos(Math.PI/6*d)*tr})
.attr('text-anchor', 'middle')
.text(function(d){return d==0?12:d});
svg.selectAll('line.p')
.data(['s', 'm', 'h'])
.enter()
.append('line')
.attr('class', function(d){return d})
.classed('p', true)
.attr('stroke', 'black')
.attr('y1', 0).attr('x1', 0);
}
function draw(h, m, s){
sr = 90;
mr = 70;
hr = 50;
svg.select('line.p.s')
.transition()
.attr('x2', Math.sin(Math.PI*2/60*s)*sr)
.attr('y2', -Math.cos(Math.PI*2/60*s)*sr);
svg.select('line.p.m')
.transition()
.attr('x2', Math.sin(Math.PI*2/60*m)*mr)
.attr('y2', -Math.cos(Math.PI*2/60*m)*mr);
svg.select('line.p.h')
.transition()
.attr('x2', Math.sin(Math.PI*2/60*h)*hr)
.attr('y2', -Math.cos(Math.PI*2/60*h)*hr);
}
init();
setInterval(function(){
var d = new Date();
var second = d.getSeconds();
var hour = d.getHours();
var minutes = d.getMinutes();
draw(hour, minutes, second);
}, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment