let posterInfo ={cumulativeNumOfBrushedQuestions: 5
nickname: "LLA"
pictureUrl: "xW6gv2ySzEBg/132"
shareQrCode: "1645494065171.jpeg"
totalSignDays: 9
}
<yt-poster showpostflag="{{posterShow}}"
poster='{{posterInfo}}'></yt-poster>
<view catchtap="close" class="c-dialog-mask" wx:if="{{showflag&& mask}}" ></view>
<block wx:if="{{showflag}}">
<view class="final-img-content" catchtap="close" wx:if="{{showflag}}" catchtouchmove="touchMove">
<image show-menu-by-longpress class="final-img" bindlongpress="submitpostshare" src="{{final_url}}" />
</view>
<view class="canvas-box">
<canvas class="canvas" id="posterCanvas" type="2d"></canvas>
</view>
<!-- <view class="save-btn" bind:tap="shareImg">保存海报</view> -->
</block>
.c-dialog-mask {
position: fixed;
z-index: 999;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.6);
width: 100vw;
height: 100vh;
}
.final-img-content {
position: fixed;
width: 100vw;
height: 100vh;
top: 50%;
left: 50%;
z-index: 1000;
transform: translateX(-50%) translateY(-50%);
.final-img {
position: absolute;
width: 311px;
height: 497px;
left: 50%;
top:50%;
border-radius: 12px;
margin-top: -249px;
margin-left: -155px;
}
}
.canvas-box {
position: absolute;
top: 0;
width: 311px;
height: 497px;
left: -667px;
.canvas {
width: 311px;
height: 497px;
background: red;
}
}
const app = getApp()
Component({
properties: {
maskClosable: {
type: Boolean,
value: true
},
mask: {
type: Boolean,
value: true
},
poster: {
type: Object,
value: {}
},
showpostflag:{
type: Boolean,
value: false
},
},
data: {
posterInfo:null,
final_url:'',
showflag:false,
tabberHeight:0,
userInfo: {}
},
observers: {
poster: function(poster) {
if(!poster){return}
this.setData({posterInfo:poster,
showflag:true})
this.initCanvas()
},
showPost(value){
this.setData({showflag:value})
}
},
async attached() {
const { userInfo } = await app.get(['userInfo'])
this.setData({
userInfo: userInfo
})
const res = app.globalData.systemInfo;
this.setData({
tabberHeight: res.capsule.bottom + res.capsule.top - res.statusBarHeight,
});
},
methods: {
initCanvas() {
wx.showLoading({ title: '玩命加载中', mask: true })
const _this = this
const query = wx.createSelectorQuery().in(this)
query
.select('#posterCanvas')
.fields({ node: true, size: true })
.exec(res => {
const canvas = res[0].node
const ctx = canvas.getContext('2d')
const system = app.globalData.systemInfo
const dpr = system.pixelRatio
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
ctx.scale(dpr, dpr)
ctx.clearRect(0, 0, 311, 497)
ctx.fillStyle = '#FFFBF0'
ctx.fillRect(0, 0, 311, 497)
const image_bg = canvas.createImage()
image_bg.src = `credit/poster.png`
image_bg.onload = () => {
ctx.drawImage(image_bg, 0, 0, 311, 497)
_this.drawAvatarName(canvas, ctx)
}
})
},
drawAvatarName(canvas, ctx) {
let { nickname, pictureUrl } = this.data.posterInfo
ctx.textAlign = 'left'
ctx.textBaseline = 'middle'
ctx.font = 'normal 500 16px sans-serif'
ctx.fillStyle = '#000000'
ctx.fillText(nickname, 78, 40)
const image_avatar = canvas.createImage()
image_avatar.src = pictureUrl.replace('https://thirdwx.qlogo.cn', 'https://wx.qlogo.cn')
image_avatar.onload = () => {
ctx.save()
ctx.arc(44, 40, 22, 0, 2 * Math.PI)
ctx.clip()
ctx.drawImage(image_avatar, 22, 18, 44,44)
ctx.restore()
this.drawStudyRecord(canvas, ctx)
}
},
drawStudyRecord(canvas, ctx) {
const { totalSignDays, cumulativeNumOfBrushedQuestions, } = this.data.posterInfo
ctx.textAlign = 'right'
ctx.textBaseline = 'middle'
ctx.font = '24px Helvetica'
ctx.fillStyle = '#000000'
ctx.fillText( totalSignDays, 96, 348)
ctx.fillText(cumulativeNumOfBrushedQuestions, 234, 348)
this.drawDate(canvas, ctx)
this.drawCodeImg(canvas, ctx)
},
drawDate(canvas, ctx) {
var today = new Date()
var currentyear= today.getFullYear();
var currentmonth=today.getMonth()+1;
var currentday = today.getDate()
ctx.textAlign = 'left'
ctx.textBaseline = 'middle'
ctx.font = '12px PingFang SC'
ctx.fillStyle = '#FFFFFF'
ctx.fillText(currentyear+'.' +currentmonth+'.'+currentday, 34, 299)
},
async drawCodeImg(canvas, ctx) {
const _this = this
const image_code = canvas.createImage()
image_code.src = _this.data.posterInfo.shareQrCode
image_code.onload = () => {
ctx.save()
ctx.beginPath()
ctx.drawImage(image_code, 225, 411, 64, 64)
ctx.restore()
wx.canvasToTempFilePath({
fileType: 'jpg',
canvas: canvas,
success(res) {
_this.setData({
final_url: res.tempFilePath
})
wx.hideLoading()
}
})
}
},
shareImg() {
var that =this
wx.showShareImageMenu({
path: this.data.final_url,
success(res) {
console.log(res)
that.postSharePost()
},
fail(err) {
console.warn(err)
}
})
},
postSharePost(){
},
close() {
if (!this.data.maskClosable) return
this.setData({showflag:false})
},
submitpostshare(){
this.postSharePost()
}
}
})
|