canvasdrawer 微信原生小程序生成海报图片

在小程序中生成海报是一种非常有效的推广方式

用户可以使用小程序的过程中生成小程序海报并分享给他人

通过海报的形式,用户可以直观地了解产品或服务的特点和优势

常见绘制海报方式

目前,小程序海报有两种常见的实现方式:

· canvas 绘制海报

· 服务端绘制海报

这两种方式各有千秋

canvas 绘制海报

使用 canvas 绘制海报主要有以下几个步骤

1、创建 canvasContext

2、获取网络图片的本地路径

3、绘制图片、文字等到 canvas

4、调用 wx.canvasToTempFilePath 导出图片

尽管 canvas 绘制功能强大,但实际使用中,这些操作看似简单,但调试起来却比较麻烦

而且面对一些复杂的排版时,使用 canvas 绘制相较于使用 CSS 绘制来说困难许多,如圆角、百分比、自定义字体等等。

除此之外,canvas 的宽高有最大限制,超出限制则会绘制空白

服务端绘制

小程序也可以通过调用服务端接口,将需要生成海报的数据传递给服务端,

由服务端使用 Canvas API 等第三方库来生成图片。

然而,这种绘制方式需要走网络请求,如果量大会给服务器带来一定的成本压力。

此外,对于复杂排版的实现,使用 Canvas 绘制也有一定的难度。

尽管小程序海报虽然好用,但是当遇到要求比较高的设计稿需要还原海报时,对小程序开发者来说是一个十分让人头疼的问题

考虑到海报在小程序中使用的广泛性,我们把canvas绘制海报封装成组件使用,通过对象配置的方式生成海报图,更加简洁易用~

引入组件

复制代码
"usingComponents": {
    "canvasdrawer": "/components/canvasdrawer/canvasdrawer"
  }

wxml

复制代码
<image src="{{shareImage}}" class="share-image"></image>
<canvasdrawer painting="{{painting}}" class="canvasdrawer" bind:getImage="eventGetImage"/>

js

复制代码
Page({
  data: {
    shareImage:'',
    paintingIndex:0,
    painting:{
      width: 375,
      height: 500,
      clear: true,
      views: [
        {
          type: 'image',
          url: 'https://defaultbg.png',
          top: 0,
          left: 0,
          width: 381,
          height: 500
        }
      ]
    },
    show:false,
    pop:false,
    share:"",
    pay:false,
    from:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getShare();
  },
  async getShare(){
    let _this = this;
    await get_share().then(res=>{
      const painting =  {
        width: 375,
        height: 500,
        clear: true,
        views: [
          {
            type: 'image',
            url: 'https://defaultbg.png',
            top: 0,
            left: 0,
            width: 381,
            height: 500
          },
          {
            type: 'image',
            url: res.data.personnel_share_img || 'https://default.png',
            top: 190,
            left: 48,
            textAlign: "center",
            borderRadius:8,
            width: 290,
            height: 130
          },
          {
            type: "text",
            content: res.data.activity_name || '',
            fontSize: 18,
            width: 280,
            color: "#000000",
            textAlign: "left",
            top: 336,
            left: 46,
            zIndex:200
          },
          {
            type: 'image',
            url: res.data.avatar_url || 'https://default.png',
            top: 405,
            left: 42,
            borderRadius:7,
            width: 70,
            height: 60,
            zIndex:200
          },
          {
            type: "text",
            content: res.data.guardian_name || '默认名称',
            fontSize: 16,
            color: "#000000",
            textAlign: "left",
            top: 415,
            left: 120,
            zIndex:200
          },
          {
            type: "text",
            content: "分享给你",
            width: 96,
            fontSize: 14,
            color: "#555555",
            textAlign: "left",
            top: 440,
            left: 120,
            zIndex:200
          },
          {
            type: 'image',
            url: res.data.qr_img_url || 'https://default.png',
            top: 390,
            left: 240,
            width: 100,
            height: 90,
            zIndex:200
          },
        ]
      };
      _this.setData({
        share:res.data,
        painting
      })
    })
     await _this.setData({
          mode: 'normal',
          painting:this.data.painting,
          paintingIndex: 1,
          show:true
        })
  },
  eventGetImage(event){
    let _this = this;
    const { tempFilePath } = event.detail
    this.setData({
      shareImage: tempFilePath
    })
  }
})

获取组件

相关推荐
眨眼猫会务智能体14 小时前
眨眼猫会务智能体会务系统的服务响应怎么样?出问题多久有人接?
安全·小程序·会务
微订校园外卖平台15 小时前
校园跑腿小程序上线:四端架构与校园外卖系统履约链路拆解
小程序·架构·外卖系统·本地生活·微订
m0_587383001 天前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析
迪飞特科技1 天前
智慧物业平台实战:Spring Boot 整合小程序实现报修巡检费用全流程
spring boot·后端·小程序
m0_587383002 天前
无人自助拼豆系统从需求分析到落地部署全流程实战开发指南
java·经验分享·后端·struts·小程序·架构·需求分析
小小王app小程序开发2 天前
DIY水晶小程序玩法开发功能分析
小程序
andrsted2 天前
练题簿在线免费刷题:顺序续练、优先新题与题型筛选,让复习一轮轮推进
微信小程序·小程序·刷题
2501_915921432 天前
详细教程:使用Perfdog工具测量iOS应用的功耗性能与优化方法
android·macos·ios·小程序·uni-app·cocoa·iphone
andrsted2 天前
练题簿在线免费刷题:Excel、CSV 批量导题与章节整理,把现成表格变成日常题库
学习·微信小程序·小程序·刷题·在线刷题
大爱宠物2 天前
选择宠物平台小程序服务商,需重点考察哪些核心标准?
小程序·宠物