当前位置:首页 > VUE

用vue实现手绘功能

2026-01-21 02:07:53VUE

实现手绘功能的核心思路

在Vue中实现手绘功能主要依赖HTML5的Canvas API,通过监听鼠标或触摸事件记录轨迹并实时绘制。以下分步骤说明具体实现方法。

创建Canvas画布

在Vue组件的模板中添加Canvas元素,并设置宽高。通过ref获取DOM节点以便后续操作:

<template>
  <canvas 
    ref="canvas"
    @mousedown="startDrawing"
    @mousemove="draw"
    @mouseup="stopDrawing"
    @touchstart="startDrawing"
    @touchmove="draw"
    @touchend="stopDrawing">
  </canvas>
</template>

初始化画布上下文

mounted生命周期中初始化Canvas的2D渲染上下文,并设置默认样式:

mounted() {
  this.canvas = this.$refs.canvas;
  this.ctx = this.canvas.getContext('2d');
  this.canvas.width = window.innerWidth;
  this.canvas.height = window.innerHeight;
  this.ctx.strokeStyle = '#000000';
  this.ctx.lineWidth = 5;
  this.ctx.lineCap = 'round';
}

处理绘制逻辑

定义绘图状态变量和事件处理函数,实现以下功能:

data() {
  return {
    isDrawing: false,
    lastX: 0,
    lastY: 0
  }
},
methods: {
  startDrawing(e) {
    this.isDrawing = true;
    const pos = this.getPosition(e);
    [this.lastX, this.lastY] = [pos.x, 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, this.lastY] = [pos.x, pos.y];
  },

  stopDrawing() {
    this.isDrawing = false;
  },

  getPosition(e) {
    const rect = this.canvas.getBoundingClientRect();
    return {
      x: (e.clientX || e.touches[0].clientX) - rect.left,
      y: (e.clientY || e.touches[0].clientY) - rect.top
    };
  }
}

添加清除功能

扩展功能以支持清除画布:

methods: {
  clearCanvas() {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  }
}

响应式调整画布大小

监听窗口变化事件,动态调整Canvas尺寸:

mounted() {
  window.addEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.canvas.width = window.innerWidth;
    this.canvas.height = window.innerHeight;
  }
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
}

完整组件示例

整合上述代码的完整Vue组件示例:

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

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0
    }
  },
  mounted() {
    this.canvas = this.$refs.canvas;
    this.ctx = this.canvas.getContext('2d');
    this.handleResize();
    window.addEventListener('resize', this.handleResize);
  },
  methods: {
    startDrawing(e) {
      this.isDrawing = true;
      const pos = this.getPosition(e);
      [this.lastX, this.lastY] = [pos.x, 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, this.lastY] = [pos.x, pos.y];
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    clearCanvas() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    },
    getPosition(e) {
      const rect = this.canvas.getBoundingClientRect();
      return {
        x: (e.clientX || e.touches[0].clientX) - rect.left,
        y: (e.clientY || e.touches[0].clientY) - rect.top
      };
    },
    handleResize() {
      this.canvas.width = window.innerWidth;
      this.canvas.height = window.innerHeight;
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  }
}
</script>

扩展功能建议

  • 颜色选择器:添加<input type="color">绑定到ctx.strokeStyle
  • 笔刷粗细:通过滑块控制ctx.lineWidth
  • 撤销功能:使用数组记录绘制路径实现多步撤销
  • 保存图片:通过canvas.toDataURL()导出绘制结果

用vue实现手绘功能

标签: 手绘功能
分享给朋友:

相关文章

vue实现名字查找功能

vue实现名字查找功能

Vue 实现名字查找功能 在 Vue 中实现名字查找功能,可以通过以下方法完成。这里假设有一个名字列表,需要根据用户输入的关键词进行筛选。 数据准备 在 Vue 的 data 中定义名字列表和搜索…

vue 实现筛选功能

vue 实现筛选功能

实现筛选功能的基本思路 在Vue中实现筛选功能通常涉及以下几个核心步骤:数据绑定、筛选逻辑处理、结果展示。以下是具体实现方法。 数据准备 准备需要筛选的数据源,通常是一个数组,可以存储在Vue组件的…

vue实现倒计时功能

vue实现倒计时功能

vue实现倒计时功能 在Vue中实现倒计时功能可以通过多种方式完成,以下是几种常见的方法: 方法一:使用setInterval和clearInterval 创建一个倒计时组件,利用setInt…

vue实现聊天功能

vue实现聊天功能

Vue实现聊天功能的方法 使用WebSocket实现实时通信 安装WebSocket客户端库如socket.io-client,在Vue组件中建立连接。创建一个消息列表和数据输入框,通过WebSock…

h5能实现的功能

h5能实现的功能

H5能实现的功能 H5(HTML5)是一种现代的网页技术标准,具备丰富的功能和特性,适用于多种场景。以下是H5能够实现的主要功能: 多媒体支持 H5原生支持音频和视频播放,无需依赖Flash等插件。…

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个流行的前端框架,用于构建用户界面和单页应用。以下是 Vue 功能实现的关键方法和技术。 数据绑定与响应式 Vue 的核心特性是数据绑定和响应式系统。通过…