• 微信小程序前端生成动态海报图


    1.  
    2. //页面显示
    3. "myCanvas" type="2d" style=" width: 700rpx; height: 600rpx;" />
    4. onShareShow(e){
    5.     var that = this;
    6.     let user_id = wx.getStorageSync('user_id');
    7.     let sharePicUrl = wx.getStorageSync('sharePicUrl');
    8.     if(app.isBlank(user_id) || app.isBlank(that.data.ids)){
    9.         wx.showToast({
    10.           title'先完善学员信息~'//提示的内容
    11.           duration2000//持续的时间
    12.           icon'error'//图标有success、error、loading、none四种
    13.           masktrue //显示透明蒙层 防止触摸穿透
    14.       })
    15.       return false;
    16.     }
    17.     // if(!sharePicUrl){
    18.       that.createPoster();
    19.       return false;
    20.     // }
    21.     that.showModal();
    22.   },
    23.   //生成动态海报
    24.   createPoster() {
    25.     wx.showToast({
    26.       title'图片生成中',
    27.       masktrue,
    28.       icon'loading',
    29.       duration100000
    30.   })
    31.     var that =this;
    32.     console.log(that.data);
    33.     let w = 0
    34.     wx.getSystemInfo({
    35.       successfunction (res) {
    36.           w = res.screenWidth
    37.       }
    38.   })
    39.   console.log(w);
    40.     // return false
    41.     wx.createSelectorQuery()
    42.     .select('#myCanvas'// 在 WXML 中填入的 id
    43.     .fields({ nodetruesizetrue })
    44.     .exec((res) => {
    45.         // Canvas 对象
    46.         const canvas = res[0].node
    47.         // 渲染上下文
    48.         const ctx = canvas.getContext('2d')
    49.         // Canvas 画布的实际绘制宽高
    50.         const width = res[0].width
    51.         const height = res[0].height
    52.         const rpx =width/750
    53.         // 初始化画布大小
    54.         const dpr = wx.getWindowInfo().pixelRatio
    55.         canvas.width = width * dpr
    56.         canvas.height = height * dpr
    57.         ctx.scale(dpr, dpr)
    58.         // 图片对象
    59.         const image = canvas.createImage()//本地背景图 
    60.         const imgs = canvas.createImage() //用户头像
    61.         const qrcodes = canvas.createImage()//接口返回二维码图片
    62.         // 图片加载完成回调
    63.         image.onload = () => {
    64.             // 将图片绘制到 canvas 上
    65.             ctx.drawImage(image, 00,width,height)
    66.             ctx.drawImage(imgs,rpx*140,rpx*260,rpx*170,rpx*200)
    67.             ctx.drawImage(qrcodes,rpx*140,rpx*520,rpx*60,rpx*60)
    68.             // 文本 一定要写到图片后面 要不然会被盖住
    69.             ctx.font = "12px SimHei";
    70.             ctx.textAlgin = "left"
    71.             ctx.fillStyle = "#333333";
    72.             ctx.fillText(that.data.userClassInfo.user_name.substr(0,8), rpx*340, rpx*330);
    73.             ctx.fillText(that.data.userClassInfo.userSubject.substr(0,8), rpx*340, rpx*410);
    74.             ctx.fillText(that.data.userClassInfo.className.substr(0,10), rpx*340, rpx*480);
    75.         }
    76.         console.log(that.data);
    77.         // 设置图片src
    78.         image.src = that.data.shareImg
    79.         imgs.src = that.data.userImgUrl
    80.         qrcodes.src = that.data.qrcode
    81.         //没加定时器之前合成的图片是一片灰色,加了之后才有图片,测试了一下最少需要400毫秒
    82.         setTimeout(()=>{
    83.           wx.canvasToTempFilePath({
    84.             canvas: canvas,
    85.             successres => {
    86.                 wx.hideToast();
    87.                 // 生成的图片临时文件路径
    88.                 this.setData({
    89.                   sharePicUrl:res.tempFilePath
    90.                 }) 
    91.                 wx.setStorageSync('sharePicUrl', res.tempFilePath);
    92.                 that.showModal();
    93.             },
    94.         })
    95.         },1000)
    96.     })
    97. },

    效果如下:

    小程序二维码 头像  班级信息 都是动态生成。

  • 相关阅读:
    【Eclipse】解决插件下载速度太慢
    C++——电话号码的字母组合问题
    4点说明,为什么说母乳是宝宝高定的独家配方?母乳到底有多独家
    【2023秋招面经】深信服 前端 一面(1h)
    关于程序化交易系统的交易体系
    MySQL版数据库原理与应用期末复习重点(1)---关系代数(除运算和自连接查询、手写例题)
    原型继承
    【webrtc】初始mia
    K8S之Deployment的介绍和使用
    Python写一个ERP系统和agent智能体协同仓库和订单的案例
  • 原文地址:https://blog.csdn.net/qq_25861247/article/details/133862830