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

在小程序中天生海报(包括用户头像和自界说文字)并且生涯到外地
使用canvas画布,,把用户头像和自界说文字定位好,,用户点击按钮生涯到外地

解决方案:判断到屏幕尺寸,,传到wxml 内里
解决方案:canvas直接支持远程图片,,不需要使用这个api
注重这里的宽度是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步上线。。。通过拖拽、拼接模浚块结构小程序商城页面,,所看即所得,,只需要美工就能做出细腻商城。。。更多小程序市肆请审查:小程序市肆
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。。KESION 一直通过手艺立异,,提供产品和服务,,助力企业向数字化转型,,通过科技驱动商业刷新,,让商业变得更智慧!
微信小程序接纳的是类似离线包加载方案,,以 转转小程序 为例,,当用户第一次翻开时会先下载好所有代码,,然后再加载页面;;;;;当用户再次进入转转小程序时,,会直接使用已下载的代码,,省去了...
微信现在官方宣布,,为知足商户及服务商更便于接入微信电子发票能力,,所有商户都可通过微信公众平台线上自助开通权限完成快速接入。。。...