123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!--
responsive clock
	| set width using the data-width attribute
	| all css units are valid (px, %, vw, vh ...)
-->

<main>
	<div id="clock" data-width="250px">
		<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path id="minutesA" d="m50 1.5c-26.5 0-48 22-48 48.5 0 26.5 21.5 48 48 48.5 26.5 0 48-22 48.5-48.5-.5-26.5-21.5-48.5-48-48.5h-.5" fill="none" stroke="#456990"/></svg>
		<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path id="minutesB" d="m50 1.5c-26.5 0-48 22-48 48.5 0 26.5 21.5 48 48 48.5 26.5 0 48-22 48.5-48.5-.5-26.5-21.5-48.5-48-48.5h-.5" fill="none" stroke="#e06b64"/></svg>
		<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path id="secondsPath" d="m50 1.5c-26.5 0-48 22-48 48.5 0 26.5 21.5 48 48 48.5 26.5 0 48-22 48.5-48.5-.5-26.5-21.5-48.5-48-48.5h-.5" fill="none" stroke="#2f3233"/></svg>
		<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path id="seconds" d="m50 1.5c-26.5 0-48 22-48 48.5 0 26.5 21.5 48 48 48.5 26.5 0 48-22 48.5-48.5-.5-26.5-21.5-48.5-48-48.5h-.5" fill="none" stroke="#fff"/></svg>
		
		<div id="hour">
			<div></div>
			<div></div>
			<div></div>
			<div></div>
			<div></div>
			<div><span></span></div>
		</div>
		
	</div>
</main>
            
          
!
            
              main {
	width: 100%;
	height: 100%;
	display: -webkit-box;
	display: -webkit-flex;
	display: -ms-flexbox;
	display: flex;
	-webkit-justify-content: space-around;
	    -ms-flex-pack: distribute;
	        justify-content: space-around;
	-webkit-box-align: center;
	-webkit-align-items: center;
	    -ms-flex-align: center;
	        align-items: center;
	background-image: radial-gradient(at top left,#585F63, #3c4042);
}

#clock {
	display: -webkit-box;
	display: -webkit-flex;
	display: -ms-flexbox;
	display: flex;
	-webkit-box-pack: center;
	-webkit-justify-content: center;
	    -ms-flex-pack: center;
	        justify-content: center;
	-webkit-box-align: center;
	-webkit-align-items: center;
	    -ms-flex-align: center;
	        align-items: center;
	border-radius: 50%;
}

svg:first-of-type,
svg:nth-of-type(2) {
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
	stroke-width: 40;
	border-radius: 50%;
}

svg:last-of-type,
svg:nth-of-type(3) {
	width: 104%;
	height: 104%;
	position: absolute;
	top: -2%;
	left: -2%;
	stroke-width: 1.5;
	border-radius: 50%;
}

svg:last-of-type {
	stroke-width: 2;
}

#hour {
	width: 80%;
	height: 80%;
	position: absolute;
	top: 10%;
	left: 10%;
	background: #transparent;
	border-radius: 50%;
}

#hour div {
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
}

#hour div:nth-child(2),
#hour div:nth-child(4) {
	-webkit-transform: rotate(30deg);
	        transform: rotate(30deg);
}

#hour div:nth-child(3),
#hour div:nth-child(5) {
	-webkit-transform: rotate(-30deg);
	        transform: rotate(-30deg);
}

#hour div::before,
#hour div::after {
	content: '';
	position: absolute;
	background: #282828;
}

#hour div:first-child::before {
	width: 2%;
	height: 12%;
	top: 10%;
	left: 49%;
}

#hour div:nth-child(2)::before,
#hour div:nth-child(3)::before {
	width: 2%;
	height: 4%;
	top: 10%;
	left: 49%;
}

#hour div:first-child::after {
	width: 2%;
	height: 12%;
	bottom: 10%;
	left: 49%;
}

#hour div:nth-child(2)::after,
#hour div:nth-child(3)::after {
	width: 2%;
	height: 4%;
	bottom: 10%;
	left: 49%;
}

#hour div:last-child::before {
	width: 12%;
	height: 2%;
	top: 49%;
	left: 10%;
}

#hour div:nth-child(4)::before,
#hour div:nth-child(5)::before {
	width: 4%;
	height: 2%;
	top: 49%;
	left: 10%;
}

#hour div:last-child::after {
	width: 12%;
	height: 2%;
	top: 49%;
	right: 10%;
}

#hour div:nth-child(4)::after,
#hour div:nth-child(5)::after {
	width: 4%;
	height: 2%;
	top: 49%;
	right: 10%;
}

#hour span {
	-webkit-transition: all 1s linear;
	transition: all 1s linear;
	width: 3%;
	height: 40%;
	position: absolute;
	bottom: 45%;
	left: 48.5%;
	background: #fff;
	border-top-left-radius: 100%;
	border-top-right-radius: 100%;
	-webkit-transform-origin: 50% 88%;
	        transform-origin: 50% 88%;
}
            
          
!
            
              // measure the length of the SVG path. All four will be the same
var pathLength = document.getElementById('minutesA').getTotalLength();

// calculate portion of pathLength that corresponds to
// one second and create global var
var addSecond = pathLength/60;
var second = 0;

// calculate portion of pathLength that corresponds to
// one second in one minute and create global var
var addMinuteSecond = addSecond/60;
var minute = 0;

// calculate degrees out of 360 that represent one second
// in a 12 hour period and create global var
var addHourSecond = ((360/12)/60)/60;
var hour = 0;

// ∞∞ build clock fn
function buildClock() {
	var clock = $('#clock');
	
	// retreive desired width
	var width = clock.data('width');
	var imgSrc = 'http://udy.io/rsc/img/tr_sq.png';
	var img = '<img src="' + imgSrc + '" width="100%" height="100%"/>';

	// set width and inject 10x10 px png to ensure it's a square
	clock.css({'width': width}).append(img);
}

// ∞∞ set clock fn
function setClock() {
	// get JS Date
	var time = new Date();
	
	// get seconds and convert to clock units
	var getSecond = time.getSeconds();
	var setSecond = getSecond*addSecond;

	// get minutes and convert to clock units
	var getMinute = time.getMinutes();
	var setMinute = getMinute*addMinuteSecond*60;

	// get hours
	var getHour = time.getHours();
	
	// convert 13h to 23h to 12h time
	var splitDay = (getHour > 11) ? getHour-12 : getHour;
	
	// convert hours to number of degrees (30deg per hour)
	// and add minutes to hour hand for continuous animation
	var setHour = (splitDay*30)+(getMinute*(addHourSecond*60));
	
	// prepare SVG paths
	$('#minutesB').css({'stroke-dasharray': pathLength});
	$('#seconds').css({'stroke-dasharray': pathLength/60 + ' ' + (pathLength/60)*59});
	
	// add necessary running transition
	setTimeout(function() {
		$('#minutesB, #seconds').css({'transition': 'all 1s linear'});
		
		// set clock
		setTimeout(function() {
			$('#seconds').css({'stroke-dashoffset': setSecond});
			$('#minutesB').css({'stroke-dashoffset': setMinute});
			$('#hour span').css({'transform': 'rotate(' + setHour + 'deg)'});
		}, 50);
	}, 50);
	
	// refresh global vars
	second = setSecond+addSecond;
	minute = setMinute+addMinuteSecond;
	hour = setHour+addHourSecond;
}

// ∞∞ run clock fn
function runClock() {
	
	// add global var values to correct elements
	$('#seconds').css({'stroke-dashoffset': second});
	$('#minutesB').css({'stroke-dashoffset': minute});
	$('#hour span').css({'transform': 'rotate(' + hour + 'deg)'});

	// refresh global vars
	second += addSecond;
	minute += addMinuteSecond;
	hour += addHourSecond;
}

// build
buildClock();

// use timeout to make sure elements have been built
setTimeout(setClock, 100);

// set one second intervals for running the clock
setInterval(runClock, 1000);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console