前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序使用Painter组件生成海报

微信小程序使用Painter组件生成海报

作者头像
明知山
发布2020-09-03 14:32:45
1.9K0
发布2020-09-03 14:32:45
举报
文章被收录于专栏:前端开发随笔
在这里插入图片描述
在这里插入图片描述

我是直接下载github源码放到项目组件中

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
{
  "usingComponents": {
    "painter": "../../component/painter/painter"
  }
}
代码语言:javascript
复制
<view class="box">
    <painter customStyle='position: absolute; left: -9999rpx;' palette="{{template}}" bind:imgOK="canvasSuc" />
    <image mode="widthFix" src="{{image}}" bindtap="previewImg" />
    <view class="save" hover-class="hoverClick" bindtap="getSave">保存图片</view>
</view>

纯Json驱动生成海报,使用很方便

代码语言:javascript
复制
Page({
  data: {
    template: {},
    image: "",
    width: 750,
    height: 1200,
    code: "https://sucai.suoluomei.cn/sucai_zs/images/20200509145410-%E9%A6%96%E5%9B%BE.jpg",
    askName: "周某",
    askAvatar: "https://sucai.suoluomei.cn/sucai_zs/images/20200523094058-1.jpg",
    answerName: "老师",
    answerAvatar: "https://sucai.suoluomei.cn/sucai_zs/images/20200523094058-1.jpg",
    askText: "问题问题问题问题问题问题问题问题问题问题问题问题问题问题",
    answerText: "答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案答案",
    time: "2020_4_1"
  },

  onLoad() {
    this.getDraw()
  },

  getDraw() {
    wx.showLoading({
      title: '正在努力生成中',
    })
    this.setData({
      template: {
        background: '#30C4A7',
        width: this.data.width + 'px',
        height: this.data.height + 'px',
        views: [{
            type: 'text',
            text: this.data.askName + " @精致女主圈",
            css: {
              top: '30px',
              left: '33px',
              fontSize: '30px',
              color: "#fff"
            },
          }, {
            type: 'text',
            text: "12580+人 都在这里提问",
            css: {
              top: '90px',
              left: '33px',
              fontSize: '28px',
              color: "#fff"
            },
          }, {
            type: 'image',
            url: this.data.code,
            css: {
              top: '30px',
              right: '30px',
              width: '126px',
              height: '126px',
              borderRadius: '10px',
              mode: "widthFix"
            },
          }, {
            type: 'rect',
            css: {
              top: '180px',
              left: this.data.width / 2 + "px",
              width: '690px',
              height: '1000px',
              borderRadius: '10px',
              color: '#fff',
              align: 'center',
            },
          }, {
            type: 'image',
            url: this.data.askAvatar,
            css: {
              top: '230px',
              left: '174px',
              width: '100px',
              height: '100px',
              borderRadius: '50px',
              mode: "aspectFill"
            },
          }, {
            type: 'text',
            text: this.data.askName,
            css: {
              top: '350px',
              left: 690 / 2 - 125 + 'px',
              fontSize: '30px',
              color: "#000",
              align: 'center',
            },
          }, {
            type: 'image',
            url: "https://sucai.suoluomei.cn/sucai_zs/images/20200523095111-54.png",
            css: {
              top: '280px',
              left: '313px',
              width: '123px',
              mode: "widthFix"
            },
          }, {
            type: 'text',
            text: "提问",
            css: {
              top: '250px',
              left: '347px',
              fontSize: '30px',
              color: "#000"
            },
          }, {
            type: 'image',
            url: this.data.answerAvatar,
            css: {
              top: '230px',
              right: '174px',
              width: '100px',
              height: '100px',
              borderRadius: '50px',
              mode: "aspectFill"
            },
          }, {
            type: 'text',
            text: this.data.answerName,
            css: {
              top: '350px',
              right: 690 / 2 - 125 + 'px',
              fontSize: '30px',
              color: "#000",
              align: 'center',
            },
          }, {
            type: 'text',
            text: this.data.askText,
            css: {
              maxLines: 2,
              lineHeight: "40px",
              width: "629px",
              top: '420px',
              left: "60px",
              fontSize: '30px',
              color: "#000"
            },
          }, {
            type: 'text',
            text: this.data.answerText,
            css: {
              maxLines: 7,
              lineHeight: "40px",
              width: "629px",
              top: '550px',
              left: "60px",
              fontSize: '30px',
              color: "#000"
            },
          }, {
            type: 'text',
            text: this.data.time,
            css: {
              top: '850px',
              right: "60px",
              fontSize: '28px',
              color: "#666666"
            },
          },
          {
            type: 'image',
            url: this.data.code,
            css: {
              top: '900px',
              left: this.data.width / 2 - 100 + 'px',
              width: '200px',
              height: '200px',
              mode: "aspectFill"
            },
          },
          {
            type: 'text',
            text: "长按或保存二维码查看原文",
            css: {
              top: '1120px',
              left: this.data.width / 2 + 'px',
              fontSize: '28px',
              color: "#666666",
              align: 'center',
            },
          },
        ],
      }
    })
  },
  // 生成成功
  canvasSuc(e) {
    console.log(e)
    wx.hideLoading()
    this.setData({
      image: e.detail.path
    })
  },
  
   getSave() {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.image,
      success: (res) => {
        wx.showToast({
          title: '保存成功',
        });
      }
    })
    wx.getSetting({
      success: (set) => {
        if (set.authSetting['scope.writePhotosAlbum'] == false) {
          wx.openSetting()
        }
      }
    })
  },

  previewImg() {
    wx.previewImage({
      current: this.data.image,
      urls: [this.data.image]
    })
  },
})
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/05/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档