微信小程序實現(xiàn)虎年春節(jié)頭像制作
馬上就到春節(jié)了,今天看到有網(wǎng)友分享了網(wǎng)頁版的虎年頭像制作工具,感覺很不錯,正好打算做個小程序練手沒啥主題,那就用這個試試吧。
先上最終效果圖:
說下實現(xiàn)流程
第一步:先獲取到當前微信的頭像圖片,主要代碼如下,注意默認獲取到的頭像圖片不是高清的,需要先轉換成高清圖片,避免生成之后很模糊。
getUserProfile(e) { console.log(e) let that = this; wx.getUserProfile({ desc: '僅用于生成頭像使用', success: (res) => { var url = res.userInfo.avatarUrl; while (!isNaN(parseInt(url.substring(url.length - 1, url.length)))) { url = url.substring(0, url.length - 1) } url = url.substring(0, url.length - 1) + "/0"; res.userInfo.avatarUrl = url; console.log(JSON.stringify(res.userInfo)); that.setData({ userInfo: res.userInfo, hasUserInfo: true }) that.drawImg(); } }); },
第二步:合成頭像,把素材圖片和第一步獲取到的頭像圖片,獲取到本地文件,然后利用小程序的cavas組件進行合成。
drawImg() { let that = this; wx.showLoading({ title: '生成頭像中...', }) let promise1 = new Promise(function (resolve, reject) { wx.getImageInfo({ src: that.data.userInfo.avatarUrl, success: function (res) { resolve(res); } }) }); var mask_id = that.data.now_mask; let promise2 = new Promise(function (resolve, reject) { wx.getImageInfo({ src: `../../assets/img/mask0${mask_id}.png`, success: function (res) { console.log(res) resolve(res); } }) }); Promise.all([ promise1, promise2 ]).then(res => { console.log(res) var windowWidth = wx.getSystemInfoSync().windowWidth var context = wx.createCanvasContext('myAvatar'); var size = windowWidth /750 * 500 // var size = 500 context.drawImage(res[0].path, 0, 0, size, size); context.draw(true) context.save(); context.drawImage('../../'+res[1].path, 0, 0, size, size); context.draw(true) context.save(); }) wx.hideLoading() },
第三步:下載合成的圖片到本地相冊。
canvasToTempFile(){ if(!this.data.userInfo){ wx.showModal({ title: '溫馨提示', content: '請先點擊上方獲取微信頭像', showCancel: false, }) return } var windowWidth = wx.getSystemInfoSync().windowWidth var size = 500 // var dpr = 750 / windowWidth wx.canvasToTempFilePath({ x: 0, y: 0, height: size, width: size, canvasId: 'myAvatar', success: (res) => { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: result => { wx.hideLoading(); wx.showModal({ content: '圖片已保存到相冊,請前往微信去設置喲!', showCancel: false, success: function(res) { if (res.confirm) { console.log('用戶點擊確定'); } } }) }, fail(e) { wx.hideLoading(); console.log("err:" + e); } }) } }); },
這樣就實現(xiàn)了主要的功能了。
最后放上小程序碼,歡迎大家掃碼體驗一下:
最后 當前項目已開源:hackun666/new-year-face: wechat new year face maker (github.com)
祝大家春節(jié)快樂,虎年大吉!
到此這篇關于微信小程序實現(xiàn)虎年春節(jié)頭像制作的文章就介紹到這了,更多相關微信小程序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于Javascript加載執(zhí)行優(yōu)化的研究報告
這篇文章主要介紹了關于Javascript加載執(zhí)行優(yōu)化的研究報告,需要的朋友可以參考下2014-12-12javascript創(chuàng)建cookie、讀取cookie
這篇文章主要介紹了javascript創(chuàng)建cookie、讀取cookie的操作方法,內容簡單易學,感興趣的小伙伴們可以參考一下2016-03-03javascript getElementsByTagName
DC大神為早期不支持getElementsByTagName的瀏覽器寫的hack,當然與原生的不能同日而言,原生的用到緩存機制呢。2011-01-01JavaScript從數(shù)組的indexOf()深入之Object的Property機制
這篇文章主要介紹了JavaScript從數(shù)組的indexOf()深入——Object的Property機制的相關資料,需要的朋友可以參考下2016-05-05moment.js輕松實現(xiàn)獲取當前日期是當年的第幾周
這篇文章主要介紹了moment.js輕松實現(xiàn)獲取當前日期是當年的第幾周,需要的朋友可以參考下2015-02-02