# wx.canvasToTempFilePath(Object object, Object this)

> __以 [Promise 风格](/miniprogram/dev/framework/app-service/api.md#异步-API-返回-Promise) 调用__：支持
> 
> __小程序插件__：支持，需要小程序基础库版本不低于 [1.9.6](1003706)
> 
> __微信 Windows 版__：支持
> 
> __微信 Mac 版__：支持
> 
> __微信 鸿蒙 OS 版__：支持

> 相关文档: [画布指南](/miniprogram/dev/framework/ability/canvas.md)、[canvas 组件介绍](/miniprogram/dev/component/canvas.md)

## 功能描述

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

## 参数

### Object object


```json
[
  {
    "属性": "x",
    "类型": "number",
    "默认值": "0",
    "必填": "否",
    "说明": "指定的画布区域的左上角横坐标",
    "最低版本": "[1.2.0](1003706)"
  },
  {
    "属性": "y",
    "类型": "number",
    "默认值": "0",
    "必填": "否",
    "说明": "指定的画布区域的左上角纵坐标",
    "最低版本": "[1.2.0](1003706)"
  },
  {
    "属性": "width",
    "类型": "number",
    "默认值": "canvas宽度-x",
    "必填": "否",
    "说明": "指定的画布区域的宽度",
    "最低版本": "[1.2.0](1003706)"
  },
  {
    "属性": "height",
    "类型": "number",
    "默认值": "canvas高度-y",
    "必填": "否",
    "说明": "指定的画布区域的高度",
    "最低版本": "[1.2.0](1003706)"
  },
  {
    "属性": "destWidth",
    "类型": "number",
    "默认值": "width*屏幕像素密度",
    "必填": "否",
    "说明": "输出的图片的宽度",
    "最低版本": "[1.2.0](1003706)"
  },
  {
    "属性": "destHeight",
    "类型": "number",
    "默认值": "height*屏幕像素密度",
    "必填": "否",
    "说明": "输出的图片的高度",
    "最低版本": "[1.2.0](1003706)"
  },
  {
    "属性": "canvasId",
    "类型": "string",
    "默认值": "",
    "必填": "否",
    "说明": "画布标识，传入 [canvas](/miniprogram/dev/component/canvas.md) 组件的 canvas-id",
    "最低版本": ""
  },
  {
    "属性": "canvas",
    "类型": "Object",
    "默认值": "",
    "必填": "否",
    "说明": "画布标识，传入 [canvas](/miniprogram/dev/component/canvas.md) 组件实例 （canvas type=\"2d\" 时使用该属性）。",
    "最低版本": ""
  },
  {
    "属性": "fileType",
    "类型": "string",
    "默认值": "png",
    "必填": "否",
    "说明": "目标文件的类型",
    "最低版本": "[1.7.0](1003706)",
    "子项": [
      {
        "合法值": "jpg",
        "说明": "jpg 图片"
      },
      {
        "合法值": "png",
        "说明": "png 图片"
      }
    ]
  },
  {
    "属性": "quality",
    "类型": "number",
    "默认值": "",
    "必填": "否",
    "说明": "图片的质量，目前仅对 jpg 有效。取值范围为 (0, 1]，不在范围内时当作 1.0 处理。",
    "最低版本": "[1.7.0](1003706)"
  },
  {
    "属性": "success",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用成功的回调函数",
    "最低版本": ""
  },
  {
    "属性": "fail",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用失败的回调函数",
    "最低版本": ""
  },
  {
    "属性": "complete",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用结束的回调函数（调用成功、失败都会执行）",
    "最低版本": ""
  }
]
```

#### object.success 回调函数
##### 参数

###### Object res

| 属性 | 类型 | 说明 |
| ---- | ---- | ---- |
| tempFilePath  | string | 生成文件的临时路径 (本地路径) |

### Object this

在自定义组件下，当前组件实例的this，以操作组件内 [canvas](/miniprogram/dev/component/canvas.md) 组件

## 示例代码

```javascript

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

```
