直接上代码()
<template>
<div class="signSoard">
<div class="signatureBox">
<div class="canvasBox" ref="canvasHW">
<canvas ref="canvasF" @touchstart='touchStart' @touchmove='touchMove' @touchend='touchEnd'></canvas>
<div class="btnBox">
<div @click="overwrite">重写</div>
</div>
<!-- <div class="zw"></div> -->
</div>
</div>
</div>
</template>
<script>
export default {
name:'signSoard',
data() {
return {
stageInfo:'',
imgUrl:'',
points: [],
canvasTxt: null,
startX: 0,
startY: 0,
moveY: 0,
moveX: 0,
endY: 0,
endX: 0,
isDown: false,
isViewAutograph: this.$route.query.isViews > 0,
contractSuccess: this.$route.query.contractSuccess
}
},
mounted() {
let canvas = this.$refs.canvasF
canvas.width = 400
console.log(this.$refs.canvasHW.offsetHeight)
this.canvasTxt = canvas.getContext('2d')
},
methods: {
touchStart(ev) {
this.stageInfo = this.$refs.canvasF.getBoundingClientRect()
ev = ev || event
ev.preventDefault()
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX - this.stageInfo.left,
y: ev.targetTouches[0].clientY - this.stageInfo.top,
}
this.canvasTxt.lineWidth = 2.2;
this.startX = obj.x
this.startY = obj.y
this.points.push(obj)
}
},
touchMove(ev) {
ev = ev || event
ev.preventDefault()
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX - this.stageInfo.left,
y: ev.targetTouches[0].clientY - this.stageInfo.top,
}
this.moveY = obj.y
this.moveX = obj.x
this.canvasTxt.beginPath()
this.canvasTxt.lineWidth = 2.2;
this.canvasTxt.moveTo(this.startX, this.startY)
this.canvasTxt.lineTo(obj.x, obj.y)
this.canvasTxt.stroke()
this.canvasTxt.closePath()
this.startY = obj.y
this.startX = obj.x
this.points.push(obj)
}
this.imgUrl=this.$refs.canvasF.toDataURL();
},
async touchEnd(ev) {
ev = ev || event
ev.preventDefault()
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX - this.stageInfo.left,
y: ev.targetTouches[0].clientY - this.stageInfo.top,
}
this.canvasTxt.beginPath()
this.canvasTxt.lineWidth = 2.2;
this.canvasTxt.moveTo(this.startX, this.startY)
this.canvasTxt.lineTo(obj.x, obj.y)
this.canvasTxt.stroke()
this.canvasTxt.closePath()
this.points.push(obj)
}
this.senfOut(this.$refs.canvasF.toDataURL())
},
async senfOut(base64,width,height){
this.$emit('base64img',(await this.resizedataURL(base64,300,120)).split(',')[1])
},
resizedataURL(datas, wantedWidth, wantedHeight){
return new Promise(async function(resolve,reject){
let img = document.createElement('img');
img.onload = function(){
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
canvas.width = wantedWidth;
canvas.height = wantedHeight;
ctx.drawImage(this, 0, 0, wantedWidth, wantedHeight);
let dataURI = canvas.toDataURL();
resolve(dataURI);
};
img.src = datas;
})
},
overwrite() {
this.canvasTxt.clearRect(0, 0, this.$refs.canvasF.width, this.$refs.canvasF.height)
this.points = []
this.imgUrl=''
this.$emit('base64img','')
},
}
}
</script>
<style lang="scss" scoped>
.signSoard{
min-height: 29vh;
.signatureBox {
width: 100%;
height: calc(100% - 50px);
box-sizing: border-box;
overflow: hidden;
z-index: 100;
border: 1px solid #ddd;
background: #eee;
flex-direction: column;
}
.canvasBox {
box-sizing: border-box;
cancas{
width: 100%;
}
}
.imgCanvas{
background: #fff;
}
.btnBox {
padding: 10px;
text-align: center;
}
}
</style>
也有插件 :官网
|