欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 实现手机手势签字功能

实现手机手势签字功能

2025/3/15 21:16:03 来源:https://blog.csdn.net/preoccupied_/article/details/146264826  浏览:    关键词:实现手机手势签字功能
1、创建Canvas组件

在src/components目录下创建一个新的组件文件SignatureCanvas.vue:

<template><div><canvasref="canvas"@mousedown="startDrawing"@mousemove="draw"@mouseup="stopDrawing"@mouseleave="stopDrawing"@touchstart="startDrawing"@touchmove="draw"@touchend="stopDrawing"></canvas></div>
</template><script lang="ts">
import { defineComponent, ref, onMounted } from 'vue';export default defineComponent({name: 'SignatureCanvas',setup() {const canvas = ref<HTMLCanvasElement | null>(null);let ctx: CanvasRenderingContext2D | null = null;let isDrawing = false;onMounted(() => {if (canvas.value) {ctx = canvas.value.getContext('2d');if (ctx) {ctx.lineWidth = 2;ctx.lineCap = 'round';ctx.strokeStyle = '#000';}}});const startDrawing = (event: MouseEvent | TouchEvent) => {isDrawing = true;const { offsetX, offsetY } = getEventPosition(event);if (ctx) {ctx.beginPath();ctx.moveTo(offsetX, offsetY);}};const draw = (event: MouseEvent | TouchEvent) => {if (!isDrawing) return;const { offsetX, offsetY } = getEventPosition(event);if (ctx) {ctx.lineTo(offsetX, offsetY);ctx.stroke();}};const stopDrawing = () => {isDrawing = false;if (ctx) {ctx.closePath();}};const getEventPosition = (event: MouseEvent | TouchEvent) => {if (canvas.value) {const rect = canvas.value.getBoundingClientRect();if (event instanceof TouchEvent) {const touch = event.touches[0];return {offsetX: touch.clientX - rect.left,offsetY: touch.clientY - rect.top,};} else {return {offsetX: event.offsetX,offsetY: event.offsetY,};}}return { offsetX: 0, offsetY: 0 };};return {canvas,startDrawing,draw,stopDrawing,};},
});
</script><style scoped>
canvas {border: 1px solid #000;touch-action: none; /* 防止触摸时页面滚动 */
}
</style>
2、在主组件中使用Canvas组件

在src/App.vue中使用刚刚创建的SignatureCanvas组件:

手机手势签字

效果图

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词