D3.js Clock

Size
3,093 Kb
Views
30,360

How do I make an d3.js clock?

Just a looping (second and minute) clock built on d3.js. . What is a d3.js clock? How do you make a d3.js clock? This script and codes were developed by Joseph Martucci on 25 October 2022, Tuesday.

D3.js Clock Previews

D3.js Clock - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>d3.js Clock</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <main></main> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://d3js.org/d3.v3.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

D3.js Clock - Script Codes CSS Codes

@-webkit-keyframes hueRotate { 0% { background: #c80064; } 50% { background: #6400c8; }
}
@-moz-keyframes hueRotate { 0% { background: #c80064; } 50% { background: #6400c8; }
}
@keyframes hueRotate { 0% { background: #c80064; } 50% { background: #6400c8; }
}
html { height: 100%;
}
body { -webkit-animation: hueRotate 60s linear infinite; -moz-animation: hueRotate 60s linear infinite; animation: hueRotate 60s linear infinite; height: 100%; background: #c80064;
}
main { position: absolute; top: 50%; margin-top: -100px; left: 50%; margin-left: -100px; width: 200px;
}

D3.js Clock - Script Codes JS Codes

var svg = d3.select("main").append("svg") .attr("width", 200) .attr("height", 200);
var millisecondsArc = d3.svg.arc() .innerRadius(30) .outerRadius(40) .startAngle(0) .endAngle(0);
var secondsArc = d3.svg.arc() .innerRadius(40) .outerRadius(60) .startAngle(0) .endAngle(0);
var minuteArc = d3.svg.arc() .innerRadius(60) .outerRadius(80) .startAngle(0) .endAngle(0);
var hourArc = d3.svg.arc() .innerRadius(80) .outerRadius(100) .startAngle(0) .endAngle(0);
var milliLoop = false;
var milliReversed = false;
var secondsLoop = false;
var secondsReversed = false;
var minuteLoop = false;
var hourLoop = false;
svg.append("path") .attr("id", "milliseconds") .attr("d", millisecondsArc) .attr("transform", "translate(100,100)") .attr("fill", "rgba(255,255,255, .1)");
svg.append("path") .attr("id", "seconds") .attr("d", secondsArc) .attr("transform", "translate(100,100)") .attr("fill", "rgba(255,255,255, .3)");
svg.append("path") .attr("id", "minutes") .attr("d", minuteArc) .attr("transform", "translate(100,100)") .attr("fill", "rgba(255,255,255, .5)");
svg.append("path") .attr("id", "hours") .attr("d", hourArc) .attr("transform", "translate(100,100)") .attr("fill", "rgba(255,255,255, .7)");
/* color variables */
var dawn = "rgb(251,242,219)";
var noon = "rgb(253, 184, 19)";
var dusk = "rgb(255,0,100)";
var night = "rgb(0,0,40)";
var drawClock = function drawClock(){ var time = new Date(); var milliseconds = time.getMilliseconds(); var millisecondAngle = 360 * (milliseconds/999); var millisecondRadians = degToRad(millisecondAngle); var seconds = time.getSeconds(); var secondAngle = 360 * (seconds/59); var secondRadians = degToRad(secondAngle); var minutes = time.getMinutes(); var minAngle = 360 * ((minutes + (seconds / 59))/59); var minRadians = degToRad(minAngle); var hours = time.getHours(); var hourAngle = 360 * ((hours + (minutes/60))/24); var hourRadians = degToRad(hourAngle); // just reversed direction if(milliLoop && milliseconds < 500 && milliLoop > 500 && milliReversed != true){ millisecondsArc.endAngle(2*Math.PI).startAngle(millisecondRadians); milliReversed = true; } else if(milliReversed === true){ if(milliLoop && milliseconds < 500 && milliLoop > 500){ milliReversed = false; millisecondsArc.startAngle(0).endAngle(millisecondRadians); } else{ millisecondsArc.endAngle(2*Math.PI).startAngle(millisecondRadians); } } else{ millisecondsArc.startAngle(0).endAngle(millisecondRadians); } milliLoop = milliseconds; // loop around second doughnut. This could probably be simplified if(secondsLoop !== false && seconds < 30 && secondsLoop > 30 && secondsLoop != seconds && secondsReversed != true){ secondsArc.endAngle(2*Math.PI).startAngle(secondRadians); secondsReversed = true; } else if(secondsReversed === true){ if(seconds < 30 && secondsLoop > 30 && secondsLoop != seconds){ secondsReversed = false; secondsArc.startAngle(0).endAngle(secondRadians); } else{ secondsArc.endAngle(2*Math.PI).startAngle(secondRadians); } } else{ secondsArc.startAngle(0).endAngle(secondRadians); } secondsLoop = seconds; minuteArc.endAngle(minRadians); hourArc.endAngle(hourRadians); svg.select("#milliseconds").attr("d", millisecondsArc); svg.select("#seconds").attr("d", secondsArc); svg.select("#minutes").attr("d", minuteArc); svg.select("#hours").attr("d", hourArc);
}
function degToRad(degrees){ return degrees * (Math.PI/180);
}
setInterval(drawClock,1);
D3.js Clock - Script Codes
D3.js Clock - Script Codes
Home Page Home
Developer Joseph Martucci
Username jjmartucci
Uploaded October 25, 2022
Rating 3.5
Size 3,093 Kb
Views 30,360
Do you need developer help for D3.js Clock?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Joseph Martucci (jjmartucci) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!