【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>
相关推荐
C_心欲无痕20 小时前
前端实现水印的两种方式:SVG 与 Canvas
前端·安全·水印
尾善爱看海1 天前
不常用的浏览器 API —— Web Speech
前端
美酒没故事°1 天前
vue3拖拽+粘贴的综合上传器
前端·javascript·typescript
jingling5551 天前
css进阶 | 实现罐子中的水流搅拌效果
前端·css
zhengxianyi5151 天前
只需3句让Vue3 打包部署后通过修改配置文件修改全局变量——实时生效
vue.js·前后端分离·数据大屏·ruoyi-vue-pro优化
悟能不能悟1 天前
前端上载文件时,上载多个文件,但是一个一个调用接口,怎么实现
前端
可问春风_ren1 天前
前端文件上传详细解析
前端·ecmascript·reactjs·js
羊小猪~~1 天前
【QT】--文件操作
前端·数据库·c++·后端·qt·qt6.3
晚风资源组1 天前
CSS文字和图片在容器内垂直居中的简单方法
前端·css·css3
Miketutu1 天前
Flutter学习 - 组件通信与网络请求Dio
开发语言·前端·javascript