title: Taro.canvasToTempFilePath(option, component)

sidebar_label: canvasToTempFilePath

Exports the content of the specified area of the current canvas to generate an image of the specified size. This method is called in the draw() callback to ensure the image is exported successfully.

Reference

Type

  1. (option: Option, component?: Record<string, any>) => Promise<SuccessCallbackResult>

Parameters

Option

Property Type Default Required Description
canvas CanvasProps No The canvas identifier, used when passing in canvas component instances (canvas type="2d")
canvasId string Yes The canvas ID, which is the canvas-id passed into the canvas component.
quality number No The image quality. This is only valid for .jpg images. Value range: (0, 1]. Values not in the range are processed as 1.
destHeight number No The height of the output image.
destWidth number No The width of the output image.
fileType "jpg" | "png" "png" No The type of the destination file.
height number No The height of the specified canvas area.
width number No The width of the specified canvas area.
x number No The x-coordinate of the top-left corner of the specified canvas area.
y number No The y-coordinate of the top-left the corner of the specified canvas area.
complete (res: any) => void No The callback function used when the API call completed (always executed whether the call succeeds or fails)
fail (res: any) => void No The callback function for a failed API call
success (res: Result) => void No The callback function for a successful API call

SuccessCallbackResult

Property Type Description
tempFilePath string The temporary path of the generated file
errMsg string Call result

fileType

Property Description
jpg A .jpg image
png A .png image

CanvasProps

Property Type Default Required Description
type string No The type of the canvas. Only webGL is supported.
canvasId string No The unique identifier of the canvas component. This property can be ignored if a type is specified.
disableScroll boolean false No Disables screen scrolling and swipe-down-to-refresh features when the a finger taps to move on the canvas and a gesture event is bound.
onTouchStart CommonEventFunction<any> No Finger touch starts
onTouchMove CommonEventFunction<any> No Finger moves after touch
onTouchEnd CommonEventFunction<any> No Finger touch ends
onTouchCancel CommonEventFunction<any> No Finger touch is interrupted by call reminder, pop-up window, etc.
onLongTap CommonEventFunction<any> No Triggered when a finger taps and holds on the screen for 500 ms. After this event is triggered, moving on the screen does not trigger screen scrolling.
onError CommonEventFunction<onErrorEventDetail> No Triggers the error event when an error occurs. detail = {{errMsg}}

onErrorEventDetail

Property Type
errMsg string

Sample Code

  1. Taro.canvasToTempFilePath({
  2. x: 100,
  3. y: 200,
  4. width: 50,
  5. height: 50,
  6. destWidth: 100,
  7. destHeight: 100,
  8. canvasId: 'myCanvas',
  9. success: function (res) {
  10. console.log(res.tempFilePath)
  11. }
  12. })

API Support

API WeChat Mini-Program H5 React Native
Taro.canvasToTempFilePath ✔️ ✔️