Advertisement
nimcap

to3

Dec 28th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  (function() {
  2.       var baseR = 31;
  3.       var r = [baseR*2.657, baseR*1.815, baseR];
  4.       var colors = [154, 180, 200];
  5.       var center = {x:150, y:150};
  6.      
  7.       var polar2cart = function(theta, r, shift){
  8.          return {x: r*Math.cos(theta)+shift.x, y: r*Math.sin(theta)+shift.y };
  9.       };
  10.      
  11.       var redraw = function() {
  12.          d3.select
  13.  
  14.          var date = new Date();
  15.          var s = date.getSeconds();
  16.          var m = date.getMinutes() + s/60;
  17.          var h = date.getHours()%12 + m/60;
  18.          to3.selectAll('#kol').remove();
  19.          
  20.          // akrep
  21.          theta = 2*Math.PI*(h-3)/12;
  22.          start = polar2cart(theta, 0, center);
  23.          end   = polar2cart(theta, r[2], center);
  24.          to3.append('line')
  25.             .attr('id', 'kol')
  26.             .attr('x1', start.x)
  27.             .attr('y1', start.y)
  28.             .attr('x2', end.x)
  29.             .attr('y2', end.y)
  30.             .attr('stroke', "black")
  31.             .attr('stroke-width', 2);
  32.  
  33.          // yelkovan
  34.          theta = 2*Math.PI*(m-15)/60;
  35.          start = polar2cart(theta, r[2], center);
  36.          end   = polar2cart(theta, r[1], center);
  37.          to3.append('line')
  38.             .attr('id', 'kol')
  39.             .attr('x1', start.x)
  40.             .attr('y1', start.y)
  41.             .attr('x2', end.x)
  42.             .attr('y2', end.y)
  43.             .attr('stroke', "black")
  44.             .attr('stroke-width', 2);
  45.       }
  46.      
  47.       var to3 = d3.select('svg');
  48.      
  49.       // circles
  50.       to3.selectAll('circle')
  51.             .data(r)
  52.          .enter().append('circle')
  53.             .attr('r', function(d) {
  54.                return d;
  55.             })
  56.          .attr('cx', center.x)
  57.          .attr('cy', center.y)
  58.          .style('fill', function(d, i) {
  59.             return 'hsl(0,0%,'+(colors[i]*100/255.0).toString()+'%)';
  60.          });
  61.          
  62.       // hour ticks
  63.       for (var i=0; i!=12; i++)
  64.       {
  65.          var theta = 2*Math.PI*i/12.0;
  66.          start = polar2cart(theta, r[1], center);
  67.          end   = polar2cart(theta, r[0], center);
  68.          to3.append('line')
  69.             .attr('x1', start.x)
  70.             .attr('y1', start.y)
  71.             .attr('x2', end.x)
  72.             .attr('y2', end.y)
  73.             .attr('stroke', 'hsl(0,0%,75%)')
  74.             .attr('stroke-width', 1);
  75.       }
  76.      
  77.       redraw();
  78.       setInterval(redraw, 1000);
  79.    })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement