最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序?qū)崿F(xiàn)虎年春節(jié)頭像制作

 更新時(shí)間:2022年02月01日 10:32:28   作者:hackun  
春節(jié)來臨之際,看到有網(wǎng)友分享了網(wǎng)頁版的虎年頭像制作工具。本文將為大家介紹一個虎年春節(jié)頭像制作小程序,文中的示例代碼講解詳細(xì),需要的可以參考一下

馬上就到春節(jié)了,今天看到有網(wǎng)友分享了網(wǎng)頁版的虎年頭像制作工具,感覺很不錯,正好打算做個小程序練手沒啥主題,那就用這個試試吧。

先上最終效果圖:

說下實(shí)現(xiàn)流程

第一步:先獲取到當(dāng)前微信的頭像圖片,主要代碼如下,注意默認(rèn)獲取到的頭像圖片不是高清的,需要先轉(zhuǎ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組件進(jìn)行合成。

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: '請先點(diǎn)擊上方獲取微信頭像',
        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: '圖片已保存到相冊,請前往微信去設(shè)置喲!',
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log('用戶點(diǎn)擊確定');
                }
              }
            })
          }, fail(e) {
            wx.hideLoading();
            console.log("err:" + e);
          }
        })
      }
    });
  },

這樣就實(shí)現(xiàn)了主要的功能了。

最后放上小程序碼,歡迎大家掃碼體驗(yàn)一下:

最后 當(dāng)前項(xiàng)目已開源:hackun666/new-year-face: wechat new year face maker (github.com)

祝大家春節(jié)快樂,虎年大吉!

到此這篇關(guān)于微信小程序?qū)崿F(xiàn)虎年春節(jié)頭像制作的文章就介紹到這了,更多相關(guān)微信小程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

安宁市| 丰都县| 楚雄市| 大田县| 洪雅县| 民和| 延长县| 唐海县| 静海县| 卢湾区| 陵川县| 夏河县| 云安县| 大邑县| 南召县| 绩溪县| 敖汉旗| 大理市| 海阳市| 中牟县| 菏泽市| 河北省| 同德县| 平原县| 新巴尔虎右旗| 凤翔县| 青阳县| 巴林右旗| 江源县| 湟源县| 武平县| 德江县| 闵行区| 韶山市| 阳曲县| 临沭县| 日土县| 曲周县| 江阴市| 繁昌县| 秭归县|