nooshu - Matt Hobbs' Web Development Blog

Kneeling on the shoulders of giants

Category: Vanilla JavaScript

Brendan Eichs baby, JavaScript what was once considered evil has finally matured into a very useful language.

Coolclock: HTML5 Clock that is Actually Quite Cool*

*: Unless you’re Internet Explorer, then not so much.

Every so often a designer asks for something a little different, this time it was for an animated clock that sits in the top right corner of a website. Usually that means looking for a Flash based solution, but Canvas is the new Flash… apparently; luckily Coolclock is available to plug the no-flash gap.

Coolclock is a fantastic little script that will generate a canvas based clock from the parameters you pass it via the class attribute (I don’t agree with that, but it works). It’s fully customisable by way of a little JSON data, allowing you to create your own skins. There are quite a few user submitted skins available to use on the Coolclock homepage if you don’t want to create your own.

I needed the clock to be able to change depending on the country that’s chosen from a dropdown list, here’s how I achieved it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Generate the clock
var generateClock = function(){
    //Create our dropdown, time difference compared to GMT as a value
    var dropHTML = "<select id='localTime'>";
    dropHTML += "<option value='0'>United Kingdom</option>";
    dropHTML += "<option value='+1'>France</option>";
    dropHTML += "<option value='-4'>New York</option>";
    dropHTML += "</select>";
    //Append to the container
    $("#time").append(dropHTML);
   
    //Initial clock appended to the page (local time)
    var clockHTML = '<canvas id="clk1" class="CoolClock:swissRail:27:noSeconds"></canvas>';
    $("#clock").append(clockHTML);
   
    //When the dropdown changes
    $("#localTime").bind("change", function(){
        var value = $(this).val();
        var localClockHTML = '<canvas id="clk1" class="CoolClock:swissRail:27:noSeconds:' + value + '"></canvas>';
        //Empty the old clock, append the new clock
        $("#clock").empty().append(localClockHTML);
        //Fire the coolclock function to generate the new clock
        CoolClock.findAndCreateClocks();
    });
}();

Very simple, the relevant HTML code is generated with JavaScript as to not leave a pointless dropdown on the page when it’s turned off. The key to the solution was calling the CoolClock.findAndCreateClocks() function from the coolclock.js file, without that the clock isn’t recreated when the dropdown is changed.

As mentioned above, the script seems to come unstuck when viewed in Internet Explorer, since IE doesn’t support the canvas tag. You can plug the lack of canvas support using ExplorerCanvas, but even then coolclock is a little quirky. I managed to get the clock working with IE6 & IE7 but IE8 wasn’t having any of it; bit of of shame as it’s a nice solution. Good news is on the horizon though, IE9 will support canvas by default so no nasty JavaScript hacks.

Eventually I decided to add the clock as a type of ‘progressive enhancement’ by wrapping the function in the following:

1
2
3
4
//IE returns false since it uses 'styleFloat'.
if(jQuery.support.cssFloat){
    //IE can't see this code
}

It’s not great, and I hate having to do it but if users want to see a cool clock they will have to upgrade to a modern browser (that’s not IE8).

For those who are interested, here’s a quick demo of the code in action.

jsFiddle: My new favourite website

Every so often you come across a site that is so amazingly obvious that you think “why didn’t I think of that!”, jsFiddle is one of those sites. It allows you to paste your HTML, CSS and JavaScript code, and view the result all in the same page…simple! There are other sites out there that do the same such as Jsbin, but they just don’t do it as well.

My favourite feature that really stands out is being able to include one of the many popular JavaScript library’s by simple toggling a drop-down box; it even has different versions of each library to choose from. This is a really handy feature if you are a plug-in developer; keep your plug-in code in jsFiddle and quickly see if the latest version of the library breaks it.

jsFiddle code snippet GUI

Quickly view your code change at the touch of a button

Library’s included so far are:

  • Mootools Core
  • jQuery
  • Prototype
  • YUI
  • BBC Glow
  • Dojo
  • Processing.js

A couple of other features available are the easy to access code examples for each library and ability to run Ajax requests directly in the page. It’s like a mini IDE in your browser, all it needs now is an error console; maybe that is on their to-do list. If you don’t use JavaScript library’s and just want vanilla JavaScript, they have that option too.

As an example I’ve added my charactersLeft jQuery plug-in to jsFiddle, take a look here. Very cool huh!

Full Frontal 2009 in Brighton

6 speakers at presenting at full frontal 2009

Full Frontal 2009 - The line up

This post is slightly late as the conference was on the 20th November 2009. But better late than never i guess!

The day was very wet and rainy but that didn’t take anything away from a brilliant conference. Some great speakers there: Simon Willison, Christian Heilmann and Peter-Paul Koch(ppk) to name a few.

The two highlights for me were the very entertaining talk on ‘Optimising where it hurts’ by Jake Archibold and a great introduction to node.js by Simon Willison. It was quite a technical talk and after a long day of listening to various people present it did make my brain hurt a little. I’ve added node.js to the ‘TODO’ list of things to look at… eventually.

Hopefully there will be another Full Frontal in 2010, if there is I’ll be going again.

Currently on page 2 of 212