Vue实现一键截屏功能

1.安装HTMLCanvas

npm install html2canvas --save

2.在需要的Vue组件中引入

import html2canvas from "html2canvas";

3.定义点击按钮和截屏方法

/*定义的截图菜单*/
<el-button
      class="button-upload"
      size="mini"
      title="生成图片"
      type="success"
      @click="toImage()"
      icon="el-icon-upload el-icon--right">上传截图</el-button>


/*js内容*/
const imageTofile = ref(null)
const toImage = () => {
  //如果步骤信息为空,那么就不需要上传截图文件
  //此处可以根据自己需求做是否满足截图上传的判断前置条件
  if (props.steps.length === 0) {
    ElMessage.warning({
      message: '还没有添加步骤详情,无需上传截图信息',
    })
    return
  }

  // 手动创建一个 canvas 标签
  const canvas = document.createElement('canvas')
  // 获取父标签,意思是这个标签内的 DOM 元素生成图片
  // imageTofile是给截图范围内的父级元素自定义的ref名称
  let canvasBox = imageTofile.value
  // 获取父级的宽高
  const width = parseInt(window.getComputedStyle(canvasBox).width)
  const height = parseInt(window.getComputedStyle(canvasBox).height)
  // 宽高 * 2 并放大 2 倍 是为了防止图片模糊
  canvas.width = width * 2
  canvas.height = height * 2
  canvas.style.width = width + 'px'
  canvas.style.height = height + 'px'
  const context = canvas.getContext('2d')
  context.scale(2, 2)
  html2canvas(canvasBox, { allowTaint: true }).then((canvas) => {
    //document.body.appendChild(canvas)  页面布局会乱
    //转换base64
    debugger
    const capture = canvas.toDataURL('image/png')
    //下载浏览器弹出下载信息的属性
    const saveInfo = {
      //导出文件格式自己定义,我这里用的是时间作为文件名
      download: getNowTime() + `.png`,
      href: capture,
    }
    //下载,浏览器弹出下载文件提示
    downloadFile(saveInfo)

    //调用服务端保存接口
    axios
      .post('/controller/testCases/uploadStempImg', {
        caseId: props.steps[0].caseId,
        projectId: props.steps[0].projectId,
        base64Img: capture,
      })
      .then((resp) => {
        if (resp['code'] === 2000) {
          ElMessage.success({
            message: resp['message'],
          })
          emit('flush')
        }
      })
  })
}

/*获取当前时间,为截图的图片提供名称 */
const getNowTime = () => {
  const yy = new Date().getFullYear()
  const MM =
    new Date().getMonth() + 1 < 10
      ? '0' + (new Date().getMonth() + 1)
      : new Date().getMonth() + 1
  const dd =
    new Date().getDate() < 10
      ? '0' + new Date().getDate()
      : new Date().getDate()
  const HH =
    new Date().getHours() < 10
      ? '0' + new Date().getHours()
      : new Date().getHours()
  const mm =
    new Date().getMinutes() < 10
      ? '0' + new Date().getMinutes()
      : new Date().getMinutes()
  const ss =
    new Date().getSeconds() < 10
      ? '0' + new Date().getSeconds()
      : new Date().getSeconds()
  return yy + MM + dd + '-' + HH + mm + ss
}

//下载截图
const downloadFile = (saveInfo) => {
  const element = document.createElement('a')
  element.style.display = 'none'
  for (const key in saveInfo) {
    element.setAttribute(key, saveInfo[key])
  }
  document.body.appendChild(element)
  element.click()
  setTimeout(() => {
    document.body.removeChild(element)
  }, 300)
}

4.别忘了给想截图范围内的父级添加ref属性,这样截图就可以根据业务截取特定部分的

  <div ref="imageTofile"></div>

参考:vue实现网页截图_suoh's Blog的博客-CSDN博客_vue 截图

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3中,使用i18n实现一键翻译的功能按钮的步骤如下: 1. 安装`vue-i18n`插件 ``` npm install vue-i18n ``` 2. 创建i18n实例 在main.js文件中创建i18n实例,并设置默认语言和语言包: ```javascript import { createApp } from 'vue' import App from './App.vue' import { createI18n } from 'vue-i18n' const i18n = createI18n({ locale: 'zh-CN', // 默认语言 messages: { 'zh-CN': require('./locales/zh-CN.json'), // 中文语言包 'en-US': require('./locales/en-US.json') // 英文语言包 } }) const app = createApp(App) app.use(i18n) app.mount('#app') ``` 3. 添加翻译按钮 在需要添加翻译按钮的组件中,添加一个按钮,并在点击事件中调用i18n实例的`setLocaleMessage`方法来切换语言: ```html <template> <div> <button @click="translate">Translate</button> <p>{{ $t('hello') }}</p> </div> </template> <script> export default { methods: { translate() { // 切换语言 this.$i18n.setLocaleMessage('zh-CN', require('../locales/en-US.json')) } } } </script> ``` 在上面的代码中,点击按钮后会将语言切换为英文。 4. 创建语言包 在项目根目录下创建一个`locales`文件夹,并在其中创建一个`zh-CN.json`文件和一个`en-US.json`文件,用于存放中文和英文的语言包。例如: ```json // zh-CN.json { "hello": "你好,世界!" } ``` ```json // en-US.json { "hello": "Hello, world!" } ``` 现在,当我们点击翻译按钮后,页面上的"你好,世界!"会自动切换为"Hello, world!"。 以上就是使用i18n实现一键翻译的功能按钮的全部步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值