用于在图像上绘制矩形的javascript [英] javascript for draw rectangle over a image

查看:72
本文介绍了用于在图像上绘制矩形的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用java脚本在php上做一个项目。我尝试使用javascript在图像上绘制一个矩形。矩形可以绘制任何大小的图像的任何位置与图像大小相比,并显示坐标绘制矩形。请任何人帮助我...我尝试了不同的方式.....

I was doing a project on php with java script.I was try to draw a rectangle over a image using javascript.The rectangle can draw any where of the image with any size as compare with image size and also display the co ordinate of drawing rectangle.Please any one help me...I was tried different ways.....

 <STYLE>
  #rubberBand {
  position: absolute;
   visibility: hidden;
   width: 0px; height: 0px;
  border: 2px solid red;
 }
 </STYLE>

 </HEAD>
 <BODY>
 <img name="myImage" id="myImage" src="a.jpg">


 <DIV ID="rubberBand"></DIV>

 <SCRIPT>

var IMG;

  function startRubber (evt) {
   if (document.all) {

       var r = document.all.rubberBand;
       r.style.width = 0;
       r.style.height = 0;
       r.style.pixelLeft = event.x;
      r.style.pixelTop = event.y;
      r.style.visibility = 'visible';
      IMG.ondragstart = cancelDragDrop; // otherwise IE will try to drag the image
   }
     else if (document.getElementById) {
   // firefox
   evt.preventDefault();
    var r = document.getElementById('rubberBand');
   r.style.width = 0;
  r.style.height = 0;
   r.style.left = evt.clientX + 'px';
    r.style.top = evt.clientY + 'px';
   r.style.visibility = 'visible';
   r.onmouseup = stopRubber;
    }
    IMG.onmousemove = moveRubber;
   }
   function moveRubber (evt) {
   if (document.all) { // IE
   var r = document.all.rubberBand;
    r.style.width = event.x - r.style.pixelLeft;
  r.style.height = event.y - r.style.pixelTop;
   }
   else if (document.getElementById) { // firefox
   var r = document.getElementById('rubberBand');
   r.style.width = evt.clientX - parseInt(r.style.left);
    r.style.height = evt.clientY - parseInt(r.style.top);
   }
   return false; // otherwise IE won't fire mouseup :/
   }
  function stopRubber (evt) {
  IMG.onmousemove = null;
   }

  function cancelDragDrop()
   {
  window.event.returnValue = false;
   }

   IMG = document.getElementById('myImage');
   IMG.onmousedown = startRubber;
   IMG.onmouseup = stopRubber;

 </SCRIPT>


推荐答案

你需要一个包装器才能绝对定位元素内。尺寸会有所不同,具体取决于您的照片和包装盒的位置。

You need a wrapper so you can absolutely-position elements inside. The dimensions will vary, depending on your photo and where you want the box.

HTML:

<div class="wrapper">
     <img src="...." />
     <div class="box"></div>
</div>

CSS:

.wrapper {
    position:relative;
}

.box {
    position:absolute;
    top:10px;
    left:10px;
    width:50px;
    height:50px;
    border:2px solid #ffffff;
    background-color:transparent
}

这篇关于用于在图像上绘制矩形的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆