Context
ArkUI defines the pixel units px and vp, which are related to the units of the underlying canvas API parameters.
canvas is a same-layer rendering component. Use the canvas component in ArkUI to draw images.
Symptom
When the has.canvasPutImageData API is called to draw an image on the canvas component, the expected result is that the width and height of the drawn image are the same as the passed values of the width and height parameters. However, the actual width and height of the drawn image are smaller than the values of the width and height parameters.
Cause
The implementation principle of has.canvasPutImageData is to convert the pixel data (unit: px) passed by a developer into a imageData object (unit: vp), and then draw the image on the canvas component using the putImageData method. Due to unit conversion (px→vp), the actual width and height of the image on the canvas component are inconsistent with the passed values of width and height.
Solution
You are advised to use the CanvasRenderingContext2D API instead of the CanvasContext API. The CanvasRenderingContext2D.putImageData API fixes this issue. It is also significantly optimized in terms of performance and feature support, providing more stable and efficient rendering capabilities.
Symptom
When the has.canvasGetImageData API is called to obtain the pixel data in a specified area, the expected result is that the returned width and height are the same as the passed width and height. However, the actual result is that the returned width and height are greater than the passed width and height.
Cause
The implementation principle of has.canvasGetImageData is to call the getImageData method to obtain the pixel data in the specified area of the canvas component (the default unit of the API parameters is vp). After obtaining the return values, the data is returned to the developer. However, ASCF sets the pixel unit of the returned imageData object to px. Due to the inconsistent pixel units, the returned values of width and height are different from passed values of width and height.
Solution
You are advised to use the CanvasRenderingContext2D API instead of the CanvasContext API. The CanvasRenderingContext2D.getImageData API fixes this issue. It is also significantly optimized in terms of performance and feature support, providing more stable and efficient rendering capabilities.