微信小程序中使用wxml-to-canvas

1. 下载:

npm install wxml-to-canvas --save

2. json文件导入

"usingComponents": {
    "wxml-to-canvas": "wxml-to-canvas/index"
},

3、使用组件

 index.wxml

<wxml-to-canvas width="350" height="550" class="widget"></wxml-to-canvas>
<button bindtap="extraImage">保存图片</button>

index.js

const { wxmls, style } = require('./demo.js')
let widget = null
Page({
 data: {
    
  },
 onLoad(options) {
    wx.showLoading({
      title: '图片生成中。。',
    })
     this.widget = this.selectComponent('.widget')
    // 必须要用定时器延迟,不然会报错
    setTimeout(()=>{
      this.renderToCanvas()
    },150)   
 },
  //生成海报
  renderToCanvas() {
    let obj = {
        avatar: '头像.png',
        topImg: '头部.png',
        contentBg: '内容背景.png'
    }
    // 传入要填入的动态数据
    let wxml = wxmls(obj)
    const p1 = this.widget.renderToCanvas({ wxml, style })
    p1.then((res) => {
       wx.hideLoading()
    })
  },
//保存海报
  extraImage() {
    const p2 = this.widget.canvasToTempFilePath()
    p2.then(res => {
      console.log(res)
      wx.saveImageToPhotosAlbum({
        filePath: res.tempFilePath,
        success(res) {
          console.log(res)
        }
      })
    }).catch(err=>{
      console.log(err)
    })
  }
})

demo.js中的配置

const wxmls = (info) => {
  return `
  <view class="container">
      <image class="bg" src="BG.jpg">                            
      </image>
      <image class="topImg" src="${info.topImg}"></image>
      <view class="content">
        <image class="contentBg" src="${info.contentBg}"></image>
        <view class="portrait">
          <image class="avatar" src="${info.avatar}"></image>
          <image class="frame" src="frame.png"></image>
        </view>
      </view>
    </view>
  `
}

const style = {
  container: {
    position: 'relative',
    width: 350,
    height: 550,
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
  },
  bg: {
    position: 'absolute',
    left: 0,
    top: 0,
    width: 350,
    height: 550,
  },
  topImg: {
    position: 'relative',
    width: 270,
    height: 120,
    marginTop: 40
  },

  content: {
    position: 'relative',
    width: 270,
    height: 270,
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
  },
  contentBg: {
    position: 'absolute',
    top: 0,
    left: 0,
    width: 270,
    height: 270
  },
  portrait: {
    position: 'relative',
    width: 100,
    height: 100,
    marginTop: -50,
    borderRadius: '50',
    overflow: 'hidden',
  },

  frame: {
    width: 100,
    height: 100,
    position: 'absolute',
    left: 0,
    top: 0,
  },

  avatar: {
    width: 100,
    height: 100,
    borderRadius: 50,
  }
}

module.exports = {
  wxmls,
  style
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[2\]: 二、官方示例使用方法 1.安装wxml-to-canvas npm install --save wxml-to-canvas 2.JSON 组件声明 { "usingComponents": { "wxml-to-canvas": "wxml-to-canvas", } } 3.wxml 引入组件 <video class="video" src="{{src}}"> <wxml-to-canvas class="widget"></wxml-to-canvas> </video> <image src="{{src}}" style="width: {{width}}px; height: {{height}}px"></image> 4.js 获取实例 const {wxml, style} = require('./demo.js') Page({ data: { src: '' }, onLoad() { this.widget = this.selectComponent('.widget') }, renderToCanvas() { const p1 = this.widget.renderToCanvas({ wxml, style }) p1.then((res) => { this.container = res this.extraImage() }) }, extraImage() { const p2 = this.widget.canvasToTempFilePath() p2.then(res => { this.setData({ src: res.tempFilePath, width: this.container.layoutBox.width, height: this.container.layoutBox.height }) }) } }) 问题:\["usingComponents"\]\["wxml-to-canvas"\]: "wxml-to-canvas" 未找到 回答: 根据提供的代码,\["usingComponents"\]\["wxml-to-canvas"\]: "wxml-to-canvas" 未找到的错误通常是由于未正确引入组件导致的。在JSON文件,需要将"wxml-to-canvas"添加到"usingComponents"字段,确保路径和组件名正确。在wxml文件使用<wxml-to-canvas>标签来引入组件。在js文件使用this.selectComponent('.widget')来获取组件实例。请检查您的代码,确保正确引入了"wxml-to-canvas"组件。 #### 引用[.reference_title] - *1* *3* [微信小程序使用wxml-to-canvas](https://blog.csdn.net/m0_49440146/article/details/126997331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [微信小程序插件--wxml-to-canvas(生成图片)](https://blog.csdn.net/animatecat/article/details/125981963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值