微信小程序實現(xiàn)張圖片合成為一張并下載
更新時間:2019年07月16日 10:22:02 作者:xhk碎語
這篇文章主要為大家詳細介紹了微信小程序實現(xiàn)張圖片合成為一張并下載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序實現(xiàn)張圖片合成為一張并下載的具體代碼,供大家參考,具體內(nèi)容如下
微信小程序海報
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
imgUrl: "項目中圖片地址", //圖片鏈接
img: '' // 合成后圖片路徑
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
let that = this;
wx.downloadFile({
url: '線上圖片地址',
success(res) {
// 繪制背景海報到canvas
var postersize = that.setCanvasSize(750);//動態(tài)設置畫布大小
const ctx = wx.createCanvasContext('shareCanvas')
ctx.drawImage(that.data.imgUrl, 0, 0, postersize.w, postersize.h)
var re = wx.getSystemInfoSync();
var scale = 750 / 180;
var width = re.windowWidth / scale;
var height = width
var leftscale = 750 / 480; // 180為left
var left = re.windowWidth / leftscale;
var topscale = 750 / 880; // 180為top
var top = re.windowWidth / topscale;
ctx.drawImage(res.tempFilePath, left, top, width, height)
ctx.draw()
setTimeout(() => {
// code_url = this.canvasToTempImage();
//獲取臨時緩存合成照片路徑,存入data中
wx.canvasToTempFilePath({
canvasId: 'shareCanvas',
success: function (res) {
var tempFilePath = res.tempFilePath;
that.setData({
img: tempFilePath
})
console.log(tempFilePath)
},
fail: function (res) {
console.log(res);
}
});
}, 1000);
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關事件處理函數(shù)--監(jiān)聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {
},
//適配不同屏幕大小的canvas
setCanvasSize: function (width) {
var size = {};
try {
var res = wx.getSystemInfoSync();
var scale = 750 / width;//不同屏幕下canvas的適配比例;設計稿是750寬
// var scale = 1
var width = res.windowWidth / scale;
var height = res.windowHeight / scale;;
size.w = width;
size.h = height;
} catch (e) {
// Do something when catch error
console.log("獲取設備信息失敗" + e);
}
return size;
},
//點擊圖片進行預覽,長按保存分享圖片
previewImg: function (e) {
var img = this.data.img;
let _this = this;
//保存二維碼到相冊
wx.saveImageToPhotosAlbum({
filePath: img,
success: function (res) {
wx.showModal({
content: '保存成功',
confirmText: '確認',
showCancel: false,
success: function (res) {
}
});
},
fail: function (res) {
wx.showModal({
content: '保存失敗',
confirmText: '確認',
showCancel: false,
success: function (res) {
}
});
}
})
},
})
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
ion content 滾動到底部會遮住一部分視圖的快速解決方法
本文給大家?guī)砹薸on content 滾動到底部會遮住一部分視圖的快速解決方法,其實解決方法超簡單的,只要在你的controller里面預先注入$ionicScrollDelegate就可以了,感興趣的朋友通過本文一起學習吧2016-09-09
鼠標移到div,浮層顯示明細,彈出層與div的上邊距左邊距重合(示例代碼)
這篇文章主要介紹了鼠標移到div,浮層顯示明細,彈出層與div的上邊距左邊距重合的實例代碼。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
IE圖片緩存document.execCommand("BackgroundImageCache",
IE6下設置背景圖片是不會被真正cache住的,就算服務器做了cache,如果想cache住只能2011-03-03

