js实现 鼠标绘制两个不相交的矩形

一、效果图


二、功能说明

1.检测、屏蔽、删除三个按钮

页面上检测或者屏蔽框只能绘制一个

点击检测才可以绘制检测区域,点击屏蔽即绘制屏蔽区域,点击删除即删除当前选中的框;


三、代码

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
<meta http-equiv="X-UA-Compatible" content="IE=8,chrome=1" />
<title>js绘制矩形</title>
<style>
	*{margin:0; padding:0;}
	html,body{width: 100%; height: 100%;}
	button{padding: 8px 30px; background: #fff; border: 1px solid #ddd;}
	#checkArea{position:absolute; background:#ccc;}
	#hideArea{position:absolute; background:#ccc;}
	.sel{ margin:30px auto; width:960px; overflow:hidden;}
	.red{background:red; color: #fff;}
	.blue{background:blue; color: #fff;}
	.xz{ width:340px; float:right;}
	.canvas{width: 500px; height: 500px; border: 2px solid #f00; margin-left: 100px; margin-top: 100px;}
</style>
</head>

<body>
	<div class="sel">
    	    <button id="check">检测</button>
    	    <button id="hide">屏蔽</button>
    	    <button id="del">删除</button>
        </div>
	<div id="canvas" class="canvas"></div>
	
<script src="js/jquery-1.11.0.min.js"></script>   
<script>
var redLine = {
	x:0,
	y:0,
	width:0,
	height:0
};
var greenLine = {
	x:0,
	y:0,
	width:0,
	height:0
}
	
function $Id(id){
	return document.getElementById(id);	
}

window.οnlοad=function(){
	var oCanvas=$Id('canvas');
	var color='';//点击按钮的时候  设置
	var xz = "";
	var checkSqu = 0;		//0-未画 1-已经画完
	var hideSqu = 0;		//0-未画 1-已经画完
	
	//选择了区域类型才可以画框
	function testDraw(){
		if(xz){
			if(xz == "hide"){
				color = "#f00";	//屏蔽区域用红色
				if(hideSqu == 0){
					draw();
				}
			}else{
				color = "#0f0";	//检测区域用绿色
				if(checkSqu == 0){
					draw();
				}
			}
		}
	}
	
	function draw(){
		oCanvas.οnmοusedοwn=function(ev){
			console.log("啦啦啦:"+oCanvas.offsetLeft);	
			if(xz == "hide"){
				if(!(xz=="hide"&&hideSqu==0)){
					return;
				}
			}else if(xz == "check"){
				if(!(xz=="check"&&checkSqu==0)){
					return;
				}
			}
			if(oCanvas.setCapture){
				oCanvas.setCapture();
			}
			var oEv=ev||window.event;
			var disX=oEv.clientX;
			var disY=oEv.clientY;
			var oR=document.createElement('div');
			oR.id=xz+"Area";
			oR.style.top=disY+'px';
			oR.style.left=disX+'px';
			oR.style.border='1px solid '+color;
			oR.style.backgroundColor='#fff';
			document.body.appendChild(oR);
			
			document.οnmοusemοve=function(ev){
				var oEv=ev||window.event;
				var x=oEv.clientX;
				var y=oEv.clientY;
				
				if(x<oCanvas.offsetLeft){
					x=oCanvas.offsetLeft;	
				}else if(x>oCanvas.offsetLeft+oCanvas.offsetWidth){
					x=oCanvas.offsetLeft+oCanvas.offsetWidth
				}
				
				if(y<oCanvas.offsetTop){
					y=oCanvas.offsetTop;	
				}else if(y>oCanvas.offsetTop+oCanvas.offsetHeight){
					y=oCanvas.offsetTop+oCanvas.offsetHeight
				}
				
				if(xz == "check"){
					console.log("01236");
					//判断是否有检测区域
					if(redLine["x"]){
						if(disY < redLine["y"]){
							if((disX < redLine["x"] && x>redLine["x"])
								|| (disX >redLine["x"]+redLine["width"] && x<redLine["x"]+redLine["width"])
								|| (disX > redLine["x"] && disX <= redLine["x"]+redLine["width"])){
								if(y > redLine["y"]){
									y = redLine["y"];
								}
							}
						}
						if(disY >= redLine["y"] && disY <= (redLine["y"]+redLine["height"])){
							if(disX < redLine["x"] && x > redLine["x"]){
								x = redLine["x"];
							}
							if(disX > redLine["x"] && x<redLine["x"]+redLine["width"]){
								x = redLine["x"]+redLine["width"];
							}
						}
						if(disY > redLine["y"] + redLine["height"]){
							if((disX > redLine["x"] && disX <= redLine["x"]+redLine["width"])
								|| (disX < redLine["x"] && x > redLine["x"])
								|| (disX >redLine["x"]+redLine["width"] && x<redLine["x"]+redLine["width"])){
								if(y < redLine["y"] + redLine["height"]){
									console.log("123");
									y = redLine["y"] + redLine["height"];
								}
							}
						}
					}
				}else{
					//画屏蔽区域,判断检测区域有没有值
					if(greenLine["x"]){
						if(disY < greenLine["y"]){
							if((disX < greenLine["x"] && x>greenLine["x"])
								|| (disX >greenLine["x"]+greenLine["width"] && x<greenLine["x"]+greenLine["width"])
								|| (disX > greenLine["x"] && disX <= greenLine["x"]+greenLine["width"])){
								if(y > greenLine["y"]){
									y = greenLine["y"];
								}
							}
						}
						if(disY >= greenLine["y"] && disY <= (greenLine["y"]+greenLine["height"])){
							if(disX < greenLine["x"] && x > greenLine["x"]){
								x = greenLine["x"];
							}
							if(disX > greenLine["x"] && x<greenLine["x"]+greenLine["width"]){
								x = greenLine["x"]+greenLine["width"];
							}
						}
						if(disY > greenLine["y"] + greenLine["height"]){
							if((disX > greenLine["x"] && disX <= greenLine["x"]+greenLine["width"])
								|| (disX < greenLine["x"] && x > greenLine["x"])
								|| (disX >greenLine["x"]+greenLine["width"] && x<greenLine["x"]+greenLine["width"])){
								if(y < greenLine["y"] + greenLine["height"]){
									y = greenLine["y"] + greenLine["height"];
								}
							}
						}
					}
				}
				
				oR.style.width=Math.abs(x-disX)+'px';
				oR.style.top=Math.min(disY,y)+'px';
				oR.style.left=Math.min(disX,x)+'px';
				oR.style.height=Math.abs(y-disY)+'px';
				
				if(xz == "hide"){
					hideSqu = 1;	//屏蔽区域已经画过
				}else{
					checkSqu = 1;	//检测区域已经画过
				}
				oCanvas.οnmοusedοwn=null;
				
				if(xz == "hide"){
					redLine["x"] = Math.min(disX,x);
					redLine["y"] = Math.min(disY,y);
					redLine["width"] = Math.abs(x-disX);
					redLine["height"] = Math.abs(y-disY);
				}else if(xz == "check"){
					greenLine["x"] = Math.min(disX,x);
					greenLine["y"] = Math.min(disY,y);
					greenLine["width"] = Math.abs(x-disX);
					greenLine["height"] = Math.abs(y-disY);
				}
			}
			document.οnmοuseup=function(ev){
				var oEv=ev||window.event;

				document.οnmοusemοve=null;
				document.οnmοuseοut=null;
				
				if(oCanvas.releaseCapture){
					oCanvas.releaseCapture();
				}	
			}
			return false;
		}
	}
	
	$("#check").click(function(){
		if(! $(this).hasClass("blue")){
			if(xz){
				$("#"+xz).removeClass();
			}
			$(this).addClass("blue");
			xz = "check";
			testDraw();
		}
	})
	
	$("#hide").click(function(){
		if(! $(this).hasClass("red")){
			if(xz){
				$("#"+xz).removeClass();
			}
			$(this).addClass("red");
			xz = "hide";
			testDraw();
		}
	})
	
	$("#del").click(function(){
		if(xz == "check"){
			//删除check的div
			$("#checkArea").remove();
			greenLine["x"] = 0;
			greenLine["y"] = 0;
			greenLine["width"] = 0;
			greenLine["height"] = 0;
			checkSqu = 0;
			testDraw();
		}else if(xz == "hide"){
			//删除hide的div
			$("#"+xz+"Area").remove();
			hideSqu = 0;
			testDraw();
			redLine["x"] = 0;
			redLine["y"] = 0;
			redLine["width"] = 0;
			redLine["height"] = 0;
		}else{
			return;
		}
	})
}	

</script>
</body>
</html>


~~~新手~代码比较冗余~是可以实现效果的~欢迎大家留言~建议~批评~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值