跳到内容
SCRM DOC
frontendvue3插件截图组件创建于2025-01-17更新于 2026-03-22

Vue组件截图插件

一个用于Vue3的组件截图插件,允许用户在运行时选择并截取任意组件的图片。支持截图前预览和确认。

特性

  • 🎯 支持选择任意DOM元素进行截图
  • 🖼️ 截图前预览和确认机制
  • 🎨 提供视觉反馈,高亮显示选中的元素
  • 🚀 简单易用的API
  • 💪 完整的TypeScript支持

安装

# 使用 npm
npm install html2canvas

# 使用 pnpm
pnpm add html2canvas

# 使用 yarn
yarn add html2canvas

使用方法

1. 注册插件

在你的主入口文件(通常是 main.ts 或 main.js)中注册插件:

import { createApp } from 'vue'
import App from './App.vue'
import createScreenshotPlugin from '@/plugins/screenshot'

const app = createApp(App)

// 注册插件
app.use(createScreenshotPlugin({
  // 可选配置
  onCapture: (dataUrl) => {
    // 处理截图结果
    console.log('截图完成:', dataUrl)
  },
  onModeChange: (active) => {
    // 截图模式状态变化
    console.log('截图模式:', active ? '已启用' : '已禁用')
  }
}))

app.mount('#app')

2. 使用控制组件

在你的Vue组件中使用截图控制按钮:

<template>
  <div>
    <ScreenshotController />
    <!-- 其他内容 -->
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import ScreenshotController from '@/components/Screenshot/index.vue'

export default defineComponent({
  components: {
    ScreenshotController
  }
})
</script>

3. 截图流程

  1. 点击"开始截图"按钮进入截图模式
  2. 鼠标移动到目标组件上时会显示蓝色边框
  3. 点击选中要截图的组件(边框变为绿色)
  4. 在顶部工具栏选择:
    • 点击"确认截图"完成截图
    • 点击"重新选择"取消当前选择
    • 点击"取消"退出截图模式

4. 自定义按钮样式

你可以通过插槽自定义截图按钮的内容:

<template>
  <ScreenshotController>
    <template #default>
      <i class="custom-icon" />
      <span>自定义截图按钮</span>
    </template>
  </ScreenshotController>
</template>

5. 通过代码控制

你也可以在组件中通过代码直接控制截图功能:

import { defineComponent, getCurrentInstance } from 'vue'

export default defineComponent({
  setup() {
    const instance = getCurrentInstance()
    const screenshot = instance?.appContext.config.globalProperties.$screenshot

    // 开始截图模式
    const startScreenshot = () => {
      screenshot?.activate()
    }

    // 停用截图模式
    const stopScreenshot = () => {
      screenshot?.deactivate()
    }

    // 确认当前选择并截图
    const confirmScreenshot = async () => {
      const dataUrl = await screenshot?.confirm()
      if (dataUrl) {
        console.log('截图结果:', dataUrl)
      }
    }

    // 取消当前选择
    const cancelSelection = () => {
      screenshot?.cancel()
    }

    // 获取当前截图状态
    const getScreenshotState = () => {
      return screenshot?.getState()
    }

    return {
      startScreenshot,
      stopScreenshot,
      confirmScreenshot,
      cancelSelection,
      getScreenshotState
    }
  }
})

API

插件选项

interface ScreenshotOptions {
  // 截图完成后的回调函数
  onCapture?: (dataUrl: string) => void
  // 开启/关闭截图模式的回调
  onModeChange?: (active: boolean) => void
}

实例方法

插件会在Vue实例上注册 $screenshot 对象,提供以下方法:

  • activate(): 激活截图模式
  • deactivate(): 停用截图模式
  • confirm(): 确认当前选择并进行截图
  • cancel(): 取消当前选择
  • getState(): 获取当前截图状态
  • capture(element: HTMLElement): 对指定元素进行截图

状态接口

interface ScreenshotState {
  isActive: boolean          // 是否处于截图模式
  selectedElement: HTMLElement | null  // 当前选中的元素
  isPending: boolean         // 是否处于确认状态
}

注意事项

  1. 确保被截图的元素已经完全加载(包括图片等资源)
  2. 考虑跨域资源的限制,可能需要配置相应的CORS策略
  3. 在生产环境中使用时,建议处理可能出现的异常情况
  4. 截图模式下,其他鼠标事件会被暂时屏蔽,以确保正确的选择体验
  5. 建议在较大屏幕上使用,以获得更好的选择体验

浏览器兼容性

该插件依赖于 html2canvas,支持以下浏览器:

  • Chrome
  • Firefox
  • Safari
  • Edge
  • IE11+

许可证

MIT

页面信息›
别名截图插件Vue screenshot

本页目录