JS如何将 DOM 生成图片并下载

最近有一个需求,h5页面生成图片,保存图片打开弹窗,并下载。如图所示:

文件结构如下:

index.vue :

<template>
  <div class="report-wrap">
    <div class="content">
      <swiper ref="Swiper" class="swiper-wrapper" :options="swiperOption">
        <swiper-slide class="section">
          <SectionOne ref="activeTalent" :data="activeTalent" :nickName="nickName" :year="year" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="communicator.speechType !== 0">
          <SectionTwo ref="communicator" :data="communicator" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="explorer.speechType !== 0">
          <SectionThree ref="explorer" :data="explorer" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="
            !(
              attendance.timeTalent.speechType === 0 &&
              attendance.earlyArrival.speechType === 0 &&
              attendance.lateReturner.speechType === 0
            )
          ">
          <SectionFour ref="attendance" :data="attendance" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="weeklyExpert.speechType !== 0">
          <SectionFive ref="weeklyExpert" :data="weeklyExpert" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="okrTalent.speechType !== 0">
          <SectionSix ref="okrTalent" :data="okrTalent" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="conferenceMaster.speechType !== 0">
          <SectionSeven ref="conferenceMaster" :data="conferenceMaster" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="approvalExpert.speechType !== 0">
          <SectionEight ref="approvalExpert" :data="approvalExpert" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="section" v-if="docExpert.speechType !== 0">
          <SectionNine ref="docExpert" :data="docExpert" :isMobile="isMobile" @slideNext="slideNext" />
        </swiper-slide>
        <swiper-slide class="">
          <SectionTen class="swiper-no-swiping" ref="endData" :data="endData" :nickName="nickName" :year="year" :isMobile="isMobile" @saveClick="saveClick" @again="again" />
        </swiper-slide>
      </swiper>
      <img ref="saveImg" class="saveImg" :src="showImg" alt="" @click="saveClick" />
    </div>

    <Preview ref="previewBox" :style="{height: endData.medalData.length > 6 ? '900px' : '812px' }">
      <component :is="getType()" :data="previewData" :nickName="nickName" :year="year" :headShow="headShow"></component>
    </Preview>
    <Dialog v-if="dialogVisible" :canvasImageUrl="canvasImageUrl" @close="close"> </Dialog>
  </div>
</template>

<script>
import SectionOne from './components/sectionOne/index.vue';
import SectionTwo from './components/sectionTwo/index.vue';
import SectionThree from './components/sectionThree/index.vue';
import SectionFour from './components/sectionFour/index.vue';
import SectionFive from './components/sectionFive/index.vue';
import SectionSix from './components/sectionSix/index.vue';
import SectionSeven from './components/sectionSeven/index.vue';
import SectionEight from './components/sectionEight/index.vue';
import SectionNine from './components/sectionNine/index.vue';
import SectionTen from './components/sectionTen/index.vue';
import Dialog from './components/dialog.vue';
import Preview from './components/preview.vue';
import { staffAnnualReport } from './services';
import 'swiper/dist/css/swiper.css';
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import { debounce } from '@/utils/util.js';
import { config as baseConfig } from '@pro-teams/base';
import domtoimage from 'dom-to-image';
const { platform } = baseConfig;
export default {
  components: {
    SectionOne,
    SectionTwo,
    SectionThree,
    SectionFour,
    SectionFive,
    SectionSix,
    SectionSeven,
    SectionEight,
    SectionNine,
    SectionTen,
    Dialog,
    Preview,
    swiper,
    swiperSlide,
  },
  data() {
    return {
      showImg: require('./images/button/保存图片.svg'),
      dialogVisible: false,
      canvasImageUrl: '',
      saveImgTime: 0,
      canvasImgTime: 0,
      fullpage: null,
      headShow: false, // 最后一页不显示头部
      year: '',
      userName: '',
      nickName: '',
      previewCom: 'sectionOne',
      previewData: {}, // 预览
      activeTalent: {}, // 日活
      communicator: {}, // 消息
      explorer: {}, // 工作台
      attendance: {
        timeTalent: {}, // 时间达人
        earlyArrival: {}, // 早鸟达人
        lateReturner: {}, // 夜猫达人
      }, // 考勤
      weeklyExpert: {}, // 周报
      okrTalent: {}, // OKR
      conferenceMaster: {}, // 会议
      approvalExpert: {}, // 审批
      do
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值