• uniapp——生成一个签字板


    在开发项目中有签名/签字的需求,以下实现:

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. showAutograph: false,
    6. ctx: '', //绘图图像
    7. points: [], //路径点集合
    8. signature: '',
    9. type: ''
    10. };
    11. },
    12. methods: {
    13. //创建并显示签名画布
    14. autograph(type) {
    15. this.type = type
    16. this.showAutograph = true;
    17. //创建绘图对象
    18. this.ctx = uni.createCanvasContext('mycanvas', this);
    19. //设置画笔样式
    20. this.ctx.lineWidth = 4;
    21. this.ctx.lineCap = 'round';
    22. this.ctx.lineJoin = 'round';
    23. },
    24. //触摸开始,获取到起点
    25. touchstart(e) {
    26. let startX = e.changedTouches[0].x;
    27. let startY = e.changedTouches[0].y;
    28. let startPoint = {
    29. X: startX,
    30. Y: startY
    31. };
    32. this.points.push(startPoint);
    33. //每次触摸开始,开启新的路径
    34. this.ctx.beginPath();
    35. },
    36. //触摸移动,获取到路径点
    37. touchmove(e) {
    38. let moveX = e.changedTouches[0].x;
    39. let moveY = e.changedTouches[0].y;
    40. let movePoint = {
    41. X: moveX,
    42. Y: moveY
    43. };
    44. this.points.push(movePoint); //存点
    45. let len = this.points.length;
    46. if (len >= 2) {
    47. this.draw(); //绘制路径
    48. }
    49. },
    50. // 触摸结束,将未绘制的点清空防止对后续路径产生干扰
    51. touchend() {
    52. this.points = [];
    53. },
    54. /* ***********************************************
    55. # 绘制笔迹
    56. # 1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
    57. # 2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
    58. # 3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
    59. ************************************************ */
    60. draw() {
    61. let point1 = this.points[0];
    62. let point2 = this.points[1];
    63. this.points.shift();
    64. this.ctx.moveTo(point1.X, point1.Y);
    65. this.ctx.lineTo(point2.X, point2.Y);
    66. this.ctx.stroke();
    67. this.ctx.draw(true);
    68. },
    69. //清空画布
    70. clear() {
    71. let that = this;
    72. uni.getSystemInfo({
    73. success: function (res) {
    74. let canvasw = res.windowWidth;
    75. let canvash = res.windowHeight;
    76. that.ctx.clearRect(0, 0, canvasw, canvash);
    77. that.ctx.draw(true);
    78. }
    79. });
    80. },
    81. //关闭并清空画布
    82. cancel() {
    83. this.showAutograph = false;
    84. this.clear();
    85. },
    86. //完成绘画并保存到本地
    87. confirm() {
    88. let that = this;
    89. uni.canvasToTempFilePath({
    90. canvasId: 'mycanvas',
    91. success: function (res) {
    92. that.$emit('ok', that.type, res.tempFilePath)
    93. that.cancel()
    94. }
    95. });
    96. }
    97. }
    98. };
    99. script>
    100. <style lang="less" scoped>
    101. page {
    102. font-family: Source Han Sans CN;
    103. }
    104. .new_file {
    105. position: fixed;
    106. left: 0;
    107. top: 0;
    108. width: 100%;
    109. height: 100%;
    110. background-color: rgba(0, 0, 0, 0.5);
    111. .buttONbOX {
    112. position: fixed;
    113. left: 50%;
    114. top: 50%;
    115. }
    116. .popupBox {
    117. position: fixed;
    118. left: 50%;
    119. bottom: 188rpx;
    120. transform: translate(-50%, 0);
    121. width: 720rpx;
    122. height: 1055rpx;
    123. background: #ffffff;
    124. border-radius: 16rpx;
    125. .popupTopBox {
    126. width: 100%;
    127. height: 80rpx;
    128. background: #1f75b4;
    129. border-radius: 16rpx 16rpx 0px 0px;
    130. text-align: center;
    131. line-height: 80rpx;
    132. font-size: 32rpx;
    133. color: #ffffff;
    134. }
    135. .mycanvas {
    136. width: 100%;
    137. height: 860rpx;
    138. }
    139. .determineBtn {
    140. width: 280rpx;
    141. height: 69rpx;
    142. background: #1f75b4;
    143. border-radius: 35rpx;
    144. margin: 0 auto;
    145. font-size: 30rpx;
    146. color: #ffffff;
    147. text-align: center;
    148. line-height: 69rpx;
    149. }
    150. }
    151. .closeIcon {
    152. position: fixed;
    153. left: 50%;
    154. bottom: 92rpx;
    155. transform: translate(-50%, 0);
    156. width: 50rpx;
    157. height: 50rpx;
    158. image {
    159. width: 100%;
    160. height: 100%;
    161. }
    162. }
    163. }
    164. style>

    实现效果:

  • 相关阅读:
    MONAI Label -- 使用 AI 加速你的分割标注
    论文笔记:Localizing Cell Towers fromCrowdsourced Measurements (intro 部分)
    Vue CLI脚手架安装、搭建、配置 和 CLI项目分析
    SELF-RAG: 让LLM集检索,生成跟评判等多种能力于一身
    配电站房门禁监控、气体监测、视频监控系统 设备安装
    状态空间方程的离散化
    批量根据execel内容生成条码
    AMQ 5805报错
    2.1.1进位计数制
    typora使用PicGo自动上传图片到chevereto图床
  • 原文地址:https://blog.csdn.net/lmyh1111/article/details/132894096