当前位置:首页 > VUE

vue实现页面手写

2026-02-11 02:31:01VUE

Vue 实现页面手写功能

使用 Canvas 实现手写板

在 Vue 中可以通过 Canvas 实现手写功能。创建一个组件,包含 Canvas 元素,监听鼠标或触摸事件来绘制路径。

<template>
  <div>
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="stopDrawing"
      @touchstart="startDrawing"
      @touchmove="draw"
      @touchend="stopDrawing"
    ></canvas>
    <button @click="clearCanvas">清除</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0,
    };
  },
  mounted() {
    this.setupCanvas();
  },
  methods: {
    setupCanvas() {
      const canvas = this.$refs.canvas;
      canvas.width = canvas.offsetWidth;
      canvas.height = canvas.offsetHeight;
      this.ctx = canvas.getContext('2d');
      this.ctx.strokeStyle = '#000';
      this.ctx.lineWidth = 2;
      this.ctx.lineCap = 'round';
    },
    startDrawing(e) {
      this.isDrawing = true;
      const pos = this.getPosition(e);
      this.lastX = pos.x;
      this.lastY = pos.y;
    },
    draw(e) {
      if (!this.isDrawing) return;
      const pos = this.getPosition(e);
      this.ctx.beginPath();
      this.ctx.moveTo(this.lastX, this.lastY);
      this.ctx.lineTo(pos.x, pos.y);
      this.ctx.stroke();
      this.lastX = pos.x;
      this.lastY = pos.y;
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    clearCanvas() {
      this.ctx.clearRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);
    },
    getPosition(e) {
      const canvas = this.$refs.canvas;
      const rect = canvas.getBoundingClientRect();
      return {
        x: (e.clientX || e.touches[0].clientX) - rect.left,
        y: (e.clientY || e.touches[0].clientY) - rect.top
      };
    }
  }
};
</script>

<style>
canvas {
  border: 1px solid #000;
  background-color: #fff;
  cursor: crosshair;
}
</style>

保存手写内容为图片

可以将 Canvas 绘制的内容转换为图片并保存或上传。

methods: {
  saveAsImage() {
    const canvas = this.$refs.canvas;
    const image = canvas.toDataURL('image/png');
    const link = document.createElement('a');
    link.href = image;
    link.download = 'signature.png';
    link.click();
  }
}

使用第三方库优化体验

对于更复杂的手写需求,可以使用第三方库如 Signature Pad:

vue实现页面手写

安装:

npm install signature_pad

使用:

vue实现页面手写

<template>
  <div>
    <canvas ref="canvas"></canvas>
    <button @click="clear">清除</button>
    <button @click="save">保存</button>
  </div>
</template>

<script>
import SignaturePad from 'signature_pad';

export default {
  mounted() {
    this.signaturePad = new SignaturePad(this.$refs.canvas);
  },
  methods: {
    clear() {
      this.signaturePad.clear();
    },
    save() {
      const data = this.signaturePad.toDataURL('image/png');
      console.log(data);
    }
  }
};
</script>

响应式处理

确保 Canvas 在窗口大小变化时保持正确尺寸:

mounted() {
  this.setupCanvas();
  window.addEventListener('resize', this.handleResize);
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.setupCanvas();
  }
}

移动端适配

对于移动设备,需要额外处理触摸事件和防止页面滚动:

methods: {
  preventScrolling(e) {
    e.preventDefault();
  }
},
mounted() {
  this.$refs.canvas.addEventListener('touchmove', this.preventScrolling, { passive: false });
}

以上方法提供了在 Vue 中实现手写功能的基本方案,可以根据实际需求进行调整和扩展。

标签: 页面vue
分享给朋友:

相关文章

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本流程 在Vue单页面应用中实现登录功能,通常需要以下几个关键步骤: 创建登录表单组件 使用Vue的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 <…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <labe…

vue nexttick 实现

vue nexttick 实现

Vue nextTick 实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它主要用于确保在 DOM 更新完成后执行某些操作。 核心实现机制 ne…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <te…

vue实现展开

vue实现展开

展开功能的实现方法 在Vue中实现展开功能可以通过多种方式完成,常见的有使用v-show、v-if指令,结合CSS过渡动画或第三方组件库。 使用v-show控制显示隐藏 通过v-show指令可以简单…