当前位置: 首页 > web >正文

用uniapp在微信小程序实现画板(电子签名)功能,使用canvas实现功能

效果:

功能:实现重签  退出  保存 等功能

解决的问题:  电子签名画布抖动问题解

注意: 保存的时候上传到自己的服务器地址,后端返回图片地址

代码:

<template><view><view class="signature" v-show="showCanvas"><canvas class="mycanvas" canvas-id="drawCanvas" id="drawCanvas" @touchstart="touchstart"@touchmove="touchmove" @touchend="touchend" @touchcancel="touchend" disable-scroll></canvas></view><view class="footer"><view class="invite left" @tap="finish">保存</view><view class="invite close" @click="close">退出</view><view class="invite right" @click="clear">重签</view></view></view>
</template><script>export default {data() {return {height: "",showCanvas: true,ctx: '', // 绘图图像points: [], // 路径点集合signature: '',canvasWidth: 0,canvasHeight: 0}},created() {const res = uni.getSystemInfoSync();this.height = res.windowHeight;this.canvasWidth = res.windowWidth;this.canvasHeight = res.windowHeight - 100; // 估算高度,保留底部按钮空间},methods: {close() {this.clear();uni.navigateBack({delta: 1,});},createCanvas() {this.showCanvas = true;this.ctx = uni.createCanvasContext("drawCanvas", this);this.ctx.lineWidth = 4;this.ctx.lineCap = "round";this.ctx.lineJoin = "round";},touchstart(e) {e.preventDefault();let startX = e.changedTouches[0].x;let startY = e.changedTouches[0].y;let startPoint = {X: startX,Y: startY};this.points.push(startPoint);this.ctx.beginPath();},touchmove(e) {e.preventDefault(); // 阻止默认滚动行为let moveX = e.changedTouches[0].x;let moveY = e.changedTouches[0].y;let movePoint = {X: moveX,Y: moveY};this.points.push(movePoint);if (this.points.length >= 2) {this.draw();}},touchend() {this.points = [];},draw() {let point1 = this.points[0];let point2 = this.points[1];this.points.shift();this.ctx.moveTo(point1.X, point1.Y);this.ctx.lineTo(point2.X, point2.Y);this.ctx.stroke();this.ctx.draw(true);},clear() {this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);this.ctx.draw(true);},finish() {let that = this;uni.canvasToTempFilePath({destWidth: 112.5,destHeight: 180,canvasId: 'drawCanvas',success: function(res) {console.log("res", res);let path = res.tempFilePath;that.$emit("store", path);},fail(res) {console.log("err", res);}}, this);}},mounted() {this.createCanvas();}}
</script><style scoped>.signature {z-index: 0;width: 100vw;touch-action: none;/* 禁用默认触摸行为 */}page {background: #fff;}.mycanvas {width: 750rpx;height: calc(100vh - 200upx);touch-action: none;/* 禁用默认触摸行为 */}.footer {display: flex;flex-direction: row-reverse;position: fixed;bottom: 0;width: 100%;padding: 10px 0;margin-bottom: 20px;}.invite {width: 72px;height: 32px;font-size: 12px;text-align: center;line-height: 32px;color: #fff;border-radius: 3px;background-color: #1e7061;margin: 0 10px;}.left {background: #316f60;}.right {background: #8c8c8c;}.close {background: #cd6666;}
</style>

http://www.xdnf.cn/news/4727.html

相关文章:

  • Android 数据持久化之 文件存储
  • SpringBoot整合PDF导出功能
  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】电商数据分析案例-9.1 业务场景与数据准备
  • Wireshark基本使用
  • $SYNT 迈入新时代:治理门户更新 + 通胀提案发布
  • [机器学习介绍与定义]机器学习-part1
  • 删除链表倒数第N个节点
  • 什么是变量提升?(形象的比喻)
  • 【AI入门】CherryStudio入门4:创建知识库,对接思源笔记
  • Spring Boot 实现验证码生成与校验:从零开始构建安全登录系统
  • Linux Shell编程之条件语句
  • Jquery ajax 提交序列化或JSON数据到后台
  • 前端缓存踩坑指南:如何优雅地解决浏览器缓存问题?
  • CKESC STONE 80A-MC 电调的全场景可靠性技术测评
  • 从零打造个人博客静态页面与TodoList应用:前端开发实战指南
  • Maven
  • AI CUBE 使用指南 目标检测格式范例 AI cube 报错数据集不合规范,请清洗数据集
  • 在Hugging Face网站像Github一样克隆repository到本地的具体步骤
  • 【数据结构】——链表OJ(下)
  • 丝路传说手游:职业选择与高难度BOSS突破指南
  • 视频编解码学习8之视频历史
  • hprof文件,java虚拟机堆转储文件,Dump文件
  • 使用adb设置wifi相关
  • AI Agent开发之门:微软官方课程全面解析
  • R1-Searcher:用强化学习解锁大语言模型检索新能力!
  • 计算机体系架构-----设计模式:状态模式(从程序员加班问题切入)
  • SpringBoot中使用MCP和通义千问来处理和分析数据-连接本地数据库并生成实体类
  • 只出现一次的数字(暴力、哈希查重、异或运算)
  • Python基于Django和MySQL实现突发公共卫生事件舆情分析系统(有大屏功能)
  • 【AI论文】FlexiAct:在异构场景中实现灵活的动作控制