Solved

"Draw" Rectangle using JavaScript

Posted on 2009-04-14
5
497 Views
Last Modified: 2012-05-06
Hi to all,

I am following the following post:
http://www.emanueleferonato.com/2006/09/02/click-image-and-get-coordinates-with-javascript/
Where a user can basically point to an area on an image and then get the X and Y coordinates. My end goal is to give the user the ability to drag and drop an area on the image and then get the image map coordinates.

The following link basically demonstrates my end goal, but there is no drag and drop functionality. You basically have to click twice on the image and then a "White" block is drawn. I want to give the user a See through block, but with the border visible.
http://www.shawngo.com/mappr/clicktest.html

Can someone please help me in getting this done.

Code Below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="JavaScript">
function point_it(event){
pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("pointer_div").offsetLeft;
pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("pointer_div").offsetTop;
document.getElementById("cross").style.left = (pos_x-1) ;
document.getElementById("cross").style.top = (pos_y-15) ;
document.getElementById("cross").style.visibility = "visible" ;
document.pointform.form_x.value = pos_x;
document.pointform.form_y.value = pos_y;
}
</script>
</head>
<body>
<form name="pointform" method="post">
<div id="pointer_div" onclick="point_it(event)" style = "background-image:url('sun.jpg');width:500px;height:333px;">
<img src="point.gif" id="cross" style="position:relative;visibility:hidden;z-index:2;"></div>
You pointed on x = <input type="text" name="form_x" size="4" /> - y = <input type="text" name="form_y" size="4" />
</form> 
</body>
</html>

Open in new window

0
Comment
Question by:ALawrence007
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 24138443
0
 

Author Comment

by:ALawrence007
ID: 24140119
mplungjan:

This is EXACTLY what I was looking for and really an amazing tool!

The following is what I am looking at and exactly what needs to happen in my application:
http://marqueetool.net/examples/send-cropped-image-coordinates-to-the-server/

The only thing I am having issues with is the getCoords(). The example and documentation is not very clear on that. I followed the example, but I am not getting the desired result: The coordinates are not being populated in my input boxes.

I don't understand the code below:

Do I have to create another page?

Thanks

And on the server: <? header("Content-Type: image/jpeg"); $image = dirname(__FILE__) . "/images/examples/example5.jpg"; if ($w && $h) { $image_rs = @imagecreatefromjpeg($image); $new_rs = @imagecreatetruecolor($w, $h); @imagecopy ($new_rs, $image_rs, 0, 0, $x, $y, $w, $h); @imagejpeg($new_rs); } readfile($image); ?> 

Open in new window

0
 

Author Comment

by:ALawrence007
ID: 24140343
My Bad..... I found my issue......

Thanks mplungjan !!!!
0
 

Author Closing Comment

by:ALawrence007
ID: 31569909
Awesome tool and help!!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24140570
you are welcome :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question