JavaScript
  Home arrow JavaScript arrow Page 4 - 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  
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

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


    Zooming in on Images with JavaScript - Attaching the JavaScript functions to the zooming controls


    (Page 4 of 4 )

    As I explained in the previous section, to complete the development of this JavaScript-driven zooming application, it’s necessary to attach the pair of functions that I defined earlier to the respective zoom controls, in this case by using a couple of “onclick” event handlers.

    This process must be performed after the pertinent web document has been loaded, which can be achieved in the following way:


    window.onload=function(){

    if(document.getElementById&&document.getElementsByTagName&&document.
    createElement){

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

    if(!btn1){return};

    btn1.onclick=function(){

    ZoomInImage();

    }

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

    if(!btn2){return};

    btn2.onclick=function(){

    ZoomOutImage();

    }

    }

    }


    Short and effective! Now the two zooming buttons will trigger each of the corresponding JavaScript functions each time they’re clicked, in this manner implementing a basic zooming mechanism, which on this specific occasion, targets only one image.

    Also, in order to complement even more thoroughly the previous explanation, below I listed the complete source code corresponding to this JavaScript-based zooming application along with an additional screen shot that shows how the sample image is zoomed in when the appropriate control is clicked.

    That being said, here’s the complete source code for this JavaScript application:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Zooming images with Javascript (uses two different functions)</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #eee;

    }

    h1{

    font: bold 14pt Arial, Helvetica, sans-serif;

    color: #000;

    text-align: center;

    }

    img{

    position: absolute;

    }

    #container{

    position: relative;

    width: 263px;

    height: 150px;

    margin-left: auto;

    margin-right: auto;

    overflow: hidden;

    background: #9cf;

    border: 1px solid #000;

    }

    #controlpanel{

    width: 263px;

    margin-left: auto;

    margin-right: auto;

    background: #9cf;

    text-align: center;

    border: 1px solid #000;

    }

    </style>

    <script type="text/javascript">

    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';

    }

    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';

    }

    window.onload=function(){

    if(document.getElementById&&document.getElementsByTagName&&document.
    createElement){

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

    if(!btn1){return};

    btn1.onclick=function(){

    ZoomInImage();

    }

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

    if(!btn2){return};

    btn2.onclick=function(){

    ZoomOutImage();

    }

    }

    }

    </script>

    </head>

    <body>

    <h1>Zooming images with Javascript (uses two different functions)</h1>

    <div id="container"><img src="sample_image.jpg" width="263" height="150"
    id="image" /></div>

    <div id="controlpanel">

    <p><input type="button" id="button1" value="+" /><input type="button"
    id="button2" value="-" /></p>

    </div>

    </body>

    </html>



    That’s all for the moment. As I say in many of my articles on web development, you’re completely free to modify all of the code samples shown here. Thus, you can incorporate your own modifications to this basic image zooming application.

    Final thoughts

    In this first installment of the series, I showed you how to build a primitive zooming application with JavaScript, which in its current version works only with one target image. However, this limitation will be properly addressed in the next part, along with the incorporation of some other significant improvements.

    Now that you’ve been warned, you won’t want to miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    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