我正在做一個項目,用於在圖像上繪製矩形框並查找矩形的座標。我使用JavaScript來編寫這個程序。在圖像上繪製矩形
我的代碼是:
<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>
我如何在形式(x1,y1)(x2,y2)
顯示矩形的座標。這是左上角和右下角座標,最後是左右角。
我試圖顯示不同的變量,但它是不正確的。請幫助我的任何人顯示矩形的座標。
考慮使用一些JavaScript框架,如jQuery,MooTools的嚮往之......它讓生活變得更加簡單。 – zeroos 2012-02-21 19:05:21
這與您提出的最後兩個問題有什麼不同? http://stackoverflow.com/questions/9397104/javascript-for-draw-rectangle-over-a-image和http://stackoverflow.com/questions/9383337/drag-and-draw-rectangles-on-an- image-in-javascript – j08691 2012-02-22 16:28:48