Cairo eye candy

To show off the new cairo bindings I have ported “MacSlow’s Cairo Clock” from C to Haskell:

The clock animates of course and can be draged around the screen and resized. Because it is drawn using vector graphics not bitmaps it looks good at all resolutions:
[img](90×90) [img](512×512)

The original C version was very slow because it redrew the whole picture every frame, even the bits of the clock that do not move. This version is about 30 times faster because it splits the image up into three layers, a background, middle and foregrund. Of these, only the middle layer containg the clock hands animates so the other two layers can be drawn just once and then saved in an intermediate surface. Then the three layers are composited together each frame. This means we still get the effect of the clock hands moving behind the reflections in the glass on the front of the clock.

A similar problem with the original was that resizing was very sluggish because it was redrawing the full image which is relatively expensive to do. With this version, as you resize it redraws using a lower quality mode which is much quicker and so the resizing is much smoother. When the user has finnished resizing the window, it redraws in the normal high quality.

This is a rather nice demo of using Gtk+ and cairo in Haskell. The code is not too long and quite clear. It has more features than the C version and yet it is only 2/3 of the size! If you want to build the program, note that it does require the development version of Gtk2Hs.

The only feature I have not tried yet is using it with an X11 desktop using a compositing manager. This should allow the clock to have a transparent background, making it appear to ‘hover’ above other windows and casting a shadow on them. I’ll be sure to post more screenshots when I’ve tried that!

Comments are closed.