Question

[HARD !!] Help in zooming to a selected area

Asked by: avner

I've been working for a while on this code and I need a second pair of eyes here.

The idea is as follow :

1. Save the Code in an HTML file.
2. Open in browser.
3. Click the [Drag/Zoom] Icon, change it to Zoom.
4. Click anywhere on the screen and try to zoom into the elephants eye.

You will notice that after 5+ zoom activities, you will lose your target.

Half of the code is a "debug" code, so you can ignore it.

Also, if anyone has a ready made code to receive the same effect (IE5.5+ only) I will consider this as an answer.


------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Blank:Document </TITLE>
<META NAME="Author" CONTENT="(C)Avner Cohen">
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
var initX="";
var initY="";
var bDrag = true;
var bDoReal = true;
function fillDiv(div,basicStr,counter)
{
var div=document.getElementById(div);
var str="";
for (var i=0;i<counter;i++)
      {
                  str+=basicStr
                  str+=(i%10==0)?" ":"";
      }
div.innerHTML=str;
}

function scrollDiv()
{
      var div=document.getElementById("moshe");
      div.scrollTop=(initY-event.y);
      div.scrollLeft=(initX-event.x);

}

function dragDiv(doEnd)
      {
var div=document.getElementById("moshe");
      if (!doEnd)
                  {
                        initX=event.x+div.scrollLeft;
                        initY=event.y+div.scrollTop;
                        div.onmousemove=scrollDiv
                        event.srcElement.style.cursor="move";
                  }
                  else
                  {
                        div.onmousemove=null
                        event.srcElement.style.cursor="";
                  }
      }

function actionDiv(doEnd)
{
if (bDrag)
      {
            dragDiv(doEnd)
      }
      else
      {
            drawBox(doEnd);
      }
}


var oBox = null;
function drawBox(doEnd)
{
var div=document.getElementById("moshe");
if (!doEnd)
      {
            if (!oBox)
                  {
                  oBox=document.createElement("DIV");
                  document.body.appendChild(oBox);
                  div.onmousemove=drawBox
                  with(oBox.style)
                        {            
                        position="absolute";
                        border="2px dashed";
                        width = "20px";
                        height = "15px";
                        }
                  }
            else //Case that mouse is now moving, drag the size
                  {
                  with(oBox.style)
                        {
                              left = event.x-2
                              top = event.y-2
                        }
                  }
      }
      else if (oBox)
      {
            //Box is up, calculate the center of the box and zoom into it.
            var oImage = div.firstChild;
            var iHeight = parseInt(oBox.offsetHeight);
            var iWidth = parseInt(oBox.offsetWidth);

            var iPosXImage = posX(oImage)
            var iPosYImage = posY(oImage)

            var iPosEvent_X = event.clientX;
            var iPosEvent_Y = event.clientY;

            var iDistanceInImage_X =  event.screenX-  iPosXImage;
            var iDistanceInImage_Y = event.screenY-  iPosYImage;
            
            var iCenter_X = Math.round( iDistanceInImage_X -( iWidth));
            var iCenter_Y = Math.round(iDistanceInImage_Y - (iHeight)) ;

            var iZoomFactor = Math.floor((oImage.style.zoom == "") ? 120 : parseInt(oImage.style.zoom)*1.2 );
            if (iZoomFactor >400)
                  {
                        iZoomFactor = 400
                  }

            //Calculate offset of  (iScrollTo_X/iScrollTo_Y) from center of Image;
            //Center of Image
            var iCenterOfImage_X =  iPosXImage + (oImage.parentNode.offsetWidth/2)
            var iCenterOfImage_Y =  iPosYImage +(oImage.parentNode.offsetHeight/2)
            //distance from posX of Event
            var iDistance_X =  iPosEvent_X - iCenterOfImage_X;
            var iDistance_Y =  iPosEvent_Y - iCenterOfImage_Y;


            //Scroll To Point :
            var iRelativeScrollTop = Math.round( parseInt(div.scrollTop) )   /// (iZoomFactor/100) );
            var iRelativeScrollLeft =  Math.round( parseInt(div.scrollLeft) )  /// (iZoomFactor/100) );


            var iScrollTo_X = iRelativeScrollLeft  + (iDistance_X * -1 ) ;
            var iScrollTo_Y = iRelativeScrollTop  + (iDistance_Y*  -1) ;

            //var iScrollTo_X = (iCenter_X + iRelativeScrollLeft + iDistance_X)  //* (iZoomFactor/100);
            //var iScrollTo_Y = (iCenter_Y + iRelativeScrollTop + iDistance_Y)  // * (iZoomFactor/100) ;

      //var iScrollTo_Y = (event.y + iRelativeScrollTop + iDistance_Y +  iCenter_Y) - (div.offsetHeight/2)
      //var iScrollTo_X = (event.x + iRelativeScrollLeft + iDistance_X +  iCenter_X) - (div.offsetWidth/2)
      
            var oTA = document.getElementById("output");
            oTA.innerText+="\n" +iIndex + ": [X:" + iScrollTo_X+",Y:" +  iScrollTo_Y+"]\n"+iIndex + ": [xdis" + iDistance_X + ",ydis" +iDistance_Y+"]";
                                                      
            
                  xPoint.value = xScroll.value = iScrollTo_X;
                  yPoint.value = yScroll.value = iScrollTo_Y;

            DrawPoint(div, iScrollTo_X, iScrollTo_Y);
            if (bDoReal)
            {
            ScrollTo(div, iScrollTo_X, iScrollTo_Y);
            oImage.style.zoom = iZoomFactor + "%";
            }
            else
            {
            document.getElementById("xShould").innerText = div.scrollTop;
            document.getElementById("yShould").innerText = div.scrollLeft;
            }


//debugger;            
            oBox.parentNode.removeChild(oBox);
                  div.onmousemove=null
                  oBox = null;
            event.srcElement.style.cursor="";
      }
}


function ScrollTo(oParent, x, y )
{
                  oParent.scrollTop = x;
                  oParent.scrollLeft = y;
}

      //Debug
var iIndex = 0;
function DrawPoint(oParent, x, y )
{

            var oPoint  = document.createElement("DIV");
            oPoint.innerText = iIndex;
            with (oPoint.style)
            {
                  fontSize="9px";
                  zIndex=999
                  overflow="hidden";
                  backgroundColor = "yellow";
                  height=oPoint.style.width="10px";
                  position="absolute";
                  left= x  ;
                  top= y;
            }
            oParent.appendChild(oPoint);
++ iIndex;
}

//Debug
function changeDoReal()
{
bDoReal = !bDoReal;
document.getElementById("DoReal").innerText = bDoReal;
}

function changeZoomState(oButton)
{
bDrag = !bDrag;
oButton.innerText = (bDrag) ?"Drag" : "Zoom";
}


function posX(obj)
{
      var curleft = 0;
      while (obj.offsetParent)
      {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
      }
      return curleft;
}

function posY(obj)
{
      var curtop = 0;
      while (obj.offsetParent)
      {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
      }
      return curtop;
}
-->
</SCRIPT>
</HEAD>

<BODY>
<button onclick ="changeDoReal()" >Do Real</button>[<span id="DoReal">true</span>]<BR>
<button onclick="DrawPoint(document.getElementById('moshe'), xPoint.value, yPoint.value);">Draw Point</button><br>
X <input type="text" style="width:30px" id="xPoint" value="10">, Y <input type="text" style="width:30px" id="yPoint" value="15"><br>
<button onclick="ScrollTo(document.getElementById('moshe'), xScroll.value, yScroll.value);">Scroll To</button><br>
X <input type="text" style="width:30px" id="xScroll" value="10">, Y <input type="text" style="width:30px" id="yScroll" value="15"><br>
Should Be: <br>
X[<span id="xShould"></span>], Y [<span id="yShould"></span>]<br>
<button onclick="changeZoomState(this)">Drag</button>(Drag/Zoom)
<button onclick="document.getElementById('moshe').firstChild.style.zoom=''">Reset Zoom</button><br>
<textarea cols="26" rows="27" id="output"></textarea>
<DIV style="position:absolute;width:395px;height:530px;top:10%;left:30%;background-color:red;overflow:scroll" id="moshe">
<img src="http://www.templetons.com/brad/elephant.jpg"  src1="TIFImage.png"  width="360px" onmousedown="actionDiv()" onmouseup="actionDiv(true)" onselectstart="return false;" ondragstart="return false;">
</DIV><br>
</BODY>
</HTML>




-----------------------------------------------------


Good luck and thanks for the effort.


Regards,
             Avner.


This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-12-22 at 01:04:24ID20832132
Tags

div

,

javascript

Topic

JavaScript

Participating Experts
2
Points
500
Comments
9

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. hard on eyes?
    Is a flat panel monitor or laptop screen less damaging on the eyes (for a person predisposed for glaucoma) than a cathode ray desktop monitor?
  2. Meta
    I got all meta tags right for search (key words) but i cant find the site when i search true search engines. Is there anything else i got to do? Use applications or what?
  3. Offsets....
    OK... I have to go to a Offset in the Boot Sector called 002Ah 0042d and read whats in!! But I don't have any Idea how to do that !! Please help me... Lycar
  4. Simple Zoom in and Zoom out
    Hello Experts, Can anyone give me an idea about how I can do simple zoomin and zoomout functions in my application? My application attempts to draw simple GDI thingies such as lines,arc,ellipses,rectangles etc on the TPaintbox canvas. I want to able to z...
  5. SUM/COUNTIF/COUNTA/OFFSET formula returning DIV …
    http://rapidshare.de/files/16562223/EXAMPLE_FORMULA.xls.html {=SUM(1/COUNTIF(OFFSET($C$5,1,0,COUNTA(C$5:C$65536)),OFFSET($C$5,1,0,COUNTA(C$5:C$65536)))*(OFFSET($C$5,1,4,COUNTA(C$5:C$65536))))} Above is the example sheet of exactly how it is being used... It uses index in c...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: PurplePerlsPosted on 2003-12-23 at 14:03:09ID: 9994003

Hello,

I do not know if that helps you, but I would change the relationship of your small box to this:

function drawBox(doEnd)
{
var div=document.getElementById("moshe");
if (!doEnd)
     {
          if (!oBox)
               {
               oBox=document.createElement("DIV");
               //document.body.appendChild(oBox);
               div.appendChild(oBox);
               div.onmousemove=drawBox
               with(oBox.style)
                    {          
                    position="absolute";
                    border="2px dashed";
                    width = "20px";
                    height = "15px";
                    }
               }
          else //Case that mouse is now moving, drag the size
               {
               with(oBox.style)
                    {
                         left = event.x-div.offsetLeft+div.scrollLeft;
                         top = event.y-div.offsetTop+div.scrollTop;
                    }
               }
     }...

 

by: avnerPosted on 2003-12-23 at 22:14:57ID: 9995374

Well, thanks for that, but what I'm looking for is just for a solution in regards to the zooming part, the box itself is fine.

 

by: PurplePerlsPosted on 2003-12-25 at 10:38:36ID: 10000336

First of all: Merry Christmas!:)

 

by: ZeroPagePosted on 2003-12-25 at 13:55:56ID: 10000542

The ScrollTo function is swapping x and y coordinates.
Change it to this:

function ScrollTo(oParent, x, y ) {
  oParent.scrollLeft = x;
  oParent.scrollTop = y;
}


Also reduce your zooming function to this:

var oBox = null;
function drawBox(doEnd) {
  var div=document.getElementById("moshe");
  if (!doEnd) {
    if (!oBox) {
      oBox=document.createElement("DIV");
      //document.body.appendChild(oBox);
      div.appendChild(oBox);
      oBox.shiftX = event.x+div.scrollLeft;
      oBox.shiftY = event.y+div.scrollTop;
      div.onmousemove=drawBox
      with(oBox.style) {          
        position="absolute";
        border="2px dashed";
        width = "20px";
        height = "15px";
      }
    } else  {//Case that mouse is now moving, drag the size
      with(oBox.style) {
        left = event.x-div.offsetLeft+div.scrollLeft;
        top = event.y-div.offsetTop+div.scrollTop;
      }
    }
  } else if (oBox) {
    //Box is up, calculate the center of the box and zoom into it.
    var oImage = div.firstChild;
    var divH = parseInt(div.offsetHeight);
    var divW = parseInt(div.offsetWidth);
    var boxH = parseInt(oBox.offsetHeight);
    var boxW = parseInt(oBox.offsetWidth);
    var boxX = oBox.offsetLeft;
    var boxY = oBox.offsetTop;
    var imgX = oImage.offsetLeft;
    var imgY = oImage.offsetTop;
    var boxCenter_X = Math.round(boxX + (boxW/2));
    var boxCenter_Y = Math.round(boxY + (boxH/2));
    var oZoomFactor = Math.floor((oImage.style.zoom == "") ? 100 : parseInt(oImage.style.zoom));
    var nZoomFactor = Math.floor(oZoomFactor * 1.2);
    if (nZoomFactor > 400) {
        nZoomFactor = 400
    }
   
    //Scroll To Point :
    var newBoxCenterX = Math.round(boxCenter_X / oZoomFactor * nZoomFactor)
    var newBoxCenterY = Math.round(boxCenter_Y / oZoomFactor * nZoomFactor)
    var divScrollTo_X = Math.round(newBoxCenterX - (divW / 2));
    var divScrollTo_Y = Math.round(newBoxCenterY - (divH / 2));
    if(divScrollTo_X<0) divScrollTo_X = 0;
    if(divScrollTo_Y<0) divScrollTo_Y = 0;
    var theForm = document.forms[0];
    var oTA = theForm.output;
    oTA.value +="\n" +iIndex + ": [X:" + divScrollTo_X+",Y:" +  divScrollTo_Y+"]\n"+iIndex + ": [xdis" + imgX + ",ydis" +imgY+"]";
    theForm.xPoint.value = theForm.xScroll.value = divScrollTo_X;
    theForm.yPoint.value = theForm.yScroll.value = divScrollTo_Y;
    DrawPoint(div, newBoxCenterX, newBoxCenterY);
    if (bDoReal) {
      ScrollTo(div, divScrollTo_X, divScrollTo_Y);
      oImage.style.zoom = nZoomFactor + "%";
    } else {
      document.getElementById("xShould").innerHTML = div.scrollTop;
      document.getElementById("yShould").innerHTML = div.scrollLeft;
    }
   //debugger;          
   oBox.parentNode.removeChild(oBox);
   div.onmousemove=null
   oBox = null;
   event.srcElement.style.cursor="";
  }
}

Add <form> and </form> inside of your <body> section and replace innerText by innerHTML.

Good luck,
ZeroPage


 

by: ZeroPagePosted on 2003-12-25 at 14:03:39ID: 10000550

Oh, perhaps that notification line should be something like this:

    oTA.value +="\n" +iIndex + ": [X:" + divScrollTo_X+",Y:" +  divScrollTo_Y+"]\n"+iIndex + ": [x:" + newBoxCenterX + ",y:" +newBoxCenterY+"]";


 

by: ZeroPagePosted on 2003-12-25 at 14:17:07ID: 10000571

Uhps, the oBox initialization missed the x and y coordinates.

So here the revised function:

var oBox = null;
function drawBox(doEnd) {
  var div=document.getElementById("moshe");
  if (!doEnd) {
    if (!oBox) {
      oBox=document.createElement("DIV");
      //document.body.appendChild(oBox);
      div.appendChild(oBox);
      div.onmousemove=drawBox
      with(oBox.style) {          
        position="absolute";
        border="2px dashed";
        width = "20px";
        height = "15px";
        left = event.x-div.offsetLeft+div.scrollLeft;
        top = event.y-div.offsetTop+div.scrollTop;
      }
    } else  {//Case that mouse is now moving, drag the size
      with(oBox.style) {
        left = event.x-div.offsetLeft+div.scrollLeft;
        top = event.y-div.offsetTop+div.scrollTop;
      }
    }
  } else if (oBox) {
    //Box is up, calculate the center of the box and zoom into it.
    var oImage = div.firstChild;
    var divH = parseInt(div.offsetHeight);
    var divW = parseInt(div.offsetWidth);
    var boxH = parseInt(oBox.offsetHeight);
    var boxW = parseInt(oBox.offsetWidth);
    var boxX = oBox.offsetLeft;
    var boxY = oBox.offsetTop;
    var imgX = oImage.offsetLeft;
    var imgY = oImage.offsetTop;
    var boxCenter_X = Math.round(boxX + (boxW/2));
    var boxCenter_Y = Math.round(boxY + (boxH/2));
    var oZoomFactor = Math.floor((oImage.style.zoom == "") ? 100 : parseInt(oImage.style.zoom));
    var nZoomFactor = Math.floor(oZoomFactor * 1.2);
    if (nZoomFactor > 400) {
        nZoomFactor = 400
    }
   
    //Scroll To Point :
    var newBoxCenterX = Math.round(boxCenter_X / oZoomFactor * nZoomFactor)
    var newBoxCenterY = Math.round(boxCenter_Y / oZoomFactor * nZoomFactor)
    var divScrollTo_X = Math.round(newBoxCenterX - (divW / 2));
    var divScrollTo_Y = Math.round(newBoxCenterY - (divH / 2));
    if(divScrollTo_X<0) divScrollTo_X = 0;
    if(divScrollTo_Y<0) divScrollTo_Y = 0;
    var theForm = document.forms[0];
    var oTA = theForm.output;
    oTA.value +="\n" +iIndex + ": [X:" + divScrollTo_X+",Y:" +  divScrollTo_Y+"]\n"+iIndex + ": [x:" + newBoxCenterX + ",y:" +newBoxCenterY+"]";
    theForm.xPoint.value = theForm.xScroll.value = divScrollTo_X;
    theForm.yPoint.value = theForm.yScroll.value = divScrollTo_Y;
    DrawPoint(div, newBoxCenterX, newBoxCenterY);
    if (bDoReal) {
      ScrollTo(div, divScrollTo_X, divScrollTo_Y);
      oImage.style.zoom = nZoomFactor + "%";
    } else {
      document.getElementById("xShould").innerHTML = div.scrollTop;
      document.getElementById("yShould").innerHTML = div.scrollLeft;
    }
   //debugger;          
   oBox.parentNode.removeChild(oBox);
   div.onmousemove=null
   oBox = null;
   event.srcElement.style.cursor="";
  }
}



 

by: avnerPosted on 2003-12-27 at 22:21:23ID: 10006837

ZeroPage,
I removed some debugging parts  from my code, here is your full version, and it still does not work :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Blank:Document </TITLE>
<META NAME="Author" CONTENT="(C)Avner Cohen">
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
var initX="";
var initY="";
var bDrag = true;
var bDoReal = true;

function scrollDiv()
{
      var div=document.getElementById("moshe");
      div.scrollTop=(initY-event.y);
      div.scrollLeft=(initX-event.x);

}

function dragDiv(doEnd)
      {
var div=document.getElementById("moshe");
      if (!doEnd)
                  {
                        initX=event.x+div.scrollLeft;
                        initY=event.y+div.scrollTop;
                        div.onmousemove=scrollDiv
                        event.srcElement.style.cursor="move";
                  }
                  else
                  {
                        div.onmousemove=null
                        event.srcElement.style.cursor="";
                  }
      }

function actionDiv(doEnd)
{
if (bDrag)
      {
            dragDiv(doEnd)
      }
      else
      {
            drawBox(doEnd);
      }
}


var oBox = null;
function drawBox(doEnd) {
 var div=document.getElementById("moshe");
 if (!doEnd) {
   if (!oBox) {
     oBox=document.createElement("DIV");
     //document.body.appendChild(oBox);
     div.appendChild(oBox);
     div.onmousemove=drawBox
     with(oBox.style) {          
       position="absolute";
       border="2px dashed";
       width = "20px";
       height = "15px";
       left = event.x-div.offsetLeft+div.scrollLeft;
       top = event.y-div.offsetTop+div.scrollTop;
     }
   } else  {//Case that mouse is now moving, drag the size
     with(oBox.style) {
       left = event.x-div.offsetLeft+div.scrollLeft;
       top = event.y-div.offsetTop+div.scrollTop;
     }
   }
 } else if (oBox) {
   //Box is up, calculate the center of the box and zoom into it.
   var oImage = div.firstChild;
   var divH = parseInt(div.offsetHeight);
   var divW = parseInt(div.offsetWidth);
   var boxH = parseInt(oBox.offsetHeight);
   var boxW = parseInt(oBox.offsetWidth);
   var boxX = oBox.offsetLeft;
   var boxY = oBox.offsetTop;
   var imgX = oImage.offsetLeft;
   var imgY = oImage.offsetTop;
   var boxCenter_X = Math.round(boxX + (boxW/2));
   var boxCenter_Y = Math.round(boxY + (boxH/2));
   var oZoomFactor = Math.floor((oImage.style.zoom == "") ? 100 : parseInt(oImage.style.zoom));
   var nZoomFactor = Math.floor(oZoomFactor * 1.2);
   if (nZoomFactor > 400) {
       nZoomFactor = 400
   }
   
   //Scroll To Point :
    var newBoxCenterX = Math.round(boxCenter_X / oZoomFactor * nZoomFactor)
   var newBoxCenterY = Math.round(boxCenter_Y / oZoomFactor * nZoomFactor)
   var divScrollTo_X = Math.round(newBoxCenterX - (divW / 2));
   var divScrollTo_Y = Math.round(newBoxCenterY - (divH / 2));
   if(divScrollTo_X<0) divScrollTo_X = 0;
   if(divScrollTo_Y<0) divScrollTo_Y = 0;
   var theForm = document.forms[0];
   
   DrawPoint(div, newBoxCenterX, newBoxCenterY);
   if (bDoReal) {
     ScrollTo(div, divScrollTo_X, divScrollTo_Y);
     oImage.style.zoom = nZoomFactor + "%";
   } else {
   }
  //debugger;          
  oBox.parentNode.removeChild(oBox);
  div.onmousemove=null
  oBox = null;
  event.srcElement.style.cursor="";
 }
}



function ScrollTo(oParent, x, y )
{
                  oParent.scrollTop = x;
                  oParent.scrollLeft = y;
}

      //Debug
var iIndex = 0;
function DrawPoint(oParent, x, y )
{

            var oPoint  = document.createElement("DIV");
            oPoint.innerText = iIndex;
            with (oPoint.style)
            {
                  fontSize="9px";
                  zIndex=999
                  overflow="hidden";
                  backgroundColor = "yellow";
                  height=oPoint.style.width="10px";
                  position="absolute";
                  left= x  ;
                  top= y;
            }
            oParent.appendChild(oPoint);
      ++ iIndex;
}

//Debug
function changeDoReal()
{
bDoReal = !bDoReal;
document.getElementById("DoReal").innerText = bDoReal;
}

function changeZoomState(oButton)
{
bDrag = !bDrag;
oButton.innerText = (bDrag) ?"Drag" : "Zoom";
}


function posX(obj)
{
      var curleft = 0;
      while (obj.offsetParent)
      {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
      }
      return curleft;
}

function posY(obj)
{
      var curtop = 0;
      while (obj.offsetParent)
      {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
      }
      return curtop;
}
-->
</SCRIPT>
</HEAD>

<BODY>
<button onclick="changeZoomState(this)">Drag</button>(Drag/Zoom)
<button onclick="document.getElementById('moshe').firstChild.style.zoom=''">Reset Zoom</button><br>

<DIV style="position:absolute;width:395px;height:530px;top:10%;left:30%;background-color:red;overflow:scroll" id="moshe">
<img src1="http://www.templetons.com/brad/elephant.jpg"  src="TIFImage.png"  width="360px" onmousedown="actionDiv()" onmouseup="actionDiv(true)" onselectstart="return false;" ondragstart="return false;">
</DIV><br>
</BODY>
</HTML>


 

by: avnerPosted on 2003-12-27 at 22:24:42ID: 10006844

On a second thought, I made some modifications and it worked fine, thanks !.

 

by: ZeroPagePosted on 2003-12-28 at 05:08:33ID: 10007241

Thank you for the points.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...