<template>
  <div class="container-box" v-if="protoThisVisible">
    <span class="fr closeX" @click="protoThisVisible = !protoThisVisible">&times</span>
    <div id="container">
      <canvas v-for="page in pages" :id="'the-canvas'+page" :key="page"></canvas>
    </div>
    <!--<div class="foot" v-if='pdfDoc'>-->
        <!--<button class='left' :style="{opacity: pageNum>1?1:0}" v-if="pageNum>1" @click="onPrevPage">上一页</button>-->
        <!--&lt;!&ndash;<input type="text" class="pageNum" v-model="currPage" @keyup.enter="jumpPage">&ndash;&gt;-->
        <!--<button class='right' :style="{opacity: pageNum<pdfDoc.numPages?1:0}" v-if="pageNum<pdfDoc.numPages" @click="onNextPage">下一页</button>-->
    <!--</div>-->
  </div>
</template>
<script>
  import PDFJS from 'pdfjs-dist'
  let Base64 = require('js-base64').Base64
  export default {
    data () {
      return {
        title: '',
        pdfDoc: null,
        loadding: false,
        pages: 0,
        protoThisVisible:true
      }
    },
    methods: {
      renderPage (num) {
        let _this = this
        this.pdfDoc.getPage(num).then(function (page) {
          let canvas = document.getElementById('the-canvas' + num)
          let ctx = canvas.getContext('2d')
          let dpr = window.devicePixelRatio || 1
          let bsr = ctx.webkitBackingStorePixelRatio ||
            ctx.mozBackingStorePixelRatio ||
            ctx.msBackingStorePixelRatio ||
            ctx.oBackingStorePixelRatio ||
            ctx.backingStorePixelRatio || 1
          let ratio = dpr / bsr
          var viewport = page.getViewport(screen.availWidth / page.getViewport(1).width)
          canvas.width = viewport.width * ratio
          canvas.height = viewport.height * ratio
          canvas.style.width = viewport.width + 'px'
          canvas.style.height = viewport.height + 'px'
          ctx.setTransform(ratio, 0, 0, ratio, 0, 0)
          var renderContext = {
            canvasContext: ctx,
            viewport: viewport
          }
          page.render(renderContext)
          if (_this.pages > num) {
            _this.renderPage(num + 1)
          }
        })
      },
      loadFile (url) {
        let _this = this
        PDFJS.getDocument(url).then(function (pdf) {
          _this.pdfDoc = pdf
          _this.pages = _this.pdfDoc.numPages
          _this.$nextTick(() => {
            _this.renderPage(1)
          })
        })
      }
    }
  }
</script>

<style scoped>
  canvas {
    display: block;
    border-bottom: 1px solid black;
  }
  .container-box,#container{
    position:absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
  }
  #container {
      background-color: rgba(0,0,0,0.75);
      z-index: 12;
      overflow-y: auto;
      text-align: center;
      }
  .foot {
      position: absolute;
      bottom: 0;
      z-index: 10;
      padding: .3rem;
      left: 50%;
      transform: translateX(-50%);
      }
  .pageNum{
      display: inline-block;
      width: 20px;
      text-align: center;
      }
  .closeX{
    position: absolute;
    right: 0;
    top: 0;
    z-index: 14;
    padding:0 .3rem;
    font-size: .55rem;
  }
</style>
