[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

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

Asked by avner in JavaScript

Tags: div, javascript

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.


[+][-]12/25/03 01:55 PM, ID: 10000542Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: JavaScript
Tags: div, javascript
Sign Up Now!
Solution Provided By: ZeroPage
Participating Experts: 2
Solution Grade: A
 
[+][-]12/23/03 02:03 PM, ID: 9994003Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/23/03 10:14 PM, ID: 9995374Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/25/03 10:38 AM, ID: 10000336Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/25/03 02:03 PM, ID: 10000550Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/25/03 02:17 PM, ID: 10000571Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/27/03 10:21 PM, ID: 10006837Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/27/03 10:24 PM, ID: 10006844Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/28/03 05:08 AM, ID: 10007241Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92