【uniapp 开发】手绘签名组件

- 代码如下:
<template>
<view class="content">
<button type="primary" @tap="doss">点击签名1</button>
<button type="primary" @tap="doss2">点击签名2</button>
<view class="imgs">
<image class="img" :src="img1" mode="widthFix"></image>
<image class="img" :src="img2" mode="widthFix"></image>
</view>
<catSignature canvasId="canvas1" @close="close" @save="save" :visible="isShow" />
<catSignature canvasId="canvas2" @close="close2" @save="save2" :visible="isShow2" />
</view>
</template>
<script>
import catSignature from "@/components/cat-signature/cat-signature.vue"
export default {
components:{catSignature},
data() {
return {
img1:'',
img2:'',
isShow:false,
isShow2:false,
}
},
onLoad() {
},
methods: {
doss(){
this.isShow = true;
},
close(){
this.isShow = false;
},
save(val){
this.isShow = false;
this.img1 = val
},
doss2(){
this.isShow2 = true;
},
close2(){
this.isShow2 = false;
},
save2(val){
this.isShow2 = false;
this.img2 = val
},
}
}
</script>
<style>
.imgs{width: 500upx;height: 500upx;display: flex;margin: 0 auto;flex-wrap: wrap;}
.imgs img{width: 100%; height: 100%;}
</style>
- 插件代码:
<template>
<view v-if="visibleSync" class="cat-signature" :class="{'visible':show}" @touchmove.stop.prevent="moveHandle">
<view class="mask" @tap="close" />
<view class="content">
<canvas class='firstCanvas' :canvas-id="canvasId" @touchmove='move' @touchstart='start($event)' @touchend='end'
@touchcancel='cancel' @longtap='tap' disable-scroll='true' @error='error' />
<view class="btns">
<view class="btn" @tap="clear">清除</view>
<view class="btn" @tap="save">保存</view>
</view>
</view>
</view>
</template>
<script>
var content = null;
var touchs = [];
var canvasw = 0;
var canvash = 0;
//获取系统信息
uni.getSystemInfo({
success: function(res) {
canvasw = res.windowWidth;
canvash = canvasw * 9 / 16;
},
})
export default{
name:'cat-signature',
props:{
visible: {
type: Boolean,
default: false
},
canvasId:{
type: String,
default: 'firstCanvas'
}
},
data(){
return{
show:false,
visibleSync: false,
signImage:'',
hasDh:false,
}
},
watch:{
visible(val) {
this.visibleSync = val;
this.show = val;
this.getInfo()
}
},
created(options) {
this.visibleSync = this.visible
this.getInfo()
setTimeout(() => {
this.show = this.visible;
}, 100)
},
methods:{
getInfo(){
//获得Canvas的上下文
content = uni.createCanvasContext(this.canvasId,this)
//设置线的颜色
content.setStrokeStyle("#000")
//设置线的宽度
content.setLineWidth(5)
//设置线两端端点样式更加圆润
content.setLineCap('round')
//设置两条线连接处更加圆润
content.setLineJoin('round')
},
//
close() {
this.show = false;
this.hasDh = false;
this.$emit('close')
},
moveHandle(){
},
// 画布的触摸移动开始手势响应
start(e){
let point = {
x: e.touches[0].x,
y: e.touches[0].y,
}
touchs.push(point);
this.hasDh = true
},
// 画布的触摸移动手势响应
move: function(e) {
let point = {
x: e.touches[0].x,
y: e.touches[0].y
}
touchs.push(point)
if (touchs.length >= 2) {
this.draw(touchs)
}
},
// 画布的触摸移动结束手势响应
end: function(e) {
//清空轨迹数组
for (let i = 0; i < touchs.length; i++) {
touchs.pop()
}
},
// 画布的触摸取消响应
cancel: function(e) {
// console.log("触摸取消" + e)
},
// 画布的长按手势响应
tap: function(e) {
// console.log("长按手势" + e)
},
error: function(e) {
// console.log("画布触摸错误" + e)
},
//绘制
draw: function(touchs) {
let point1 = touchs[0]
let point2 = touchs[1]
// console.log(JSON.stringify(touchs))
content.moveTo(point1.x, point1.y)
content.lineTo(point2.x, point2.y)
content.stroke()
content.draw(true);
touchs.shift()
},
//清除操作
clear: function() {
//清除画布
content.clearRect(0, 0, canvasw, canvash)
content.draw(true)
// this.close()
this.hasDh = false;
this.$emit('clear')
},
save(){
var that = this;
if(!this.hasDh){
uni.showToast({title:'请先签字',icon:'none'})
return;
}
uni.showLoading({title:'生成中...',mask:true})
setTimeout(()=>{
uni.canvasToTempFilePath({
canvasId: this.canvasId,
success: function(res) {
that.signImage = res.tempFilePath;
that.$emit('save',res.tempFilePath);
uni.hideLoading()
that.hasDh = false;
that.show = false;
},
fail:function(err){
console.log(err)
uni.hideLoading()
}
},this)
},100)
}
}
}
</script>
<style lang="scss">
.cat-signature.visible {
visibility: visible
}
.cat-signature{
display: block;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
z-index: 11;
height: 100vh;
visibility: hidden;
.mask{display: block;opacity: 0;position: absolute;top: 0;left: 0;width: 100%;height: 100%;background: rgba(0, 0, 0, .4);transition: opacity .3s;}
.content{display: block;position: absolute;top: 0;left: 0;bottom:0;right: 0;margin: auto; width:94%;height: 500upx;background: #fff;border-radius: 8upx;box-shadow: 0px 3px 3px #333;
// canvas
.firstCanvas {background-color: #fff;width: 100%;height: 400upx;}
// canvas
.btns{padding: 0 15px;height: 100upx;overflow: hidden; position: absolute;bottom: 10upx;left: 0;right: 0;margin: auto;display: flex;justify-content: space-between;
.btn{width: 40%;text-align: center;font-size: 28upx;height:60upx;line-height: 60upx;background-color: #999;color: #fff;border-radius: 6upx;}
}
}
}
.visible .mask {
display: block;
opacity: 1
}
</style>
【uniapp 开发】手绘签名组件的更多相关文章
- Blazor组件自做二 : 使用JS隔离制作手写签名组件
Blazor组件自做二 : 使用JS隔离制作手写签名组件 本文相关参考链接 JavaScript 模块中的 JavaScript 隔离 Viewer.js工程 Blazor组件自做一 : 使用JS隔离 ...
- Microsoft Tech Summit 2018 课程简述:利用 Windows 新特性开发出更好的手绘视频应用
概述 Microsoft Tech Summit 2018 微软技术暨生态大会将于10月24日至27日在上海世博中心举行,这也会是国内举办的最后一届 Tech Summit,2019 年开始会以 Mi ...
- uni-app通过canvas实现手写签名
分享一个uni-app实现手写签名的方法 具体代码如下: <template> <view > <view class="title">请在下面 ...
- Blazor组件自做四 : 使用JS隔离封装signature_pad签名组件
运行截图 演示地址 响应式演示 感谢szimek写的棒棒的signature_pad.js项目, 来源: https://github.com/szimek/signature_pad 正式开始 1. ...
- uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤
uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤 这里以vant-weapp为例 uni-app官方文档介绍引入组件的方法 1. 新建相关目录 根目录下创建 wxcomponen ...
- uni-app开发踩坑记录
大部分问题是我在h5端看不到而在android.iOS平台上暴露出来的,不包含小程序 1.:class="['defaultStyle', dynamicStyle]" 不支持直接 ...
- Ionic5手写签名SignaturePad
测试程序下载:https://hanzhe.lanzous.com/itt47kncw3a 初始化项目 1. 首先新建一个Ionic5的项目: ionic start test-1 blank 2. ...
- CVPR2020论文解读:手绘草图卷积网络语义分割
CVPR2020论文解读:手绘草图卷积网络语义分割 Sketch GCN: Semantic Sketch Segmentation with Graph Convolutional Networks ...
- Android基于mAppWidget实现手绘地图(一)--简介
http://lemberg.github.io/mappwidget/user_guide.html 最近在看一些导游类应用,发现一些景区的导览图使用的完全是自定义地图,也就是手绘地图.这种小范围使 ...
随机推荐
- 跨越DDD从理论到工程落地的鸿沟
摘要:本文从DDD的核心概念讲起,重点放在如何把理论落地成代码,期望给那些正在探索DDD的同学一些指引和启发. 本文分享自华为云社区<跨越DDD从理论到工程落地的鸿沟>,作者:敏捷小智. ...
- CSS简介,基础选择器,字体属性,文本属性
欢迎大家去博客冰山一树Sankey,浏览效果更好.直接右上角搜索该标题即可 博客园主页:博客园主页-冰山一树Sankey CSDN主页:CSDN主页-冰山一树Sankey 前端学习:学习地址:黑马程序 ...
- webgl未使用独立显卡报告
0x00 楔子 最近客户在使用我们的的三维可视化平台的时候,总是会出现浏览器崩溃,webgl context lost的情况. 这种请一般出现在电脑配置很低的电脑上,比如没有独显,只有很小现存的集成显 ...
- 重磅 | 腾讯云服务网格开源项目 Aeraki Mesh 加入 CNCF 云原生全景图
作者 赵化冰,腾讯云工程师,Aeraki Mesh 创始人,Istio member,Envoy contributor,目前负责 Tencent Cloud Mesh 研发工作. 摘要 近日,腾讯云 ...
- 从此 Typora 代码块有了颜色
起因 平时喜欢用typora记笔记,但是typora默认代码块没有指定语言,没有高亮看着很不舒服,所以用Autohotkey花了半天写了个脚本,按自己的快捷键就可以自动生成代码块并添加语言,这样就方便 ...
- 前端面试题(js)
js 基础面试题 css 面试题 js 面试题 JavaScript 有几种类型的值?,你能画一下他们的内存图吗 原始数据类型(Undefined,Null,Boolean,Number.String ...
- rancher接管已部署的集群
1.选择一台服务器部署rancher服务 docker pull rancher/rancher:v2.5.6 #拉取rancher镜像 docker run --privileged -d -v / ...
- .Net Core 实现账户充值,还款,用户登录(WebApi的安全)
个人未开通网站: http://justin1107.pc.evyundata.cn/vip_justin1107.html Api using System; using System.Collec ...
- phpstorm+xdebug调试详细教程
对于PHP开发,初来咋到,开发环境的搭建和理解感觉是最烦人的一件事了.不像JAVA,打开一个Eclipse就可以开搞,Php的Debug都要几个插件来配合工作.这些都是死的,好说.但是对于Xdebug ...
- Kafka 消费者是否可以消费指定分区消息?
Kafa consumer消费消息时,向broker发出fetch请求去消费特定分区的消息,consumer指定消息在日志中的偏移量(offset),就可以消费从这个位置开始的消息,customer拥 ...