<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bruce Ledbetter on Medium]]></title>
        <description><![CDATA[Stories by Bruce Ledbetter on Medium]]></description>
        <link>https://medium.com/@lbrucel?source=rss-d9c0d8a79988------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SsRxLfYGBdD9n6UMJr2tQg.png</url>
            <title>Stories by Bruce Ledbetter on Medium</title>
            <link>https://medium.com/@lbrucel?source=rss-d9c0d8a79988------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:47:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lbrucel/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Brief Intro to D3.js - Concentric Clock]]></title>
            <link>https://medium.com/@lbrucel/an-brief-intro-to-d3-js-concentric-clock-348500587b50?source=rss-d9c0d8a79988------2</link>
            <guid isPermaLink="false">https://medium.com/p/348500587b50</guid>
            <category><![CDATA[geometry]]></category>
            <category><![CDATA[d3js]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Bruce Ledbetter]]></dc:creator>
            <pubDate>Thu, 16 Aug 2018 14:20:10 GMT</pubDate>
            <atom:updated>2018-08-16T14:23:33.494Z</atom:updated>
            <content:encoded><![CDATA[<h3>Concentric Clock Using D3.js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*65nREBNtIjxC1y7wIEhNaQ.png" /><figcaption>Concentric Clock — Can you tell what time it is by looking at where the circles touch each other?</figcaption></figure><p>I get a lot of satisfaction from building things, solving problems and especially playing with geometry. For a recent hackathon, I decided to build a clock made strictly of circles. It was the perfect opportunity to dive into D3.js — a popular JavaScript library for manipulating data, typically used for data visualization. In this article I’ll explain the idea, my development process, and step through my code to brings the clock to life.</p><blockquote>Resources:<br><a href="https://d3js.org/">https://d3js.org</a> — for an overview, examples, documentation and the source code <a href="https://github.com/d3/d3/wiki/Tutorials">https://github.com/d3/d3/wiki/Tutorials</a> — tons of samples from the d3 community</blockquote><h4>The Idea — A Minimalist Clock Made Strictly of Circles</h4><blockquote><a href="https://clock.lbrucel.com/">https://clock.lbrucel.com</a> — the finished version</blockquote><ul><li><strong>Clock Face</strong> is the outter circle — its the only circle that doesn’t change</li><li><strong>Hour</strong> is represented by the larges thick black circle — the current hour is represented by the point where the hour circle touches the clock face. The hour circle moves at the top of the hour.</li><li><strong>Minute</strong> is represented by middle black circle. The current <em>minute</em> is represened by the point where its circle touches the <em>hour</em> circle. It rotates 1 degree every 60 seconds</li><li><strong>Second</strong> is the innner circle represents seconds and its value is the point where it touches the <em>minute</em> circle.</li></ul><h3>Development Plan</h3><blockquote><a href="http://blockbuilder.org/">http://blockbuilder.org/</a> is an excellent web-based tool for developing and deploying your D3 projects.</blockquote><p>First and foremost I needed to see that the idea made sense to me. I needed to simply draw a static version of the clock, then manually change the time to see that everything renders as I expected. I opened a new project on blockbuild.org and began to layout out the page. Once I got the foundation in place, I moved on to make the clock dynamic — to change appropriately over time.</p><h4>Stepping Through The Code (Static Version)</h4><blockquote><a href="http://blockbuilder.org/lbrucel/fbab9e0af6f9594a0bc7bd7f93741f09">http://blockbuilder.org/lbrucel/fbab9e0af6f9594a0bc7bd7f93741f09</a></blockquote><p>Add a script tag to the D3.js library of your choice. Hop over to <a href="https://d3js.org/">https://d3js.org</a> to grab the latest version.</p><pre>&lt;script src=&quot;https://d3js.org/d3.v4.min.js&quot;&gt;&lt;/script&gt;</pre><p>Next, lets set up some variables we’ll reference later in the code. Notice I’ve commented out getting of the actual date so that I’ll can set hours, minutes and seconds manually until I’m ready for real time.</p><pre>const margin = { top: 0, right: 0, bottom: 0, left: 0 }<br>const width = window.innerWidth - margin.left - margin.right<br>const height = window.innerHeight - margin.top - margin.bottom<br><br>// let date = new Date();  hours = date.getHours() % 12; minutes = date.getMinutes(); seconds = date.getSeconds()<br>// set hours, minutes and seconds manually - play with these values to see that the circles layout appropriately<br>let hours = 3   //range from 0 - 12<br>let minutes = 30 //range from 0 - 60<br>let seconds = 45 //range from 0 - 60</pre><p>Now we’ll set a few more constants for each of the circles. This may look a little tedious but it allows us to play with the size of the circles and have everything from here on out render nicely.</p><pre>// set constants to define the radius of each circle<br>// faceRadius is 95% of smaller(width, height)<br>const outterRadius = (Math.min(width, height) / 2) * 0.95<br>const faceR = outterRadius<br>const hourR = faceR * 0.75<br>const minR = hourR * 0.75<br>const secR = minR * 0.75<br><br>// set constants to define the distance the center of the clock face<br>const hRadiusDiff = faceR - hourR<br>const mRadiusDiff = hourR - minR<br>const sRadiusDiff = minR - secR<br>//clock face is the maximum circle in the viewport, centered, set the coordinates for its center here<br>const faceCenter = [width / 2, height / 2]</pre><p>Add the function <em>xyFromBase</em> to do the magic of transforming time to radians and return the coordindates of the center of the circle.</p><pre>//xyFromBase is a function that returns the center of the circle<br>xyFromBase = (baseTime, radius, base) =&gt; {<br>  //baseTime is current minutes, seconds or hours<br>  //radius is distance from the current circle to the next outer circle<br>  //base is 60 for minutes and seconds; 12 for hours<br>  let radians = (baseTime * 360) / base<br>  radians = (radians * Math.PI) / 180<br>  radians = radians - Math.PI / 2<br>  return [Math.cos(radians) * radius, Math.sin(radians) * radius]<br>}</pre><p><strong>Finally getting into some D3.js</strong> here! We’ll add an SVG element to the body of the DOM, then append the four circles: clockFaceCircle, hourCircle, minuteCircle and secondCircle. Note the call to the function <em>xyFromBase</em> before setting up hours, minutes and seconds.</p><pre>//append an svg element to the body<br>let svg = d3<br>  .select(&#39;body&#39;)<br>  .append(&#39;svg&#39;)<br>  .attr(&#39;width&#39;, width)<br>  .attr(&#39;height&#39;, height)<br><br>let clockFaceCircle = svg<br>  .append(&#39;circle&#39;)<br>  .style(&#39;fill&#39;, &#39;lightblue&#39;)  //using lightblue for illustration purposes<br>  .attr(&#39;cx&#39;, faceCenter[0])<br>  .attr(&#39;cy&#39;, faceCenter[1])<br>  .attr(&#39;r&#39;, faceR)<br><br>//draw the hour circle<br>let hCenter = xyFromBase(hours, hRadiusDiff, 12)<br>hCenter[0] += faceCenter[0]<br>hCenter[1] += faceCenter[1]<br>let hourCircle = svg<br>  .append(&#39;circle&#39;)<br>  .style(&#39;fill&#39;, &#39;none&#39;)<br>  .style(&#39;stroke&#39;, &#39;#990000&#39;)<br>  .style(&#39;stroke-width&#39;, 6)<br>  .attr(&#39;cx&#39;, hCenter[0])<br>  .attr(&#39;cy&#39;, hCenter[1])<br>  .attr(&#39;r&#39;, hourR)<br><br>//draw the minute circle<br>let mCenter = xyFromBase(minutes, mRadiusDiff, 60)<br>mCenter[0] += hCenter[0]<br>mCenter[1] += hCenter[1]<br>let minuteCircle = svg<br>  .append(&#39;circle&#39;)<br>  .style(&#39;fill&#39;, &#39;none&#39;)<br>  .style(&#39;stroke&#39;, &#39;#20b2aa&#39;)<br>  .style(&#39;stroke-width&#39;, 6)<br>  .attr(&#39;cx&#39;, mCenter[0])<br>  .attr(&#39;cy&#39;, mCenter[1])<br>  .attr(&#39;r&#39;, minR)<br><br>//draw the seconds circle<br>let sCenter = xyFromBase(seconds, sRadiusDiff, 60)<br>sCenter[0] += mCenter[0]<br>sCenter[1] += mCenter[1]<br>let secondCircle = svg<br>  .append(&#39;circle&#39;)<br>  .style(&#39;fill&#39;, &#39;none&#39;)<br>  .style(&#39;stroke&#39;, &#39;#001be5&#39;)<br>  .style(&#39;stroke-width&#39;, 6)<br>  .attr(&#39;cx&#39;, sCenter[0])<br>  .attr(&#39;cy&#39;, sCenter[1])<br>  .attr(&#39;r&#39;, secR)</pre><p>Last but not least, we’ll add some code to display the actual time.</p><pre>//add the time in text -- and make it clean!<br>addZeroBefore = time =&gt; {<br>  return (time &lt; 10 ? &#39;0&#39; : &#39;&#39;) + time<br>}<br>const timeText = [<br>  {<br>    value: `${addZeroBefore(hours)}:${addZeroBefore(<br>        minutes<br>      )}:${addZeroBefore(seconds)}`,<br>    size: 0,<br>    label: &#39;&#39;<br>  },<br>]<br><br>let field = svg<br>  .selectAll(&#39;.field&#39;)<br>  .data(timeText)<br>  .enter()<br>  .append(&#39;g&#39;)<br>  .attr(&#39;class&#39;, &#39;field&#39;)<br><br>let label = field<br>  .append(&#39;text&#39;)<br>  .attr(&#39;x&#39;, 40)<br>  .attr(&#39;y&#39;, height - 40)<br>  .attr(&#39;font-size&#39;, &#39;20px&#39;)<br>  .attr(&#39;font-family&#39;, &#39;sans-serif&#39;)<br>  .style(&#39;fill&#39;, &#39;white&#39;)<br>  .attr(&#39;class&#39;, &#39;label&#39;)<br><br>label.text(function(d) {<br>    return d.value + d.label<br>})</pre><p>You can view the finished piece complete with transitions / animation here:</p><p><a href="http://blockbuilder.org/lbrucel/7c20298c7a3f1992c1dc5accce6cdeb9">http://blockbuilder.org/lbrucel/7c20298c7a3f1992c1dc5accce6cdeb9</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=348500587b50" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>