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

用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>

相关文章:

  • 聊一聊接口的压力测试如何进行的?
  • Algolia - Docsearch的申请配置安装【以踩坑解决版】
  • Java线程安全问题深度解析与解决方案
  • Vue2 中 el-dialog 封装组件属性不生效的深度解析(附 $attrs、inheritAttrs 原理)
  • 【记录】HunyuanVideo 文生视频工作流
  • 用于构建安全AI代理的开源防护系统
  • 辰鳗科技朱越洋:紧扣时代契机,全力投身能源转型战略赛道
  • 射频前端模组芯片(PA)三伍微电子GSR2337 兼容替代SKY85337, RTC7646, KCT8247HE
  • 2025年小程序DDoS与CC攻击防御全指南:构建智能安全生态
  • Linux下的c/c++开发之操作Sqlite3数据库
  • 使用 Vite 创建 Vue 3 项目并手动配置路由的完整步骤
  • 蓝桥杯青少 图形化编程(Scratch)每日一练——校门外的树
  • 基于vueflow可拖拽元素的示例(基于官网示例的单文件示例)
  • 面试实践AND面经热点题目总结
  • 探索 C++23 的 views::cartesian_product
  • 基于机器学习的攻击检测与缓解,以及 SDN 环境中的多控制器布局优化
  • 微程序控制器的详细工作过程
  • 如何在Jmeter中调用C程序?
  • 深入理解Embedding Models(嵌入模型):从原理到实战(上)
  • 2025-05-08 Unity 网络基础9——FTP通信
  • 妻子藏匿一岁幼儿一年多不让丈夫见,法院发出人格权侵害禁令
  • 上海加力提速推进优化营商环境,明确“十大攻坚突破任务”
  • 对话|蓬皮杜策展人布莱昂:抽象风景中的中国审美
  • 叙利亚政权领导人首访西方国家,与法国总统讨论叙局势
  • 中俄领导人将讨论从俄罗斯经蒙古至中国天然气管道项目?外交部回应
  • 央视315晚会曝光“保水虾仁”后,湛江4家涉事企业被罚超800万元