当前位置:首页 > VUE

vue实现盖章

2026-02-10 01:56:10VUE

Vue 实现盖章功能

在 Vue 中实现盖章功能可以通过结合 Canvas 或 SVG 技术来实现动态盖章效果。以下是两种常见的实现方法:

使用 Canvas 绘制盖章

Canvas 提供强大的绘图能力,适合动态生成印章。以下是一个简单的 Vue 组件示例:

<template>
  <div>
    <canvas ref="stampCanvas" width="200" height="200"></canvas>
    <button @click="applyStamp">盖章</button>
  </div>
</template>

<script>
export default {
  methods: {
    applyStamp() {
      const canvas = this.$refs.stampCanvas;
      const ctx = canvas.getContext('2d');

      // 绘制圆形印章
      ctx.beginPath();
      ctx.arc(100, 100, 80, 0, Math.PI * 2);
      ctx.strokeStyle = 'red';
      ctx.lineWidth = 3;
      ctx.stroke();

      // 添加文字
      ctx.font = '16px Arial';
      ctx.fillStyle = 'red';
      ctx.textAlign = 'center';
      ctx.fillText('公司公章', 100, 100);
    }
  }
}
</script>

使用 SVG 实现盖章

SVG 是矢量图形,适合需要缩放而不失真的场景:

<template>
  <div>
    <svg ref="stampSvg" width="200" height="200">
      <circle cx="100" cy="100" r="80" fill="none" stroke="red" stroke-width="3"/>
      <text x="100" y="100" text-anchor="middle" fill="red" font-family="Arial" font-size="16">公司公章</text>
    </svg>
    <button @click="applyStamp">盖章</button>
  </div>
</template>

<script>
export default {
  methods: {
    applyStamp() {
      // 可以在这里动态修改SVG属性
      const text = this.$refs.stampSvg.querySelector('text');
      text.textContent = '已盖章';
    }
  }
}
</script>

实现拖拽盖章

如果需要实现拖拽盖章效果,可以结合鼠标事件:

<template>
  <div @mousemove="dragStamp" @mouseup="stopDrag">
    <div 
      v-if="showStamp" 
      :style="{ left: stampPos.x + 'px', top: stampPos.y + 'px' }"
      class="stamp"
      @mousedown="startDrag"
    >
      公章
    </div>
    <button @click="showStamp = true">显示印章</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showStamp: false,
      stampPos: { x: 0, y: 0 },
      isDragging: false,
      dragOffset: { x: 0, y: 0 }
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true;
      this.dragOffset = {
        x: e.clientX - this.stampPos.x,
        y: e.clientY - this.stampPos.y
      };
    },
    dragStamp(e) {
      if (!this.isDragging) return;
      this.stampPos = {
        x: e.clientX - this.dragOffset.x,
        y: e.clientY - this.dragOffset.y
      };
    },
    stopDrag() {
      this.isDragging = false;
    }
  }
}
</script>

<style>
.stamp {
  position: absolute;
  width: 100px;
  height: 100px;
  border: 3px solid red;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: red;
  cursor: move;
}
</style>

印章图片叠加

如果需要使用预设的印章图片:

vue实现盖章

<template>
  <div>
    <div class="document" @click="placeStamp($event)">
      <!-- 文档内容 -->
    </div>
    <img 
      v-if="showStamp" 
      :src="stampImage" 
      :style="{ left: stampPos.x + 'px', top: stampPos.y + 'px' }"
      class="stamp-image"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      showStamp: false,
      stampPos: { x: 0, y: 0 },
      stampImage: require('@/assets/stamp.png')
    }
  },
  methods: {
    placeStamp(e) {
      this.stampPos = {
        x: e.offsetX - 50, // 居中
        y: e.offsetY - 50
      };
      this.showStamp = true;
    }
  }
}
</script>

<style>
.document {
  width: 800px;
  height: 1000px;
  border: 1px solid #ccc;
  position: relative;
}
.stamp-image {
  position: absolute;
  width: 100px;
  height: 100px;
  pointer-events: none;
}
</style>

以上方法可以根据实际需求进行调整和组合,实现不同风格的盖章功能。

标签: vue
分享给朋友:

相关文章

vue实现目录

vue实现目录

Vue 实现目录功能 在 Vue 中实现目录功能通常需要动态生成目录结构,并支持点击跳转到对应内容区域。以下是几种常见实现方式: 基于滚动监听和动态生成 安装依赖(如需): npm install…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

django vue实现

django vue实现

Django与Vue.js整合实现方案 Django作为后端框架与Vue.js作为前端框架的整合,可以通过以下方式实现: 分离式开发 前后端完全分离,Django仅提供API接口,Vue.js通过a…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…