Tony Gahlinger wrote:
Tony Gahlinger wrote: I'm using my browser (Mozilla/5.0 Linux i686 Gecko/20031007 Firebird/0.7)
to do some client-side image processing. I want to capture the
sequence of coordinates a user clicks on in xxx.jpg in the following html
This uses some mouse tracking code I got from a website, it is used here
as an example, I would recommend looking for a better mouse-tracking
approach, maybe someone here will post a method that doesn't rely upon
browser detection. This works in my IE6, Netscape 7.1, Firefox 0.8 but I
have no idea what other browsers will do, esp. Opera.
What I did here was: track the mouse position, when the image is clicked
send the current mouse coordinates to a function that subtracts the
image position on the page (left, top) to get the image (x, y) position
of the mouse, store those numbers in an array.
I have no idea how to save the array contents to a file, sorry.
<script type="text/javascript">
//TRACK MOUSE BEGIN
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0
// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft
tempY = event.clientY + document.body.scrollTop
} else { // grab the x-y pos.s if browser is NS
tempX = e.pageX
tempY = e.pageY
}
// catch possible negative values in NS4
if (tempX < 0){tempX = 0}
if (tempY < 0){tempY = 0}
// show the position values in the form named Show
// in the text fields named MouseX and MouseY
//document.Show.MouseX.value = tempX
//document.Show.MouseY.value = tempY
return true
}
//TRACK MOUSE END
//array to store mouse coord's
var coords=new Array();
//store mouse coord's in array after normalizing to img top, left
function storepoint(x,y, target){
//alert(x+', '+y+'\nTop '+target.offsetTop+' Left '+target.offsetLeft);
alert((x-target.offsetLeft)+', '+(y-target.offsetTop));
//alert(target.offsetTop);
//alert(document.getElementById('img1').left);
coords[coords.length]=[x-target.offsetLeft,y-target.offsetTop];
}
function showarr(){
var s1='';
for (var i=0; i<coords.length; i++)
s1+='\n'+coords[i][0]+', '+coords[i][1];
alert(s1);
}
<img name="img1" id="img1" src="iamges/logo.gif" width="543"
height="113" onmousedown="storepoint(tempX, tempY, this)">
</p>
<p>
<input type="button" value="Show Contents of Array (click coordinates)"
onclick="showarr()">
Good Luck,
Mike