效果图
在这里插入图片描述
1、插件和上传接口

import html2canvas from 'html2canvas'
import { upload } from '@/api/accept'

2、结构

<button @click='generateImage'>生成</button>

<!-- 中间信息展示 -->
      <div class="sevenInfo textLeft" id="target-element">
        <div class="name">{{ info ? info.auserName : '天翼爱音乐科技有限公司' }}</div>
        
        <div class="congratulation">
          <img src="@/assets/images/annualReport/congratulation.png" alt="" />
          <span>{{month.currentDate}}</span>
          <img class="mon" :src="imageSource" alt="" />
        </div>
        <div class="number">
          <div class="title">{{month.currentDate}}月商务视频彩铃播放成绩:</div>

          <div v-if="!tag" class="items">
            <div class="item">
              <span>观看人数{{ YearData6.watch_uv }}</span>
            </div>
            <div class="item">
              <span>人员遍布城市{{ YearData6.city }}</span>
            </div>
            <div class="item">
              <span>播放次数{{ YearData6.all_pv }}</span>
            </div>
            <div class="item">
              <span>播放时长{{ YearData6.total_duration }}</span>
            </div>
          </div>
          <div v-else class="items">
            <div class="item">
              <span>观看人数1716</span>
            </div>
            <div class="item">
              <span>人员遍布城市229</span>
            </div>
            <div class="item">
              <span>播放次数2297</span>
            </div>
            <div class="item">
              <span>播放时长29,443</span>
            </div>
          </div>
        </div>
        <div class="comeItem">
          <span>{{month.currentDate+1}}</span>
          <img class="come" src="@/assets/images/annualReport/come.png" alt="" />
        </div>
      </div>

  generateImage() {
      this.$toast('生成海报后长按分享')
      let that = this
      setTimeout(function () {
        console.log("执行");
        
        const element = document.getElementById('target-element') // 替换为目标元素的id或class
        html2canvas(element,{backgroundColor: '#0e0e0e'}).then(canvas => {

          const image = canvas.toDataURL('image/png')
          scale: 5
          dpi: 600, console.log('image', image)
          let url = that.dataURLtoBlob(image)
          console.log('url', url)
          that.videoFile(url)
        })
      }, 2000) 
    },

// 将base64转成blob 为了后续能上传
    dataURLtoBlob(dataurl) {
      var arr = dataurl.split(',')
      //注意base64的最后面中括号和引号是不转译的
      var _arr = arr[1].substring(0, arr[1].length - 2)
      var mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(_arr),
        n = bstr.length,
        u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new Blob([u8arr], {
        type: mime
      })
    },
// 上传图片
    videoFile(file) {
      let params = new window.FormData()
      let fileOfBlob = new File([file], new Date().getTime() + '.jpg')
      params.append('file', fileOfBlob)

      upload(params).then(res => {
        console.log('上传结果', res)
        if (res.success) {
          this.url = res.data.url
          this.savePic(res.data.url)
        }
      })
    },
GitHub 加速计划 / vu / vue
82
16
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:4 个月前 )
9e887079 [skip ci] 2 个月前
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐