SunQuest
 
       JavaScript
  Home arrow JavaScript arrow Page 3 - Zooming in on Images with JavaScript
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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
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

Zooming in on Images with JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-03-04

    Table of Contents:
  • Zooming in on Images with JavaScript
  • Developing a basic front-end
  • Defining a couple of handy JavaScript functions
  • Attaching the JavaScript functions to the zooming controls

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Zooming in on Images with JavaScript - Defining a couple of handy JavaScript functions


    (Page 3 of 4 )

    Simply put, the zooming JavaScript application that I plan to build here will be comprised of two primary functions. The first function will be responsible for zooming in the target image that you saw earlier, while the second one will zoom out.

    Having explained how these JavaScript functions are going to work, I suggest you look at their respective signatures, which are shown below:


    // define ‘ZoomInImage()’ function


    function ZoomInImage(){

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

    if(!img){return};

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

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

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

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

    img.style.left=parseInt(img.style.left)-10+'px';

    img.style.top=parseInt(img.style.top)-10+'px';

    }



    // define ‘ZoomOutImage()’ function


    function ZoomOutImage(){

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

    if(!img){return};

    img.setAttribute('width',parseInt(img.getAttribute('width'))-20);

    img.setAttribute('height',parseInt(img.getAttribute('height'))-20);

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

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

    img.style.left=parseInt(img.style.left)+10+'px';

    img.style.top=parseInt(img.style.top)+10+'px';

    }


    As shown previously, the above JavaScript functions, which I called “ZoomInImage()” and “ZoomOutImage()” respectively, were defined in such a way that they’re capable of performing a simple zoom in/out effect on the sample image that you saw earlier, which in this case was identified as “image.”

    Of course, as you can see, the programming logic implemented by these functions is actually very simple to grasp. Basically, it’s based upon dynamically changing the respective “width” and “height” attributes of the targeted image via the popular (although non-standard) “style” object.

    In addition, you should notice that each of the prior functions achieve the zooming effect by modifying the width and height attributes of the selected image at predefined increments and decrements of 10px each, all while maintaining the image in question completely centered within its respective container. This last task is performed by altering the image’s top and left coordinates, in this way implementing a simple –yet effective – zooming mechanism.

    All right, at this point I firmly believe that you've grasped how the two previous JavaScript functions do their things. Thus, my next move will be to attach the  functions to the respective zoom controls that you saw earlier, so each time they’re clicked on, the image in question will be enlarged or reduced in increments of 10px.

    To see how those zooming controls will be turned into fully-functional elements, you’ll have to jump forward and read the last section of this tutorial. Don’t worry, I’ll be there, waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Zooming in on bitmapped images with JavaScript is actually a simple process that can...
     

    JAVASCRIPT ARTICLES

    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway