Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Forum Contest: Create an Analog Clock

EDIT: This contest has ended! Congrats to Christine Dawson for her winning grandfather clock entry. The next forum contest is all about designing a responsive data table.

Hi everybody,

It's time for a new Treehouse Forum contest! First, watch this video to learn more:

After you've watched the video, please read the details below carefully. I'm looking forward to your entries!

How to Enter: Create your analog clock entry as a "pen" on Codepen.io and then post the link to your pen as an answer to this post.

Due Date: All entries must be submitted by Monday, May 26th at 11:45pm ET. Here's a timezone chart so you can see what time that is for your locale.

Prize: The entries will be judged by Treehouse teachers based on both design and code. One winner will receive a free month of Treehouse Pro on us! :) We'll announce the winner on Tuesday, May 27th and reveal the next contest.

So does it matter what we program it in? Like can I use swingFX with CSS and core JavaSE or just some swing and JavaSE.......not valid and have to be in Android? Has to be a language that has a course track on the site what's the deal sir? Sorry I'm new I couldn't find anywhere. I was just curious. Sounded fun and I am bored for the day..

You I think Team Learning Tree and their wonderful team of staff should seriously consider adding a delete button these comments. Now I can't delete my irrelevant comment above.

Like the theme. Unfortunately I don't know Javascript.

Here's my solution mostly CSS based solution. Took me 18 minutes. Would work only in modern browsers because of CSS3 and querySelector.

http://codepen.io/viktorbezdek/full/HdeGt/

26 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Good luck to all participates! Looking forward to seeing your designs.

Done mine! http://codepen.io/lematy/pen/JmeEb

but i photoshopped some images for the hands and the clock. Is that allowed or does it all have to be in css?

John Coffin
John Coffin
10,359 Points

This looks awesome. I'm a little ticked. I was hoping to submit first. ;)

FYI, I'm using images for clock face and hands as well (or at least my "first draft" ... and I have yet to get to a second draft in a weeks time). There is nothing in the rules that says you have to animate every pixel using CSS, and the use of images is smart.

John Coffin
John Coffin
10,359 Points

This looks awesome. I'm a little ticked. I was hoping to submit first. ;)

FYI, I'm using images for clock face and hands as well (or at least my "first draft" ... and I have yet to get to a second draft in a weeks time). There is nothing in the rules that says you have to animate every pixel using CSS, and the use of images is smart.

Becky Castle
Becky Castle
15,294 Points

Oh man, it's the "school clock"... Hee hee, nice work

Thanks :D and i tried without images but that didnt look as good lol

John Coffin
John Coffin
10,359 Points

The game is afoot!!!

Here's my first iteration: Full Page / Code.

I have the basic mechanics working, but I'll need to get my second iteration done before week's end (for the first time ever) if I am to beat Daniel.

--- later ---

Here's my second iteration: Full / Code

-- much later --

Final submission: Full Page / CodePen

Thats awesome! :D

John Coffin
John Coffin
10,359 Points

Here's my second iteration:

Full / Code

See below for more details.

Becky Castle
Becky Castle
15,294 Points

Nice. Is that an Armani? ;)

I don't know if moderators are allowed to compete in these or not, but I wanted to participate. I've probably disqualified myself by using SVG, but I realised I have never experimented with SVG and this seemed like a nice opportunity to do so.

I opted for a simple, minimal design because I didn't want to complicate the SVG too much. I used CSS as little as possible — I'm only centering the container div on the page.

All the colors are defined in the SVG, including the clock shadow gradient. The background color of the page is set in JavaScript as it's dynamic — it changes according to the time of day (it gets lighter towards noon and darker towards midnight).

The shadow that the clock throws also moves during daytime, travelling from left to right as the "Sun" moves across the sky. It also changes intensity (opacity). I picked arbitrary sunrise (5am) and sunset times (7pm) to keep the code as simple as possible.

During "night" the shadow is fixed at an angle of -45deg, and its opacity is set to 0.2, due to some imaginary static light source.

All the functions that calculate hand and shadow data were declared as methods on the Date.prototype property. I chose to extend the built-in object to add analog clock functionality to it. It seemed appropriate in the context of the contest.

Finally, this currently doesn't work in Firefox, as Firefox doesn't have transform-origin support for SVG.

Okay, here's the clock: Full page/Code

John Coffin
John Coffin
10,359 Points

This is deceptively simple. I'm glad you gave the long description; I'm going to check it out later.

IMO you should not disqualify yourself for either. I personally enjoy the fact that I am competing with people who are much better than I am at many different things and who know a wider range of skills. I find that I learn more about things that I might not have tried otherwise.

Nick Pettit
Nick Pettit
Treehouse Teacher

Moderators are definitely allowed to participate. :)

Nick Pettit, that's good to know. :)

John Coffin, I figured I'd provide a detailed explanation as the dynamic shadow/background could get missed at a glance.

Finally, I've decided against fixing this for Firefox. Any solution that I could come up with either complicates the SVG too much or the JavaScript code running the whole thing.

This is more of an experiment than anything else, so I hope no one will hold it against me.

Here's my entry: Full / Code

John Coffin
John Coffin
10,359 Points

Ok. You made me open my mouth in awe. Does it "dong" on the hour?

:D Wish I'd thought of making it chime!

John Coffin
John Coffin
10,359 Points

It would be really cool if you did.

John Coffin
John Coffin
10,359 Points

I think that your clock is trending.

Awesome!

Charlie Hield
Charlie Hield
4,767 Points

Went a totally different direction with mine.

Word Clock -- fullscreen & code

John Coffin
John Coffin
10,359 Points

You did, didn't you.

Good choice in changing the color between minutes. Did you try out having seconds, minutes, and hours as bars? I'm curious if making it easier to tell time at a glance (by doing this) destroys the simple charm of only showing seconds.

John Coffin
John Coffin
10,359 Points

Is there any way to randomize the start-up color? Otherwise, I might not wait until the minute is over to discover the color changing (well ... I would, but someone else might not).

Charlie Hield
Charlie Hield
4,767 Points

Yep, just added random on load.

Sam Nabhan
Sam Nabhan
2,157 Points

That's brilliant, Charlie. Way to think outside the box :)

Well done.

Dude that is just pure out of the box thinking.

Becky Castle
Becky Castle
15,294 Points

Sam is right. If this is box: [], here's you, way over here --------------------> 8-) That's an inspiring design.

Really nice one. +1

John Coffin
John Coffin
10,359 Points

Here comes my (wait for it) second iteration:

Full / Code

There are a few "known issues," not the least of which is that the responsive design went out the window. Oh, and I only have webkit imlementation, so you'll need Chrome or Safari. Hopefully I'll have this all cleaned up right quick.

-- much later --

Final submission: Full Page / CodePen

Nice! I like the badges idea :)

John Coffin
John Coffin
10,359 Points

Thanks! This was something that I've wanted to do, so I'm happy that I finally got a chance.

Okay, here's my second version: Full / Code

Wait! That looks like my first version! Well, yes... visually, it's identical, but I've made significant under the hood changes.

First of all, only the shadow is an SVG. The clock itself is a collection of divs.

I grabbed the clock functionality from my first version and turned it into a JavaScript clock library called mikulas.js. I named it after the Czech clockmaker who co-created the Prague astronomical clock.

I am no longer extending the Date object, I have a new Mikulas object that contains a date property.

It will work both on divs and SVG as all you need to do is pass it the ids of the elements representing clock hands. Optionally, you can pass it a callback function which will get executed every time the clock is updated (I use this for the shadow and background changes):

var c = new Mikulas("h", "m", "s", function() {
  console.log("Clock updated!");
});

You can set the time to a particular time or let it run normally:

c.setClockTime(someDateObject); // setting it to particular time
c.start(); // letting the clock work normally based on local time

If you wish, you can also stop the clock at some point:

c.stop();

Basically, as long as you have a bunch of elements resembling clock hands (set at 12 o'clock), it'll work. The transform-origin property is set programatically on the hand elements.

This version also works in Firefox because I'm not rotating the SVG path, instead, I'm rotating the div containing the SVG.

I'm importing mikulas.js from my GitHub page inside the CodePen, but you can see what it looks like here or on Github.

The shadow and background dynamics aren't a part of mikulas, instead they are custom functions called in the optional callback argument.

I created this using HTML5 Canvas which goes hand in hand with Javascript. There are no images or external libraries :) I must say this was quite fun!

Full screen: http://codepen.io/joshsanger/full/yAFpa/ Code: http://codepen.io/joshsanger/pen/yAFpa

Jacques Vincilione
Jacques Vincilione
17,292 Points

Left my answer as a comment on the post. Oops.

Here ya go, hope you like it:

http://codepen.io/jvincilione/full/gakLF

Jacques Vincilione
Jacques Vincilione
17,292 Points

After looking at some of the others, I decided to change mine a bit.

Full Page With Editor

Comments/Critiques are appreciated. Entire analog clock built with canvas.

Ashlynn Pai
Ashlynn Pai
11,679 Points

Nice! I think you may be able to sell that.

Ashlynn Pai
Ashlynn Pai
11,679 Points

Modifications: replaced the golden hand pointer image replaced outer ring image

Hope it's okay for me to keep tweaking after I post.

http://codepen.io/ashlynnpai/full/Cphar

http://codepen.io/ashlynnpai/pen/Cphar/

My clock is based on the Astronomical Clock in Prague. I wanted to work on something Czech so luckily this project came along because I was going to make something beer related. I did nearly all the work today so there may be some mistakes I can't see from being too tired, so feel free to let me know.

Functions of the clock:

Outer ring -- This is an approximation of old Czech time and show the hours until sunset, with the golden number 24 marking the time of sunset. It doesn't use the proper algorithms because I didn't want to get bogged down here (http://www.mysundial.ca/files/H2SSManual040801.pdf). Instead from Dec 12, the ring rotates clockwise .42 degrees per day. From June 21, it rotates counterclockwise .42 degrees per day. These are the days of earliest and latest sunset in Prague this year. Maybe later I will fix the algorithm, but not today =).

Main ring -- This shows the hour according to astronomical time, with noon at 0 degrees and midnight at 180 degrees. The golden hand points to the current hour. I added the minute and second hand (which is not authentic) so people could see something in motion as everything else doesn't move often.

Inner ring -- This shows the signs of the Zodiac and the position of the sun on the hand pointer is supposed to identify the current Zodiac sign, so it rotates along with the hour hand. Right now it's 30 days behind so I'll have to redo my calculations later. If anyone spots my error that would be great. I'm too tired to see it.

Sorry no figurine show! I was going to make one then decided not to.

Ashlynn Pai
Ashlynn Pai
11,679 Points

It may have been only one day off because of rounding and now it's corrected, as yesterday was only one day past the start of the new zodiac. It seems to be working now.

Juan Carlos Vega Abarca
Juan Carlos Vega Abarca
2,295 Points

Hey Guys, here is my Clock. I have used Canvas and some Math to develop this.

Pen - Full

I have added a button to increase speed of time, so you can move forward to see all states of this clock.

Thanks

Charlie Hield
Charlie Hield
4,767 Points

Nice! Totally reminds me of Polarclock from way back when.

Ian Hudson
Ian Hudson
13,457 Points

Juan,

Very nice and futuristic. :D

Ian

NICE!! I love the concept. Very well-polished look also.

Chris Mitchell
Chris Mitchell
12,719 Points

needed help with javascript so ty to Dino for your explanations, I have never participated in any contest before but here is my attempt:

full page: http://codepen.io/jangofett/full/zutcv

code: http://codepen.io/jangofett/pen/zutcv?editors=001

This is made in pure CSS3, no Javascript: Full | Code

John Coffin
John Coffin
10,359 Points

Final submission: Full Page / CodePen

In the end, simplicity won the day. There were too many bells and whistles with the badges ... and they looked ugly.

Matthew Hicks
Matthew Hicks
9,641 Points

Here's mine!

http://codepen.io/Talmarei/pen/vmxcb

This is my first contest entry, had lots of fun with this - hope you like it. Looking forward to taking part in more!

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi everyone,

Wow! Once again, there are tons of amazing entries. We went through all of them several times today and you certainly didn't make it easy to decide a winner. :) You should all feel proud of the work you've done here, because now you have that much more experience and another item to include in your portfolio.

This week's winner is Christine Dawson! She put together this amazing grandfather clock using HTML, CSS, and JavaScript. Like most of the designs here, this wouldn't quite make semantic sense on a webpage, but it certainly looks very well polished. Congrats, Christine!

Our next forum contest is all about creating a responsive data table.

Oh wow, that's made my day! :D Thanks!

Here's my solution mostly CSS based solution. Took me 18 minutes. Would work only in modern browsers because of CSS3 and querySelector.

http://codepen.io/viktorbezdek/full/HdeGt/