微信小程序?qū)崿F(xiàn)張圖片合成為一張并下載
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)張圖片合成為一張并下載的具體代碼,供大家參考,具體內(nèi)容如下
微信小程序海報(bào)
Page({ /** * 頁面的初始數(shù)據(jù) */ data: { imgUrl: "項(xiàng)目中圖片地址", //圖片鏈接 img: '' // 合成后圖片路徑 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { let that = this; wx.downloadFile({ url: '線上圖片地址', success(res) { // 繪制背景海報(bào)到canvas var postersize = that.setCanvasSize(750);//動(dòng)態(tài)設(shè)置畫布大小 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(); //獲取臨時(shí)緩存合成照片路徑,存入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 () { }, /** * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點(diǎn)擊右上角分享 */ onShareAppMessage: function () { }, //適配不同屏幕大小的canvas setCanvasSize: function (width) { var size = {}; try { var res = wx.getSystemInfoSync(); var scale = 750 / width;//不同屏幕下canvas的適配比例;設(shè)計(jì)稿是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("獲取設(shè)備信息失敗" + e); } return size; }, //點(diǎn)擊圖片進(jìn)行預(yù)覽,長按保存分享圖片 previewImg: function (e) { var img = this.data.img; let _this = this; //保存二維碼到相冊 wx.saveImageToPhotosAlbum({ filePath: img, success: function (res) { wx.showModal({ content: '保存成功', confirmText: '確認(rèn)', showCancel: false, success: function (res) { } }); }, fail: function (res) { wx.showModal({ content: '保存失敗', confirmText: '確認(rèn)', showCancel: false, success: function (res) { } }); } }) }, })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序動(dòng)態(tài)設(shè)置導(dǎo)航欄標(biāo)題的實(shí)現(xiàn)步驟
實(shí)際開發(fā)中很多時(shí)候我們需要通過上個(gè)頁面?zhèn)鬟^來的值動(dòng)態(tài)的更改標(biāo)題欄文字,下面這篇文章主要給大家介紹了關(guān)于微信小程序動(dòng)態(tài)設(shè)置導(dǎo)航欄標(biāo)題的實(shí)現(xiàn)步驟,需要的朋友可以參考下2023-02-02JavaScript實(shí)現(xiàn)cookie的寫入、讀取、刪除功能
cookie 是存在有效期的,在默認(rèn)情況下,一個(gè) cookie 的生命周期就是在瀏覽器關(guān)閉的時(shí)候結(jié)束,本文給大家介紹關(guān)于javascript對于cookie的簡單操作,比如對于cookie的寫入和刪除,代碼簡單易懂,需要的朋友參考下2015-11-11es6中的import導(dǎo)入模塊 和 export導(dǎo)出模塊詳解
require 是 CommonJS 模塊系統(tǒng)中用于導(dǎo)入模塊的關(guān)鍵字,采用同步加載方式;而 import 和 export 是 ES6 模塊系統(tǒng)中用于導(dǎo)入和導(dǎo)出模塊的關(guān)鍵字,采用靜態(tài)加載方式,并且支持異步加載,這篇文章主要介紹了es6中的import導(dǎo)入模塊 和 export導(dǎo)出模塊,需要的朋友可以參考下2023-11-11uni-app實(shí)現(xiàn)點(diǎn)贊評論功能
這篇文章主要介紹了uni-app實(shí)現(xiàn)點(diǎn)贊評論功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11非阻塞動(dòng)態(tài)加載javascript廣告實(shí)現(xiàn)代碼
非阻塞動(dòng)態(tài)加載javascript廣告,需要的朋友可以參考下。2010-11-11微信小程序?qū)崿F(xiàn)經(jīng)典window掃雷游戲
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)經(jīng)典window掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實(shí)現(xiàn)示例
需要一個(gè)用戶上傳頭像預(yù)覽的功能,因此寫了一段上傳圖片預(yù)覽JS腳本,Input file圖片預(yù)覽的實(shí)現(xiàn),需要的朋友可以看看2014-10-10