# Canvas.toTempFilePath(Object object)

> __以 [Promise 风格](/miniprogram/dev/framework/app-service/api.md#异步-API-返回-Promise) 调用__：不支持

## 功能描述

将当前 Canvas 保存为一个临时文件。**如果使用了开放数据域，则生成后的文件仅能被用于以下接口：`wx.saveImageToPhotosAlbum`、`wx.shareAppMessage`、`wx.onShareAppMessage`、`wx.previewImage`、`wx.previewMedia`、`wx.onShareTimeline`、`wx.showShareImageMenu`**

## 参数

### Object object


```json
[
  {
    "属性": "x",
    "类型": "number",
    "默认值": "0",
    "必填": "否",
    "说明": "截取 canvas 的左上角横坐标"
  },
  {
    "属性": "y",
    "类型": "number",
    "默认值": "0",
    "必填": "否",
    "说明": "截取 canvas 的左上角纵坐标"
  },
  {
    "属性": "width",
    "类型": "number",
    "默认值": "canvas 的宽度",
    "必填": "否",
    "说明": "截取 canvas 的宽度"
  },
  {
    "属性": "height",
    "类型": "number",
    "默认值": "canvas 的高度",
    "必填": "否",
    "说明": "截取 canvas 的高度"
  },
  {
    "属性": "destWidth",
    "类型": "number",
    "默认值": "canvas 的宽度",
    "必填": "否",
    "说明": "目标文件的宽度，会将截取的部分拉伸或压缩至该数值"
  },
  {
    "属性": "destHeight",
    "类型": "number",
    "默认值": "canvas 的高度",
    "必填": "否",
    "说明": "目标文件的高度，会将截取的部分拉伸或压缩至该数值"
  },
  {
    "属性": "fileType",
    "类型": "string",
    "默认值": "png",
    "必填": "否",
    "说明": "目标文件的类型",
    "子项": [
      {
        "合法值": "jpg",
        "说明": "jpg 文件"
      },
      {
        "合法值": "png",
        "说明": "png 文件"
      }
    ]
  },
  {
    "属性": "quality",
    "类型": "number",
    "默认值": "1.0",
    "必填": "否",
    "说明": "jpg图片的质量，仅当 fileType 为 jpg 时有效。取值范围为 0.0（最低）- 1.0（最高），不含 0。不在范围内时当作 1.0"
  },
  {
    "属性": "success",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用成功的回调函数"
  },
  {
    "属性": "fail",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用失败的回调函数"
  },
  {
    "属性": "complete",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用结束的回调函数（调用成功、失败都会执行）"
  }
]
```

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

###### Object res

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

## 示例代码

Canvas.toTempFilePath
```javascript

canvas.toTempFilePath({
  x: 10,
  y: 10,
  width: 200,
  height: 150,
  destWidth: 400,
  destHeight: 300,
  success: (res) => {
    wx.shareAppMessage({
      imageUrl: res.tempFilePath
    })
  }
})

```

Canvas.toTempFilePathSync
```javascript

let tempFilePath = canvas.toTempFilePathSync({
  x: 10,
  y: 10,
  width: 200,
  height: 150,
  destWidth: 400,
  destHeight: 300
})
wx.shareAppMessage({
  imageUrl: tempFilePath
})

```
