JavaScript
  Home arrow JavaScript arrow Page 3 - Improving an Image Zooming Application wit...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Improving an Image Zooming Application with JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-03-11

    Table of Contents:
  • Improving an Image Zooming Application with JavaScript
  • Listing the complete source code of the initial JavaScript zooming application
  • Shortening the JavaScript code
  • Listing the improved source code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In the previous article, you saw how to zooming on bitmapped images with a simple...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT