<!DOCTYPE html>
<html>
<head>
<title>画橡皮筋矩形并可以调整矩形大小</title>
<script type="text/javascript">
var ctx;
var linew=1;
var color="rgb(255,0,0)";
var isClick;
var startx=100;//起始位置
var starty=100;//起始位置
var endx=200;//结束位置
var endy=200;//结束位置
var wid=100;//矩形宽度
var hei=100;//矩形高度
var tid;
function setstyle(w,c){
ctx.lineWidth=w;
ctx.strokeStyle=c;
}
/*获取当前鼠标位置坐标*/
function getpoint(ev){
if(ev.clientX||ev.clientX==0){
mx=ev.clientX;
my=ev.clientY;
}else if(ev.offsetX||ev.offsetX==0){
mx=ev.offsetX;
my=ev.offsetY;
}
}
/*画出矩形框*/
function drawall(){
ctx.clearRect(0,0,400,400);
setstyle(linew,color);
ctx.beginPath();
ctx.strokeRect(startx,starty,wid,hei);
ctx.stroke();
setstyle(2,"rbg(0,0,0");
ctx.strokeRect(0,0,400,400);
}
/*初始化*/
function init(){
isClick=false;
ctx=document.getElementById('canvas').getContext('2d');
drawall();
canvasl=document.getElementById('canvas');
canvasl.addEventListener("mousedown",findball,false);//
canvasl.addEventListener("mousemove",moveit,false);
canvasl.addEventListener("mouseup",finish,false);
addElementDiv("parent");
}
/*鼠标按下,获取矩形框起始点*/
function findball(ev){
isClick=true;
document.f.lg.value="mousedown";
getpoint(ev);
startx=mx;
starty=my;
removeElementDiv("newDiv");
}
/*鼠标移动,添加鼠标移动时矩形慢慢变化的效果*/
function moveit(ev){
if(isClick){
document.f.lg.value="mousemove";
getpoint(ev);
endx=mx;
endy=my;
wid=endx-startx;
hei=endy-starty;
drawall();
}
}
/*鼠标抬起事件,获取矩形框结束位置并画出来 */
function finish(ev){
if(isClick){
isClick=false;
document.f.lg.value="mouseup";
getpoint(ev);
endx=mx;
endy=my;
wid=endx-startx;
hei=endy-starty;
drawall();
addElementDiv("parent");
ctx.clearRect(2,2,396,396);
}
}
/*动态添加矩形框*/
function addElementDiv(obj){
var parent=document.getElementById(obj);
if(!parent){return;}
ctx.clearRect(2,2,396,396);
//添加 div
var div = document.createElement("div");
//设置 div 属性,如 id
div.setAttribute("id", "newDiv");
if(wid<0){
div.style.left=endx+"px";
if(hei<0){
div.style.top=endy+"px";
}else{
div.style.top=starty+"px";
}
}else{
div.style.left=startx+"px";
if(hei<0){
div.style.top=endy+"px";
}else{
div.style.top=starty+"px";
}
}
div.style.width=Math.abs(wid)+"px";
div.style.height=Math.abs(hei)+"px";
/*设置div可编辑*/
/*div.setAttribute("contenteditable",true);*/
parent.appendChild(div);
chanDivSize("newDiv");
}
function removeElementDiv(el){
var child = document.getElementById(el);
if(!child){return;}
child.parentNode.removeChild(child);
}
/*调整矩形大小*/
function chanDivSize(obj){
var oDiv = document.getElementById(obj);
oDiv.onmousedown = function(ev){
// 获取event对象,兼容性写法
var ev = ev || event;
// 鼠标按下时的位置
var mouseDownX = ev.clientX;
var mouseDownY = ev.clientY;
// 方块上下左右四个边的位置和方块的长宽
var T0 = this.offsetTop;
var B0 = this.offsetTop + this.offsetHeight;
var L0 = this.offsetLeft;
var R0 = this.offsetLeft + this.offsetWidth;
var W = this.offsetWidth;
var H = this.offsetHeight;
// 设置方块的识别范围
var areaT = T0 + 20;
var areaB = B0 - 10;
var areaL = L0 + 20;
var areaR = R0 - 10;
// 判断改变方块的大小的方向
// 左
var changeL = mouseDownX < areaL;
// 右
var changeR = mouseDownX > areaR;
// 上
var changeT = mouseDownY < areaT;
// 下
var changeB = mouseDownY > areaB;
// IE8 取消默认行为-设置全局捕获
if(oDiv.setCapture){
oDiv.setCapture();
}
document.onmousemove = function(ev){
var ev = ev || event;
// 鼠标移动时的鼠标位置
var mouseMoveX = ev.clientX;
var mouseMoveY = ev.clientY;
//根据改变方块大小的方向不同进行大小的改变
// 左
if(changeL){
oDiv.style.width = (mouseDownX - mouseMoveX) + W + 'px';
oDiv.style.left = L0 - (mouseDownX - mouseMoveX) + 'px';
}
// 右
if(changeR){
oDiv.style.width = (mouseMoveX - mouseDownX) + W + 'px';
}
// 上
if(changeT){
oDiv.style.height = (mouseDownY - mouseMoveY) + H + 'px';
oDiv.style.top = T0 - (mouseDownY - mouseMoveY) + 'px';
}
// 下
if(changeB){
oDiv.style.height = (mouseMoveY - mouseDownY) + H +'px';
}
// 限定范围
if(parseInt(oDiv.style.width) < 50){
oDiv.style.width = 50 + 'px';
}
if(parseInt(oDiv.style.height) < 50){
oDiv.style.height = 50 + 'px';
}
}
document.onmouseup = function(){
document.onmousemove = null;
// 释放全局捕获
if(oDiv.releaseCapture){
oDiv.releaseCapture();
}
}
return false;
}
}
</script>
<style type="text/css">
#parent{
position: relative;
}
#newDiv{
overflow: hidden;
border: 1px solid #000;
position: absolute;
}
</style>
</head>
<body onLoad="init();">
<div id="parent">
<canvas id="canvas" width="400" height="400"></canvas>
</div>
<form id="f" name="f">
LOG:<input type="text" name="lg" id="lg" value="false"/>
</form>
</body>
</html>