Skip to content

mpx.canvasToTempFilePath(Object object, Object this)

把当前画布指定区域的内容导出生成指定大小的图片。在 draw() 回调里调用该方法才能保证图片导出成功。

支持情况: 微信、支付宝

属性类型默认值必填说明最低版本
xnumber0指定的画布区域的左上角横坐标1.2.0
ynumber0指定的画布区域的左上角纵坐标1.2.0
widthnumbercanvas 宽度 - x指定的画布区域的宽度1.2.0
heightnumbercanvas 高度 - y指定的画布区域的高度1.2.0
destWidthnumberwidth × 屏幕像素密度输出的图片的宽度1.2.0
destHeightnumberheight × 屏幕像素密度输出的图片的高度1.2.0
canvasIdstring画布标识,传入 canvas 组件的 canvas-id
canvasObject画布标识,传入 canvas 组件实例(canvas type="2d" 时使用该属性)。
fileTypestringpng目标文件的类型,合法值:pngjpg1.7.0
qualitynumber图片的质量,目前仅对 jpg 有效。取值范围为 (0, 1],不在范围内时当作 1.0 处理。1.7.0
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数

Object res

属性类型说明
tempFilePathstring生成文件的临时路径 (本地路径)

Object this
在自定义组件下,当前组件实例的 this,以操作组件内 canvas 组件

示例代码

js
mpx.canvasToTempFilePath({
  x: 100,
  y: 200,
  width: 50,
  height: 50,
  destWidth: 100,
  destHeight: 100,
  canvasId: "myCanvas",
  success(res) {
    console.log(res.tempFilePath)
  },
})