微信小程序实现国旗头像,国庆个性化头像。国庆头像

摘要:
viewstyle=“margin top;margin bottom;viewclass=“tx”>viewclass=”热门项目列表“>viewclass=”热门商业列表“<key=“id”&gt:viewclass='预览'>view style=“display;弯曲包保证金;/按钮>&书信电报;

如需自取,完整项目源码:https://gitee.com/vxsoft/online-h

若对你有帮助,烦请star一个

微信小程序实现国旗头像,国庆个性化头像。国庆头像第1张

请给我一面国旗@微信官方,先上生成的头像效果图

 小程序的制作国庆头像的页面

微信小程序实现国旗头像,国庆个性化头像。国庆头像第2张

利用 canvas 绘制头像:

核心代码:

       wx.canvasToTempFilePath({
          x: 0,
          y: 0,
           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=" 80%; margin: 0 auto;">
  <canvas canvas-id="shareImg" style="margin-left: 73px;margin-bottom:20px;"></canvas>
</view>


<view class="hot-biz" style=" 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; 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,
           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:有兴趣可到以下小程序体验效果~

微信小程序实现国旗头像,国庆个性化头像。国庆头像第3张

 

免责声明:内容来源于网络,仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【最小生成树】口袋的天空Varnish简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android实现简单的检测手机自由落体关闭屏幕

实现功能如下:在背景运行app,检测到自由落体状态时,熄灭屏幕,可重复测试。 1. 检测自由落体动作  需要使用到加速度感应器 TYPE_ACCELEROMETER SensorManager mSensorManager; private float mLastX; private float mLastY; private float mLastZ;...

HTML邮件模板

邮件要求兼容 outlook 等邮箱软件,发现很多样式都不生效。找到的模板如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x...

ros自定义消息的时候报错ImportError: No module named em

Traceback (most recent call last):  File "/opt/ros/kinetic/share/gencpp/cmake/../../../lib/gencpp/gen_cpp.py", line 41, in <module>    import genmsg.template_tools  File "/o...

【原创】QT简单计算器

代码 //main.cpp #include "calculator_111.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Calculator_111 w; w.show(); retu...

如何通过ABAP程序添加SAP_ALL权限

从网上抄来的程序,没运行过。。。仅供参考: 更新了usr04,ust04,usrbf2这三张表。             ABAP   REPORT ZTESTCREATEUSER. data: l_USR04 LIKE USR04 , l_UST04 LIKE UST04 , l_PROFS LIKE USR04-PROFS , l_USRBF2...

使用dom4j解析xml文件,并封装为javabean对象

See Also:http://blog.csdn.net/tao_sun/article/details/12452167 延伸: /** * 将xml内容封装到Vehicle对象中 * @param resultXML 内容为String格式的xml * @return */ @SuppressW...