微信小程序,截屏截圖,都需要重新繪制畫布,生成圖片.
簡單的靜態(tài)布局,可以直接使用canvas.
然而,大量動態(tài)數(shù)據(jù), 動態(tài)布局, 使用canvas,實現(xiàn)起來就有些過于繁瑣了.在實現(xiàn)對界面截圖的過程中, 使用到了painter組件.接下來介紹一下功能實現(xiàn)過程中painter的使用.
.json文件
{
"navigationBarTitleText": "圖片名片",
"backgroundColor": "#f5f5f5",
"usingComponents": {
"painter": "/pages/components/painter/painter"
}
}
.wxml文件
palette: 是繪制屬性,賦值后, 畫布繪制保存到本地,就是按照此屬性的賦值數(shù)據(jù)來的.
dancePalette: 是展示在現(xiàn)存界面里的布局;
在實現(xiàn)截圖功能時, 還是覺得 wxml的布局效率比較精準(zhǔn), 比較高, 所以展示給用戶的, 是用wxml實現(xiàn)的布局.并沒有給dancePalette賦值,當(dāng)然在調(diào)試的時候 也是可以給dancePalette直接賦和palette一樣的值, 來查看繪制效果的, 具體用不用,看個人習(xí)慣和項目需求;
<painter
palette="{{template}}"
bind:imgOK="onImgOK"
/>
.wxss文件
painter{width: 100%;height: 1264rpx;}
.js文件(核心繪制代碼)
注:
*繪制代碼看似比較繁瑣, 其實有輔助工具的喲painter-custom-poster,
可以先利用painter-custom-poster生成大概布局數(shù)據(jù),再進行微調(diào), 很方便*
為了 方便動態(tài)賦值實現(xiàn)動態(tài)布局,我直接在.js文件里處理了布局數(shù)據(jù). 如果界面相對簡單, 可以獨立出來一個.js 文件,導(dǎo)入本界面index.js文件中.
//這里額外創(chuàng)建了一個.js文件,將一些靜態(tài)布局數(shù)據(jù)分離了出去. 所以需要做一下導(dǎo)入
import Card from './draw/getpic.js';
onShow: function() {
this.getWritePhotos();//獲取微信授權(quán)保存相冊
},
//此函數(shù)就是簡單的界面數(shù)據(jù)請求,不做贅述.將drawPic函數(shù)在此調(diào)用確保繪制圖片的時候,數(shù)據(jù)已經(jīng)拿到
getCardDetail: function(cardId) {
app.$ajax({
url: '本頁面數(shù)據(jù)網(wǎng)絡(luò)請求接口',
data: {
入?yún)⒚?入?yún)? },
contentType: "application/json",
method: "GET"
}).then(res => {
that.drawPic();
});
}
// 繪制
drawPic: function() {
//這里額外創(chuàng)建了一個.js文件,將一些靜態(tài)布局數(shù)據(jù)分離了出去.
let card = new Card().palette();
this.setData({
template: card,
});
//繪制動態(tài)數(shù)據(jù)
this.manageCardPainterPhoto();
},
//繪制動態(tài)數(shù)據(jù)
manageCardPainterPhoto: function() {
let that = this;
let card = this.data.template;
// 用戶信息
card.views.push({
"type": "image",
"url": `${that.data.cardInfo.headImg}`,
"css": {
"width": "160rpx",
"height": "160rpx",
"top": "88rpx",
"left": "300rpx",
"rotate": "0",
"borderRadius": "80rpx",
"borderWidth": "1rpx",
"borderColor": "#fff",
"shadow": "",
"mode": "scaleToFill"
}
}, {
"type": "text",
"text": `${that.data.cardInfo.userName}`,
"css": {
"color": "#fff",
"background": "rgba(0,0,0,0)",
"width": "690rpx",
"height": "118rpx",
"top": "264rpx",
"left": "30rpx",
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "44rpx",
"fontWeight": "bold",
"maxLines": "1",
"lineHeight": "62rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
}, {
"type": "text",
"text": `${that.data.cardInfo.companyName}`,
"css": {
"color": "#fff",
"background": "rgba(0,0,0,0)",
"width": "690rpx",
"height": "37rpx",
"top": "376rpx",
"left": "30rpx",
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "26rpx",
"fontWeight": "normal",
"maxLines": "1",
"lineHeight": "37rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
}, {
"type": "text",
"text": `${that.data.cardInfo.phone}`,
"css": {
"color": "#fff",
"background": "rgba(0,0,0,0)",
"width": "690rpx",
"height": "37rpx",
"top": "410rpx",
"left": "30rpx",
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "26rpx",
"fontWeight": "normal",
"maxLines": "1",
"lineHeight": "37rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
});
//用戶身份
if (that.data.identityList.length > 0) {
let identityCount = that.data.identityList.length;
let margin_L = (750 - 84 - (84 + 24) * (identityCount - 1)) / 2;
for (let i = 0; i < that.data.identityList.length; i++) {
card.views.push({
"type": "text",
"text": `${that.data.identityList[i]}`,
"css": {
"color": "#fff",
"background": "rgba(255,255,255,0.1)",
"width": "84rpx",
"height": "28rpx",
"top": "332rpx",
"left": `${margin_L + i * (84 + 24)}rpx`,
"rotate": "0",
"borderRadius": "14rpx",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "20rpx",
"fontWeight": "normal",
"maxLines": "1",
"lineHeight": "28rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
});
}
}
// let views = card.views;
// let brand_top = 528;
let shop_top = 528;
// //品牌
if (this.data.brandList.length > 0) {
card.views.push({
"type": "text",
"text": `品牌${that.data.brandNum}個`,
"css": {
"color": "#000000",
"background": "rgba(0,0,0,0)",
"width": "230rpx",
"height": "31rpx",
"top": "528rpx",
"left": "260rpx",
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "28rpx",
"fontWeight": "bold",
"maxLines": "1",
"lineHeight": "30rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
});
for (let i = 0; i < that.data.brandList.length; i++) {
if (i === 3) {
card.views.push({
"type": "rect",
"css": {
"background": "#fff",
"width": "144rpx",
"height": "144rpx",
"top": "592rpx",
"left": `${62 + i * (144 + 16)}rpx`,
"rotate": "0",
"borderRadius": "0",
"borderWidth": "1rpx",
"borderColor": "#e5e5e5",
"shadow": "",
"color": "#fff"
}
}, {
"type": "text",
"text": "···",
"css": {
"color": "#333",
"background": "#fff",
"width": "144rpx",
"height": "34rpx",
"top": "648rpx",
"left": `${62 + i * (144 + 16)}rpx`,
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "24rpx",
"fontWeight": "normal",
"maxLines": "1",
"lineHeight": "34rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
})
} else {
let model = that.data.brandList[i];
let brandName = that.appendBrandName([model.brandName, model.brandNameEn, model.brandNameOther]);
card.views.push({
"type": "rect",
"css": {
"background": "#fff",
"width": "144rpx",
"height": "144rpx",
"top": "592rpx",
"left": `${62 + i*(144+16)}rpx`,
"rotate": "0",
"borderRadius": "0",
"borderWidth": "1rpx",
"borderColor": "#e5e5e5",
"shadow": "",
"color": "#fff"
}
}, {
"type": "image",
"url": `${model.brandLogo}`,
"css": {
"width": "112rpx",
"height": "112rpx",
"top": "593rpx",
"left": `${78 + i * (144 + 16)}rpx`,
"rotate": "0",
"borderRadius": "0",
"borderWidth": "1rpx",
"borderColor": "#fff",
"shadow": "",
"mode": "scaleToFill"
}
}, {
"type": "text",
"text": `${brandName}`,
"css": {
"color": "#333",
"background": "rgba(0,0,0,0)",
"width": "144rpx",
"height": "27rpx",
"top": "704rpx",
"left": `${62 + i * (144 + 16)}rpx`,
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "24rpx",
"fontWeight": "normal",
"maxLines": "1",
"lineHeight": "27rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
});
}
}
shop_top = 776;
that.setData({
template: card
})
}
//店鋪
if (this.data.shopList.length > 0) {
card.views.push({
"type": "text",
"text": `店鋪${that.data.shopNum}個`,
"css": {
"color": "#000000",
"background": "rgba(0,0,0,0)",
"width": "230rpx",
"height": "31rpx",
"top": `${shop_top}rpx`,
"left": "260rpx",
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "28rpx",
"fontWeight": "bold",
"maxLines": "1",
"lineHeight": "30rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
});
for (let i = 0; i < that.data.shopList.length; i++) {
if (i === 3) {
card.views.push({
"type": "rect",
"css": {
"background": "#fff",
"width": "304rpx",
"height": "112rpx",
"top": `${shop_top + 64 + parseInt(i / 2) * 128}rpx`,
"left": `${62 + i % 2 * 320}rpx`,
"rotate": "0",
"borderRadius": "12rpx",
"borderWidth": "1rpx",
"borderColor": "#e5e5e5",
"shadow": "",
"color": "#fff"
}
}, {
"type": "text",
"text": "···",
"css": {
"color": "#333",
"background": "#fff",
"width": "304rpx",
"height": "34rpx",
"top": `${shop_top + 64 + parseInt(i / 2) * 128 + 39}rpx`,
"left": "382rpx",
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "24rpx",
"fontWeight": "bold",
"maxLines": "1",
"lineHeight": "34rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "center"
}
})
} else {
let model = that.data.shopList[i];
let level_img = "/images/shoplevelicons/shoplevel_" + model.shopLevel + ".png";
card.views.push({
"type": "rect",
"css": {
"background": "#fff",
"width": "304rpx",
"height": "112rpx",
"top": `${shop_top + 64 + parseInt(i / 2) * 128}rpx`,
"left": `${62 + i % 2 * 320}rpx`,
"rotate": "0",
"borderRadius": "14rpx",
"borderWidth": "1rpx",
"borderColor": "#e5e5e5",
"shadow": "",
"color": "#fff"
}
}, {
"type": "rect",
"css": {
"background": "#fff",
"width": "80rpx",
"height": "80rpx",
"top": `${shop_top + 64 + parseInt(i / 2) * 128 + 16}rpx`,
"left": `${62 + i % 2 * 320 + 16}rpx`,
"rotate": "0",
"borderRadius": "42rpx",
"borderWidth": "1rpx",
"borderColor": "#e5e5e5",
"shadow": "",
"color": "#f8f8f8"
}
}, {
"type": "image",
"url": `${model.shopImg}`,
"css": {
"width": "80rpx",
"height": "80rpx",
"top": `${shop_top + 64 + parseInt(i / 2) * 128 + 16}rpx`,
"left": `${62 + i % 2 * 320 + 16}rpx`,
"rotate": "0",
"borderRadius": "42rpx",
"borderWidth": "1rpx",
"borderColor": "#fff",
"shadow": "",
"mode": "scaleToFill"
}
}, {
"type": "text",
"text": `${model.shopName}`,
"css": {
"color": "#333",
"background": "rgba(0,0,0,0)",
"width": "180rpx",
"height": "31rpx",
"top": `${shop_top + 64 + parseInt(i / 2) * 128 + 16}rpx`,
"left": `${62 + i % 2 * 320 + 112}rpx`,
"rotate": "0",
"borderRadius": "",
"borderWidth": "",
"borderColor": "#000000",
"shadow": "",
"padding": "0px",
"fontSize": "28rpx",
"fontWeight": "bold",
"maxLines": "1",
"lineHeight": "30rpx",
"textStyle": "fill",
"textDecoration": "none",
"fontFamily": "",
"textAlign": "left"
}
}, {
"type": "image",
"url": `${level_img}`,
"css": {
"width": "auto",
"height": "24rpx",
"top": `${shop_top + 64 + parseInt(i / 2) * 128 + 64}rpx`,
"left": `${62 + i % 2 * 320 + 112}rpx`,
"rotate": "0",
"borderRadius": "0",
"borderWidth": "1rpx",
"borderColor": "#fff",
"shadow": "",
"mode": "auto"
}
});
}
}
}
// 底部二維碼
card.views.push({
"type": "image",
"url": `${that.data.qrCodeUrl}`,
"css": {
"width": "180rpx",
"height": "180rpx",
"top": "1100rpx",
"left": "516rpx",
"rotate": "0",
"borderRadius": "",
"borderWidth": "1rpx",
"borderColor": "#fff",
"shadow": "",
"mode": "scaleToFill"
}
});
that.setData({
template: card
})
},
onImgErr(e) {
wx.hideLoading()
wx.showToast({
title: '生成分享圖失敗瓶埋,請刷新頁面重試'
})
},
//生成圖片成功后會觸發(fā)的函數(shù)
onImgOK(e) {
wx.hideLoading()
this.setData({
sharePath: e.detail.path,
visible: true,
})
},
// 打開授權(quán)設(shè)置頁面
openSetting: function() {
wx.openSetting();
this.setData({
showAuthDialog: false
});
},
//獲取微信授權(quán)保存相冊
getWritePhotos: function() {
let that = this;
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.setData({
writePhotosAlbum: true
});
},
fail() {
that.setData({
writePhotosAlbum: false
});
}
});
} else {
that.setData({
writePhotosAlbum: true
});
}
}
});
},
界面wxml布局效果圖---> 保存到相冊的畫布效果圖