let promise2 = new Promise(function (resolve, reject) {
src: app.globalData.userInfo.avatarUrl,//服務器返回的圖片地址
Promise.all([
//promise1, promise2
promise2, promise3, promise4
]).then(res => {
//console.log("進入promise")
const ctx = wx.createCanvasContext('shareImg')
ctx.drawImage('../../' + res[0].path, 0, 0, 545, 771)
ctx.drawImage('../../' + res[2].path, 14, 658, 90, 90)
//ctx.drawImage(_this.data.avatarUrl, 0, 0, 70, 70)
//主要就是計算好各個圖文的位置
//繪制圓角頭像
ctx.save(); // 先保存狀態 已便於畫完圓再用
ctx.beginPath(); //開始繪制
ctx.arc(272, 257, 50, 0, Math.PI * 2, false);
ctx.clip();//畫了圓 再剪切 原始畫布中剪切任意形狀和尺寸。一旦剪切了某個區域,則所有之后的繪圖都會被限制在被剪切的區域內
ctx.drawImage(res[1].path, 220, 208, 100, 100); // 推進去圖片
ctx.restore(); //恢復之前保存的繪圖上下文 恢復之前保存的繪圖上下午即狀態 可以繼續繪制
//console.log("頭像繪制成功")
//ctx.draw();
//繪制名字
ctx.setTextAlign('left')
ctx.setFillStyle('#ffffff')
ctx.font = 'normal bold 32px sans-serif';
ctx.fillText(app.globalData.userInfo.nickName, (540 - ctx.measureText(app.globalData.userInfo.nickName).width) / 2, 118)
//ctx.fillText('可愛的小公舉', (540 - ctx.measureText('可愛的小工具').width) / 2, 118)
var chr = _this.data.leftDesc.split("");//將一個字符串分割成字符串數組
var temp = "";
var row = [];
ctx.setFillStyle('#211f18')
ctx.setTextAlign('left')
ctx.font = 'normal normal 20px sans-serif';
for (var a = 0; a < chr.length; a++) {
if (ctx.measureText(temp).width < 220) {
temp += chr[a];
}
else {
a--; //這里添加了a-- 是為了防止字符丟失,效果圖中有對比
row.push(temp);
temp = "";
}
}
row.push(temp);
ctx.draw(true,setTimeout(() => {//在draw回調里調用該方法才能保證圖片導出成功。
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: 'xxx',
height: 'xxx',
destWidth: 'xxx',
destHeight: 'xxx',
canvasId: 'shareImg',
success: function (res) {
_this.setData({
prurl: res.tempFilePath,
hidden: false
})
wx.hideLoading()
},
fail: function (res) {
//console.log("最后繪制失敗");
}
})
}, 200))
})
4.保存圖片到相冊(獲取用戶保存到相冊授權)
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
//console.log('用戶已經同意小程序使用保存到相冊功能')
// 用戶已經同意小程序使用保存到相冊功能,后續調用 wx.startRecord 接口不會彈窗詢問
//wx.startWritePhotosAlbum()
},
fail(){
//console.log('用戶不同意小程序使用保存到相冊功能')
wx.showModal({
title: '警告',
content: '你點擊了拒絕授權將無法保存圖片,點擊確定重新獲取授權。',
showCancel: false,
confirmText: '返回授權',
success: function (res) {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting["scope.writePhotosAlbum"]) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
//console.log('用戶已經同意小程序使用保存到相冊功能')
// 用戶已經同意小程序使用保存到相冊功能,后續調用 wx.startRecord 接口不會彈窗詢問
//wx.startWritePhotosAlbum()
},
})
}
}
})
}
}
})
}
})
}else{
//console.log('用戶之前同意過小程序使用保存到相冊功能')
wx.saveImageToPhotosAlbum({
filePath: that.data.prurl,
success(res) {
wx.showToast({
title: '已保存到相冊',
icon: '',
duration: 1000,
mask: true
})
}
})
}
}
})
5.長按分享圖片
sharepic:function(e){
var current = e.target.dataset.src;
wx.previewImage({
current: current,
urls: [current]
})
}
6.//獲取頁面的高度,從而實現滾動
pageScrollToBottom: function () {
var _this = this;
wx.createSelectorQuery().select('#wrap').boundingClientRect(function (rect) {
// 使頁面滾動到底部
_this.setData({
scrollTop: rect.height
})
}).exec()
},
剛開始我是直接在里面設置wx.pageScrollTo來實現,每次將頁面滑到最底部,后來發現這種情況頁面抖動十分厲害,故只用上述方法獲取高度,
然后使用
<scroll-view scroll-y class="container" enable-back-to-top="true" style="height: {{windowHeight}}rpx;" bindscroll="touchclose" scroll-with-animation="true" scroll-top="{{scrollTop}}">
<!-- 內容 -->
<view>-----略------</view>
</scroll-view>
設置scrollTop的值即可
7.最后說說基於存在再測一次頁面實現的整體結構
因為頁面可以無限次循環,每次又是從第一次循環,所以這邊根據數據渲染得出,
當第一題有答案時顯示第二題,當第二題顯示時出現第三題,依次執行,五題執行完又可以實現再測一次從第一次實現
我想到了用wx:for,用wx:for一下循環五項,判斷是否展示的條件不變,用二維數組保存,剛開始測試的第一組存放在arr[0]一維數組索引為0的
第一個二維數組里,每點擊再測一次,數組的length加一,添加到下一個arr[1]數組里,這樣即可實現無限循環。
如果有用到上述api出現問題的,可以共同探討下原因,最后說一下,小程序官方api內容還是很全的,大家可以嘗試各種項目。