Javascript HTML5 Canvas实现的一个画板

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>DEMO6:自定义画板</title> </head> <body> <canvas> 浏览器不支持canvas <!-- 如果不支持会显示这段文字 --> </canvas> <br/> <button>YELLOW</button> <button>RED</button> <button>BLUE</button> <button>GREEN</button> <button>WHITE</button> <button>BLACK</button> <br/> <button>4PX</button> <button>8PX</button> <button>16PX</button> <br/> <button>EXPORT</button> <br/> <img src=""> <br/> <script type="text/javascript"> var canvas = document.getElementById('canvas'); //获取标签 var ctx = canvas.getContext("2d"); var fillStyle = "black"; ctx.fillRect(0,0,600,300); var onoff = false; //按下标记 var oldx = -10; var oldy = -10; //设置颜色 var linecolor = "white"; var linw = 4; canvas.addEventListener("mousemove",draw,true); //鼠标移动事件 canvas.addEventListener("mousedown",down,false); //鼠标按下事件 canvas.addEventListener("mouseup",up,false); //鼠标弹起事件 function down(event){ onoff = true; oldx = event.pageX - 10; oldy = event.pageY - 10; } function up(){ onoff = false; } function draw(event){ if (onoff==true) { var newx = event.pageX - 10; var newy = event.pageY - 10 ctx.beginPath(); ctx.moveTo(oldx,oldy); ctx.lineTo(newx,newy); ctx.strokeStyle = linecolor; ctx.lineWidth = linw; ctx.lineCap = "round"; ctx.stroke(); oldx = newx; oldy = newy; } } function copyimage(event) { var img_png_src = canvas.toDataURL("image/png"); //将画板保存为图片格式的函数 document.getElementById("image_png").src = img_png_src; } </script> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzppgd.html