zoukankan      html  css  js  c++  java
  • HTML canvas原生js实现鼠标画图

     效果展示:

     查看效果可点击下载源文件 http://i.cnblogs.com/Files.aspx

     html完整代码:

    <!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>canvas鼠标画图</title>
    <style>
    	body { background-color:yellow;}
    	#c1 { background-color: white; }
    </style>
    <script>
    window.onload = function() {
      var oC = document.getElementById('c1');
      var oCG = oC.getContext('2d');
      oC.onmousedown = function(ev) {
    	var ev = ev || window.event;
    	oCG.moveTo(ev.clientX-oC.offsetLeft,ev.clientY-oC.offsetTop); //ev.clientX-oC.offsetLeft,ev.clientY-oC.offsetTop鼠标在当前画布上X,Y坐标
    	document.onmousemove = function(ev) {
    		var ev = ev || window.event;//获取event对象
    		oCG.lineTo(ev.clientX-oC.offsetLeft,ev.clientY-oC.offsetTop); 
    		oCG.stroke();
    		};
    	oC.onmouseup = function() {
    		document.onmousemove = null;
    		document.onmouseup = null;
    		};
        };
    };
    </script>
    </head>
    
    <body>
    	<canvas id="c1" width="400px" height="400px"> <!--宽高写在html里,写在css里有问题-->
        	<span>该浏览器不支持canvas内容</span> <!--对于不支持canvas的浏览器显示-->
        </canvas>
    </body>
    </html>
    
     
    

    年级1

  • 相关阅读:
    IOS之Block的应用-textFeild的回调应用
    KVC与KVO的不同
    git
    perl读取excel
    Linux用户管理
    Linux软件包的管理
    linux系统学习(二)
    linux系统学习(一)
    js模版渲染
    Discuz核心函数的解析
  • 原文地址:https://www.cnblogs.com/mixue/p/4934397.html
Copyright © 2011-2022 走看看