uniapp手写签名

摘要:
签名˂vi

<template>
<view class="">
<button @tap="createCanvas">签名</button>
<view v-show="showCanvas">
<canvas canvas- @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
<view class="footer">
<view @click="finish">保存</view>
<view @click="clear">清除</view>
<view @click="close">关闭</view>
</view>
</view>
</view>
</template>

<script>
var x = 20;
var y =20;
export default {
data() {
return {
showCanvas:false,
ctx:'', //绘图图像
points:[], //路径点集合
signature:''
}
},
methods: {
//关闭并清空画布
close:function(){
this.showCanvas = false;
this.clear();
},
//创建并显示画布
createCanvas:function(){
this.showCanvas = true;
this.ctx = uni.createCanvasContext("mycanvas",this); //创建绘图对象
//设置画笔样式
this.ctx.lineWidth = 4;
this.ctx.lineCap = "round"
this.ctx.lineJoin = "round"
},
//触摸开始,获取到起点
touchstart:function(e){
let startX = e.changedTouches[0].x;
let startY = e.changedTouches[0].y;
let startPoint = {X:startX,Y:startY};
this.points.push(startPoint);
//每次触摸开始,开启新的路径
this.ctx.beginPath();
},
//触摸移动,获取到路径点
touchmove:function(e){
let moveX = e.changedTouches[0].x;
let moveY = e.changedTouches[0].y;
let movePoint = {X:moveX,Y:moveY};
this.points.push(movePoint); //存点
let len = this.points.length;
if(len>=2){
this.draw(); //绘制路径
}
},
// 触摸结束,将未绘制的点清空防止对后续路径产生干扰
touchend:function(){
this.points=[];
},
/* ***********************************************
# 绘制笔迹
# 1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
# 2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
# 3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
************************************************ */
draw: function() {
let point1 = this.points[0]
let point2 = this.points[1]
this.points.shift()
this.ctx.moveTo(point1.X, point1.Y)
this.ctx.lineTo(point2.X, point2.Y)
this.ctx.stroke()
this.ctx.draw(true)
},
//清空画布
clear:function(){
let that = this;
uni.getSystemInfo({
success: function(res) {
let canvasw = res.windowWidth;
let canvash = res.windowHeight;
that.ctx.clearRect(0, 0, canvasw, canvash);
that.ctx.draw(true);
},
})
},
//完成绘画并保存到本地
finish:function(){
let that = this;
uni.canvasToTempFilePath({
canvasId: 'mycanvas',
success: function(res) {
console.log(res)
//上传到服务器
that.api.uploadFile({
url: 'user/upload/one',
filePath: res.tempFilePath,
name: 'file',
success: (uploadFileRes) => {
console.log(uploadFileRes)
that.signature = uploadFileRes.data.url;
that.clear();
that.showCanvas = false;
}
})
//保存到本地
/*
let path = res.tempFilePath;
uni.saveImageToPhotosAlbum({
filePath:path,
}) */
}
})
},
}
}
</script>

<style lang="scss">
.signature {position: fixed;top: 10px;left: 2%;z-index: 999;96%;}
page{
background: #fff;
}
.container {
padding: 20rpx 0 120rpx 0;
box-sizing: border-box;
}
.title{
height:50upx;
line-height: 50upx;
font-size: 16px;
}
.mycanvas{
100%;
height: calc(100vh - 200upx);
background-color: #ECECEC;
}
.footer{
font-size: 14px;
height: 150upx;
display: flex;
justify-content: space-around;
align-items: center;
}
.left,.right,.close{
line-height: 100upx;
height: 100upx;
220upx;
text-align: center;
font-weight: bold;
color: white;
border-radius: 5upx;
}
.left{
background: #007AFF;
}
.right{
background:orange;
}
.close {
background:#A3A3A3;
}
</style>

免责声明:文章转载自《uniapp手写签名》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇297如何用PostMan请求WebApi下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

CSS-顶部滚动进度条

Documentbody{background-image:linear-gradient(torighttop,#f0050%,#ece50%);background-repeat:no-repeat;height:300vh;position:relative;background-size:100%calc(100%-100vh+5px);}body:...

Webstorm快捷键

网店快捷键1.搜索/替换,包括全局搜索和文件搜索。...

数据不平衡的相关

大多数常见的机器学习算法不能很好地处理不平衡的数据集。例如,搜索引擎的点击预测(点击页面往往占很小的比例)、电子商务中的产品推荐(正在购买的推荐产品的比例很低)、信用卡欺诈检测、网络攻击识别、癌症检测等。处理数据不平衡的方法主要有以下几种。2.数据级别2.1重新采样2.1.1欠采样(下采样)欠采样通过减少丰富类的大小来平衡数据集。它试图通过增加稀有样本的数量...

禅道从windows迁移到linux

windows下图片路径/zentao/www/data/upload/1备份到Linux下路径/opt/zbox/app/zentao/www/data/upload/1二、Linux下安装禅道注意一定要安装相同版本的禅道2.1、安装禅道有很多方法,禅道官网也有详细说明,这里主要讲linux用一键安装包及遇到的问题2.2、下载安装包禅道官网下载界面很乱,大...

TabWidget修改tab颜色,自定义样式

您可以通过tabWidget-˃setStyleSheet方法设置样式,包括选项卡样式。...

axios 处理超时问题 记录

前言:记录最近两天处理请求超时的逻辑。...