首页 > 其他分享 >uniapp做移动端手写签名生成图片

uniapp做移动端手写签名生成图片

时间:2023-11-07 17:55:34浏览次数:30  
标签:uniapp dom res 视图 生成 html2canvas 签名 手写 图片

1、电子签名

电子签名目前已经有很多现成的插件,所以我这里直接选择了一个,有需要的可以看下https://ext.dcloud.net.cn/plugin?id=4354

代码如下:

HTML:

<view class="content">
		<view style="width: 750rpx ;height: 400px;">
			<l-signature disableScroll backgroundColor="#fff" ref="signatureRef" :penColor="penColor" :penSize="penSize"
				:openSmooth="openSmooth"></l-signature>
		</view>
			<view style="display: flex;">
			<button @click="onClick('clear')">清空</button>
			<button @click="onClick('undo')">撤消</button>
			<button @click="onClick('save')">保存</button>
			<button @click="onClick('openSmooth')">压感{{openSmooth?'开':'关'}}</button>
		</view>
	</view>

JS:

onClick(type) {
				if (type == 'openSmooth') {
					this.openSmooth = !this.openSmooth
					return
				}
				if (type == 'save') {
					this.$refs.signatureRef.canvasToTempFilePath({
						success: (res) => {
							// 是否为空画板 无签名
							console.log(res.isEmpty)
							// 生成图片的临时路径
							// app | H5 | 微信小程序 生成的是base64
							this.url = res.tempFilePath
							// console.log(res.tempFilePath)
							uni.setStorageSync('url',res.tempFilePath)
							uni.navigateTo({
								url:'/pages/result/result'
							})
						}
					})
					return
				}
				if (this.$refs.signatureRef)
					this.$refs.signatureRef[type]()
			}

效果如下:
在这里插入图片描述

2、电子签名图片的拖动缩放

在第一步完成之后,我们可以拿到一个base64格式的图片,在有些需求中签名显示的位置可能是不固定的,需要手动拖动调整,当然一般不会这么做。

拖动缩放我这里直接使用了uniapp自带的movable-area 和 movable-view组件,具体使用方式参见官方文档。

在这里插入图片描述
拖动代码如下:

			<movable-area class="max">
				<movable-view class="quyu" direction="all">
					<image :src="url" mode=""></image>
				</movable-view>
			</movable-area>
  • 1
  • 2
  • 3
  • 4
  • 5

3、合同图片的生成

这一步主要使用了html2canvas和uniapp中的renderjs
html2canvas非常牛逼的一款插件,具体使用方式参见官方文档 http://html2canvas.hertzen.com/

renderjs是一个运行在视图层的js。它比WXS更加强大。它只支持app-vue和web。

renderjs的主要作用有2个:
1、大幅降低逻辑层和视图层的通讯损耗,提供高性能视图交互能力
2、在视图层操作dom,运行 for web 的 js库

我们在这里主要用到的是它的第二个作用,操作DOM。因为html2canvas是需要根据dom来生成图片的。

主要代码如下:

<script lang="renderjs" module="canvasImage">
	import html2canvas from 'html2canvas'
	export default {
		methods: {
			// 生成图片需要调用的方法
			generateImage(e, ownerFun) {
				ownerFun.callMethod('_showLoading', '正在生成图片')
				setTimeout(() => {
					const dom = document.getElementById('pagePoster') 
					html2canvas(dom, {
						width: dom.clientWidth, 
						height: dom.clientHeight,
						scrollY: 0, // html2canvas默认绘制视图内的页面,需要把scrollY,scrollX设置为0
						scrollX: 0,
						useCORS: true, //支持跨域
						// scale: 2,
					}).then((canvas) => {
						console.log('啊啊啊',canvas)
						// html2canvas 生成成功的图片链接需要转成 base64位的url
						ownerFun.callMethod('receiveRenderData', canvas.toDataURL('image/png'))
					}).catch(err => {
						ownerFun.callMethod('_errAlert', `【生成图片失败,请重试】${err}`)
					})
				}, 300)
			}
		}
	}
</script>
 

标签:uniapp,dom,res,视图,生成,html2canvas,签名,手写,图片
From: https://www.cnblogs.com/prince11/p/17815549.html

相关文章

  • 手写简单生产者消费者阻塞队列
    主要实现生产者定时生产,消费者只要队列消息中有就消费。importjava.util.*;importjava.util.concurrent.atomic.AtomicInteger;importjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.ReentrantLock;publicclassProductConsumerQueue<E>{......
  • uniapp实用功能代码(小程序支付,图片保存,返回刷新,分享到朋友圈)
    1.uniapp小程序支付:uni.request({url:"http://xxxxxx/payOrder",//后端接口返回调起支付需要的参数data:{userId:1,//此接口需要的参数一般有多个此仅为示例},method:"POST",success:(res)=>{console.log(res.data,"这......
  • APP 备案公钥、签名 MD5、SHA-1、SHA-256获取方法。
    ​公钥和MD5值可以通过苹果开发工具、Keytool、appuploder等多种工具获取,最简单的就是以appuploader为例。 1.下载appuploader工具,点击此处下载appuploader 工具。    ​2.下载完成后,解压压缩包,双击appuploder运行。    ​ 3.运行后,在appuploder......
  • TLS可信任自签名CA证书配置
    直接使用openssl制作的CA证书,由于没有加入访问机器的“受信任的根证书颁发机构”,导致在chrome等浏览器中访问自签名证书的网站时,会有“不可信任证书”提示,进而导致websocket无法成功建立。现在通过mkcert工具可以颁发自签名CA证书,并同时在加入“受信任的根证书颁发机构” ......
  • uniApp:使用vue3+Vite4+pinia+sass技术栈构建(02)-封装api请求
    前言在纯vue3开发的时候,使用axios进行api请求,但在uniapp中还需要安装axios的适配器uniapp-axios-adapter,否则小程序或者app请求不兼容。文档地址uniapp-axios-adapter-DCloud插件市场但在这里我们不使用axios,而是使用uniapp提供的请求方法uni.request进行封装。uni.request方......
  • uniapp+微信小程序 激励广告
    防忘首先在onready里面准备好代码 包括广告准备就绪 准备出错,广告关闭data(){return{showAd:false,canShowAd:false,rewardedVideoAd:null,}},onReady()......
  • 关于对iOS企业签名的类型如何选择,分别有什么优势
    了解过苹果签名的都知道苹果签名的方式有好几种,其中企业签名是其中使用最广泛,性价比最高的一种签名方式,企业签名还分以下三种签名方式,共享企业签名,稳定版企业签名,超稳版企业签名。大家可能还不太了解这三种签名方式的区别,今天大家就一起了解如何选择签名方式,选择哪一种签名方式......
  • 使用uniapp开发小程序getLocation报错
    uniapp中使用uni.getLocation()报错,报错如下:getLocation:failtheapineedtobedeclaredintherequiredPrivateInfosfieldinapp.json/ext.json 首先检查uniapp的manifest文件发现位置权限已经开启: 后翻阅微信文档后发现原来是微信官方做了调整,uniapp只勾选这个还......
  • android 系统修改签名:以android13为例
    android系统修改签名:以android13为例修改签名方式修改签名文件使用签名工具(development/tools/)修改签名文件development/tools/make_keyplatform'/C=CN/ST=ShenZhen/L=NanShan/O=Tripod/OU=WCD/CN=demo/[email protected]'  注意:以上两张图片表......
  • Git 提交时提示 GPG 签名错误
    本来应该一切都是正常的,但今天提交的时候提示GPG签名错误。错误的信息就是GPG签名失败。gpg:skipped"942395299055675C":Nosecretkeygpg:signingfailed:Nosecretkeyerror:gpgfailedtosignthedatafatal:failedtowritecommitobject解决方案开始认为......