飞道的博客

H5移动端项目实现手写签名功能 vue实现手写签名

747人阅读  评论(0)

vue 移动端实现手写签名效果,功能很完美,保存时保存为base64格式。

base64转file文件格式 vue中将base64转file文件格式

 

刚好项目用到此功能,就网上找了一下,清理了无用代码,简单方便,因为项目中多个地方需要使用,所以我将它整理为组件,通过ref和传值控制,下面代码我单独整理出来,可自行封装组件(只支持移动端,不支持pc端)。

代码中,css自行修改一下宽高,背景之类即可。

代码如下


  
  1. <template>
  2. <!-- 手写签名组件 -->
  3. <div class="page sign-page">
  4. <div class="content">
  5. <div class="sign-wrap" id="signWrap">
  6. <canvas id="myCanvas" width="100%" height="100%"> </canvas>
  7. </div>
  8. </div>
  9. <div class="con-btn">
  10. <span class="staging-btn size14" @click="clearArea()">清除签名 </span>
  11. <span class="submit-btn size14" @click="saveSign()">确认签名 </span>
  12. </div>
  13. </div>
  14. </template>

  
  1. <script>
  2. export default {
  3. name: "signature",
  4. data() {
  5. return {
  6. image: "",
  7. mousePressed: false,
  8. c: "",
  9. ctx: "",
  10. lastX: 0,
  11. lastY: 0,
  12. };
  13. },
  14. mounted() {
  15. this.image = "";
  16. this.mousePressed = false;
  17. var lastX, lastY;
  18. this.ctx = document.getElementById( "myCanvas").getContext( "2d");
  19. this.c = document.getElementById( "myCanvas");
  20. var signWrap = document.getElementById( "signWrap");
  21. this.c.width = signWrap.clientWidth; // 设置宽度
  22. this.c.height = signWrap.clientHeight; // 设置高度
  23. // 监听touchstart事件,touchmove事件,touchend事件等事件
  24. this.InitThis();
  25. },
  26. methods: {
  27. InitThis() {
  28. // 触摸屏
  29. var that = this;
  30. this.c.addEventListener(
  31. "touchstart",
  32. function (event) {
  33. if (event.targetTouches.length == 1) {
  34. event.preventDefault(); // 阻止浏览器默认事件,重要
  35. var touch = event.targetTouches[ 0];
  36. this.mousePressed = true;
  37. that.Draw(
  38. touch.pageX - this.offsetLeft,
  39. touch.pageY - this.offsetTop,
  40. false
  41. );
  42. }
  43. },
  44. false
  45. );
  46. this.c.addEventListener(
  47. "touchmove",
  48. function (event) {
  49. if (event.targetTouches.length == 1) {
  50. event.preventDefault(); // 阻止浏览器默认事件,重要
  51. var touch = event.targetTouches[ 0];
  52. if ( this.mousePressed) {
  53. that.Draw(
  54. touch.pageX - this.offsetLeft,
  55. touch.pageY - this.offsetTop,
  56. true
  57. );
  58. }
  59. }
  60. },
  61. false
  62. );
  63. this.c.addEventListener(
  64. "touchend",
  65. function (event) {
  66. if (event.targetTouches.length == 1) {
  67. event.preventDefault(); // 阻止浏览器默认事件,防止手写的时候拖动屏幕,重要
  68. this.mousePressed = false;
  69. }
  70. },
  71. false
  72. );
  73. // 鼠标
  74. this.c.onmousedown = function (event) {
  75. this.mousePressed = true;
  76. that.Draw(
  77. event.pageX - this.offsetLeft,
  78. event.pageY - this.offsetTop,
  79. false
  80. );
  81. };
  82. this.c.onmousemove = function (event) {
  83. if ( this.mousePressed) {
  84. that.Draw(
  85. event.pageX - this.offsetLeft,
  86. event.pageY - this.offsetTop,
  87. true
  88. );
  89. }
  90. };
  91. this.c.onmouseup = function (event) {
  92. this.mousePressed = false;
  93. };
  94. },
  95. Draw(x, y, isDown) {
  96. if (isDown) {
  97. this.ctx.beginPath();
  98. this.ctx.strokeStyle = "#000"; // 颜色
  99. this.ctx.lineWidth = 3; // 线宽
  100. this.ctx.lineJoin = "round";
  101. this.ctx.lineMax = 10; // 设置画笔最大线宽
  102. this.ctx.lineMin = 3; // 设置画笔最小线宽
  103. this.ctx.linePressure = 1.2; // 设置画笔笔触压力
  104. this.ctx.smoothness = 30; // 设置画笔笔触大小变化的平滑度
  105. this.ctx.moveTo( this.lastX, this.lastY);
  106. this.ctx.lineTo(x, y);
  107. this.ctx.closePath();
  108. this.ctx.stroke();
  109. }
  110. this.lastX = x;
  111. this.lastY = y;
  112. },
  113. // 清空画板
  114. clearArea() {
  115. this.ctx.setTransform( 1, 0, 0, 1, 0, 0);
  116. this.ctx.clearRect( 0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  117. },
  118. // 提交签名
  119. saveSign() {
  120. this.checkEmpty(); // 调用 表单非空验证
  121. },
  122. checkEmpty() {
  123. var c = document.getElementById( "myCanvas"); // 获取html的canvas对象,我这个id="myCanvas"
  124. if ( this.isCanvasBlank(c)) {
  125. alert( "请在签名区域签名后再次确认");
  126. return;
  127. } else {
  128. var image = this.c.toDataURL( "image/png"); // 得到生成后的签名base64位 url 地址
  129. console.log(image); // 打印图片base64 url
  130. }
  131. },
  132. // 验证canvas画布是否为空函数
  133. isCanvasBlank(canvas) {
  134. var blank = document.createElement( "canvas"); // 系统获取一个空canvas对象
  135. blank.width = canvas.width;
  136. blank.height = canvas.height;
  137. return canvas.toDataURL() == blank.toDataURL(); // 比较值相等则为空
  138. },
  139. },
  140. };
  141. </script>

  
  1. < style lang=" scss" scoped>
  2. .page {
  3. width: 100%;
  4. .content {
  5. width: 100%;
  6. height: 1.7rem;
  7. background: url(../assets/img/photo_qmq.png) no-repeat;
  8. background-size: 100% 100%;
  9. background-position: center center;
  10. .sign-wrap {
  11. width: 100%;
  12. height: 100%;
  13. }
  14. }
  15. .con-btn {
  16. width: 100%;
  17. display: flex;
  18. align-content: center;
  19. justify-content: space-between;
  20. opacity: 0.75;
  21. span {
  22. font-size: 0.14rem;
  23. width: 100%;
  24. height: 0.48rem;
  25. display: flex;
  26. align-items: center;
  27. justify-content: center;
  28. }
  29. .staging-btn {
  30. color: #4154ff;
  31. background: #fff;
  32. }
  33. .submit-btn {
  34. color: #fff;
  35. background: #4154ff;
  36. }
  37. }
  38. }
  39. </ style>

原文围观地址 http://www.sharedblog.cn/post/217.html

个人博客http://sharedblog.cn/

软件下载http://iqzhan.com/

 


转载:https://blog.csdn.net/tianpeng1996/article/details/113704211
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场