Home arrow JavaScript arrow Page 3 - Improving an Image Zooming Application with JavaScript
JAVASCRIPT

Improving an Image Zooming Application with JavaScript


Among the plethora of JavaScript applications that can be developed to extend the existing functionality of a web site, zooming into and out of bitmapped images is quite possibly one of easiest to build and implement. Thus, if you’re a web developer who’s interested in learning how to incorporate basic zooming capabilities into your own web applications, then don’t waste any more time. Start reading this article now!

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
March 11, 2008
TABLE OF CONTENTS:
  1. · Improving an Image Zooming Application with JavaScript
  2. · Listing the complete source code of the initial JavaScript zooming application
  3. · Shortening the JavaScript code
  4. · Listing the improved source code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Improving an Image Zooming Application with JavaScript - Shortening the JavaScript code
(Page 3 of 4 )

As I explained in the section that you just read, it's highly desirable to merge the two previous JavaScript functions into a unique one, in this way shortening the complete source code of this JavaScript application, while making it slightly more robust and efficient too.

In simple terms, the two aforementioned functions can be easily integrated into one piece of code, if they're coded in the following manner:


unction ZoomImage(inc){

var img=document.getElementById('image');

if(!img){return};

img.setAttribute('width',parseInt(img.getAttribute('width'))+inc);

img.setAttribute('height',parseInt(img.getAttribute('height'))+inc);

if(!img.style.left){img.style.left='0px'};

if(!img.style.top){img.style.top='0px'};

img.style.left=parseInt(img.style.left)-(inc/2)+'px';

img.style.top=parseInt(img.style.top)-(inc/2)+'px';

}


That was pretty simple. As you can see, now the above "ZoomImage()" JavaScript function implements all of the business logic required for zooming into and out of the selected bitmapped image. But as demonstrated in this particular case, the function in question has merged the functionality of its respective ancestors into one snippet of code.

Also, you should notice that the prior "ZoomImage()" function takes on the increment value that should be applied to the width and height attributes of the targeted image to achieve the zooming in/out effects. Of course, these can be performed by feeding the function with both positive and negative integer values, one process that is clearly illustrated by the JavaScript code below:


window.onload=function(){

var btn1=document.getElementById('button1');

if(!btn1){return};

btn1.onclick=function(){

ZoomImage(20);

}

var btn2=document.getElementById('button2');

if(!btn2){return};

btn2.onclick=function(){

ZoomImage(-20);

}

}


As shown in the above JavaScript code, each zooming button built previously is simply attached to the "ZoomImage()" function. This will cause the function to be triggered every time the button is clicked. Each zoom control will pass to the function a different zooming value that is positive or negative, depending on the type of effect that needs to be achieved.

At this stage, I believe that you already grasped the underlying logic that drives the previous "ZoomImage()" JavaScript function. As you can see, it is now capable of performing zooming in/out effects on a selected image.

So the question that comes up here is: what's the next step? Well, assuming that you understood how the previous JavaScript function works, in the last section of this tutorial I'm going to show you how this function can be linked with the other modules of this client-side zooming application, that is the pertinent structural markup and CSS styles as well.

Logically, to see the complete source code corresponding to this image zooming JavaScript application, you'll have to click on the link that appears below and read the next few lines.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials