手写签名

This commit is contained in:
bb_pan 2025-08-29 19:32:56 +08:00
parent b57ae2a71b
commit 66a8af58e8
1 changed files with 78 additions and 5 deletions

View File

@ -38,6 +38,7 @@
<script setup>
import { ref, reactive, watch, getCurrentInstance } from 'vue'
import { useMemberStore } from '@/stores'
const emits = defineEmits(['update:modelValue', 'complete'])
const props = defineProps({
@ -57,12 +58,82 @@ let hasDrawn = ref(false)
//
const showToast = ref(false)
const toastMsg = ref('')
const memberStore = useMemberStore()
console.log('🚀 ~ memberStore:', memberStore.userInfo.nickName)
watch(
() => props.modelValue,
(val) => {
if (val) {
// canvas
setTimeout(() => {
uni.createSelectorQuery()
.in(_this)
.select('#mycanvas')
.fields({ size: true, rect: true }, (data) => {
if (data) {
const { width, height } = data
drawBackground(canvaCtx, width, height)
}
})
.exec()
}, 50) // 30~100ms
}
}
)
const showRotateToast = (msg) => {
toastMsg.value = msg
showToast.value = true
setTimeout(() => (showToast.value = false), 2000)
}
// +
const drawBackground = (ctx, width = 100, height = 100) => {
ctx.save()
ctx.setStrokeStyle('#e0e0e0')
ctx.setLineWidth(1)
const gridSize = 120
//
for (let y = gridSize; y < height; y += gridSize) {
ctx.moveTo(0, y)
ctx.lineTo(width, y)
}
for (let x = gridSize; x < width; x += gridSize) {
ctx.moveTo(x, 0)
ctx.lineTo(x, height)
}
//
for (let x = 0; x < width; x += gridSize) {
for (let y = 0; y < height; y += gridSize) {
ctx.moveTo(x, y)
ctx.lineTo(x + gridSize, y + gridSize)
ctx.moveTo(x + gridSize, y)
ctx.lineTo(x, y + gridSize)
}
}
ctx.stroke()
//
ctx.setFontSize(160)
ctx.setFillStyle('rgba(180,180,180,0.25)')
ctx.setTextAlign('center')
ctx.setTextBaseline('middle')
ctx.translate(width / 2, height / 2)
ctx.rotate(Math.PI / 2)
ctx.fillText(memberStore.userInfo.nickName, 0, 0)
ctx.restore()
// draw()
ctx.draw(false, () => {
console.log('背景绘制完成')
})
}
//
const touchstart = (e) => {
let startX = e.changedTouches[0].x
@ -101,8 +172,7 @@ const touchend = () => {
//
const clear = () => {
return new Promise((resolve, reject) => {
uni
.createSelectorQuery()
uni.createSelectorQuery()
.in(_this)
.select('#mycanvas')
.fields({ size: true, rect: true }, (data) => {
@ -112,13 +182,16 @@ const clear = () => {
}
const { width, height } = data
canvaCtx.clearRect(0, 0, width, height)
canvaCtx.draw(true)
canvaCtx.draw(false, () => {
drawBackground(canvaCtx, width, height) //
hasDrawn.value = false
resolve()
})
})
.exec()
})
}
//
const confirm = () => {
if (!hasDrawn.value) {