JS简易画图

JS简易画图

用Jscript做的一个建议画图网页,想要的直接复制就好,没有额外的文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style>
    #draw1{
	    border:0px solid red;
	    background:#ccc;
		border-radius: 10px;
		box-shadow: 0 0 10px 3px grey;
    }
	#div1{
		border: 0px solid red;
		margin-left: 810px;
		margin-top: -602px;
		width: 400px;
		height: 600px;
		border-radius: 5px;
		box-shadow: 0 0 10px 3px grey;
	}
	.bt{
		margin-left: 10px;
		margin-top: 15px;
		background-color:rgb(78,110,242);
		color: white;
		font-size: 23px;
		width: 70px;
		height: 40px;
		border: 0px solid white;
		border-radius: 5px;
		overflow: hidden;
		outline: none;
		box-shadow: 0 0 2px 2px grey;
	}
	#t1{
		width: 250px;
		height: 30px;
		margin-top: 20px;
		outline: none;
		border: 2px solid rgb(78,110,242);
		border-radius: 3px;
		font-size: 25px;
		font-family: 微软雅黑;
	}
	.xy{
		width: 60px;
		height: 30px;
		margin-top: 20px;
		outline: none;
		border: 2px solid rgb(78,110,242);
		border-radius: 3px;
		font-size: 25px;
		font-family: 微软雅黑;
	}
	.tcfs{
		margin-top: 30px;
		outline: none;
	}
	.bffs{
		margin-top: 20px;
		outline: none;
	}
	#jdt{
		border: 0px solid green;
		border-radius: 5px;
		background:#ccc;
		width: 250px;
		height: 20px;
  	    cursor: pointer;
		overflow: hidden;
		margin-top: -22px;
		margin-left: 70px;
	}
	#jdtn{
		width: 250px;
   	 	float: left;
		height: 20px;
		border-radius: 3px;
    	cursor: pointer;
		margin-left: -250px;
   		background-color:cadetblue; 
	}
	#color1{
		margin-top: 20px;
		width: 200px;
	}
</style>

<body>
<canvas id="draw1" width="800" height="600" onClick="showx()"></canvas>
<div id="div1" align="center">
	<form id="form1" name="form1" ><br/>
		<input type="button" id="b1" class="bt" value="圆形" onclick="yuan()" onMouseDown="bd(this)" onMouseUp="bu(this)" onMouseMove="bm(this)" onMouseOut="bo(this)">
		<input type="button" id="b2" class="bt" value="线段" onclick="xian()" onMouseDown="bd(this)" onMouseUp="bu(this)" onMouseMove="bm(this)" onMouseOut="bo(this)">
		<input type="button" id="b3" class="bt" value="矩形" onclick="rect()" onMouseDown="bd(this)" onMouseUp="bu(this)" onMouseMove="bm(this)" onMouseOut="bo(this)">
		<input type="button" id="bb" class="bt" value="鼠标" onclick="tt()" onMouseDown="bd(this)" onMouseUp="bu(this)" onMouseMove="bm(this)" onMouseOut="bo(this)">
		<br/><br/><hr style="width: 90%;">
<!---------------------------------------------------------------------------------------------------->
		<input type="radio" class="bffs" name="bf" id="tt1" value="bottom" checked>bottom
 	   	 &nbsp;&nbsp;&nbsp;
  	   	<input type="radio" class="bffs" name="bf" id="tt2" value="top">top
		 &nbsp;&nbsp;&nbsp;
		<input type="radio" class="bffs" name="bf" id="tt3" value="hanging">hanging
 	   	 &nbsp;&nbsp;&nbsp;
  	   	<input type="radio" class="bffs" name="bf" id="tt3" value="middle">middle<br/>
		<b id="f1" style="font-size: 25px;font-family: 微软雅黑;">文字:</b>
		<input type="text" id="t1"><br/>
		<b id="fx" style="font-size: 25px;font-family: 微软雅黑;">横轴X:</b>
		<input type="text" id="t2" class="xy">
		<b id="fy" style="font-size: 25px;font-family: 微软雅黑;">纵轴Y:</b>
		<input type="text" id="t3" class="xy"><br>
		<b id="fy" style="font-size: 25px;font-family: 微软雅黑;">文字大小:</b>
		<input type="text" id="t4" class="xy">
		<input type="button" id="b4" class="bt" value="显示文字"
         onclick="text()" onMouseDown="bd(this)" onMouseUp="bu(this)" onMouseMove="bm(this)" onMouseOut="bo(this)" style="width: 120px;">
		<br/><br/>
        <hr style="width: 90%;">
<!---------------------------------------------------------------------------------------------------->
		<b id="f2" style="font-size: 25px;font-family: 微软雅黑; margin-left: -118px;">填充:</b>
		<input type="radio" class="tcfs" name="fs" id="tc1" value="fill">填充&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  	   	<input type="radio" class="tcfs" name="fs" id="tc2" value="stroke" checked>描边<br/>
		<b id="f3" style="font-size: 25px;font-family: 微软雅黑;margin-left: -50px;">颜色:</b>
		<input type="color" id="color1" onClick=""><br/>
		<div id="f3" style="margin-left: -262px;margin-top: 20px;"><font style="font-size: 25px;font-family: 微软雅黑;font-weight: 700">粗细:</font>
        </div>
		<div id="jdt" onMouseDown="timeleap(this)" onMouseMove="jdtmove()" onMouseUp="jdtup()">
            <div id="jdtn"></div>
        </div>
		<br/>
		<input type="button" id="b3" class="bt" value="清空本地记录" onclick="clearLocation()" onMouseDown="bd(this)" onMouseUp="bu(this)" onMouseMove="bm(this)" onMouseOut="bo(this)" style="width: 200px;">
	</form>
	
</div>
</body>
<script>
    var context;
	var sX,sY;
	var mouseTag;
	var type;
	var btn=0;
	var cx=2;
    var imgcounter;
	var tmpPointArray=new Array();
    var t=localStorage.getItem("ucounter");

	window.onload=function(){
		var tCanvas=document.getElementById("draw1");
		tCanvas.addEventListener("mousedown",mouseDown);
		tCanvas.addEventListener("mousemove",mouseMove);
		tCanvas.addEventListener("mouseup",mouseUp);
		context=tCanvas.getContext("2d");
		mouseTag=0;
		initLocal();
	}
	function initLocal(){
		var t=localStorage.getItem("ucounter");
		if(t==null){imgcounter=0;}
		else{imgcounter=localStorage.getItem("ucounter")*1;}
		var d=t*1;
		for(i=1;i<=d;i++)
        {
			if(localStorage.getItem("type"+i)=="yuan"){
				context.beginPath();
				var cx=localStorage.getItem("cx"+i);
				var cy=localStorage.getItem("cy"+i);
                var cr=localStorage.getItem("cr"+i);
                var cs=localStorage.getItem("cs"+i);
                var cc=localStorage.getItem("cc"+i);
                var ccx=localStorage.getItem("ccx"+i);
				if(cs=="stroke"){
                    context.lineWidth=ccx;
					context.strokeStyle=cc;
					context.arc(cx,cy,cr,0,2*Math.PI,true);
					context.stroke();
				}
				else{
					context.fillStyle=cc;
					context.arc(cx,cy,cr,0,2*Math.PI,true);
					context.fill();
				}
				context.closePath();
			}
			if(localStorage.getItem("type"+i)=="line"){
				context.beginPath();
                context.lineCap="round";
				context.lineWidth=localStorage.getItem("lcx"+i);
				context.strokeStyle=localStorage.getItem("lc"+i);
				context.moveTo(localStorage.getItem("lx1"+i) , localStorage.getItem("ly1"+i));
				context.lineTo(localStorage.getItem("lx2"+i) , localStorage.getItem("ly2"+i));
				context.closePath();
                context.stroke();
			}
			if(localStorage.getItem("type"+i)=="rect"){
				context.lineWidth=localStorage.getItem("ccx"+i);
                rcx=localStorage.getItem("rcx"+i);
				rs=localStorage.getItem("rs"+i);
				rx=localStorage.getItem("rx"+i);
				ry=localStorage.getItem("ry"+i);
				rw=localStorage.getItem("rw"+i);
				rh=localStorage.getItem("rh"+i);
                rc=localStorage.getItem("rc"+i);
                context.lineWidth=rcx;
				if(rs=="stroke"){
					context.strokeStyle=rc;
					context.strokeRect(rx,ry,rw,rh);
					context.stroke();
				}
				else{
					context.fillStyle=rc;
					context.fillRect(rx,ry,rw,rh);
					context.fill();
				}
			}
			if(localStorage.getItem("type"+i)=="text"){
				tbf=localStorage.getItem("tbf"+i);
                ccx=localStorage.getItem("ccx"+i);
                msg=localStorage.getItem("cmsg"+i);
                s=localStorage.getItem("cs"+i);
				x=localStorage.getItem("cx"+i);
				y=localStorage.getItem("cy"+i);
                e=localStorage.getItem("cct"+i);
                c=localStorage.getItem("cc"+i);
				context.font=e+"em bold 微软雅黑";
                context.textBaseline=tbf;
				if(s=="stroke"){
					context.strokeStyle=c;
                    context.lineWidth=ccx;
					context.strokeText(msg,x,y);
				}
				else{
					context.fillStyle=c;
                    context.lineWidth=ccx;
					context.fillText(msg,x,y);
				}
			}	
		}
	}
    function mouseMove(){//move
		initLocal();
		if(mouseTag==1&&type==1){//圆
		    context.fillStyle="#ccc";
		    context.fillRect(0,0,800,600);
			initLocal();
		    var currentR=Math.sqrt(Math.pow(event.clientX-sX,2)+Math.pow(event.clientY-sY,2));
		    context.beginPath();
			context.strokeStyle=document.getElementById("color1").value;
			context.lineWidth=cx;
		    context.arc(sX-8,sY-8,currentR,0,2*Math.PI,true);
			context.stroke();	
		    context.closePath();
			context.beginPath();
			context.moveTo(sX-8,sY-8);
			context.lineTo(event.clientX-8,event.clientY-8);
			context.stroke();
			context.closePath();
			context.font="20px 微软雅黑";///半径//
			context.fillStyle="red";
			var msg="圆半径:"+parseInt(currentR);
			var mt=context.measureText(msg);
			context.fillText(msg,event.clientX-8,event.clientY-8);
			context.font="20px 微软雅黑";///圆心//
        	context.fillStyle="red";
			var msg="圆心"+sX+","+sY;
			var mt=context.measureText(msg);
			context.fillText(msg,sX,sY);
		}
		if(mouseTag==1&&type==2){//线
		    context.fillStyle="#ccc";
		    context.fillRect(0,0,800,600);
			initLocal();
			//jilu();
			//initcanvas();
		    context.beginPath();
			context.lineWidth=cx;
			context.strokeStyle=document.getElementById("color1").value;
		    context.moveTo(sX-8,sY-8);
			context.lineTo(event.clientX-8,event.clientY-8);
		    context.stroke();
		    context.closePath();
			///
			var currentR=Math.sqrt(Math.pow(event.clientX-sX,2)+Math.pow(event.clientY-sY,2));
		    context.beginPath();
			context.font="20px 微软雅黑";
			context.fillStyle="red";
			var msg="线长度:"+parseInt(currentR);
			var mt=context.measureText(msg);
			context.fillText(msg,event.clientX-8,event.clientY-8);
		}
		if(mouseTag==1&&type==3){//矩形
		    context.fillStyle="#ccc";
		    context.fillRect(0,0,800,600);
			initLocal();
			var width=event.clientX-sX;
			var height=event.clientY-sY;
			context.strokeStyle=document.getElementById("color1").value;
			context.lineWidth=cx;
			context.strokeRect(sX,sY,width,height);
			context.beginPath();
			context.moveTo(sX,sY);
			context.lineTo(event.clientX,event.clientY);
			context.stroke();
			context.closePath();
			
			var currentR=Math.sqrt(Math.pow(event.clientX-sX,2)+Math.pow(event.clientY-sY,2));
			context.font="20px 微软雅黑";
			context.fillStyle="red";
			var msg="矩形对角线:"+parseInt(currentR);
			var mt=context.measureText(msg);
			context.fillText(msg,event.clientX,event.clientY);
		}
	}
	function mouseDown(){//down
		//jilu();
		initLocal();
		sX=event.clientX;
		sY=event.clientY;
		mouseTag=1;
	}
	function mouseUp(){//up
		if(mouseTag==1&&type==1){//圆
			var currentR=Math.sqrt(Math.pow(event.clientX-sX,2)+Math.pow(event.clientY-sY,2));
            var c=document.getElementById("color1").value;
			var s=document.form1.fs.value;
            context.fillStyle="#ccc";
			context.fillRect(0,0,800,600);
            context.beginPath();
			context.fillStyle=c;
			context.strokeStyle=c;
			context.arc(sX-8,sY-8,currentR,0,2*Math.PI,true);
            context.closePath();
			if(s=="stroke"){
                context.lineWidth=cx;
				context.stroke();
			}
		    else{
				context.fill();
			}
            imgcounter++;
			localStorage.setItem("ucounter",imgcounter);
			localStorage.setItem("type"+imgcounter,"yuan");
			localStorage.setItem("cx"+imgcounter,sX-8);
			localStorage.setItem("cy"+imgcounter,sY-8);
			localStorage.setItem("cr"+imgcounter,currentR);
			localStorage.setItem("cc"+imgcounter,c);
			localStorage.setItem("cs"+imgcounter,s);
			localStorage.setItem("ccx"+imgcounter,cx);
			mouseTag=0;
		}
		if(mouseTag==1&&type==2){//线
			context.fillStyle="#ccc";
			context.fillRect(0,0,800,600);
            var c=document.getElementById("color1").value;
			context.beginPath();
            context.lineWidth=cx;
			context.lineCap="round";
			context.strokeStyle=c;
			context.moveTo(sX-8,sY-8);
			context.lineTo(event.clientX-8,event.clientY-8);
			context.closePath();
            context.stroke();
			imgcounter++;
			localStorage.setItem("ucounter",imgcounter);
			localStorage.setItem("type"+imgcounter,"line");
			localStorage.setItem("lx1"+imgcounter,sX-8);
			localStorage.setItem("ly1"+imgcounter,sY-8);
			localStorage.setItem("lx2"+imgcounter,event.clientX-8);
			localStorage.setItem("ly2"+imgcounter,event.clientY-8);
			localStorage.setItem("lc"+imgcounter,c);
			localStorage.setItem("lcx"+imgcounter,cx);
			mouseTag=0;
		}
		if(mouseTag==1&&type==3){//矩形
		    var width=event.clientX-sX;
			var height=event.clientY-sY;
			var c=document.getElementById("color1").value;
			var s=document.form1.fs.value;
            context.fillStyle="#ccc";
			context.fillRect(0,0,800,600);
			context.lineWidth=cx;
			if(s=="stroke"){
                context.strokeStyle=c;
				context.strokeRect(sX,sY,width,height);
			}
		    else{
                context.fillStyle=c;
				context.fillRect(sX,sY,width,height);
			}
			imgcounter++;
			localStorage.setItem("ucounter",imgcounter);
			localStorage.setItem("type"+imgcounter,"rect");
			localStorage.setItem("rx"+imgcounter,sX);
			localStorage.setItem("ry"+imgcounter,sY);
			localStorage.setItem("rw"+imgcounter,width);
			localStorage.setItem("rh"+imgcounter,height);
			localStorage.setItem("rc"+imgcounter,c);
			localStorage.setItem("rs"+imgcounter,s);
			localStorage.setItem("rcx"+imgcounter,cx);
			mouseTag=0;
		}
		initLocal();
	}
    function text(){
		context.fillStyle="#ccc";
		context.fillRect(0,0,800,600);
		initLocal();
        var c=document.getElementById("color1").value;
        var msg=document.getElementById("t1").value;
        var x=document.getElementById("t2").value;
		var y=document.getElementById("t3").value;
        var e=document.getElementById("t4").value;
        var tbf=document.form1.bf.value;
        var mt=context.measureText(msg);
		var s=document.form1.fs.value;
		context.font=e+"em bold 微软雅黑";
		context.textBaseline=tbf;
		if(s=="stroke"){
			context.lineWidth=cx;
            context.strokeStyle=c;
			context.strokeText(msg,x,y);
		}
		else{
			context.lineWidth=cx;
            context.fillStyle=c;
			context.fillText(msg,x,y);
		}
		imgcounter++;
		localStorage.setItem("ucounter",imgcounter);
		localStorage.setItem("type"+imgcounter,"text");
		localStorage.setItem("cx"+imgcounter,x);
		localStorage.setItem("cy"+imgcounter,y);
		localStorage.setItem("cmsg"+imgcounter,msg);
		localStorage.setItem("cc"+imgcounter,c);
		localStorage.setItem("cs"+imgcounter,s);
		localStorage.setItem("ccx"+imgcounter,cx);
		localStorage.setItem("cct"+imgcounter,e);
        localStorage.setItem("tbf"+imgcounter,tbf);
	}

    function clearLocation(){
		localStorage.clear();   
   		//alert("清理成功,即将刷新页面");
		location.reload();
	}	
    function showx(){
		document.getElementById("t2").value=event.clientX;
		document.getElementById("t3").value=event.clientY;
	}

	function bm(obj){//move
		obj.style.background="rgb(20,60,216)";
	}
	function bo(obj){//out
		obj.style.background="rgb(78,110,242)";
	}
	function bd(obj){//down
		obj.style.background="rgb(15,50,185)";
		obj.style.boxShadow=" 0 0 5px 2px rgb(15,50,185)";
	}
	function bu(obj){//up
		obj.style.background="rgb(78,110,242)";
		obj.style.boxShadow=" 0 0 2px 2px grey";
	}
    function timeleap(obj){
		var tlen=event.clientX-928;
		var len=-250+tlen;
		cx=parseInt(tlen/250*10)+1;
		document.getElementById("jdtn").style.marginLeft=len+"px";
		btn=1;
	}
	function jdtmove(){
		if(btn==1){
			var tlen=event.clientX-928;
			var len=-250+tlen;
			cx=parseInt(tlen/250*10)+1;
			document.getElementById("jdtn").style.marginLeft=len+"px";
		}
	}
	function jdtup(){btn=2;}
	function yuan(){type=1;}
	function xian(){type=2;}
	function rect(){type=3;}
	function tt(){type=999;}
	
</script>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值