微信小程序怎么实现手写签名
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,本文小编为大家详细介绍"微信小程序怎么实现手写签名",内容详细,步骤清晰,细节处理妥当,希望这篇"微信小程序怎么实现手写签名"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。在
千家信息网最后更新 2026年11月04日微信小程序怎么实现手写签名
本文小编为大家详细介绍"微信小程序怎么实现手写签名",内容详细,步骤清晰,细节处理妥当,希望这篇"微信小程序怎么实现手写签名"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
在微信小程序上实现手写签名,获取canvascontext新版本和旧版本有点坑,新版本在获取canvas后如果页面有滑动,则签名坐标出现异常(在微信开发者工具上会出现2022-2-17),但是在真机上即使滑动也不会出现异常,为了防止出现问题,暂时使用旧版本获取canvascontext
1.效果图
2.相关代码
canvas代码
新版2d canvas
旧版canvas
js相关
获取新版2d canvas对象
const query = uni.createSelectorQuery().in(this);query.select('.canvas').node(res => { const { _width, _height } = res.node; /* 获取canvas wxml节点 */ this.canvas = res.node; this.canvasWidth = _width; this.canvasHeight = _height; /* 获取canvas 2dcontext */ this.canvasContext= this.canvas.getContext('2d'); /* 缩放设置canvas画布大小,防止笔迹错位 */ const ratio = wx.getSystemInfoSync().pixelRatio; this.canvas.width = this.canvasWidth * ratio; this.canvas.height = this.canvasHeight * ratio; this.canvasContext.scale(ratio, ratio); /* 设置线条颜色 */ this.canvasContext.strokeStyle = '#2A2A2A'; /* 设置线条粗细 */ this.canvasContext.lineWidth = 4; /* 设置线条的结束端点样式 */ this.canvasContext.lineCap = 'round';}).exec()缩放设置canvas画布大小,防止笔迹错位,这点和页面滑动没有关系,不设置也会导致坐标错位
const ratio = wx.getSystemInfoSync().pixelRatio;this.canvas.width = this.canvasWidth * ratio;this.canvas.height = this.canvasHeight * ratio;this.canvasContext.scale(ratio, ratio);
旧版本获取canvas
this.canvasContext = uni.createCanvasContext('canvas', this);/* 设置线条颜色 */this.canvasContext.setStrokeStyle('#2A2A2A');/* 设置线条粗细 */this.canvasContext.setLineWidth(4);/* 设置线条的结束端点样式 */this.canvasContext.setLineCap('round');签名js方法,新版本和旧版本只有一个draw的区别,新版本不需要使用draw方法
/* 触摸开始 */handleTouchStart(e) { this.drawStartX = e.changedTouches[0].x; this.drawStartY = e.changedTouches[0].y; this.canvasContext.beginPath();},/* 触摸移动 */handleTouchMove(e) { /* 记录当前位置 */ const tempX = e.changedTouches[0].x; const tempY = e.changedTouches[0].y; /* 画线 */ this.canvasContext.moveTo(this.drawStartX, this.drawStartY); this.canvasContext.lineTo(tempX, tempY); this.canvasContext.stroke(); /* 旧版draw方法,新版本不需要draw */ this.canvasContext.draw(true); /* 重新记录起始位置 */ this.drawStartX = tempX; this.drawStartY = tempY;},/* 触摸结束 */handleTouchEnd(e) { this.canvasContext.save();},/* 触摸取消 */handleTouchCancel(e) { this.canvasContext.save();},/* 清空画布 */clearCanvas() { this.canvasContext.clearRect(0, 0, this.canvasWidth, this.canvasHeight);},canvas生成本地图片(我这里封装了组件,需要传入this防止this指向异常)
/* 生成签名图片 */generateSignImage() { return new Promise((resolve, reject) => { uni.canvasToTempFilePath({ x: 0, y: 0, // canvas: this.canvas, // 新版 canvasId: 'canvas', // 旧版使用id width: this.canvasWidth, height: this.canvasHeight, destWidth: this.canvasWidth, destHeight: this.canvasHeight, fileType: 'png', quality: 1, success: res => { resolve(res.tempFilePath) }, fail: err => { reject(err); } }, this) })},新版本的canvas主要是canvas wxml节点和canvas context中做了区分,旧版则只有一个canvas context就可以做全部的操作,在生成图片时,新版本是传入wxml对象,旧版本则是传入唯一canvasId,新版本canvas取消了draw方法
读到这里,这篇"微信小程序怎么实现手写签名"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
线条
版本
程序
方法
图片
文章
画布
代码
位置
内容
只有
坐标
大小
对象
样式
端点
笔迹
粗细
节点
页面
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技产业
金融科技与互联网金融
善班互联网科技
九亚互联网科技
智慧科技互联网
奇士达互联网科技
长亮科技 互联网金融
太原新华互联网科技学校怎么样
山东吉时兔互联网科技有限公司
隆华科技工业互联网
恒生互联网科技指数跳水
亳州好赞互联网科技有限公司
互联网科技创新的路径
银行科技岗属于互联网吗
互联网科技快讯热点
互联网安防科技感
互联网金融运营模式中的金融科技
互联网科技大佬云集
东风日产互联网科技公司叫什么
互联网大会出现的黑科技
易互联网科技有限公司
致骋互联网科技
唐信互联网科技 大连
现代科技和互联网有关系吗
中国的互联网科技企业的崛起
世界互联网科技公司市值排名
青海科技产业互联网发展
无锡工业互联网科技有限公司
深圳互联网科技园地址
广州全民伙伴互联网科技有限公司