1. 程式人生 > 其它 >微信小程式實現國旗頭像,國慶個性化頭像。國慶頭像

微信小程式實現國旗頭像,國慶個性化頭像。國慶頭像

請給我一面國旗@微信官方,先上生成的頭像效果圖

小程式的製作國慶頭像的頁面

利用 canvas 繪製頭像:

核心程式碼:

       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() } })

wx.canvasToTempFilePath的使用及各個屬性介紹可到官方文件瞭解

https://developers.weixin.qq.com/miniprogram/dev/api/canvas/wx.canvasToTempFilePath.html

1,index.wxml

<!-- 畫布大小按需定製 這裡我按照背景圖的尺寸定的  -->
<view style="margin-top:88px;margin: 80px auto;">
<image src="../../images/20190906-logo2.png" height="50px" class="header"></image> </view> <view style="width: 80%; margin: 0 auto;"> <canvas canvas-id="shareImg" style="margin-left: 73px;margin-bottom:20px;"></canvas> </view> <view class="hot-biz" style="width: 85%;margin: 0 auto;border-radius: 10px;margin-bottom:15px;"
> <view class="hot-top"> <view class="tx"> 熱門 </view> </view> <view class="hot-item-list"> <scroll-view scroll-x> <view class="hot-biz-list" > <view class="item" wx:for="{{list}}" wx:key="id"> <image bindtap='selectImg' data-id='{{item}}' data-src='../../images/hat{{item}}.png' src="../../images/hat{{item}}.png" mode='aspectFill'></image> </view> </view> </scroll-view> </view> </view> <!-- 預覽區域 --> <view class='preview'> <view style="display: flex;flex-wrap: wrap;width: 90%; margin: 0 auto;"> <button size='primary' lang="zh_CN" bindtap="getUserProfile" class="btn1">點我生成</button> <button size='primary' lang="zh_CN" bindtap="save" class="btn1">儲存頭像</button> </view> <button type='primary' open-type="share" bindtap='handleShare' class="btn2">分享好友,讓TA也換上</button> </view>

2,index.wxss


/* 設定整個頁面的背景圖 */
page{
  background-image: url('https://7778-wx-cloud-vyavv-1255811323.tcb.qcloud.la/20190829-bg.png?sign=482d59e33f6df0dd658b48cfabf69a4b&t=1632849707');
}

.header{
  width: 315px!important;
  height: 125px!important;
}

.btn1{
  background-color:#EB9A41;
  margin:0 auto;
  border-radius: 50px;
  color:#ffffff;
  margin-bottom:26px;
  letter-spacing: 1px;
  font-weight: 700;

  width: 150px!important;
  height: 46px!important;
  line-height: 29px;


  display: flex;
  flex-direction: column;
}

.btn2{
  background-color:#EB9A41!important;
  
  margin:0 auto;
  border-radius: 50px;
  letter-spacing: 1px;
  font-weight: 700;

  width: 310px!important;
  height: 46px!important;
  line-height: 29px;
}

/* list公共 */
.hot-biz{
  margin-top: 10px;
  background: #fff;
}
.hot-biz .tx{
  font-size: 15px;
  margin-left: 10px;
  padding: 9px 0;
  font-weight: 700;
  color: #FF5651;
}
.hot-top{
  display: flex;
}

/* 熱門桌布 */
.hot-item-list{
  margin: 0 auto;
  width: 94%;
  margin-bottom: 20px;
}
.hot-biz-list { 
  display: flex; 
  justify-content: space-between; 
  /* flex-wrap: wrap; */
}
.hot-biz-list .item { 
  width: 50px;  
  flex-direction: column; 
  align-items: center; 
  height: 50px;
  padding-right: 8px;
}
.hot-biz-list image { 
  width: 50px; 
  height: 50px;
  border-radius:5px;
  margin: 0 auto;
  display: block;
}
/* end */

3,index.js的核心程式碼

drawImg(avatarUrl){
    let that = this;
    console.log("-- drawImg --");
    // `${that.data.userInfo.avatarUrl}`
    let promise1 = new Promise(function(resolve, reject) {
      wx.getImageInfo({
        src: avatarUrl,
        success: function(res) {
          console.log("promise1", res)
          resolve(res);
        }
      })
    });
    var index = that.data.defaultImg;
    // ../../images/head${index}.png
    // hat0.png  avg.jpg
    let promise2 = new Promise(function(resolve, reject) {
      wx.getImageInfo({
        src: `../../images/hat${index}.png`,
        success: function(res) {
          console.log(res)
          resolve(res);
        }
      })
    });
    Promise.all([
      promise1, promise2
    ]).then(res => {
      console.log("Promise.all", res)
      //主要就是計算好各個圖文的位置
      let num = 150;
      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: 960,
          destHeight: 960,
          canvasId: 'shareImg',
          success: function(res) {
            that.setData({
              prurl: res.tempFilePath
            })
            // wx.hideLoading()
          },
          fail: function(res) {
            wx.hideLoading()
          }
        })
      })
    })

  },

end:有興趣可到以下小程式體驗效果~