当前位置:首页 > VUE

vue实现盖章

2026-02-10 01:56:10VUE

Vue 实现盖章功能

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

使用 Canvas 绘制盖章

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

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 是矢量图形,适合需要缩放而不失真的场景:

vue实现盖章

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

印章图片叠加

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

<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 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…