jQuery

jQuery GooglePlus Widget

I have just been toying around a few minutes with the google+ api.

The result is a small jQuery plugin that can display and update a users activity stream.

Setting up the plugin is really simple – just include it using

<script src="https://raw.github.com/wbg/jQuery-GooglePlus-Widget/master/js/jquery.googleplus.widget.min.js"></script>

And start it using

$(function() {
  	       $('#target').googleplusWidget({api_key:'API KEY',user_id:'USER ID'}).trigger('start_gplus');
});

You can register an API key using the google api console: https://code.google.com/apis/console/

Get it on github


Javascript Graphics Resources – SVG, Canvas, Infovis

Lazy weekend collection – some useful visualization libraries for your Javascript Applications.

jQuery SVG is a nice structured plugin that allows you to interact with SVG-Canvas Elements. It also features extensions for graphing and plotting.

Raphaël on the other hand is a simply kept library to handle your Vector-Graphics needs on the web. Especially interesting is the support of SVG as well as VML. The last one providing a decent fallback for some well known not-so-good browsers.

Interesting new development: mozilla added mozRequestAnimationFrame for better animation timing with html5 / css3 browsers. Looks very interesting, the only drawback is, that it is an experiment and not guaranteed to stay.

And finally some really awesome stuff: JavaScript InfoVis Toolkit is a stunning tool for creating infovisualizations on the web. Developed since 2008 it features a boatload of visualization techniques and what is especially impressive: you can interact with your graphs and everything is smoothly animated.


Javascript libraries – this time nodejs and some extensions

node.js is a high performance, strictly non-blocking I/O server based on googles V8 javascript engine. As HTTP is a first-class protocoll in node, it seems to be very suitable for specific tasks like streaming content over HTTP or high-performance realtime web-applications.

A really nice and clean sinatra-style web application framework for node.js is expressjs. It seems to be a good choice for lightweight web-applications.

Josi also looks like a very usable MVC framework for node and the broke framework is something to look out for as it tries to reimplement the awesome django in javascript (they are looking for helping developers btw.).

Some database drivers/connectors for node.js:

And finally there is a nice article on howtonode.org combining node.js, expressjs and mongodb to build a simple blog engine. Great introduction!


Some HTML5, CSS3, JS links

Sitepoint published a large series of Presentations covering CSS3 on youtube. Really worth watching.

If you are not too satisfied with the user experience of the jQuery API documentation you can always build your own using the jQuery API Search. learningjquery.com has a gentle introduction to it.

Neteye published a handy little jQuery plugin that takes the pain out of performing all those nice css3 transformations. A good way to speed up development of effect heavy applications.

And some valid reasons for using the newest browser features by Thomas Fuchs. I’m glad that we only have to support one browser on our interactive displays and can use all those goodies.

Last, be sure to check out the newest Firefox builds with JaegerMonkey JVM enabled. A pretty impressive speedup, possibly faster than the IE9 beta, maybe even a competitor for safari / chrome.


HTML5 Flickr Visualization (iPad Test)

This post describes a little experiment using Flickr API, a webkit browser with support for CSS animations and transformations i.e. -webkit-animate and -webkit-transform and a little bit of jQuery that holds everything together. One important aspect is, that the developed animation should also run smoothly on mobile devices like the iPhone and the iPad.

The main idea is an endless stream of photos flying by the viewport using some nice CSS animations. So the perfect photosource seems to be the public Flickr stream. This brings us to step 1: Load Photos from Flickr.

Load Photos from Flickr

Collecting our photos is quite simple as Flickr offers their public streams in jsonp format – i.e. JSON Data passed to a Javascript function call. The Base url for all Flickr REST Methods is:

http://api.Flickr.com/services/rest/

The required parameter for this url is ‘method’, in our case we want to get the recent public photos so we use

method=Flickr.photos.getRecent

In addition to that we want some extra data (the url to the medium sized file, and the name of the flickr member posting the file) in our json stream. Therefore we set two additional parameters:

format=json&extras=url_m,owner_name

Last we want to pass a json callback so we can overcome ajax crossdomain restrictions. Luckily jquery helps us here and we only have to add

&jsoncallback=?

So the complete url to our Flickr stream should look something like this:

http://api.Flickr.com/services/rest/?method=Flickr.photos.getRecent&format=json&extras=url_m,geo,owner_name&jsoncallback=?

Combined with some jQuery code we can load all photos inside the json stream and put the photos in our animation queue (aka a simple javascript array):

Now that we finished the code to access our data source, we can create the basic HTML and CSS for our page.

Set up HTML and CSS

Next thing up we will define the stage on which our animation takes place. In essence we just need a container and some element to hold our photo. So the basic HTML structure looks something like this:

To make this structure look like a polaroid we simply reset the ul to no margin, padding and list type. The main style is attached to the li element using a white border, a nice dropshadow and a little bit rounded corners. In addition to that we offset the image just outside the top left corner of our viewport:

The div and small elements are just positioned at the top of the polaroid with a white background – nothing special there. Now our Polaroid should look something like this:

As a nice touch we can also add some gradient to the body background using for example:

background:-webkit-gradient(linear, 40% 0%, 24% 100%, from(#666666), to(#969696))

This finishes the basic CSS setup and we can go on to defining the animations needed.

Defining the animations needed

To animate the photos we first need to setup our viewport aka stage with some perspective – 800 seems to look quite nice – and in addition to that preserve the 3d style for child elements:

To animate the polaroid i choose three steps (can be combined into one, but i split it up just for clarity). First we want to move the photo from outside the stage to the center and add a little bit of life to this animation with some 3d rotation. This can easily be accomplished using @-webkit-animation – see the example below:

By using some additional keyframes at different points in time (25%, 75%) the pace and style of the animation can be alterated. To start the animation you just need to define an additional css class with the -webkit-animate property. As soon as this CSS class is added to an element, the animation starts:

The next two animation steps pop up the image to a larger size using the scale transformation. After a certain amount of time, the third animation simply drops the image out of the viewport:

Nothing more is needed to define the animations! Only one piece remains – a little bit of jquery code that defines the Application flow.

jQuery code

The last step is the code that binds together the bits and pieces. The workflow is quite Simple: on pageload we load the Flickr stream and fill our image queue. Then we start the animation cycle: We take the first image out of the queue and add it to our polaroid element. At the same Time we also start preloading the next image in the queue. Then we start the animation by first moving the image to the center, then popping it up, waiting a little bit, then dropping it out. As soon as the dropping out animation is finished, we reset out polaroids to its initial position and start all over again, gradually working down our image queue:

Note that i simply used javascript timers with setTimeout to reduce this examples lenght. It should of course be the preferred way to use the webkit animation events (webkitAnimationStart, webkitAnimationEnd) as described on Apples developer site.

With the flow described above we use just one html element that we animate all over again. Adding new elements for each photo would be possible too, but this can trigger a reflows that reduce performance. On mobile devices like the iPad this could result in long running applications fill up the available memory. This inevitably leads to a browser crash. To add a little bit more life to the effect it would always be possible to add some more elements and alterate between them.

The final Result

The final result looks something like the picture below, but of course it is better to view the live demo using a webkit based browser.


jQuery plugin: stop cluttering your animation queue with hoverFlow

A typical Problem with jQuery hover animations is, that they build up the animation queue if the mouse is moved before the animation finished. There are quite a few solutions.

First you can use the excellent hoverIntent plugin and set it’s threshold to be above or equal to the animations duration. Another solution is to use jQuery’s stop() function and reset the elements style. Both of these tend to result in rather unnatural behaviors.

A nicer way to reset animations is implemented in the hoverFlow Plugin, that smoothly resets your animations and prevents the buildup of the animation queue. Thanks to Jürgen Genser for sharing it with me.

Implementing it is really straighforward – you simply replace jQuery’s animate() with hoverFlow(). See a quick example below:


Mobile Safari Memory Usage and Images

Mobile Safari is known to quite often crash / terminate silently when loading certain Webpages. Since iOS 4, this happens frequently on older devices like the iPhone 3GS, but sometimes it also happens on newer Phones and the iPad.

The crashes are caused by mobile safari using up all available memory on the device, and as iOS 4 needs more memory for itself, it worsens the situation.

It is quite well known, that mobile safari does not like images that much. So the obvious solution is quite simple: reduce the amount of images on your page. This should be a standard procedure during the development of a useable mobile design anyway, but as Thomas Fuchs pointed out, advanced tricks like replacing gradient images with custom canvas elements can be beneficial too.

This solution is not possible for things like galleries or news sites with large images. In these cases ajax and dynamic loading of image content seems to be the best solution. In essence you load images as they start to move into the viewport and remove them as soon as they leave it – i.e. the user scrolls away. The simple way would be to just remove the image from the dom like in jquery:

$('.my-image').remove()

In our tests this however did not solve the problem, as constant loading and removing of images sooner or later overloaded the little mobile browser and lead to a crash. Still after a little bit of work on our side, we found a solution that seems to prevent memory growth quite effectively. The general idea is to reuse javascript Image objects to load inline image data – some sample code below:

This leads to a bit more work when implementing the frontend, but can quite effectively reduce memory usage and increase stability on mobile safari. We did not test this solutio on devices other than iPhone and iPad, but as mobile safari is webkit based, this solution should also improve pages on android devices. Though these are frequently equipped with much more memory anyway.

And last but not least: try to restrict your use of google web fonts or typekit fonts to a minimum, as loading more than one font weight / loading more than one font seems to crash mobile safari – more details on these problems can be found  on google code as well as the typekit blog.