Git Product home page Git Product logo

wxml-to-canvas's Introduction

wxml-to-canvas

小程序内通过静态模板和样式绘制 canvas ,导出图片,可用于生成分享图等场景。代码片段

使用方法

Step1. npm 安装,参考 小程序 npm 支持

npm install --save wxml-to-canvas

Step2. JSON 组件声明

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

Step3. 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>
属性列表
属性 类型 默认值 必填 说明
width Number 400 画布宽度
height Number 300 画布高度

Step4. 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
      })
    })
  }
})

wxml 模板

支持 viewtextimage 三种标签,通过 class 匹配 style 对象中的样式。

<view class="container" >
  <view class="item-box red">
  </view>
  <view class="item-box green" >
    <text class="text">yeah!</text>
  </view>
  <view class="item-box blue">
      <image class="img" src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3582589792,4046843010&fm=26&gp=0.jpg"></image>
  </view>
</view>

样式

对象属性值为对应 wxml 标签的 cass 驼峰形式。需为每个元素指定 width 和 height 属性,否则会导致布局错误。

存在多个 className 时,位置靠后的优先级更高,子元素会继承父级元素的可继承属性。

元素均为 flex 布局。left/top 等 仅在 absolute 定位下生效。

const style = {
  container: {
    width: 300,
    height: 200,
    flexDirection: 'row',
    justifyContent: 'space-around',
    backgroundColor: '#ccc',
    alignItems: 'center',
  },
  itemBox: {
    width: 80,
    height: 60,
  },
  red: {
    backgroundColor: '#ff0000'
  },
  green: {
    backgroundColor: '#00ff00'
  },
  blue: {
    backgroundColor: '#0000ff'
  },
  text: {
    width: 80,
    height: 60,
    textAlign: 'center',
    verticalAlign: 'middle',
  }
}

接口

f1. renderToCanvas({wxml, style}): Promise

渲染到 canvas,传入 wxml 模板 和 style 对象,返回的容器对象包含布局和样式信息。

f2. canvasToTempFilePath({fileType, quality}): Promise

提取画布中容器所在区域内容生成相同大小的图片,返回临时文件地址。

fileType 支持 jpgpng 两种格式,quality 为图片的质量,目前仅对 jpg 有效。取值范围为 (0, 1],不在范围内时当作 1.0 处理。

支持的 css 属性

布局相关

属性名 支持的值或类型 默认值
width number 0
height number 0
position relative, absolute relative
left number 0
top number 0
right number 0
bottom number 0
margin number 0
padding number 0
borderWidth number 0
borderRadius number 0
flexDirection column, row row
flexShrink number 1
flexGrow number
flexWrap wrap, nowrap nowrap
justifyContent flex-start, center, flex-end, space-between, space-around flex-start
alignItems, alignSelf flex-start, center, flex-end, stretch flex-start

支持 marginLeft、paddingLeft 等

文字

属性名 支持的值或类型 默认值
fontSize number 14
lineHeight number / string '1.4em'
textAlign left, center, right left
verticalAlign top, middle, bottom top
color string #000000
backgroundColor string transparent

lineHeight 可取带 em 单位的字符串或数字类型。

变形

属性名 支持的值或类型 默认值
scale number 1

wxml-to-canvas's People

Contributors

juneandgreen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wxml-to-canvas's Issues

出现`运行内存不足`

连续生成几次海报图后会报内存不足

类型 操作系统 系统版本 微信版本
bug IOS 13.4.1 7.0.12

动态设置高变形问题

  • < wxml-to-canvas class="widget" width="500" height="{{height}}" >

this.setData({height:500});
高度发生改变后canvas生成的图片都变形

TypeScript中怎么使用?

VM290:48 页面【miniprogram_npm/wxml-to-canvas/index]错误:
 TypeError: Cannot read property 'mark' of undefined
    at VM282 index.js:734
    at VM282 index.js:813
    at Object.<anonymous> (VM282 index.js:1060)
    at __webpack_require__ (VM282 index.js:45)
    at Object.<anonymous> (VM282 index.js:202)
    at __webpack_require__ (VM282 index.js:45)
    at VM282 index.js:125
    at VM282 index.js:127
    at webpackUniversalModuleDefinition (VM282 index.js:17)
    at VM282 index.js:23

构建npm编译报错如上

wxml-to-canvas组件适配的问题

需求:
下载海报,每张海报都是wxml和wxss写的,所以想转成canvas,在下载。
问题:
在用的过程中发现,如果想下载,必须先画在页面上,但是画在页面上的wxml-to-canvas宽高不能动态设置,设置之后canvas的宽高都变成了0px,无法达到自适应。
尝试过画到页面中,但是将其隐藏:

<view style="display:none">
  <wxml-to-canvas/>
</view>

但是执行到canvasToTempFilePath()卡住了,没有任何报错信息,这是为什么呢?

渲染图片报错

MiniProgramError Cannot read property 'nodeId' of undefined TypeError: Cannot read property 'nodeId' of undefined at Image.set [as src]

请问如何动态传值?

我想绘制一个宣传海报,上面有用户的头像和昵称,因为每个用户的信息都不同,请问应该如何动态传值满足需求?

模板布局CSS属性——position 有问题

文档中说 position 布局的 relative, absolute 两个值,实际使用时会报错,提示 relative 或 absolute 未定义。
使用的是wxml-to-canvas最新版,在官方demo基础上随意添加position属性可以重现。

Unhandled promise rejection TypeError: Cannot read property 'top' of undefined

操作过程:
1、在页面中点击“运行片段”:https://developers.weixin.qq.com/s/r6UBlEm17pc6
2、在小程序开发工具IDE中打开代码片段
3、在小程序开发IDE中,直接点击“导出图片”按钮就出现提下错误:

错误信息:
Unhandled promise rejection TypeError: Cannot read property 'top' of undefined
at index.js? [sm]:165
at new W (WASubContext.js?t=wechat&s=1648970985557&v=2.23.2:2)
at Mi.canvasToTempFilePath (index.js? [sm]:163)
at Mi.extraImage (index.js? [sm]:18)
at Object.i.safeCallback (WASubContext.js?t=wechat&s=1648970985557&v=2.23.2:2)
at WASubContext.js?t=wechat&s=1648970985557&v=2.23.2:2
at wr (WASubContext.js?t=wechat&s=1648970985557&v=2.23.2:2)
at WASubContext.js?t=wechat&s=1648970985557&v=2.23.2:2
at h (WASubContext.js?t=wechat&s=1648970985557&v=2.23.2:2)
at WASubContext.js?t=wechat&s=1648970985557&v=2.23.2:2(env: Windows,mp,1.05.2203070; lib: 2.23.2)

在库文件index.js代码中,出错的位置:
canvasToTempFilePath(args = {}) {
return new Promise((resolve, reject) => {
const {
top, left, width, height
} = this.boundary
wx.canvasToTempFilePath({
x: left,
y: top,
width,

小程序开发工具版本:1.05.22
调试基础库:2.23.2

iphone8 生成带图片的海报失败, 微信版本号: 8.0.7, 调试库:2.16.1

tmp/YEBHErVHQ86De13348d5ef2982c193d1becaefde55e5.jpg#devtools_no_referrer:1 GET http://tmp/YEBHErVHQ86De13348d5ef2982c193d1becaefde55e5.jpg net::ERR_PROXY_CONNECTION_FAILED(env: macOS,mp,1.05.2105170; lib: 2.16.1)
Image (async)
set src @ VM380:1
_drawImage @ index.js? [sm]:623
success @ index.js? [sm]:641
forEach.u. @ VM389 WAService.js:2
d @ VM389 WAService.js:2
(anonymous) @ VM389 WAService.js:2
p @ VM389 WAService.js:2
(anonymous) @ VM389 WAService.js:2
d @ VM389 WAService.js:2
(anonymous) @ VM389 WAService.js:2
(anonymous) @ VM389 WAService.js:2
value @ VM389 WAService.js:2
B @ VM389 WAService.js:2
(anonymous) @ VM389 WAService.js:2
(anonymous) @ VM389 WAService.js:2
c @ VM8 asdebug.js:1
u @ VM8 asdebug.js:1
(anonymous) @ VM8 asdebug.js:1
f @ VM8 asdebug.js:1
g @ VM8 asdebug.js:1
(anonymous) @ VM8 asdebug.js:1
_ws.onmessage @ VM8 asdebug.js:1
share.js? [sm]:144 ****error: Error: createImage fail: http://tmp/YEBHErVHQ86De13348d5ef2982c193d1becaefde55e5.jpg
at HTMLImageElement.Image.onerror (index.js? [sm]:622)

画布尺寸问题

查看dom结构发现wxml-to-canvas组件内置的canvas画布尺寸是写死的400*300,那么如果要绘制长图或者其他尺寸的图,则需要等比缩小,太复杂,建议优化

MiniProgramError null is not an object (evaluating 'n.getContext')

打开代码片段,预览PC端小程序会报错,canvas 渲染失败:

MiniProgramError
null is not an object (evaluating 'n.getContext')
at canvas (miniprogram_npm/wxml-to-canvas/index.js:126:23)
at (WAService.js:2:2241907)
at
at (WAService.js:2:122361)
at (WAService.js:2:2242060)
forEach@[native code]
at n (WAService.js:2:2242039)
at (WAService.js:2:2242187)
at (WAService.js:2:3165496)
at n (WAService.js:2:3072125)
at (WAService.js:2:3072313)
at (WAService.js:2:1909240)
at Q (WAService.js:2:78233)
global code@

操作系统:macOS Monterey 12.0.1
微信开发者工具版本:1.05.2110290

设置边框变填充问题修复建议

如果给一个DOM元素设置边框: borderWidth: 2 , borderColor: #000,
生成的效果不是2宽度的边框,而是整个元素被填充了#000颜色,我翻了源码,定位问题如下
image
这里采用fillStyle是不正确的,还有this.roundRect方法的参数也传少了,我的修复方案如下
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.