Skip to main content
Version: 2.x

Taro.canvasGetImageData(option, component)

获取 canvas 区域隐含的像素数据。

参考文档

类型

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

参数

Option

参数类型必填说明
canvasIdstring画布标识,传入 canvas 组件的 canvas-id 属性。
heightnumber将要被提取的图像数据矩形区域的高度
widthnumber将要被提取的图像数据矩形区域的宽度
xnumber将要被提取的图像数据矩形区域的左上角横坐标
ynumber将要被提取的图像数据矩形区域的左上角纵坐标
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(res: CallbackResult) => void接口调用失败的回调函数
success(result: SuccessCallbackResult) => void接口调用成功的回调函数

SuccessCallbackResult

参数类型说明
dataUint8ClampedArray图像像素点数据,一维数组,每四项表示一个像素点的 rgba
heightnumber图像数据矩形的高度
widthnumber图像数据矩形的宽度
errMsgstring调用结果

示例代码

Taro.canvasGetImageData({
canvasId: 'myCanvas',
x: 0,
y: 0,
width: 100,
height: 100,
success: function (res) {
console.log(res.width) // 100
console.log(res.height) // 100
console.log(res.data instanceof Uint8ClampedArray) // true
console.log(res.data.length) // 100 * 100 * 4
}
})

API 支持度

API微信小程序H5React Native
Taro.canvasGetImageData✔️✔️