效果展示:
查看效果可点击下载源文件 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
作者:米雪321 出处:http://www.cnblogs.com/mixue/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。