在Vue中用canvas實(shí)現(xiàn)二維碼和圖片合成海報(bào)的方法
在項(xiàng)目中經(jīng)常會(huì)遇到需要將不同的二維碼放到一張通用圖片上,提供用戶下載
簡(jiǎn)單來(lái)說(shuō),就是利用canvas將同等比例的二維碼在圖片上疊加,生成海報(bào)
1. 設(shè)置相應(yīng)比例
一般來(lái)說(shuō)海報(bào)背景都是固定的,可以直接放在public文件夾,二維碼可根據(jù)后臺(tái)返回?cái)?shù)據(jù),也可用canvas生成,在此不多贅述
import posterBgImg from '../public/images/poster_bg.png';// 海報(bào)底圖
import qrcodeImg from '../public/images/qrcode.png';// 二維碼
export default{
name: 'qrcode-in-poster',
data(){
return {
posterBgImg,
qrcodeImg,
posterSize: 930/650,// 海報(bào)高寬比例
qrCodeSize: {// 二維碼與海報(bào)對(duì)應(yīng)比例 =》 用于設(shè)置二維碼在海報(bào)中的位置
width: 270/650,
height: 270/930,
left: 190/650,
top: 448/650
},
poster: '',// 合成圖片
}
}
};
2. 獲取屏幕寬度
限定移動(dòng)端最大寬度為 480px
computed: {
screenWidth(){
let w = document.body.clientWidt || document.documentElement.clientWidth || 375;
return w > 480 ? 480 : w ;
}
};
3. 組合圖片
methods: {
combinedPoster(_url){
let that = this,
qrcode = this.qrcodeImg; // 二維碼地址
console.log("open draw: ", _url, qrcode)
let base64 = '',
canvas = document.createElement('canvas'),
ctx = canvas.getContext("2d"),
_w = this.screenWidth * 2, // 圖片寬度: 由于手機(jī)屏幕時(shí)retina屏,都會(huì)多倍渲染,在此只設(shè)置2倍,如果直接設(shè)置等于手機(jī)屏幕,會(huì)導(dǎo)致生成的圖片分辨率不夠而模糊
_h = this.posterSize * _w, // 圖片高度
_qr_w = this.qrCodeSize.width * _w, // 二維碼寬 = 比例 * 寬度
_qr_h = this.qrCodeSize.height * _h, // 二維碼高 = 比例 * 高度
_qr_t = this.qrCodeSize.top * _w, // 二維碼頂部距離 = 比例 * 寬度
_qr_l = this.qrCodeSize.left * _w; // 二維碼左側(cè)距離 = 比例 * 寬度
// 設(shè)置canvas寬高
canvas.width = _w;
canvas.height = _h;
ctx.rect(0, 0, _w, _h);
ctx.fillStyle = '#fff'; // 填充顏色
ctx.fill();
// 迭代生成: 第一層(底圖)+ 第二層(二維碼)
// file:文件,size:[頂部距離,左側(cè)距離,寬度,高度]
let _list = [
{
file: _url,
size: [0, 0, _w, _h]
}, {
file: qrcode,
size: [_qr_l, _qr_t, _qr_w, _qr_h]
}
];
// 開(kāi)始繪畫
let drawing = (_index) => {
// 判斷當(dāng)前索引 =》 是否已繪制完畢
if (_index < _list.length) {
// 等圖片預(yù)加載后畫圖
let img = new Image(),
timeStamp = new Date().getTime();
// 防止跨域
img.setAttribute('crossOrigin', 'anonymous')
// 鏈接加上時(shí)間戳
img.src = _list[_index].file + '?' + timeStamp
img.onload = function() {
// 畫圖
ctx.drawImage(img, ..._list[_index].size)
// 遞歸_list
drawing(_index + 1)
}
} else {
// 生成圖片
base64 = canvas.toDataURL("image/png")
if (base64) {
// 賦值相應(yīng)海報(bào)上
this.$set(that, 'poster', base64)
}
}
}
drawing(0)
}
};
mounted(){
// 需要合成海報(bào)的圖片
this.draw(this.posterBgImg)
}
4. 下載
點(diǎn)擊下載合成圖片
methods: {
handleDownload(){
if(this.poster){
let a = document.createElement("a");
a.setAttribute("download", "海報(bào)下載-"+(new Date().getTime()));
a.href = this.poster
a.click()
}else{
console.log("海報(bào)不存在,請(qǐng)重新生成!")
}
}
}
tips:不適用于微信瀏覽器,只能提示用戶長(zhǎng)按保存。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue+ element ui 表單驗(yàn)證有值但驗(yàn)證失敗問(wèn)題
這篇文章主要介紹了vue+ element ui 表單驗(yàn)證有值但驗(yàn)證失敗,本文通過(guò)實(shí)例代碼給大家分享解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
在Vue中實(shí)現(xiàn)拖拽功能的實(shí)例
Vue實(shí)現(xiàn)拖拽功能的基本原理是監(jiān)聽(tīng)鼠標(biāo)事件,實(shí)時(shí)更新拖拽元素的位置,最后在合適的時(shí)機(jī)停止拖拽并更新元素位置,在Vue中,我們可以通過(guò)綁定相關(guān)事件來(lái)實(shí)現(xiàn)這一功能2025-02-02
Vue3與pywebview實(shí)現(xiàn)獲取本地文件夾的絕對(duì)路徑
這篇文章主要為大家詳細(xì)介紹了Vue3如何結(jié)合pywebview實(shí)現(xiàn)獲取本地文件夾的絕對(duì)路徑,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-11-11
vue之input輸入框防抖debounce函數(shù)的使用方式
這篇文章主要介紹了vue之input輸入框防抖debounce函數(shù)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

