当前位置:首页 > VUE

vue实现涂鸦工具

2026-02-10 18:11:01VUE

Vue 实现涂鸦工具的方法

使用 HTML5 Canvas 和 Vue 结合

HTML5 的 <canvas> 元素是实现涂鸦功能的核心,结合 Vue 的数据绑定和事件处理能力,可以轻松构建一个涂鸦工具。

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

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0,
    };
  },
  mounted() {
    this.ctx = this.$refs.canvas.getContext('2d');
    this.resizeCanvas();
    window.addEventListener('resize', this.resizeCanvas);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas);
  },
  methods: {
    resizeCanvas() {
      const canvas = this.$refs.canvas;
      canvas.width = canvas.offsetWidth;
      canvas.height = canvas.offsetHeight;
    },
    startDrawing(e) {
      this.isDrawing = true;
      [this.lastX, this.lastY] = [e.offsetX, e.offsetY];
    },
    draw(e) {
      if (!this.isDrawing) return;
      this.ctx.beginPath();
      this.ctx.moveTo(this.lastX, this.lastY);
      this.ctx.lineTo(e.offsetX, e.offsetY);
      this.ctx.stroke();
      [this.lastX, this.lastY] = [e.offsetX, e.offsetY];
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    clearCanvas() {
      this.ctx.clearRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);
    },
  },
};
</script>

<style>
canvas {
  border: 1px solid #000;
  width: 100%;
  height: 500px;
}
</style>

添加画笔颜色和粗细控制

扩展涂鸦工具的功能,允许用户选择画笔颜色和粗细。

vue实现涂鸦工具

<template>
  <div>
    <div>
      <input type="color" v-model="color" @change="updateBrush">
      <input type="range" v-model="lineWidth" min="1" max="50" @change="updateBrush">
    </div>
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="stopDrawing"
    ></canvas>
    <button @click="clearCanvas">清除</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0,
      color: '#000000',
      lineWidth: 5,
    };
  },
  methods: {
    updateBrush() {
      this.ctx.strokeStyle = this.color;
      this.ctx.lineWidth = this.lineWidth;
      this.ctx.lineCap = 'round';
      this.ctx.lineJoin = 'round';
    },
    // 其他方法保持不变
  },
  mounted() {
    this.ctx = this.$refs.canvas.getContext('2d');
    this.updateBrush();
    this.resizeCanvas();
  },
};
</script>

支持触摸设备

为了让涂鸦工具在移动设备上也能使用,需要添加触摸事件的支持。

vue实现涂鸦工具

<template>
  <div>
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="stopDrawing"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
    ></canvas>
  </div>
</template>

<script>
export default {
  methods: {
    handleTouchStart(e) {
      e.preventDefault();
      const touch = e.touches[0];
      const rect = this.$refs.canvas.getBoundingClientRect();
      this.startDrawing({
        offsetX: touch.clientX - rect.left,
        offsetY: touch.clientY - rect.top,
      });
    },
    handleTouchMove(e) {
      e.preventDefault();
      const touch = e.touches[0];
      const rect = this.$refs.canvas.getBoundingClientRect();
      this.draw({
        offsetX: touch.clientX - rect.left,
        offsetY: touch.clientY - rect.top,
      });
    },
    handleTouchEnd() {
      this.stopDrawing();
    },
  },
};
</script>

保存涂鸦结果

添加功能将涂鸦结果保存为图片。

<template>
  <div>
    <button @click="saveImage">保存图片</button>
  </div>
</template>

<script>
export default {
  methods: {
    saveImage() {
      const link = document.createElement('a');
      link.download = 'drawing.png';
      link.href = this.$refs.canvas.toDataURL('image/png');
      link.click();
    },
  },
};
</script>

使用第三方库

如果需要更复杂的功能,可以考虑使用第三方库如 fabric.jskonva

npm install fabric
<template>
  <canvas ref="canvas"></canvas>
</template>

<script>
import { fabric } from 'fabric';

export default {
  mounted() {
    this.canvas = new fabric.Canvas(this.$refs.canvas, {
      isDrawingMode: true,
    });
  },
};
</script>

通过以上方法,可以在 Vue 中实现一个功能丰富的涂鸦工具,支持绘图、调整画笔属性、触摸操作和保存图片等功能。

标签: 涂鸦工具
分享给朋友:

相关文章

uniapp解包工具

uniapp解包工具

uniapp解包工具推荐 HBuilderX内置解包功能 HBuilderX是uniapp官方开发工具,内置解包功能。在项目目录中找到unpackage文件夹,包含编译后的各平台代码。iOS平台为di…

制作css工具

制作css工具

CSS 工具制作指南 制作 CSS 工具可以帮助开发者更高效地编写和管理样式。以下是几种常见的 CSS 工具类型及其实现方法。 在线 CSS 生成器 创建一个在线工具,允许用户通过界面生成 CSS…

制作css工具

制作css工具

CSS 工具制作指南 制作自定义 CSS 工具可以帮助简化开发流程,提升工作效率。以下是几种常见的 CSS 工具类型及其实现方法: 自定义 CSS 生成器 创建基于用户输入的 CSS 代码生成器,例…

js实现画图工具

js实现画图工具

实现基础画布与绘图功能 使用HTML5的Canvas API创建画布,并通过JavaScript监听鼠标事件实现绘图功能。以下是核心代码示例: const canvas = document.get…

uniapp抓包工具

uniapp抓包工具

uniapp抓包工具推荐 Charles 适用于Windows、macOS和Linux平台,支持HTTP和HTTPS抓包。配置代理后,可捕获uniapp应用的网络请求。需在设备上安装Charles证书…

elementui工具

elementui工具

ElementUI 工具概述 ElementUI 是一套基于 Vue.js 的桌面端组件库,提供丰富的 UI 组件和工具,适用于快速开发企业级中后台产品。以下从核心功能、使用方法和常见工具组件展开说明…