Skip to content

Instantly share code, notes, and snippets.

@tomgp
Last active March 24, 2025 05:13
Show Gist options
  • Save tomgp/6475678 to your computer and use it in GitHub Desktop.
Save tomgp/6475678 to your computer and use it in GitHub Desktop.

Revisions

  1. tomgp revised this gist Apr 16, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions LICENSE.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    ISC License (ISC)
    Copyright 2019 Tom Pearson

    Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2. Tom Pearson revised this gist Sep 8, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -58,8 +58,6 @@
    </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 radians = 0.0174532925,
  3. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -142,7 +142,6 @@
    .attr('class', 'second-label')
    .attr('text-anchor','middle')
    .attr('x',function(d){
    console.log('x')
    return secondLabelRadius*Math.sin(secondScale(d)*radians);
    })
    .attr('y',function(d){
    @@ -172,7 +171,6 @@
    .attr('class', 'hour-label')
    .attr('text-anchor','middle')
    .attr('x',function(d){
    console.log('x')
    return hourLabelRadius*Math.sin(hourScale(d)*radians);
    })
    .attr('y',function(d){
  4. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -222,7 +222,7 @@

    function updateData(){
    var t = new Date();
    handData[0].value = (t.getHours() % 11) + t.getMinutes()/60 ;
    handData[0].value = (t.getHours() % 12) + t.getMinutes()/60 ;
    handData[1].value = t.getMinutes();
    handData[2].value = t.getSeconds();
    }
  5. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,5 @@
    A simple clock made with D3
    A simple clock made with D3.

    After I'd made it I got a bit caught up in trying to make it look like Dieter Ram's Braun clock face adding a whole bunch fo extra stuff...

    It's still not quite there.
  6. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 0 additions and 0 deletions.
    Binary file modified thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  7. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -50,6 +50,10 @@
    font-size: 12px;
    }

    .hour-label{
    font-size: 24px;
    }


    </style>
    <body>
    @@ -72,7 +76,9 @@
    hourTickStart = clockRadius,
    hourTickLength = -18
    secondLabelRadius = clockRadius + 16;
    secondLabelYOffset = 5;
    secondLabelYOffset = 5
    hourLabelRadius = clockRadius - 40
    hourLabelYOffset = 7;


    var hourScale = d3.scale.linear()
    @@ -159,6 +165,22 @@
    return 'rotate(' + hourScale(d) + ')';
    });

    face.selectAll('.hour-label')
    .data(d3.range(3,13,3))
    .enter()
    .append('text')
    .attr('class', 'hour-label')
    .attr('text-anchor','middle')
    .attr('x',function(d){
    console.log('x')
    return hourLabelRadius*Math.sin(hourScale(d)*radians);
    })
    .attr('y',function(d){
    return -hourLabelRadius*Math.cos(hourScale(d)*radians) + hourLabelYOffset;
    })
    .text(function(d){
    return d;
    });


    var hands = face.append('g').attr('id','clock-hands');
  8. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 33 additions and 12 deletions.
    45 changes: 33 additions & 12 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@

    svg{
    stroke: #000;
    font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
    }

    #rim {
    @@ -45,6 +46,10 @@
    stroke-width:8; //same as the miute hand
    }

    .second-label{
    font-size: 12px;
    }


    </style>
    <body>
    @@ -53,17 +58,21 @@
    <script src="http://d3js.org/topojson.v1.min.js"></script>
    <script>

    var clockRadius = 200, margin = 50
    var radians = 0.0174532925,
    clockRadius = 200,
    margin = 50,
    width = (clockRadius+margin)*2,
    height = (clockRadius+margin)*2,
    hourHandLength = 2*clockRadius/3,
    minuteHandLength = clockRadius,
    secondHandLength = clockRadius,
    secondHandLength = clockRadius-12,
    secondHandBalance = 30,
    secondTickStart = clockRadius + 2;
    secondTickLength = 10,
    hourTickStart = clockRadius - 5,
    hourTickLength = 18;
    secondTickStart = clockRadius;
    secondTickLength = -10,
    hourTickStart = clockRadius,
    hourTickLength = -18
    secondLabelRadius = clockRadius + 16;
    secondLabelYOffset = 5;


    var hourScale = d3.scale.linear()
    @@ -105,11 +114,6 @@
    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);*/

    //add marks for seconds
    face.selectAll('.second-tick')
    @@ -123,6 +127,24 @@
    .attr('transform',function(d){
    return 'rotate(' + secondScale(d) + ')';
    });
    //and labels

    face.selectAll('.second-label')
    .data(d3.range(5,61,5))
    .enter()
    .append('text')
    .attr('class', 'second-label')
    .attr('text-anchor','middle')
    .attr('x',function(d){
    console.log('x')
    return secondLabelRadius*Math.sin(secondScale(d)*radians);
    })
    .attr('y',function(d){
    return -secondLabelRadius*Math.cos(secondScale(d)*radians) + secondLabelYOffset;
    })
    .text(function(d){
    return d;
    });

    //... and hours
    face.selectAll('.hour-tick')
    @@ -156,7 +178,6 @@
    })
    .attr('x1',0)
    .attr('y1',function(d){
    console.log(d.balance);
    return d.balance ? d.balance : 0;
    })
    .attr('x2',0)
  9. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 24 additions and 5 deletions.
    29 changes: 24 additions & 5 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,10 @@
    fill:#000;
    }

    .hour-tick{
    stroke-width:8; //same as the miute hand
    }


    </style>
    <body>
    @@ -56,11 +60,14 @@
    minuteHandLength = clockRadius,
    secondHandLength = clockRadius,
    secondHandBalance = 30,
    secondTickLength = 10;
    secondTickStart = clockRadius + 2;
    secondTickLength = 10,
    hourTickStart = clockRadius - 5,
    hourTickLength = 18;


    var hourScale = d3.scale.linear()
    .range([0,354])
    .range([0,330])
    .domain([0,11]);

    var minuteScale = secondScale = d3.scale.linear()
    @@ -111,13 +118,25 @@
    .attr('class', 'second-tick')
    .attr('x1',0)
    .attr('x2',0)
    .attr('y1',clockRadius+1)
    .attr('y2',clockRadius+1 + secondTickLength)
    .attr('y1',secondTickStart)
    .attr('y2',secondTickStart + secondTickLength)
    .attr('transform',function(d){
    return 'rotate(' + secondScale(d) + ')';
    });

    var hourTicks = d3.range(0,12);
    //... and hours
    face.selectAll('.hour-tick')
    .data(d3.range(0,12)).enter()
    .append('line')
    .attr('class', 'hour-tick')
    .attr('x1',0)
    .attr('x2',0)
    .attr('y1',hourTickStart)
    .attr('y2',hourTickStart + hourTickLength)
    .attr('transform',function(d){
    return 'rotate(' + hourScale(d) + ')';
    });



    var hands = face.append('g').attr('id','clock-hands');
  10. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 25 additions and 3 deletions.
    28 changes: 25 additions & 3 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -36,6 +36,11 @@
    fill:#fff;
    }

    .second-tick{
    stroke-width:3;
    fill:#000;
    }


    </style>
    <body>
    @@ -50,7 +55,8 @@
    hourHandLength = 2*clockRadius/3,
    minuteHandLength = clockRadius,
    secondHandLength = clockRadius,
    secondHandBalance = 30;
    secondHandBalance = 30,
    secondTickLength = 10;


    var hourScale = d3.scale.linear()
    @@ -92,11 +98,27 @@
    var face = svg.append('g')
    .attr('id','clock-face')
    .attr('transform','translate(' + (clockRadius + margin) + ',' + (clockRadius + margin) + ')');
    face.append('circle')
    /* face.append('circle')
    .attr('id','rim')
    .attr('x',0)
    .attr('y',0)
    .attr('r',clockRadius);
    .attr('r',clockRadius);*/

    //add marks for seconds
    face.selectAll('.second-tick')
    .data(d3.range(0,60)).enter()
    .append('line')
    .attr('class', 'second-tick')
    .attr('x1',0)
    .attr('x2',0)
    .attr('y1',clockRadius+1)
    .attr('y2',clockRadius+1 + secondTickLength)
    .attr('transform',function(d){
    return 'rotate(' + secondScale(d) + ')';
    });

    var hourTicks = d3.range(0,12);


    var hands = face.append('g').attr('id','clock-hands');

  11. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 12 additions and 6 deletions.
    18 changes: 12 additions & 6 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -46,10 +46,12 @@

    var clockRadius = 200, margin = 50
    width = (clockRadius+margin)*2,
    height = (clockRadius+margin)*2
    hourHandLength = 2*clockRadius/3
    minuteHandLength = clockRadius
    secondHandLength = clockRadius;
    height = (clockRadius+margin)*2,
    hourHandLength = 2*clockRadius/3,
    minuteHandLength = clockRadius,
    secondHandLength = clockRadius,
    secondHandBalance = 30;


    var hourScale = d3.scale.linear()
    .range([0,354])
    @@ -76,7 +78,8 @@
    type:'second',
    value:0,
    length:-secondHandLength,
    scale:secondScale
    scale:secondScale,
    balance:secondHandBalance
    }
    ];

    @@ -111,7 +114,10 @@
    return d.type + '-hand';
    })
    .attr('x1',0)
    .attr('y1',0)
    .attr('y1',function(d){
    console.log(d.balance);
    return d.balance ? d.balance : 0;
    })
    .attr('x2',0)
    .attr('y2',function(d){
    return d.length;
  12. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    }

    svg{
    stroke: #000;
    stroke: #000;
    }

    #rim {
    @@ -31,6 +31,10 @@
    stroke-linecap:round;
    }

    .hands-cover{
    stroke-width:3;
    fill:#fff;
    }


    </style>
    @@ -93,6 +97,12 @@

    var hands = face.append('g').attr('id','clock-hands');

    face.append('g').attr('id','face-overlay')
    .append('circle').attr('class','hands-cover')
    .attr('x',0)
    .attr('y',0)
    .attr('r',clockRadius/20);

    hands.selectAll('line')
    .data(handData)
    .enter()
  13. Tom Pearson revised this gist Sep 7, 2013. 1 changed file with 0 additions and 0 deletions.
    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  14. tomgp revised this gist Sep 7, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    A simple clock made with D3
  15. tomgp created this gist Sep 7, 2013.
    138 changes: 138 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,138 @@
    <!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>