【vue/uniapp】使用 smooth-signature 实现 h5 的横屏电子签名

通过github链接进行下载,然后代码参考如下,功能包含了清空、判断签名内容是否为空、生成png/jpg图片等。

签名效果:

预览效果:

下载 smooth-signature 链接:https://github.com/linjc/smooth-signature

代码参考:

javascript 复制代码
<template>
	<div>
	    <div class="btn">
	        <button class="item" @click="submit">确定</button>
	        <button class="item" @click="clear">清空</button>
	    </div>
	    <canvas />
	    <u-toast ref="uToast" />
	</div>
</template>

<script>
import SmoothSignature from "smooth-signature";
export default {
	name: "handWriteSign",
	data() {
	    return {
	        canvas: '',
	        options: {},
	        signature: ''
	    }
	},
	mounted() {
	    this.initSignature()
	},
	methods: {
	    initSignature() {
	        // 初始化
	        this.canvas = document.querySelector("canvas");
	        // 配置项
	        this.options = {
	            // 画布在页面实际渲染的宽度、高度
	            width: window.innerWidth,
	            height: window.innerHeight - 100,
	            // 画布缩放
	            scale: 2,
	            minWidth: 4,
	            maxWidth: 10,
	            color: '#000',
	            bgColor: '#fff'
	        };
	        this.signature = new SmoothSignature(this.canvas, this.options)
	    },
	    clear() {
	        this.signature.clear()
	    },
	    submit() {
	        // 判断签名内容是否为空
	        const isEmpty = this.signature.isEmpty();
	        if (isEmpty) {
	            return this.$refs.uToast.show({
	                title: '内容为空,请签名',
	                type: 'error'
	            })
	        }
	        // 生成旋转后的新画布 -90/90/-180/180
	        const canvas1 = this.signature.getRotateCanvas(-90)
	        const pngUrl = canvas1.toDataURL()
	        // let signPic = this.signature.getPNG()
	        console.log('signPic', pngUrl);
	        uni.setStorageSync('signPic', pngUrl)
	        if (pngUrl) {
	            uni.navigateBack({
	                delta: 1, // 返回的页面数,如果是1表示返回上一级
	            });
	        }
	    },
	},
};
</script>

<style lang="scss">
	.btn {
	    display: flex;
	    align-items: center;
	    height: 80px;
	    .item {
	        transform: rotate(90deg);
	        border-radius: 8px;
	        border: 1px solid #f0f0f0;
	    }
	}
</style>

然后在要展示签名的页面:

js 复制代码
// 每次进入页面时执行
onShow() {
    this.img1 = uni.getStorageSync('signPic')
},
html 复制代码
<image :src="img1" style="width: 100%;" mode="widthFix"></image>
相关推荐
孟祥_成都7 分钟前
让 AI 自动写 SQL、读文档,前端也能玩转 Agent! langchain chains 模块解析
前端·人工智能
天蓝色的鱼鱼35 分钟前
别再瞎转Base64了!一文打通前端二进制任督二脉
前端
哟哟耶耶38 分钟前
Plugin-安装Vue.js devtools6.6.3扩展(组件层级可视化)
前端·javascript·vue.js
梦6501 小时前
【前端实战】图片元素精准定位:无论缩放,元素始终钉在指定位置
前端·html·css3
2501_915921431 小时前
没有 iOS 源码的前提下如何进行应用混淆,源码混淆失效后的替代
android·ios·小程序·https·uni-app·iphone·webview
计算机学姐1 小时前
基于SpringBoot的美妆销售系统【个性化推荐算法+数据可视化统计+库存预警+物流信息】
java·vue.js·spring boot·后端·mysql·信息可视化·mybatis
烟袅1 小时前
一文搞懂 useRef:它到底在“存”什么?
前端·react.js
Knight_AL1 小时前
Vue + Spring Boot 项目统一添加 `/wvp` 访问前缀实践
前端·vue.js·spring boot
前端er小芳1 小时前
前端虚拟列表滚动功能实现与核心知识点详解
前端
wuhen_n1 小时前
Promise状态机与状态流转
前端