小程序实现手写签名

在微信小程序上实现手写签名,获取canvascontext新版本和旧版本有点坑,新版本在获取canvas后如果页面有滑动,则签名坐标出现异常(在微信开发者工具上会出现2022-2-17),但是在真机上即使滑动也不会出现异常,为了防止出现问题,暂时使用旧版本获取canvascontext

1.效果图

在这里插入图片描述
在这里插入图片描述

#2.相关代码

  • 1.canvas代码
    • 新版2d canvas
<canvas
	id="canvas"
	class="canvas"
	canvas-id="canvas"
	type="2d"
	:disable-scroll="true"
	@touchstart="handleTouchStart"
	@touchmove="handleTouchMove"
	@touchend="handleTouchEnd"
	@touchcancel="handleTouchCancel"
></canvas>
  • 旧版canvas
<canvas
	class="canvas"
	canvas-id="canvas"
	:disable-scroll="true"
	@touchstart="handleTouchStart"
	@touchmove="handleTouchMove"
	@touchend="handleTouchEnd"
	@touchcancel="handleTouchCancel"
></canvas>
  • 2.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方法

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
uniapp 是一种基于 Vue.js 框架的跨平台开发框架,可以用于开发多个平台的应用程序,其中包括安卓和苹果的移动应用。如果想要在 uniapp 中实现手写签名功能,可以按照以下步骤进行操作。 1. 导入手写签名组件:在 uniapp 工程中,可以通过 npm 或者其他方式导入适用于手写签名的组件。确保所导入的组件能够兼容 uniapp 平台。 2. 创建手写签名页面:在 uniapp 中创建一个专门用于手写签名的页面,可以是新建的页面或者是已有页面的部分。 3. 在页面中引入手写签名组件:在手写签名页面中,使用 `import` 导入手写签名组件,并在页面的 `components` 中注册。 4. 在页面中使用手写签名组件:在模版中使用手写签名组件的标签,例如 `<handwriting-signature></handwriting-signature>`。 5. 实现手写签名功能:在手写签名组件的方法中,实现手写签名的功能,可以使用 HTML5 的 Canvas API 来实现手写效果。可以监听手指或者鼠标的移动,将轨迹绘制到画布上。 6. 设置保存签名功能:在手写签名组件中,设置保存签名的功能,可以通过调用 uniapp 提供的 API 将绘制的手写签名保存为图片。 7. 添加保存签名按钮:在手写签名页面上,添加一个保存签名的按钮,当用户点击按钮后,将调用手写签名组件的保存签名方法,将签名保存为图片。 8. 其他操作:根据需要,还可以添加清除签名、撤销操作等功能。 实现了以上步骤,就可以在 uniapp 中实现手写签名的功能了。需要注意的是,在实际开发中,根据具体需求可能还要进行一些功能的扩展和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值