• 當前位置:首頁 > IT技術 > 微信平臺 > 正文

    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像
    2021-08-08 14:13:12

    最近朋友圈里經常有看到這樣的頭像

    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_微信頭像
    既然這么火,大家要圖又這么難,作為程序員的自己當然要自己動手實現一個。

    老規矩,先看效果圖

    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_微信頭像_02

    仔細研究了下,發現實現起來并不難,核心代碼只有下面10行。

           wx.canvasToTempFilePath({
              x: 0,
              y: 0,
              width: num,
              height: num,
              destWidth: num,
              destHeight: num,
              canvasId: 'shareImg',
              success: function(res) {
                that.setData({
                  prurl: res.tempFilePath
                })
                wx.hideLoading()
              },
              fail: function(res) {
                wx.hideLoading()
              }
           })
    
    一,首先要創建一個小程序

    至于如何創建小程序,我這里就不在細講了,我也有寫過創建小程序的文章,也有路過相關的學習視頻,去翻下我歷史文章找找就行。

    二,創建好小程序后,我們就開始來布局

    布局很簡單,只有下面幾行代碼。

    <!-- 畫布大小按需定制 這里我按照背景圖的尺寸定的  -->
    <canvas canvas-id="shareImg"></canvas>
    <!-- 預覽區域  -->
    <view class='preview'>
      <image src='{{prurl}}' mode='aspectFit'></image>
      <button size='mini' open-type="getUserInfo" bindgetuserinfo="shengcheng" data-k="1">生成頭像1</button>
      <button size='mini' open-type="getUserInfo" bindgetuserinfo="shengcheng" data-k="2">生成頭像2</button>
      <button size='mini' open-type="getUserInfo" bindgetuserinfo="shengcheng" data-k="3">生成頭像3</button>
      <button size='mini' open-type="getUserInfo" bindgetuserinfo="shengcheng" data-k="4">生成頭像4</button>
      <button type='primary' bindtap='save'>保存分享圖</button>
    </view>
    

    實現效果圖如下
    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_微信頭像_03

    三,使用canvas來畫圖

    其實我們實現微信頭像掛紅旗,原理很簡單,就是把頭像放在下面,然后把有紅旗的相框蓋在頭像上面
    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_微信小程序_04
    下面就直接把核心代碼貼給大家

    let promise1 = new Promise(function(resolve, reject) {
          wx.getImageInfo({
            src: "../../images/xiaoshitou.jpg",
            success: function(res) {
              console.log("promise1", res)
              resolve(res);
            }
          })
        });
        let promise2 = new Promise(function(resolve, reject) {
          wx.getImageInfo({
            src: `../../images/head${index}.png`,
            success: function(res) {
              console.log(res)
              resolve(res);
            }
          })
        });
        Promise.all([
          promise1, promise2
        ]).then(res => {
          console.log("Promise.all", res)
          //主要就是計算好各個圖文的位置
          let num = 1125;
          ctx.drawImage('../../'+res[0].path, 0, 0, num, num)
          ctx.drawImage('../../' + res[1].path, 0, 0, num, num)
          ctx.stroke()
          ctx.draw(false, () => {
            wx.canvasToTempFilePath({
              x: 0,
              y: 0,
              width: num,
              height: num,
              destWidth: num,
              destHeight: num,
              canvasId: 'shareImg',
              success: function(res) {
                that.setData({
                  prurl: res.tempFilePath
                })
                wx.hideLoading()
              },
              fail: function(res) {
                wx.hideLoading()
              }
            })
          })
        })
    

    來看下畫出來的效果圖
    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_頭像掛紅旗_05

    四,頭像加紅旗畫好以后,我們就要想辦法把圖片保存到本地了

    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_國慶頭像_06
    保存圖片的代碼也很簡單。

    save: function() {
        var that = this
        wx.saveImageToPhotosAlbum({
          filePath: that.data.prurl,
          success(res) {
            wx.showModal({
              content: '圖片已保存到相冊,趕緊曬一下吧~',
              showCancel: false,
              confirmText: '好噠',
              confirmColor: '#72B9C3',
              success: function(res) {
                if (res.confirm) {
                  console.log('用戶點擊確定');
                }
              }
            })
          }
        })
      }
    

    來看下保存后的效果圖
    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_微信小程序_07
    到這里,我的微信頭像就成功的加上了小紅旗了。
    小程序10行代碼實現微信頭像掛紅旗,國慶節個性化頭像_微信小程序_08

    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码