1、html2canvas
1、html2canvas官方文档
使用案例:
1、index.vue
<template>
<div>
<div id="capture">
<div>截图内容<div/>
<!-- data-html2canvas-ignore 截图时忽略 -->
<button @click="printscreen" data-html2canvas-ignore>点击截图</button>
</div>
<poster1 v-model="showPoster"></poster1>
</div>
</template>
<script>
import poster1 from '@/components/canvas/poster1'
export default {
components: {
poster1
},
data() {
return {
showPoster: false
}
},
created() {},
mounted() {},
methods: {
printscreen() {
this.showPoster = true
}
}
}
</script>
<style scoped lang="scss">
</style>
2、poster1.vue 组件
<template>
<div>
<!-- 遮罩层 -->
<div :class="{hidePoster:showPoster}"></div>
<!-- <div v-if="showPoster" class="fx_box"> -->
<div v-if="showPoster" class="fx_content">
<img :src="imgURL" alt />
<img class="close" @click="close" src="../../assets/close.png" alt />
</div>
<!-- </div> -->
</div>
</template>
<script>
import html2canvas from 'html2canvas'
export default {
props: {
value: Boolean
},
watch: {
value: function(val) {
if (val) {
this.showPoster = val
this.init()
}
}
},
data() {
return {
showPoster: false,
imgURL: ''
}
},
created() {},
mounted() {},
methods: {
init() {
html2canvas(document.querySelector('#capture'), {
backgroundColor: 'white',
useCORS: true,
scale: 1
}).then(canvas => {
this.imgURL = canvas.toDataURL('image/png')
})
},
close() {
this.showPoster = false
this.$emit('input', false)
}
}
}
</script>
<style scoped lang="scss">
.hidePoster {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 998;
background: rgba(0, 0, 0, 0.2);
}
.fx_content {
position: absolute;
z-index: 999;
width: 85%;
top: 10%;
left: 50%;
transform: translate(-50%);
border-radius: 15px;
padding: 10px 10px 30px 10px;
box-sizing: border-box;
background-color: #fff;
img {
width: 100%;
height: 100%;
border-radius: 15px;
}
.close {
width: 60px;
height: 60px;
position: absolute;
bottom: -80px;
left: 50%;
transform: translate(-50%);
z-index: 999;
}
}
</style>
2、domtoimage(可支持有滚动条的情况)
参考文档
使用案例:
1.index.vue
<template>
<div>
<div id="capture">
<div>截图内容<div/>
</div>
<button @click="printscreen" data-html2canvas-ignore>点击截图</button>
<poster2 v-model="showPoster"></poster2>
</div>
</template>
<script>
import poster2 from '@/components/canvas/poster2'
export default {
components: {
poster2
},
data() {
return {
showPoster: false
}
},
created() {},
mounted() {},
methods: {
printscreen() {
this.showPoster = true
}
}
}
</script>
<style scoped lang="scss">
</style>
2、poster2组件
<template>
<div>
<!-- 遮罩层 -->
<div :class="{hidePoster:showPoster}"></div>
<!-- <div v-if="showPoster" class="fx_box"> -->
<div v-if="showPoster" class="fx_content">
<img :src="imgURL" alt />
<img class="close" @click="close" src="../../assets/close.png" alt />
</div>
<!-- </div> -->
</div>
</template>
<script>
import domtoimage from 'dom-to-image'
export default {
props: {
value: Boolean
},
data() {
return {
showPoster: false,
imgURL: ''
}
},
watch: {
value: function(val) {
if (val) {
this.showPoster = val
this.init()
}
}
},
created() {},
mounted() {},
methods: {
init() {
let node = document.getElementById('capture')
domtoimage
.toPng(node)
.then(dataUrl => {
this.imgURL = dataUrl
})
.catch(function(error) {
console.error('oops, something went wrong!', error)
})
},
close() {
this.showPoster = false
this.$emit('input', false)
}
}
}
</script>
<style scoped lang="scss">
.hidePoster {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 998;
background: rgba(0, 0, 0, 0.2);
}
.fx_content {
position: absolute;
z-index: 999;
width: 85%;
top: 10%;
left: 50%;
transform: translate(-50%);
border-radius: 15px;
padding: 10px 10px 30px 10px;
box-sizing: border-box;
background-color: #fff;
img {
width: 100%;
height: 100%;
border-radius: 15px;
}
.close {
width: 60px;
height: 60px;
position: absolute;
bottom: -80px;
left: 50%;
transform: translate(-50%);
z-index: 999;
}
}
</style>