Posted by & filed under JavaScript, JQuery.

check the canvas snow demo

In one of my last posts I created a snow storm with jquery and div elements, one disadvantages Of course is that the flakes are square. one advantage is that using divs makes it far more easier to create a creative snowstorm. For example the script could be easily recreated in a snow-tagcloud. Therefore I decides to recreate a snowstorm example this time however with the html5 canvas tag.
Beside the features I was curious about the performance of the animation. I expected the Canvas element to outrun jQuery and the DOM, in terms of animation. And indeed I ran both scripts with 1000 flakes and the canvas animation run much smoother.

image of the canvas snowstorm

And of course round snow flakes appear much more realistic

Posted by & filed under CSS, JavaScript, JQuery.

check snowstorm demo

Inspired by the current weather in the Netherlands I wanted to make snow with jQuery. A year Ago I would have used flash for this task, and I still believe flash is better for this specific job ( anyone fancies square snow flakes ). However I think the animation still stands strong. Of course the parallax effect and the opacity make the snowstorm realistic as well as the mousemove handler which makes it possible to change the wind direction.

the plugin itself is relatively small and can be found in scripts.js file, I did not make a separate plugin file because this snowstorm plugin is only a start, and most users would customize it extensively

screenshot of the snowstorm

The plugin follows the following steps.

1. interpret the options, and use them in local variables and objects

settings = jQuery.extend({
        width: 640,
        height: 480,
        viewColor: '#000000',
        flakes: 100
    }, settings);
    //plugin vars
    var view = this;
    var xOffset = 0;
    var extendedWidth = settings.width*2;

2. create and style the view container

// setView
    var cssObj = {
      'background-color' : settings.viewColor,
      'width' : settings.width,
      'height' : settings.height,
      'margin' : '0px',
      'padding' : '0px',
      'overflow': 'hidden',
      'position': 'relative'

3. add a mousemove handler to the view container

// create the mouse move handler
    view.bind("mousemove", function(event)
       xOffset = event.clientX - (settings.width/2) ;

4. create the flakes in a for loop

 //Add the flakes
    for( var i = 0; i < settings.flakes; i++ )
       // extend the possible x beyond the view
       var tx =  Math.ceil(  Math.random()* extendedWidth); 
       var ty = ( Math.ceil( Math.random()*50 ) ) -150; 
       var tsize = Math.ceil( Math.random()*10 + 4 ); 
       var topacity = tsize * 0.1;
       snowFlake( i, tx, ty, tsize, tsize, topacity);

5. Each flake starts it own animation function which call itself again

//Animate each flake
    function animateFlake( flake )
        // animate
        var flakeDelay = Math.ceil( Math.random()*7000 );
        var distanceY  = settings.height + 20;
        var ySpeed =  Math.ceil( (1/flake.width() )*20000 );
          "top": distanceY,
          "left": ("+="+xOffset)
        }, ySpeed, function() 
            //reset flake position
            var cssResetObj = {
              'top': ( -flake.width() + 'px'),
              'left': Math.ceil(  ( Math.random()* extendedWidth)- (settings.width*0.5)  )
            animateFlake( flake );

My next steps

After this experiment i want to to realize the same snowstorm with canvas. I am interested in the difference in performance. Of course Canvas should be way way faster because it lacks all the DOM hassle. And it would be possible to create much nicer snowflakes then white squares.

Posted by & filed under books.

Last weekend I finished the book “Code Complete 2” by. It was a book which I was interested in for a long. Luckily I found it on the bookshelf of my new employer. It is not the kind of book to finish in a weekend, it took me 2 month ( besides work and my family ). Most of the book is fun to read and well written, mainly because the content of the book is realistic and highly recognizable.
The beginning of the book starts is about the principle “measure twice, cut once”. Make sure you understand the requirements and that they are realistic.
The middle and the largest part provides us with a ton of coding best practices for any language, how to write logic, readable and performing code. During the whole book best practices are funded with (scientific) research data, which made it harder for me to ignore en neglect the best practices, which is a good thing.

The last main section of the book describes best practices to improve just written or existing applications. How the debug and refactor and a major requirement how to development collaborative. The last part of this section is not the least, is describes what the developers/ programmers personality and attitude should be to keep yourself motivated and highly skilled. It also discusses how to deal with managers, and how to keep your cost estimations to stand strong. instead of pleasing the client at first with a low price but begging for more budget to complete the project in the end.

Overall the best ( 6 year old! ) book I have read to increase my knowledge on software development. I would recommend it to ( front-end )webdevelopers who get involved in an increasing codebase as well as experienced programmers.

Book Details

  • Book: Code Complete 2
  • Author: Steve McConnel
  • publisher: Microsoft Press
  • pages: 860

Posted by & filed under JQuery.

I made this plug in while I was playing arround with jQuery. I am trying to transfer my flash skills to html. Learning actionscript started with a bouncing ball class. Therefore I tried to create the same in jQuery.

And here it is the boucingelement plugin. It was fun to make and you could do a lot of useless stuf with it. Like the form example on this page. Click in one of the input fields to start the plugin


or with parameters

$('#password').bouncingElement({ xpos: 80, ypos: 95, speedX: 1, speedY: 3, fps: 15 });

The main code is very basic as you can see in the snippet below. A simple setInterval functionin the plugin provides the animation you can change the speed by setting the fps parameter or or the speedX/ speedY parameter. It is important to position the bouncing elements absolute.

// change the position
o.xpos += o.speedX;
o.ypos += o.speedY;
ball.css("left", o.xpos);
ball.css("top", o.ypos);

// reverse the speed if the element hits the boundary
if (o.xpos < 0 || o.xpos > (canvas.width() - ball.width()))
o.speedX *= -1;

if (o.ypos < 0 || o.ypos > (canvas.height() - ball.height()))
o.speedY *= -1;

You can get the plugin and an example here.

Posted by & filed under flash, JQuery.

It seems to be that ExternalInterface will not work if the flash file is referenced with jQuery. Which is odd because the jQuery wil translate to the javascript function getElementById. However the following code gave javascript errors that the function send2Flash could not be found.

if( $("#debaak").length > 0 )

After discussing this subject we found the following solutions. JQuery returns an object, with the get function it is possible to get any element in the jQuery object returned by the jQuery selector


The javascript way also works perfectly whithout any errors.

if (document.getElementById)
flashMovie = document.getElementById("debaak");

if (flashMovie)

Below you can find the necessary code for flash.

public function Main()
ExternalInterface.addCallback("send2Flash", callJS);
private function callJS(msg)
//called from js
trace('call from js:' + msg)