当前位置:首页 > VUE

vue实现页面手写

2026-01-14 23:29:39VUE

Vue 实现手写功能

在 Vue 中实现手写功能通常需要使用 HTML5 的 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;
    },
    startDrawing(e) {
      this.isDrawing = true;
      const canvas = this.$refs.canvas;
      const rect = canvas.getBoundingClientRect();
      this.lastX = e.clientX - rect.left;
      this.lastY = e.clientY - rect.top;
    },
    draw(e) {
      if (!this.isDrawing) return;
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      const rect = canvas.getBoundingClientRect();
      const currentX = e.clientX - rect.left;
      const currentY = e.clientY - rect.top;

      ctx.beginPath();
      ctx.moveTo(this.lastX, this.lastY);
      ctx.lineTo(currentX, currentY);
      ctx.strokeStyle = '#000000';
      ctx.lineWidth = 2;
      ctx.stroke();

      this.lastX = currentX;
      this.lastY = currentY;
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    clearCanvas() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      ctx.clearRect(0, 0, canvas.width, canvas.height);
    },
  },
};
</script>

优化手写体验

为了提升手写体验,可以增加以下优化:

  • 平滑线条:使用贝塞尔曲线替代直线连接点,使线条更平滑。
  • 压力敏感:根据鼠标移动速度调整线条粗细。
  • 触摸支持:确保在移动设备上也能正常使用。
draw(e) {
  if (!this.isDrawing) return;
  const canvas = this.$refs.canvas;
  const ctx = canvas.getContext('2d');
  const rect = canvas.getBoundingClientRect();
  const currentX = e.clientX - rect.left;
  const currentY = e.clientY - rect.top;

  // 计算两点之间的距离,用于动态调整线条粗细
  const distance = Math.sqrt(
    Math.pow(currentX - this.lastX, 2) + 
    Math.pow(currentY - this.lastY, 2)
  );
  const lineWidth = Math.min(10, Math.max(1, distance / 2));

  ctx.beginPath();
  ctx.moveTo(this.lastX, this.lastY);
  // 使用二次贝塞尔曲线平滑线条
  ctx.quadraticCurveTo(
    this.lastX, 
    this.lastY, 
    (currentX + this.lastX) / 2, 
    (currentY + this.lastY) / 2
  );
  ctx.strokeStyle = '#000000';
  ctx.lineWidth = lineWidth;
  ctx.lineCap = 'round';
  ctx.lineJoin = 'round';
  ctx.stroke();

  this.lastX = currentX;
  this.lastY = currentY;
}

保存手写内容

可以将 Canvas 内容保存为图片,或转换为数据供后续使用。

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

响应式调整

确保 Canvas 大小随窗口变化自动调整。

vue实现页面手写

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

通过以上步骤,可以在 Vue 中实现一个功能完整的手写组件,支持绘制、清除和保存操作,并在不同设备上提供良好的用户体验。

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

相关文章

vue实现input

vue实现input

Vue 实现 Input 输入框的方法 基本双向绑定 使用 v-model 指令实现双向数据绑定,适用于大多数表单场景: <template> <input v-model="…

vue翻页实现

vue翻页实现

Vue翻页实现方法 基础分页组件实现 使用v-for和计算属性实现基础分页逻辑: <template> <div> <ul> <li…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…

vue实现登录认证

vue实现登录认证

Vue 登录认证实现方法 使用 Vue Router 和 Vuex 管理登录状态 在 Vue 项目中,通常使用 Vue Router 进行路由管理,Vuex 进行状态管理。登录认证的核心是验证用户身份…