这期内容当中小编将会给大家带来有关canvas怎么在html5 中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
成都创新互联是一家专注于成都网站设计、成都网站建设与策划设计,吴堡网站建设哪家好?成都创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:吴堡等地区。吴堡做网站价格咨询:028-86922220<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>HTML5示例</title> <style type="text/css"> #container{border:1px solid #ccc;width:800px;height:600px;position:relative;} canvas{position:absolute;top:0px;left:0px;z-index:1;} </style> </head> <body> <select id="tools"> <option value="pen">铅笔</option> <option value="line">直线</option> <option value="rect">矩形</option> <option value="circle">圆形</option> <option value="ellipse">椭圆</option> </select> <div id="container"> <canvas id="canvas" width="800" height="600"></canvas> <canvas id="canvas_temp" width="800" height="600"></canvas> </div> <script type="text/javascript"> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var _canvas = document.getElementById('canvas_temp'); var _context = _canvas.getContext('2d'); var tools= document.getElementById('tools'); tools.onchange = function (e){ tool[this.value](); }; var tool = { pen:function (){ this.reset(); _canvas.onmousedown=function (e){ _context.moveTo(e.layerX,e.layerY); _canvas.onmousemove=function (e){ _context.lineTo(e.layerX,e.layerY); _context.stroke(); }; _canvas.onmouseup=function (e){ _canvas.onmousemove=null; _canvas.onmouseup=null; tool.updata(); }; }; }, line:function (){ this.reset(); _canvas.onmousedown=function (e){ var _e = e; _canvas.onmousemove=function (e){ _context.clearRect(0,0,canvas.width,canvas.height); _context.beginPath(); _context.moveTo(_e.layerX,_e.layerY); _context.lineTo(e.layerX,e.layerY); _context.stroke(); _context.closePath(); }; _canvas.onmouseup=function (e){ _canvas.onmousemove=null; _canvas.onmouseup=null; tool.updata(); }; } }, rect:function (){ this.reset(); _canvas.onmousedown=function (e){ var _e = e; _context.strokeStyle="#000"; _canvas.onmousemove=function (e){ _context.clearRect(0,0,canvas.width,canvas.height); _context.strokeRect(_e.layerX,_e.layerY,e.layerX-_e.layerX,e.layerY-_e.layerY); }; _canvas.onmouseup=function (e){ _canvas.onmousemove=null; _canvas.onmouseup=null; tool.updata(); }; } }, circle:function (){ this.reset(); _canvas.onmousedown=function (e){ var _e = e; _canvas.onmousemove=function (e){ _context.clearRect(0,0,canvas.width,canvas.height); _context.beginPath(); _context.arc(_e.layerX,_e.layerY,e.layerX-_e.layerX,0,Math.PI*2,true); _context.stroke(); _context.closePath(); }; _canvas.onmouseup=function (e){ _canvas.onmousemove=null; _canvas.onmouseup=null; tool.updata(); }; } }, ellipse:function (){ this.reset(); _canvas.onmousedown=function (e){ var _e = e; _canvas.onmousemove=function (e){ var st=0; _context.clearRect(0,0,canvas.width,canvas.height); _context.beginPath(); _context.moveTo(_e.layerX+(e.layerX-_e.layerX)*Math.cos(st), _e.layerY+(e.layerX-_e.layerX)*Math.sin(st)); st+=1/180*Math.PI; for(var i=0;i<360;i++){ _context.lineTo(_e.layerX+(e.layerX-_e.layerX)*Math.cos(st),_e.layerY+(e.layerY-_e.layerY)*Math.sin(st)); st+=1/180*Math.PI; } _context.stroke(); _context.closePath(); }; _canvas.onmouseup=function (e){ _canvas.onmousemove=null; _canvas.onmouseup=null; tool.updata(); }; } }, reset:function (){ _canvas.onmousedown=null; _canvas.onmouseup=null; _canvas.onmousemove=null; }, updata:function (){ context.drawImage(_canvas, 0, 0); _context.clearRect(0, 0, canvas.width, canvas.height); } }; tool['pen'](); </script> </body> </html>
上述就是小编为大家分享的canvas怎么在html5 中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
分享标题:canvas怎么在html5中使用-创新互联
文章分享:https://www.cdcxhl.com/article16/dhgsgg.html
成都网站建设公司_创新互联,为您提供建站公司、域名注册、网站维护、营销型网站建设、小程序开发、网站内链
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联