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


    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. },

    效果如下:

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

  • 相关阅读:
    zk的二阶段提交图解
    NDIS小端口驱动开发(三)
    Spark杂谈
    简明SQL条件查询指南:掌握WHERE实现数据筛选
    ​性能测试基础——性能测试方案(示例)
    vscode一键生成佛祖保佑永无bug
    微信小程序---配置和属性(全局配置的简单使用,运算符以及一些常用属性)
    【疑难杂症】-一种简单高效的Spring Security oauth token兼容JSON格式的办法
    vue3输入单号和张数,自动生成连号的单号
    基于java+ssm幼儿园教学网站管理系统vue-计算机毕业设计
  • 原文地址:https://blog.csdn.net/qq_25861247/article/details/133862830