目录

How can I resolve canvas drawing issues?

Context

Why are the width and height of the image drawn by the has.canvasPutImageData API inconsistent with the parameter values?

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.

Why are the width and height returned by has.canvasGetImageData inconsistent with the passed parameter values?

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.