【微信小程序canvas】实现小程序手写板用户签名(附代码)

2024-05-21 02:08

本文主要是介绍【微信小程序canvas】实现小程序手写板用户签名(附代码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【微信小程序canvas】实现小程序手写板用户签名(附代码)

工作中公司业务需要的微信小程序用户签字功能

先看效果图:

 

demo

wxml

<view class="wrapper"><view class="handBtn"><button catchtap="retDraw" class="delBtn">重写</button><button catchtap="subCanvas" class="subBtn">完成</button> </view><view class="handCenter"><canvas class="handWriting" disable-scroll="true" bindtouchstart="uploadScaleStart" bindtouchmove="uploadScaleMove"bindtouchend="uploadScaleEnd" bindtap="mouseDown" canvas-id="handWriting"></canvas></view><view class="handRight"><view class="handTitle">手写板</view></view>
</view>

js

data

page({data: {canvasName: 'handWriting',ctx: '',canvasWidth: 0,canvasHeight: 0,transparent: 1, // 透明度selectColor: 'black',lineColor: '#1A1A1A', // 颜色lineSize: 1.5,  // 笔记倍数lineMin: 0.5,   // 最小笔画半径lineMax: 4,     // 最大笔画半径pressure: 1,     // 默认压力smoothness: 60,  //顺滑度,用60的距离来计算速度currentPoint: {},currentLine: [],  // 当前线条firstTouch: true, // 第一次触发radius: 1, //画圆的半径cutArea: { top: 0, right: 0, bottom: 0, left: 0 }, //裁剪区域bethelPoint: [],  //保存所有线条 生成的贝塞尔点;lastPoint: 0,chirography: [], //笔迹currentChirography: {}, //当前笔迹linePrack: [], //划线轨迹 , 生成线条的实际点}
})

初始化

page({onLoad () {let canvasName = this.data.canvasNamelet ctx = wx.createCanvasContext(canvasName)this.setData({ctx: ctx})var query = wx.createSelectorQuery();query.select('.handCenter').boundingClientRect(rect => {query.select('.handCenter').boundingClientRect(rect => {this.setData({this.setData({canvasWidth: rect.width,canvasWidth: rect.width,canvasHeight: rect.heightcanvasHeight: rect.height})})}).exec(); }).exec();}, },
})

事件函数

笔迹开始

// 笔迹开始uploadScaleStart (e) {if (e.type != 'touchstart') return false;let ctx = this.data.ctx;ctx.setFillStyle(this.data.lineColor);  // 初始线条设置颜色ctx.setGlobalAlpha(this.data.transparent);  // 设置半透明let currentPoint = {x: e.touches[0].x,y: e.touches[0].y}let currentLine = this.data.currentLine;currentLine.unshift({time: new Date().getTime(),dis: 0,x: currentPoint.x,y: currentPoint.y})this.setData({currentPoint,// currentLine})if (this.data.firstTouch) {this.setData({cutArea: { top: currentPoint.y, right: currentPoint.x, bottom: currentPoint.y, left: currentPoint.x },firstTouch: false})}this.pointToLine(currentLine);},

笔迹移动

// 笔迹移动uploadScaleMove (e) {if (e.type != 'touchmove') return false;if (e.cancelable) {// 判断默认行为是否已经被禁用if (!e.defaultPrevented) {e.preventDefault();}}let point = {x: e.touches[0].x,y: e.touches[0].y}//测试裁剪if (point.y < this.data.cutArea.top) {this.data.cutArea.top = point.y;}if (point.y < 0) this.data.cutArea.top = 0;if (point.x > this.data.cutArea.right) {this.data.cutArea.right = point.x;}if (this.data.canvasWidth - point.x <= 0) {this.data.cutArea.right = this.data.canvasWidth;}if (point.y > this.data.cutArea.bottom) {this.data.cutArea.bottom = point.y;}if (this.data.canvasHeight - point.y <= 0) {this.data.cutArea.bottom = this.data.canvasHeight;}if (point.x < this.data.cutArea.left) {this.data.cutArea.left = point.x;}if (point.x < 0) this.data.cutArea.left = 0;this.setData({lastPoint: this.data.currentPoint,currentPoint: point})let currentLine = this.data.currentLinecurrentLine.unshift({time: new Date().getTime(),dis: this.distance(this.data.currentPoint, this.data.lastPoint),x: point.x,y: point.y})// this.setData({//   currentLine// })this.pointToLine(currentLine);},

笔迹结束

// 笔迹结束uploadScaleEnd (e) {if (e.type != 'touchend') return 0;let point = {x: e.changedTouches[0].x,y: e.changedTouches[0].y}this.setData({lastPoint: this.data.currentPoint,currentPoint: point})let currentLine = this.data.currentLinecurrentLine.unshift({time: new Date().getTime(),dis: this.distance(this.data.currentPoint, this.data.lastPoint),x: point.x,y: point.y})// this.setData({//   currentLine// })if (currentLine.length > 2) {var info = (currentLine[0].time - currentLine[currentLine.length - 1].time) / currentLine.length;//$("#info").text(info.toFixed(2));}//一笔结束,保存笔迹的坐标点,清空,当前笔迹//增加判断是否在手写区域;this.pointToLine(currentLine);var currentChirography = {lineSize: this.data.lineSize,lineColor: this.data.lineColor};var chirography = this.data.chirographychirography.unshift(currentChirography);this.setData({chirography})var linePrack = this.data.linePracklinePrack.unshift(this.data.currentLine);this.setData({linePrack,currentLine: []})},

wxss

page {background: #fbfbfb;height: auto;overflow: hidden;
}.wrapper {width: 100%;height: 95vh;margin: 30rpx 0;overflow: hidden;display: flex;align-content: center;flex-direction: row;justify-content: center;font-size: 28rpx;
}.handWriting {background: #fff;width: 100%;height: 95vh;
}.handRight {display: inline-flex;align-items: center;
}.handCenter {border: 4rpx dashed #e9e9e9;flex: 5;overflow: hidden;box-sizing: border-box;
}.handTitle {transform: rotate(90deg);flex: 1;color: #666;
}.handBtn button {font-size: 28rpx;
}.handBtn {height: 95vh;display: inline-flex;flex-direction: column;justify-content: space-between;align-content: space-between;flex: 1;
}.delBtn {position: absolute;top: 550rpx;left: 0rpx;transform: rotate(90deg);color: #666;
}.delBtn image {position: absolute;top: 13rpx;left: 25rpx;
}

 

这篇关于【微信小程序canvas】实现小程序手写板用户签名(附代码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/995968

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM