当前位置: 首页 > news >正文

vue封装独立组件:实现手写签名功能

目录

第一章 效果展示

第二章 准备工作

2.1 使用的工具vue-sign

2.1.1 安装

2.1.2 了解

2.1.3 参数说明

第三章 源代码

第一章 效果展示

第二章 准备工作

2.1 使用的工具vue-esign

2.1.1 安装

npm install vue-esign --save

2.1.2 了解

  • 兼容pc端和移动端
  • 有对应的参数让我们自定义画布尺寸(导出图尺寸),画笔粗细、颜色,画布背景色
  • 能支持裁剪,在画布设定尺寸基础上裁掉四周空白部分

2.1.3 参数说明

属性类型默认值说明
widthNumber800画布宽度,即导出图片的宽度
heightNumber300画布高度,即导出图片的高度
lineWidthNumber4画笔粗细
lineColorString#000000画笔颜色
bgColorString画布背景色,为空时画布背景透明,
支持多种格式 '#ccc','#E5A1A1','rgb(229, 161, 161)','rgba(0,0,0,.6)','red'
isCropBooleanfalse是否裁剪,在画布设定尺寸基础上裁掉四周空白部分

第三章 源代码

  • 父组件
<el-col :span="13"><el-form-item label="被调查者签名" prop="respondentSign" :rules="[{type: 'string',required: true,message: '被调查者请签名',trigger: ['change']}]"><div @click="signreVisible=true" style="width: 400px;height: 150px;background-color: #d9d9d9;"><el-image :src="inputForm.respondentSign"style="width: 400px;height: 150px;display: flex;align-items: center;justify-content: center;color: #999;"><div slot="error" >点击签名</div></el-image></div></el-form-item>
</el-col>
<!--引用封装好的组件--->
<el-dialog title="被调查者签字" :visible.sync="signreVisible" width="700px"><sign @setsignin="setsignre"></sign>
</el-dialog>
<!---封装好的组件可以复用了-->
<el-dialog title="调查者签字" :visible.sync="signinVisible" width="700px"><sign @setsignin="setsignin"></sign>
</el-dialog>// 引入自定义封装的组件
import sign from './component/sign.vue'signreVisible: false,
inputForm:{respondentSign = ''
}// 被调查者签字图片,获取子组件传的值
setsignre (img) {this.inputForm.respondentSign = imgthis.signreVisible = false
},
  • 子组件
<template><div><el-card class="qianming-container" body-style="padding:0px"><!---vue-esign组件--><vue-esignref="esign":isCrop="isCrop":width="600":height="300":lineWidth="lineWidth":lineColor="lineColor":format="'image/png'":bgColor.sync="bgColor"></vue-esign><div class="contro-container"><el-button type="danger" @click="handleReset">清空画板</el-button><el-button type="primary" @click="handleGenerate">确认签名</el-button></div></el-card></div>
</template><script>
// 引入组件
import vueEsign from 'vue-esign'
// 这个是请求文件路径的接口
import fileService from '@/api/file/fileService.js'
export default {components: { vueEsign },name: 'sign',data () {return {lineWidth: 6,lineColor: '#000000',bgColor: '',resultImg: '',isCrop: false}},methods: {// 清空画板..handleReset () {this.$refs.esign.reset()this.resultImg = ''},// 生成签名图片..handleGenerate () {this.$refs['esign'].generate().then((res) => {this.resultImg = res // 得到了签字生成的base64图片// console.log('resultImg', this.resultImg)// 这里直接传base64到父组件,然后在父组件处理数据调用接口// this.$emit('setsignin', res)// 也可以转换成在线地址const bl = this.dataURLtoFile(res)let formData = new FormData()formData.append('file', bl, Date.now() + '.png')// console.log('file', formData.get('file'))// 接口请求fileService.upload(formData).then((result) => {// 向父组件传已经转好的地址this.$emit('setsignin', result.data)})}).catch((err) => {// 没有签名,点击生成图片时调用alert(err + ' 未签名!')})},// 将base64转成blob流dataURLtoFile (urlData) {const type = 'image/png'let bytes = nullif (urlData.split(',').length > 1) {bytes = window.atob(urlData.split(',')[1])} else {bytes = window.atob(urlData)}let ab = new ArrayBuffer(bytes.length)let ia = new Uint8Array(ab)for (let i = 0; i < bytes.length; i++) {ia[i] = bytes.charCodeAt(i)}return new Blob([ab], { type })}}
}
</script><style scoped>
button {height: 40px;
}
.contro-container {width: 600px;height: 50px;display: flex;flex-direction: row;align-items: center;justify-content: space-around;background-color: #d3d3d3;position: absolute;bottom: 0px;
}
.qianming-container {width: 600px;height: 350px;margin: 10px auto;position: relative;
}
.text {font-size: 14px;
}
.item {margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {display: table;content: '';
}
.clearfix:after {clear: both;
}
.box-card {width: 95%;margin-left: 2.5%;margin-top: 20px;
}
</style>

http://www.lryc.cn/news/219375.html

相关文章:

  • 图及谱聚类商圈聚类中的应用
  • npx 和 npm 区别
  • HTML_案例1_注册页面
  • Adobe After Effects 2024(Ae2024)在新版本中的升级有哪些?
  • 超越 GLIP! | RegionSpot: 识别一切区域,多模态融合的开放世界物体识别新方法
  • webgoat-(A1)injection
  • 51单片机-中断
  • Canvas 梦幻树生长动画
  • Unity之UI、模型跟随鼠标移动(自适应屏幕分辨率、锚点、pivot中心点)
  • 竞赛 深度学习猫狗分类 - python opencv cnn
  • S4.2.4.7 Start of Data Stream Ordered Set (SDS)
  • CentOS操作系统的特点
  • Go基础(待更新)
  • 二、Hadoop分布式系统基础架构
  • 数据结构(超详细讲解!!)第二十一节 特殊矩阵的压缩存储
  • Python最强自动化神器Playwright!再也不用为爬虫逆向担忧了!
  • 为什么 conda 不能升级 python 到 3.12
  • 0X02
  • 【手写数据库所需C语言基础】可变结构体,结构体成员计算,类型强制转换为统一类型,数据库中使用C语言方法和技巧
  • Android Studio(适配器Adapter)
  • 携程AI布局:三重创新引领旅游行业智能化升级
  • IOS手机耗电量测试
  • LeetCode.6 N字形变换
  • BlockingQueue实现简易消息队列处理器 可分区顺序消费
  • 第一章 03Java入门-编写第一个Java程序HelloWorld以及JVM、JDK和JRE概念
  • windbg的常见调试命令
  • VBA之正则表达式(44)-- 拆分商品和规格
  • 听GPT 讲Rust源代码--library/std(13)
  • 计算机视觉任务图像预处理之去除图像中的背景区域-------使用连通域分析算法(包含完整代码)
  • SurfaceFlinger的硬件Vsync深入分析-千里马android framework车机手机系统开发