記錄一下手簽功能代碼
<template>
<div class="signName" :style="{ top: 0, left: differ + 'px'}">
<div class="close" @click="close">X</div>
<div class="autographBox">
<div ref="canvasHW">
<canvas @touchstart="touchStart($event)" @touchmove="touchMove($event)" @touchend="touchEnd($event)" ref="canvasF" />
</div>
<p v-if="!isDraw">請(qǐng)本人簽名</p>
</div>
<div class="btn-box">
<div @click="overwrite">重簽</div>
<div @click="seaveImages">確定</div>
</div>
</div>
</template>
<script>
import { ref, onMounted } from 'vue'
export default {
name: 'sign-canvas',
setup(props, cxt) {
let differ = ref(document.documentElement.clientWidth)
let canvasF = ref(null)
let canvasHW = ref(null)
let canvasTxt = null // 畫布
let points = [] // 記錄點(diǎn)
let isDraw = ref(false) // 簽名標(biāo)記
let startX = 0 // 開始坐標(biāo)x
let startY = 0 // 開始坐標(biāo)y
let moveY = 0
let moveX = 0
let strDataURI = '' // 保存的canvas圖像
onMounted(() =>{
let canvas = canvasF.value
canvas.height = canvasHW.value.offsetHeight
canvas.width = canvasHW.value.offsetWidth
canvasTxt = canvas.getContext('2d')
canvasTxt.strokeStyle = '#333'
canvasTxt.lineWidth = '3'
})
const touchStart = (ev) => {
ev = ev || event
ev.preventDefault()
if (ev.touches.length == 1) {
isDraw.value = true // 簽名標(biāo)記
let obj = {
x: ev.targetTouches[0].clientY,
y: differ.value - ev.targetTouches[0].clientX - (differ.value * 0.08)
} // y的計(jì)算值中:document.body.offsetHeight*0.5代表的是除了整個(gè)畫板signatureBox剩余的高盼忌,this.$refs.canvasHW.offsetHeight*0.1是畫板中標(biāo)題的高
startX = obj.x
startY = obj.y
canvasTxt.beginPath() // 開始作畫
points.push(obj) // 記錄點(diǎn)
}
}
const touchMove = (ev)=> {
ev = ev || event
ev.preventDefault()
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientY,
y: differ.value- ev.targetTouches[0].clientX - (differ.value * 0.08)
}
moveY = obj.y
moveX = obj.x
canvasTxt.moveTo(startX, startY) // 移動(dòng)畫筆
canvasTxt.lineTo(obj.x, obj.y) // 創(chuàng)建線條
canvasTxt.stroke() // 畫線
startY = obj.y
startX = obj.x
points.push(obj) // 記錄點(diǎn)
}
}
const touchEnd = (ev)=> {
ev = ev || event
ev.preventDefault()
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientY,
y: differ.value- ev.targetTouches[0].clientX - (differ.value * 0.08)
}
points.push(obj) // 記錄點(diǎn)
points.push({ x: -1, y: -1 }) // 記錄點(diǎn)
canvasTxt.closePath() // 收筆
canvasTxt.fill()
}
}
const overwrite = ()=> {
canvasTxt.clearRect(
0,
0,
canvasF.value.width,
canvasF.value.height
)
points = []
isDraw.value = false // 簽名標(biāo)記
}
function seaveImages() {
if (isDraw.value) {
strDataURI = canvasF.value.toDataURL('image/png')
cxt.emit('autographConfirm', {
baseCode:strDataURI,
})
}
}
function close(){
cxt.emit('close', {
closeFlag:false,
});
}
return {
differ,
canvasF,
canvasHW,
isDraw,
touchStart,
touchMove,
touchEnd,
overwrite,
seaveImages,
close
}
}
}
</script>
<style scoped lang="scss">
.signName {
position: fixed;
height: 100vw;
width: 100vh;
background-color: #fff;
transform: rotate(90deg);
-webkit-transform: rotate(90deg);
transform-origin: left top;
z-index: 1000;
display: flex;
flex-direction: column;
p {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
font-size: 4rem;
font-weight: bolder;
color:#436CDF;
opacity: 0.1;
}
}
.btn-box {
height: 12%;
display: flex;
align-items: center;
padding-left: 2rem;
div {
width: 5rem;
height: 60%;
border: 1px solid #D10021;
display: flex;
align-items: center;
justify-content: center;
color: #D10021;
border-radius: .4rem;
}
div:last-child {
margin-left: 2rem;
background: linear-gradient(180deg, #D10021 0%, #AE000E 100%);
color: #fff;
}
}
.close {
width: 100%;
height: 8%;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: right;
padding-right: 2rem;
}
.autographBox {
flex: 1;
background: rgba(227, 227, 227, 1);
border-top: 4px solid rgba(212, 212, 212, 1);
border-bottom: 4px solid rgba(212, 212, 212, 1);
div {
height: 100%;
}
}
</style>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者