pp电子

登录
免费开通

小程序天生海报生涯分享图片完全指南

在我们分享小程序时,,会有一个图片式的海报,,这种功效显示是怎样显示的,,下面小编为各人解答小程序天生海报生涯分享图片完全指南

小程序天生海报生涯分享图片完全指南

营业

在小程序中天生海报(包括用户头像和自界说文字)并且生涯到外地

实现思绪

使用canvas画布,,把用户头像和自界说文字定位好,,用户点击按钮生涯到外地

小程序天生海报生涯分享图片完全指南

注重事项 难点

小程序canvas不支持自界说宽高,,横竖我没找到,,canvas画布大部分营业都需要全屏,,响应式,,至少宽100%
解决方案:判断到屏幕尺寸,,传到wxml 内里
远程图片不可直接使用 getImageInfo 获取,,需要生涯到外地
解决方案:canvas直接支持远程图片,,不需要使用这个api

手艺栈

  • canvas
  • wx.createCanvasContext
  • wx.canvasToTempFilePath
  • Promise

实战

首先我们在wxml内里写一个canvas占位
注重这里的宽度是100%,,响应式,,海报的高posterHeight 是从js内里动态盘算的
<canvas canvas-id="starkImg" style="width:100%;height:{{posterHeight}}px;"></canvas>

凭证屏幕动态盘算海报的尺寸

data: {
  motto: 'Hello World',
  hidden: true,
  userInfo: {},
  hasUserInfo: false,
  windowWidth: '',
  posterHeight: '',
},
onLoad: function () {
  const poster = {
    "with": 375,
    "height": 587
  }
  const systemInfo = wx.getSystemInfoSync()
  let windowWidth = systemInfo.windowWidth
  let windowHeight = systemInfo.windowHeight
  let posterHeight = parseInt((windowWidth / poster.with) * poster.height)
  this.setData({
    windowWidth: windowWidth,
    posterHeight: posterHeight
  })
}

配景图片天生

  const that = this
  // 图片路径
  const imagePath = '../../static/image/common/'
  let bgimgPromise = new Promise(function (resolve, reject) {
    console.log('data', that.data)
    wx.getImageInfo({
      src: imagePath + "base.png",
      success: function (res) {
        resolve(res);
      }
    })
  });

头像直接使用远程头像

初始化的时间,,调取,,一定在天生海报之前
此处可以存储外地,,或使用状态都可以

wxml

// 可以从后端接口获取 或 官方自己远程地点

 
  <button class="share" type="primary" open-type="getUserInfo" bindgetuserinfo="getUserInfo">最先答题(获取用户信息)</button>

js

  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    let userInfo = e.detail.userInfo
    console.log('userInfo', userInfo)
    // 更新用户信息
    // api.post('更新用户信息的url', userInfo)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

天生海报配景和图片

wxml

bgimgPromise.then(res => {
      console.log('Promise.all', res)
      const ctx = wx.createCanvasContext('shareImg')
      ctx.width = windowWidth
      ctx.height = posterHeight
      console.log(windowWidth, posterHeight)
      // 配景图
      ctx.drawImage('../../' + res[0].path, 0, 0, windowWidth, posterHeight, 0, 0)
      // 头像
      ctx.drawImage(that.data.userInfo.avatarUrl, 48, 182, 58, 58, 0, 0)
      ctx.setTextAlign('center')
      ctx.setFillStyle('#000')
      ctx.setFontSize(22)
      // ctx.fillText('分享文字2:stark.wang出品', 88, 414)
      ctx.fillText('分享文字1我的博客:https://shudong.wang', 55, 414)
      ctx.stroke()
      ctx.draw()
    })

生涯到外地

onLoad: function () {
  share: function () {
    var that = this
    wx.showLoading({
      title: '正在制作海报。。。。。。。。。'
    })
    new Promise(function (resolve, reject) {
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 444,
        height: 500,
        destWidth: 555,
        destHeight: 666,
        canvasId: 'starkImg',
        success: function (res) {
          console.log(res.tempFilePath);
          that.setData({
            prurl: res.tempFilePath,
            hidden: false
          })
          wx.hideLoading()
          resolve(res)
        },
        fail: function (res) {
          console.log(res)
        }
      })
    }).then(res => {
      console.log(res)
      this.save()
    })
  }
}

更新头像裁剪为圆形

ctx.save() // 对目今区域生涯
ctx.beginPath() // 最先新的区域
ctx.arc(73, 224, 38, 0, 2 * Math.PI);
ctx.clip();  // 从画布上裁剪出这个圆形
ctx.drawImage(res[1], 36, 186, 94, 94, 0, 0) // 把图片填充进裁剪的圆形
ctx.restore() // 恢复

上面是远程毗连容易爆发请求失败

把头像提前存到外地存储中解决
getImg: function () {
  let avatarUrl = this.data.userInfo.avatarUrl
  downLoadFile(avatarUrl).then((res) => {
    console.log(res)
    wx.saveFile({
      tempFilePath: res.data.tempFilePath,
      success: function (res) {
        wx.setStorageSync('avatarUrl', res.savedFilePath)
      }
    })
  })
},

获取头像

// 头像
let promiseAvatarUrl = new Promise(function (resolve, reject) {
  resolve(wx.getStorageSync('avatarUrl'))
}).catch(res=>{
  console.log('catch',res)
});

配景照旧稳固

const that = this
let promiseBdImg = new Promise(function (resolve, reject) {
  console.log('data', that.data)
  wx.getImageInfo({
    src: imagePath + "base1.png",
    success: function (res) {
      console.log('promiseBdImg', res)
      resolve(res);
    }
  })

此时天生canvas更新

Promise.all([
    promiseBdImg, promiseAvatarUrl
  ]).then(res => {
    console.log('Promise.all', res)
    const ctx = wx.createCanvasContext('shareImg')
    ctx.width = windowWidth
    ctx.height = posterHeight
    console.log(windowWidth, posterHeight)
    //主要就是盘算好各个图文的位置
    ctx.drawImage('../../' + res[0].path, 0, 0, windowWidth, posterHeight, 0, 0)
    ctx.save() // 对目今区域生涯
    ctx.beginPath() // 最先新的区域
    ctx.arc(73, 224, 38, 0, 2 * Math.PI);
    ctx.clip();  // 从画布上裁剪出这个圆形
    ctx.drawImage(res[1], 36, 186, 94, 94, 0, 0) // 把图片填充进裁剪的圆形
    ctx.restore() // 恢复
    ctx.setTextAlign('center')
    ctx.setFillStyle('#000')
    ctx.setFontSize(22)
    ctx.save()
    ctx.beginPath();
    ctx.fillText('作者:stark.wang', 545 / 2, 130)
    ctx.fillText('我的博客:http://shudong.wang', 190, 414)
    ctx.stroke()
    ctx.draw()
  })

以上是小程序天生海报生涯分享图片完全指南,,更多小程序开发内容,,请审查本网站,,谢谢。 。。

小程序工具提供多类型商城/门店小程序制作,,可视化编辑 1秒天生5步上线。 。。通过拖拽、拼接模浚块结构小程序商城页面,,所看即所得,,只需要美工就能做出细腻商城。 。。更多小程序市肆请审查:小程序市肆


【本站声明】
  1、本站文章中所选用的图片及文字泉源于网络以及用户投稿,,由于未联系到知识产权人或未发明有关知识产权的挂号,,若有知识产权人并不肯意我们使用,,若是有侵权请连忙联系。 。。
  2、本网站差池文章中所涉及的内容真实性、准确性、可靠性认真,,仅系客观性形貌,,如您需要相识该类商品/服务详细的资讯,,请您直接与该类商品/服务的提供者联系。 。。


KESION pp电子软件

KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。 。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。 。。

KESION 一直通过手艺立异,,提供产品和服务,,助力企业向数字化转型,,通过科技驱动商业刷新,,让商业变得更智慧!



▼点击进入pp电子官网相识更多



热门标签
SaaS
上/下篇
  • 转转小程序分包加载实例

  • 微信电子发票开发流程,微信电子发票接入流程

换一换相关推荐
精选内容
热门精选
pp电子·模拟器(试玩游戏)官方网站 pp电子·模拟器(试玩游戏)官方网站 pp电子·模拟器(试玩游戏)官方网站
【网站地图】
小程序天生海报生涯分享图片完全指南 - KESION pp电