Connecting Tech Pros Worldwide Forums | Help | Site Map

Solution to drag and draw rectangles on an image in javascript

Newbie
 
Join Date: Apr 2007
Posts: 1
#1   Apr 26 '07
After hours of searching and coding, i finally have a code to drag and draw rectangles on images. I got most of the code pasted below from a post almost 2 years old and did some patch work to it. Now it works with both firefox and IE7. But with IE7, it has a minor defect...but still works. I will try to post the patch as soon as i have an answer. Here you go guys
Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <META http-equiv=imagetoolbar content=no>
  4. <TITLE>
  5.  
  6. </TITLE>
  7. <STYLE>
  8. #rubberBand {
  9. position: absolute;
  10. visibility: hidden;
  11. width: 0px; height: 0px;
  12. border: 2px solid red;
  13. }
  14. </STYLE>
  15.  
  16. </HEAD>
  17. <BODY>
  18. <img name="myImage" id="myImage" src="VK.jpg" height=400
  19. width=400>
  20.  
  21.  
  22. <DIV ID="rubberBand"></DIV>
  23.  
  24. <SCRIPT>
  25.  
  26. var IMG;
  27.  
  28. function startRubber (evt) {
  29. if (document.all) {
  30. // IE
  31. var r = document.all.rubberBand;
  32. r.style.width = 0;
  33. r.style.height = 0;
  34. r.style.pixelLeft = event.x;
  35. r.style.pixelTop = event.y;
  36. r.style.visibility = 'visible';
  37. IMG.ondragstart = cancelDragDrop; // otherwise IE will try to drag the image
  38. }
  39. else if (document.getElementById) {
  40. // firefox
  41. evt.preventDefault();
  42. var r = document.getElementById('rubberBand');
  43. r.style.width = 0;
  44. r.style.height = 0;
  45. r.style.left = evt.clientX + 'px';
  46. r.style.top = evt.clientY + 'px';
  47. r.style.visibility = 'visible';
  48. r.onmouseup = stopRubber;
  49. }
  50. IMG.onmousemove = moveRubber;
  51. }
  52. function moveRubber (evt) {
  53. if (document.all) { // IE
  54. var r = document.all.rubberBand;
  55. r.style.width = event.x - r.style.pixelLeft;
  56. r.style.height = event.y - r.style.pixelTop;
  57. }
  58. else if (document.getElementById) { // firefox
  59. var r = document.getElementById('rubberBand');
  60. r.style.width = evt.clientX - parseInt(r.style.left);
  61. r.style.height = evt.clientY - parseInt(r.style.top);
  62. }
  63. return false; // otherwise IE won't fire mouseup :/
  64. }
  65. function stopRubber (evt) {
  66. IMG.onmousemove = null;
  67. }
  68.  
  69. function cancelDragDrop()
  70. {
  71. window.event.returnValue = false;
  72. }
  73.  
  74. IMG = document.getElementById('myImage');
  75. IMG.onmousedown = startRubber;
  76. IMG.onmouseup = stopRubber;
  77.  
  78. </SCRIPT>
  79. </BODY>
  80. </HTML>
  81.  
-- edit by iam_clint: thanks for posting possibly put a link to an example of it in action.



Reply