vue的h5開發中,將頁面保存爲圖片

需求:長按頁面保存到手機html

實現方式:npm

  將頁面保存爲圖片,而後再當前頁面實際上展現的是一張圖片,canvas

利用移動端自帶的功能(長按圖片能夠保存到手機)來實現這個需求this

 

代碼:spa

一、使用 html2canvascode

npm install html2canvas -dhtm

 

二、blog

<template>
    <div>
        <div id="captureId" v-show="firstFlag">
            <div>
                <div class="wrap">
                    <div>
                        <span class="star-title">爸氣指數:</span>
                    </div>
                    <span class="text">掃碼測一測你的父親節爸氣指數</span>
                </div>
            </div>
        </div>
        <img class="show-img" :src="dataURL" alt="" v-show="!firstFlag">
    </div>
    
</template>
<script>
import html2canvas from 'html2canvas'
export default {
    data () {
        return {
            firstFlag: true,
            dataURL:''
        }
    },
    methods: {
        toImg () {
            html2canvas(document.querySelector('#captureId')).then(canvas => {
                let imgUrl = canvas.toDataURL('image/png');
                that.dataURL = imgUrl;
                that.firstFlag = false;
            }).catch(error => {
            })
        },
    },
    created () {
        this.imgSRC = window.location.href
        this.firstFlag = true
    },
    mounted() {
        const that = this
        that.$nextTick(function(){
            that.toImg()
        })
    }
}
</script>
相關文章
相關標籤/搜索