当前位置:首页 > 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>

添加画笔颜色和粗细控制

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

<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>

支持触摸设备

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

<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 中实现一个功能丰富的涂鸦工具,支持绘图、调整画笔属性、触摸操作和保存图片等功能。

vue实现涂鸦工具

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

相关文章

制作css工具

制作css工具

设计CSS工具的基本架构 CSS工具通常需要包含核心功能模块,如代码生成器、可视化编辑器、代码优化器和浏览器兼容性检查器。架构设计应当采用模块化方式,便于功能扩展和维护。 现代CSS工具常基于We…

js实现画图工具

js实现画图工具

实现基础画布 使用HTML5的Canvas元素作为画布基础,通过JavaScript获取画布上下文。示例代码初始化一个全屏画布: <canvas id="drawingCanvas" widt…

js实现涂鸦面板

js实现涂鸦面板

实现涂鸦面板的基本思路 涂鸦面板的核心功能包括鼠标事件监听、路径绘制和画布操作。使用HTML5的Canvas API可以高效实现这一功能。 初始化画布和上下文 在HTML中创建Canvas元素并获取…

uniapp解包工具

uniapp解包工具

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

vue 实现工具

vue 实现工具

Vue 实现工具的常用方法 Vue CLI Vue CLI 是官方提供的脚手架工具,用于快速初始化 Vue 项目。支持插件系统,可集成 Babel、TypeScript、ESLint 等工具。 安装命…