手写签名

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> <script setup>
import { ref, reactive, watch, getCurrentInstance } from 'vue' import { ref, reactive, watch, getCurrentInstance } from 'vue'
import { useMemberStore } from '@/stores'
const emits = defineEmits(['update:modelValue', 'complete']) const emits = defineEmits(['update:modelValue', 'complete'])
const props = defineProps({ const props = defineProps({
@ -57,12 +58,82 @@ let hasDrawn = ref(false)
// //
const showToast = ref(false) const showToast = ref(false)
const toastMsg = ref('') 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) => { const showRotateToast = (msg) => {
toastMsg.value = msg toastMsg.value = msg
showToast.value = true showToast.value = true
setTimeout(() => (showToast.value = false), 2000) 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) => { const touchstart = (e) => {
let startX = e.changedTouches[0].x let startX = e.changedTouches[0].x
@ -101,8 +172,7 @@ const touchend = () => {
// //
const clear = () => { const clear = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
uni uni.createSelectorQuery()
.createSelectorQuery()
.in(_this) .in(_this)
.select('#mycanvas') .select('#mycanvas')
.fields({ size: true, rect: true }, (data) => { .fields({ size: true, rect: true }, (data) => {
@ -112,13 +182,16 @@ const clear = () => {
} }
const { width, height } = data const { width, height } = data
canvaCtx.clearRect(0, 0, width, height) canvaCtx.clearRect(0, 0, width, height)
canvaCtx.draw(true) canvaCtx.draw(false, () => {
hasDrawn.value = false drawBackground(canvaCtx, width, height) //
resolve() hasDrawn.value = false
resolve()
})
}) })
.exec() .exec()
}) })
} }
// //
const confirm = () => { const confirm = () => {
if (!hasDrawn.value) { if (!hasDrawn.value) {