O'Reilly logo

Supercharged JavaScript Graphics by Raffaele Cecco

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Converting into a jQuery Plug-in

Converting the bouncy sprite application into a jQuery plug-in adds functionality that takes advantage of jQuery’s ability to easily search for DOM elements via CSS selectors and return them as a list for further processing. The plug-in will search for any specified elements and attach multiple bouncySprite instances to them via the bouncyBoss object. It will also allow you to change the background color of the elements being attached to and the number of bouncySprite instances being attached.

Although converting the bouncy sprite application into a flexible jQuery plug-in may sound like a substantial job, there’s actually less work involved than you might expect. Because of the modular and self-contained way in which the DHTMLSprite, bouncySprite, and bouncyBoss objects were developed, they will slip unobtrusively into a jQuery plug-in structure.

The initial solitary semicolon may seem odd (it is not a typo), but it protects against problems arising from cases where the code preceding the plug-in does not end with a semicolon as expected. Normally, this is not an issue, as JavaScript will identify the plug-in code after a line break as a new statement. However, if the preceding code and the plug-in were minified, the whitespace—including line breaks—would probably be removed. The plug-in would subsequently fail due to the lack of an identifying break between it and the preceding code.

;    // Initial solitary semicolon.

Here, we define an anonymous function. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required