246 lines
5.7 KiB
Vue
246 lines
5.7 KiB
Vue
<template>
|
||
<view class="sign">
|
||
<head-view title="绘制质损图"></head-view>
|
||
<view class="containe contentFixedr">
|
||
<view class="sign-box">
|
||
<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
|
||
@touchend="touchend" disable-scroll="true"></canvas>
|
||
<view class="canvasBg">
|
||
<image src="../../static/images/zs2.png" mode=""></image>
|
||
</view>
|
||
</view>
|
||
<view class="sigh-btns">
|
||
<van-button class="btn" type="default" @tap="handleCancel">取消</van-button>
|
||
<van-button class="btn" type="default" @tap="handleReset">重写</van-button>
|
||
<van-button class="btn" type="default" @tap="handleConfirm">确认</van-button>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
<script>
|
||
import {
|
||
pathToBase64,
|
||
base64ToPath
|
||
} from '../../js_sdk/mmmm-image-tools/index.js'
|
||
import utils from '@/common/util.js';
|
||
var x = 20;
|
||
var y = 20;
|
||
var tempPoint = []; //用来存放当前画纸上的轨迹点
|
||
var id = 0;
|
||
var type = '';
|
||
let that;
|
||
export default {
|
||
data() {
|
||
return {
|
||
ctx: '', //绘图图像
|
||
points: [], //路径点集合,
|
||
width: 0,
|
||
height: 0,
|
||
loginObj: {},
|
||
signImg: "",
|
||
url: "",
|
||
};
|
||
},
|
||
onLoad(option) {
|
||
that = this;
|
||
id = option.id;
|
||
type = option.type;
|
||
this.init()
|
||
this.loginObj = uni.getStorageSync('loginObj')
|
||
},
|
||
methods: {
|
||
init() {
|
||
this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象
|
||
//设置画笔样式
|
||
this.ctx.lineWidth = 4;
|
||
this.ctx.lineCap = 'round';
|
||
this.ctx.lineJoin = 'round';
|
||
var that = this
|
||
uni.getImageInfo({
|
||
src: '../../static/images/zs2.png',
|
||
success(res1) {
|
||
//填充白色背景图片
|
||
that.ctx.drawImage('../../static/images/zs2.png', 0, 0, 624, 224);
|
||
let pattern = that.ctx.createPattern(img, 'no-repeat')
|
||
that.ctx.fillStyle = pattern;
|
||
},
|
||
fail() {
|
||
console.log("fail");
|
||
}
|
||
});
|
||
},
|
||
//触摸开始,获取到起点
|
||
touchstart: function(e) {
|
||
let startX = e.changedTouches[0].x;
|
||
let startY = e.changedTouches[0].y;
|
||
let startPoint = {
|
||
X: startX / 1.75,
|
||
Y: startY / 1.75
|
||
};
|
||
|
||
/* **************************************************
|
||
#由于uni对canvas的实现有所不同,这里需要把起点存起来
|
||
* **************************************************/
|
||
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 / 1.75,
|
||
Y: moveY / 1.75
|
||
};
|
||
this.points.push(movePoint); //存点
|
||
let len = this.points.length;
|
||
if (len >= 2) {
|
||
this.draw(); //绘制路径
|
||
}
|
||
tempPoint.push(movePoint);
|
||
},
|
||
|
||
// 触摸结束,将未绘制的点清空防止对后续路径产生干扰
|
||
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.strokeStyle = 'red';
|
||
this.ctx.lineWidth = 2;
|
||
this.ctx.moveTo(point1.X, point1.Y);
|
||
this.ctx.lineTo(point2.X, point2.Y);
|
||
this.ctx.stroke();
|
||
this.ctx.draw(true);
|
||
},
|
||
|
||
handleCancel() {
|
||
uni.navigateBack({
|
||
delta: 1
|
||
});
|
||
},
|
||
|
||
//清空画布
|
||
handleReset: function() {
|
||
var that = this
|
||
that.ctx.clearRect(0, 0, 624, 224);
|
||
that.ctx.draw(true);
|
||
tempPoint = [];
|
||
that.init()
|
||
},
|
||
|
||
//将签名笔迹上传到服务器,并将返回来的地址存到本地
|
||
handleConfirm: function() {
|
||
let that = this
|
||
uni.canvasToTempFilePath({
|
||
canvasId: 'mycanvas',
|
||
success: function(e) {
|
||
const myPromise = new Promise((resolve, reject) => {
|
||
// 执行异步操作
|
||
// 当操作完成时调用resolve()或reject()
|
||
pathToBase64(e.tempFilePath).then(path => {
|
||
that.url = path
|
||
uni.setStorageSync('hzzsImg', that.url)
|
||
uni.navigateBack({
|
||
delta: 1
|
||
});
|
||
})
|
||
.catch(error => {
|
||
console.error(error)
|
||
})
|
||
});
|
||
|
||
}
|
||
});
|
||
},
|
||
base64ToFile(base64, name) {
|
||
if (typeof base64 != 'string') {
|
||
return;
|
||
}
|
||
var arr = base64.split(',')
|
||
var type = arr[0].match(/:(.*?);/)[1]
|
||
var fileExt = type.split('/')[1]
|
||
var bstr = atob(arr[1])
|
||
var n = bstr.length
|
||
var u8arr = new Uint8Array(n)
|
||
while (n--) {
|
||
u8arr[n] = bstr.charCodeAt(n)
|
||
}
|
||
return new File([u8arr], `${name}.` + fileExt, {
|
||
type: type
|
||
})
|
||
},
|
||
}
|
||
};
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.container {
|
||
height: calc(100vh - 68px);
|
||
padding-bottom: 30px;
|
||
background-color: #fff;
|
||
}
|
||
|
||
.sign-box {
|
||
display: flex;
|
||
justify-content: center;
|
||
position: relative;
|
||
margin-top: 48px;
|
||
}
|
||
|
||
.sign-view {
|
||
height: 100%;
|
||
}
|
||
|
||
.sigh-btns {
|
||
display: flex;
|
||
justify-content: center;
|
||
}
|
||
|
||
.btn {
|
||
margin: 100px 10px 0;
|
||
}
|
||
|
||
/deep/.van-button {
|
||
width: 100px;
|
||
height: 50px;
|
||
background-color: #fff;
|
||
}
|
||
|
||
.mycanvas {
|
||
margin: auto 0rpx;
|
||
background-color: transparent;
|
||
margin-top: 130px;
|
||
width: 624px;
|
||
height: 224px;
|
||
z-index: 999;
|
||
transform: scale(1.75)
|
||
}
|
||
|
||
.canvasBg {
|
||
width: 624px;
|
||
height: 224px;
|
||
position: absolute;
|
||
top: 130px;
|
||
transform: scale(1.75)
|
||
}
|
||
|
||
.canvsborder {
|
||
border: 1rpx solid #333;
|
||
position: fixed;
|
||
top: 0;
|
||
left: 10000rpx;
|
||
}
|
||
</style> |