vue使用html2canvas生成圖片實(shí)現(xiàn)方式
更新時間:2026年03月23日 09:10:37 作者:木子靜靜
這篇文章主要介紹了vue使用html2canvas生成圖片實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
1.安裝html2canvas插件
npm install --save html2canvas
2.在需要用到的.vue文件中引入
import html2canvas from 'html2canvas'
3.代碼片段
說明:
- 如果要被生成圖片的dom是超出屏幕需要滾動才能查看的話,
- 需要加上width,height windowWidth,windowHeight 這些屬性才能保證截圖的完整性,
- windowHeight的值用document.body.scrollHeight也行,具體看需求如何。
<template>
<div>
<button @click="createImg">生成圖片并下載到本地</button>
<button @click="saveImg">生成圖片并上傳到服務(wù)器</button>
<div ref="content" style="width:200px;height:200px;border:1px solid #333333;">
此dom是要被保存成圖片的
</div>
</div>
</template>
<script>
import html2canvas from 'html2canvas'
export default {
data(){
return {
}
},
methods:{
createImg(){
let content = this.$refs.content
let scrollHeight = content.scrollHeight
let scrollWidth = content.scrollWidth
html2canvas(content,{
scale: window.devicePixelRatio*2,
useCORS: true , //開啟跨域配置,但和allowTaint不能共存
width:scrollWidth,
height:scrollHeight,
windowWidth:scrollWidth,
windowHeight:scrollHeight,
x:0,
y:window.pageYOffset
}).then((canvas) => {
this.operType = 'edit'
let dataURL = canvas.toDataURL("image/jpg");
let link = document.createElement("a");
link.href = dataURL;
let filename = `${new Date().getTime()}.jpg`; //文件名稱
link.setAttribute("download", filename);
link.style.display = "none"; //a標(biāo)簽隱藏
document.body.appendChild(link);
link.click();
})
},
saveImg(){
html2canvas(this.$refs.content,{
scale: window.devicePixelRatio*2,
useCORS: true , //開啟跨域配置,但和allowTaint不能共存
width:content.scrollWidth,
height:content.scrollHeight,
windowWidth:content.scrollWidth,
windowHeight:content.scrollHeight,
x:0,
y:window.pageYOffset
}).then((canvas) => {
let dataURL = canvas.toDataURL("image/png");
this.operType = 'edit'
let filename = `${new Date().getTime()}.png`;
let file_url = dataURLtoFile(dataURL, filename,"image/png"); //將文件轉(zhuǎn)換成file的格式,就可以使用file_url傳遞給服務(wù)端了
let formData = new FormData()
formData.append('file',file_url)
//這個uploadFile 根據(jù)自己上傳接口的名字寫
uploadFile(formData).then(res=>{
//此處具體業(yè)務(wù)具體操作
})
});
},
//將base64格式轉(zhuǎn)換成file的格式
dataURLtoFile(base64, filename,contentType) {
let arr = base64.split(',') //去掉base64格式圖片的頭部
let bstr = atob(arr[1]) //atob()方法將數(shù)據(jù)解碼
let leng = bstr.length
let u8arr = new Uint8Array(leng)
while(leng--){
u8arr[leng] = bstr.charCodeAt(leng) //返回指定位置的字符的 Unicode 編碼
}
return new File([u8arr],filename,{type:contentType})
}
}
}
</script>
具體的屬性可以參考官方文檔
4.小記
//1. 生成的背景默認(rèn)為白色,可以通過 backgroundColor 屬性修改背景顏色
html2canvas(this.$refs.content, {
backgroundColor: null // null 表示設(shè)置背景為透明色
}.then(img => {});
//2.截圖有時會錯位,是因?yàn)椴恢С帜承ヽss屬性造成的,調(diào)整css樣式即可,有時margin 也會有影響。
5.補(bǔ)充
在移動端H5使用中,會有ios截圖失效問題,目前的解決方案是 修改html2canvas版本為1.0.0
調(diào)用方法如下:
(window.html2canvas || html2canvas)(content,
{
scale: window.devicePixelRatio, //window.devicePixelRatio * 2
useCORS: true, //開啟跨域配置,但和allowTaint不能共存
width: scrollWidth,
height: scrollHeight,
windowWidth: scrollWidth,
windowHeight: scrollHeight,
x: 0,
y: 0
}).then(canvas => {
let url = canvas.toDataURL("image/jpg");
console.log(url)
this.picUrl = url
})
.catch(err => {
// do sth
});
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
前端vue中實(shí)現(xiàn)嵌入代碼編輯器的詳細(xì)代碼
隨著Web技術(shù)的不斷發(fā)展,前端開發(fā)也正日新月異,下面這篇文章主要給大家介紹了關(guān)于前端vue中實(shí)現(xiàn)嵌入代碼編輯器的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
vue項(xiàng)目打包優(yōu)化的方法實(shí)戰(zhàn)記錄
最近入職了新公司,接手了一個新拆分出來的Vue項(xiàng)目,針對該項(xiàng)目做了個打包優(yōu)化,把經(jīng)驗(yàn)分享出來,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目打包優(yōu)化的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Vue項(xiàng)目新一代狀態(tài)管理工具Pinia的使用教程
pinia是一個輕量級的狀態(tài)管理庫,屬于 vue3 生態(tài)圈新的成員之一,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目新一代狀態(tài)管理工具Pinia的使用教程,需要的朋友可以參考下2022-11-11
vue ssr+koa2構(gòu)建服務(wù)端渲染的示例代碼
這篇文章主要介紹了vue ssr+koa2構(gòu)建服務(wù)端渲染的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
setup+ref+reactive實(shí)現(xiàn)vue3響應(yīng)式功能
這篇文章介紹了通過setup+ref+reactive實(shí)現(xiàn)vue3響應(yīng)式功能,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
Vue+Microapp實(shí)現(xiàn)微前端的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何實(shí)現(xiàn)以vite+vue3+Microapp為主應(yīng)用,以vue2+element為子應(yīng)用的微前端,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2023-06-06

