testcanvas2.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <title></title>
  7. <style>
  8. html,
  9. body {
  10. margin: 0;
  11. }
  12. .container {
  13. width: calc(100vw - 40px);
  14. height: calc(100vh - 40px);
  15. padding: 20px;
  16. }
  17. .canvas-body {
  18. width: calc(80vw);
  19. height: calc(80vh);
  20. margin: 20px auto;
  21. }
  22. #sign {
  23. background-color: #f3f5f7;
  24. border-radius: 4px;
  25. border: 1px dashed #0F6BFF;
  26. }
  27. .btns {
  28. width: calc(80vw - 40px);
  29. text-align: right;
  30. margin: 0 auto;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="container">
  36. <div id="canvas-body" class="canvas-body">
  37. <canvas id="sign" style="width: 100%; height: 100%;"></canvas>
  38. </div>
  39. <div class="btns">
  40. <button id="reset">重置</button>
  41. <button id="showImg">生成图片</button>
  42. </div>
  43. </div>
  44. <script>
  45. let canvasBody = document.getElementById('canvas-body');///canvas的外层区域
  46. let canvas = document.getElementById('sign');///canvas签字区域
  47. let reset = document.getElementById('reset');///重置按钮
  48. let showImg = document.getElementById('showImg');///生成图片按钮
  49. canvas.width = canvasBody.clientWidth;
  50. canvas.height = canvasBody.clientHeight;
  51. let ctx = canvas.getContext('2d');
  52. ctx.lineWidth = 10;
  53. ctx.strokeStyle = '#333';
  54. ctx.lineCap = 'round';
  55. ctx.lineJoin = 'round';
  56. let isDrawing = false;
  57. let dataURL = '';
  58. let initX;
  59. let initY;
  60. // 事件监听--开始绘制
  61. canvas.addEventListener("mousedown",function(e){
  62. isDrawing = true;
  63. initX = e.offsetX;
  64. initY = e.offsetY
  65. });
  66. canvas.addEventListener('mousemove', draw);
  67. canvas.addEventListener("mouseup",function(){
  68. isDrawing = false
  69. });
  70. canvas.addEventListener("mouseout",function(){
  71. isDrawing = false
  72. });
  73. // 绘制中...
  74. function draw(e) {
  75. if (!isDrawing) return
  76. ctx.beginPath();
  77. // 起点
  78. ctx.moveTo(initX, initY);
  79. // 终点
  80. ctx.lineTo(e.offsetX, e.offsetY);
  81. ctx.stroke();
  82. initX = e.offsetX;
  83. initY = e.offsetY
  84. }
  85. ///重置
  86. function clear() {
  87. ctx.clearRect(0, 0, canvas.width, canvas.height);
  88. if (dataURL) {
  89. dataURL = '';
  90. }
  91. }
  92. function canvasToBase64() {
  93. dataURL = canvas.toDataURL();
  94. console.log(dataURL);
  95. alert(dataURL)
  96. }
  97. reset.addEventListener('click', clear);
  98. showImg.addEventListener('click', canvasToBase64);
  99. </script>
  100. <script>
  101. </script>
  102. </body>
  103. </html>