练习2_签字版.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #can{
  9. background-color: #000;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <canvas id="can" width="500" height="500"></canvas>
  15. <script>
  16. // 获取画布
  17. var oCan = document.getElementById("can");
  18. // 初始化画布
  19. var oCtx = oCan.getContext("2d");
  20. // 设置画笔样式
  21. oCtx.lineWidth = 3;
  22. oCtx.strokeStyle = '#fff';
  23. // 绑定事件
  24. oCan.onmousedown = function(e){
  25. // 落笔
  26. oCtx.moveTo(e.clientX,e.clientY);
  27. // 移动事件
  28. oCan.onmousemove = function(e){
  29. // 移动画笔
  30. oCtx.lineTo(e.clientX,e.clientY);
  31. // 绘制
  32. oCtx.stroke();
  33. }
  34. }
  35. // 鼠标抬起
  36. oCan.onmouseup = function(){
  37. // 停止绘画
  38. oCan.onmousemove = null;
  39. }
  40. </script>
  41. </body>
  42. </html>