Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image zoom

Status
Not open for further replies.

Bravogolf

Programmer
Nov 29, 2002
204
GB
Hi Guys,
Im trying to write some code that when the user moves the mouse over a shrunk down image, another image will then display the selected portion of the shrunk image in the proper size.
For example, I have the code below which more or less does the job, but for the life of me, I can't get the coordinates to match.

Code:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-15" />
<style type="text/css">
#largeImage {
	position:relative;
	width:400px;
	height:180px;
	border:1px solid #000000;
	background-image:url("bgimage.jpg");
	background-repeat:no-repeat;
	background-position:0px 0px;
}

#smallImage {
	position:relative;
	border:1px solid #000000;
	margin-top:5px;
	width:250px;
	height:155px;
}

#marker {
	position:absolute;
	top:0px;
	left:0px;
	border:1px solid #FF0000;
	width:55px;
	height:55px;
	cursor:pointer;
	background-image:url("tp.gif");
}

h1 {
	font-size:12pt;
	font-family:arial;	
	font-weight:bold;
	margin-bottom:5px;
}

hr {
	color:#000000;
	background-color:#000000;
	height:1px;
	width:100%;
	border-style:none;
}

#nfo {
	font-size:9px;
	font-family:verdana;
	margin:5px;
}
</style>
<script language="javascript" type="text/javascript">

var mDown = false;
var offsetX=0;
var offsetY=0;
function init() {
	document.onmousedown = captureMouseDown;
	document.onmousemove = captureMouseMove;
	document.onmouseup = captureMouseUp;
	document.getElementById("marker").onmousedown = captureOffset;
	document.getElementById("marker").style.top = "0px";
	document.getElementById("marker").style.left = "0px";
}

function captureOffset(e) {
	x = parseInt(document.getElementById("marker").style.left);
	y = parseInt(document.getElementById("marker").style.top);
	if(document.all) {
		offsetX=window.event.clientX - x;
		offsetY=window.event.clientY - y;
	} else {
		offsetX = e.pageX - x;
		offsetY = e.pageY - y;
	}
}

function captureMouseDown() {
	mDown=true;
}

function captureMouseUp() {
	mDown = false;
}

function captureMouseMove(e) {
	if(!mDown)return;
	x = document.all?window.event.clientX - offsetX:e.pageX - offsetX;
	y = document.all?window.event.clientY - offsetY:e.pageY - offsetY;
	
	if(x<=-1 || x>=250 || y<=-1 || y >= 250)return;
	document.getElementById("marker").style.left = x + "px";	
	document.getElementById("marker").style.top = y + "px";

	x2 = parseInt(document.getElementById("marker").style.left);
	y2 = parseInt(document.getElementById("marker").style.top);
	x2 = (x2 - (x2*2))*2; 
	y2 = (y2 - (y2*2))*2;
	document.getElementById("largeImage").style.backgroundPosition = x2 + "px " + y2 + "px";
}
</script>
</head>
<body onload="init();">
<h1>Image Navigator</h1>
<hr />
<div id="nfo">
Click and drag the red square on the bottom photo to navigate the zoomed image, much like you would in Photoshop.
This works by sliding the background-image of the #largeImage DIV element around using background-position to correspond with the coordinates of the
#marker DIV.
</div>
<div id="largeImage"></div>
<div id="smallImage">
	<img src="bgimage.jpg" height="500" width="500" />
	<div id="marker"></div>
</div>
</body>

I would very much appreciate if someone could show me where I am going wrong :)
 
This solves some problems:
Code:
function captureMouseMove(e) {
    if(!mDown)return;
    x = document.all?window.event.clientX - offsetX:e.pageX - offsetX;
    y = document.all?window.event.clientY - offsetY:e.pageY - offsetY;
    
    oMarker = document.getElementById("marker");
    oImg = document.getElementById("smallImage").childNodes[0];
    
    if (x < 0 || x > oImg.width - parseInt(oMarker.style.width)) return;
    if (y < 0 || y > oImg.height - parseInt(oMarker.style.height)) return;
    
    oMarker.style.left = x + "px";    
    oMarker.style.top = y + "px";    
    
    document.getElementById("largeImage").style.backgroundPosition = -x + "px " + -y + "px";
}
To fix all problems, you'll have to rewrite mouse capturing code. Wanna do it - tell me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top