微信小程序-绘制图片并分享下载(painter)

1、引入painter插件

painter官网地址

1.1 可通过官网的方法引入painter插件,

官方插件下载地址

1.2 可下载本文附带的插件包直接引入
1.2.1 复制下载下来的文件中的painter文件夹,将其放在components目录下
1.2.2 页面中引入并使用

.json

javascript 复制代码
{
  "usingComponents": {
    "painter":"/components/painter/painter"
  },
}

.wxml

javascript 复制代码
  <painter palette="{{posterData}}" bind:imgOK="onImageOK" />

.js

javascript 复制代码
const poster = require("./posterData")

  data:{
     posterImageUrl: "", //海报图片
     posterData: {},
  }
  onLoad(options) {
     this.setData({
         posterData: poster.getPosterData('这个是一个问题','这个是一个回答')
     })
  }
  //监听海报是否生成成功
  onImageOK(e) {
    wx.hideLoading();
    this.setData({
      posterImageUrl: e.detail.path
    })
    wx.showShareImageMenu({
      path: this.data.posterImageUrl
    })
  },

posterData.js

这个文件放在上面几个文件的同级目录下

javascript 复制代码
//图片路径一定要是绝对路径或者网络路径,相对路径是无法显示的
export function getPosterData(question = '', answer = '') {
  return {
    width: '686rpx',
    height: '686rpx',
    background: "#FFFFFF",
    borderRadius: " 24rpx 24rpx 24rpx 24rpx",
    views: [{
        type: "text",
        text: "我与AI小硒的对话",
        css: {
          top: "32rpx",
          left: "32rpx",
          fontSize: "36rpx",
          color: "#000000",
          maxLines: 2,
          lineHeight: "52rpx",
          textAlign: "left",
          fontWeight: "bold"
        }
      },
      {
        type: 'image',
        url: "/agriculturalGrandModel/images/location.png",
        css: {
          top: '94rpx',
          left: '32rpx',
          width: '32rpx',
          height: '32rpx',
        },
      },
      {
        type: "text",
        text: "来源:富硒农业认知大模型",
        css: {
          top: "94rpx",
          left: "72rpx",
          fontSize: "24rpx",
          color: "rgba(0,0,0,0.9)",
          lineHeight: "40rpx",
          textAlign: "left",
        }
      },
      {
        type: 'image',
        url: '',
        css: {
          top: '32rpx',
          right: '32rpx',
          width: '100rpx',
          height: '100rpx',
        },
      },
      {
        type: 'rect',
        css: {
          top: '154rpx',
          right: '32rpx',
          width: '620rpx',
          height: '480rpx',
          color: "#F5F6F7",
          borderRadius: "10rpx 10rpx 10rpx 10rpx"
        },
      },
      {
        type: 'image',
        url: "/agriculturalGrandModel/images/user-icon.png",
        css: {
          top: '176rpx',
          right: '54rpx',
          width: '66rpx',
          height: '66rpx',
          borderRadius: "50%"
        },
      },
      {
        type: "text",
        text: question,
        css: {
          width: "435rpx",
          top: "192rpx",
          right: "154rpx",
          maxLines: 3,
          fontSize: "23rpx",
          color: "#fff",
          lineHeight: "36rpx",
          textAlign: "left",
          background: "linear-gradient( 273deg, #44BE35 0%, #6ECB63 100%)",
          boxShadow: "0rpx 2rpx 7rpx 0rpx rgba(0,0,0,0.05)",
          borderRadius: "20rpx 0rpx 20rpx 20rpx",
          padding: "20rpx"
        }
      },
      {
        type: 'image',
        url: "/agriculturalGrandModel/images/ai-icon.png",
        css: {
          top: "360rpx",
          left: '50rpx',
          width: '66rpx',
          height: '66rpx',
          borderRadius: "50%"
        },
      }, {
        type: "text",
        text: answer,
        css: {
          width: "435rpx",
          top: "360rpx",
          left: "150rpx",
          maxLines: 6,
          fontSize: "23rpx",
          color: "rgba(0, 0, 0, 0.90)",
          lineHeight: "36rpx",
          textAlign: "left",
          background: "#fff",
          boxShadow: "0rpx 2rpx 7rpx 0rpx rgba(0,0,0,0.05)",
          borderRadius: "0rpx 20rpx 20rpx 20rpx",
          padding: "20rpx"
        }
      },
    ],
  }
}

效果如下

相关推荐
拖孩10 小时前
用 AI 重解千年观音灵签,做了一个微信小程序,每天摇一摇,命运给你回应
前端·后端·微信小程序
黄华SJ520it13 小时前
顶俏洗衣液模式制度开发介绍:S2B2C社交分销+多门店核销系统全解析
前端·数据库·小程序·零售·系统开发
北极糊的狐15 小时前
钉钉小程序报错data.formatTime is not a function是因为 axml 模板中不能直接调用 Page 内自定义方法!
java·小程序·钉钉
java1234_小锋16 小时前
【免费】微信小程序网约车(打车,在线叫车,移动出行)系统(SpringBoot4+Vue3) 锋哥原创出品,必属精品
微信小程序·网约车·在线叫车·移动出行
show4332 天前
多格式导出怎么做?2026免费小程序TXT/SRT实践
开发语言·小程序·c#
AI砖家2 天前
AI编程—微信小程序开发规范
微信小程序·小程序·notepad++·ai编程
show4332 天前
2026小程序端视频转文字技术对比:识别引擎精度实测
小程序·音视频
sam-zy2 天前
微信小程序+ESP8266+Arduino 开发智能插座,有本地OTA功能
微信小程序·小程序
A24207349303 天前
微信小程序开发:常用基础语法与指令详解
微信小程序·小程序·notepad++
黄华SJ520it3 天前
青蓝送水类型社交裂变小程序系统技术拆解
小程序·系统开发