Matteo Spinelli's

Cubiq.org

> Autopsy of a site redesign

The good part of having a personal blog is that you can play and experiment with it. I’ve finally abandoned the XHTML madness and embraced HTML5, I explored the canvas element (you can see it in the logo) and finally added tablet and smartphone compatibility thanks to media queries. Read more for backstage access.

Starting from IE9 the designer is in charge. We are a little less slaves of browsers discrepancies and we can enjoy our job once again (or for the first time?). Of course this is not completely true, IE is still lagging behind, but is getting there. I must say that I was surprised to see that the layout was working with very small adjustments on IE9.

Webfonts

Actually the worst part has been choosing the right @font-face. Each browser on each platform renders text in a very different way, and finding the type face that looked good on all clients wasn’t easy.

A very good resource is Google webfonts, I wanted to use Oswald but anti-aliasing is so different between browsers that it almost looks like you are using different types on each client.

The alternative is to host your own optimized webfont. Fontsquirrel has a very good collection of ready to use fonts. I’ve chosen Miso Regular for cubiq.org, but the default font-face kit wasn’t rendering fine on anything but Chrome. Fortunately Fontsquirrel has an invaluable conversion tool, and the solution was to set X-height Matching to Arial in the expert options.

Another not so trivial aspect is how to declare your @font-face in the stylesheet. Paul Irish suggests the “smiley” solution:

Canvas (aka ain’t flash!)

I also wanted to study the feasibility of a flash-free rotating 3d cube in my logo.

A full CSS version might be possible, but I chose the canvas path for its wider compatibility across browsers.

Please note that the cube is not webGL, but plain old 2d canvas with a faked 3d effect. The logo and rotation are calculated on the fly (no pre-rendering) and the code involved is just 4kb un-minified (no external libraries used). You can enjoy the simplicity of the script by looking at the source code.

I’m using requestAnimationFrame for a steady frame-rate and to reduce CPU usage on compatible browsers.

All the script does is to make some basic matrix rotations. Since we are not in a real 3d world, the only challenge was to decide when a side of the cube is back and not visible to the viewer’s eye.

This has been solved with a dirty trick. The light source comes straight from the front. I’ve added a vector for the color shading that is converted into a value from 0 to 255 (the RGB intensity). With trial and error I’ve found that a color intensity lesser than 22 means that the front light doesn’t reach the cube side that translates into the side not being visible to the user.

Basically if the light doesn’t reach a side, the user’s eye (that is parallel to the light source) can’t see it and so there’s no need to draw that face. I think it’s easier to understand looking at the code than to explain.

I bet there are better ways to accomplish the same task, but I’m no 3D guru, and my high school trigonometry is rusty to say the least… but hey, it works and it’s free for you to use.

The result is compatible with all the browsers I was targeting (anything equal or above to IE9), performance is good even on older iPhones and CPU usage is very low. The black ship in this scenario is Android tablet, it’s really sad to see a higher frame-rate on a first generation iPod touch than on a latest generation Android tablet.

Media queries

If you are targeting [r/d]ecent browsers @media is all you need to serve different styles to the various platforms.

For my blog I use one default stylesheet with two variants: one for tablets…

@media screen and (min-width:481px) and (max-width:800px) {
...
}

… and one for smartphones:

@media screen and (max-width:480px) {
...
}

Note that I use max-width:800px (and not 768) so to better serve Android tablets too.

I also don’t like the max-device-width directive, I find the max-width more “polite”, easier to debug and less prone to false-positives.

If you are on desktop and resize your browser you’ll see the three site versions.

Why not an elastic layout instead? It might work, especially for tablets, but there are certain elements of the layout that need to be refactored to better fit the smaller screens. This is especially true for smartphones where links must be larger, page scrolling/swiping should be reduced and less importants bits (eg: the sidebar) should be relegated to the very bottom of the page.

I like the professional look of the site. Thank for sharing your experience with html5 and sharing some of the source code. I want to give my site a redesign (and actually include more), but haven’t had the time.