has.createCanvasContext(string canvasId, Object this)
Creates a CanvasContext object of canvas.
In versions earlier than ASCF 2.0.1, you can obtain the CanvasContext object with type="2d". In 2.0.1 and later versions, you can obtain the CanvasRenderingContext2D object with type="2d" through Canvas.getContext('2d').
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| canvasId | string | Yes | The canvas-id attribute of the canvas component of the context is to be obtained. |
| this | Object | No | this of the current component instance operates the component canvas under custom components. Since: 1.0.13 |
Returns
CanvasContext object.
Example:
const ctx = has.createCanvasContext('firstCanvas'); has.canvasToTempFilePath(Object object, Object this)
Exports and generates the content in the specified area of the current canvas to an image of a specified size. To successfully export the image, call this method within the draw() callback.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| object | Object | Yes | Information object to be specified. |
| this | Object | No | this of the current component instance operates the component canvas under custom components. |
object parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| x | number | 0 | No | Top-left X coordinate of the specified canvas area. |
| y | number | 0 | No | Top-left Y coordinate of the specified canvas area. |
| width | number | Canvas width-x | No | Width of the specified canvas area. |
| height | number | Canvas height-y | No | Height of the specified canvas area. |
| destWidth | number | Width × Screen pixel density | No | Width of the output image. |
| destHeight | number | Height × Screen pixel density | No | Height of the output image. |
| canvasId | string | - | Yes | Canvas ID, which is the canvas-id passed in the canvas component. |
| canvas | Object | - | No | Canvas ID, which is the canvas component instance. This attribute is used when the canvas type is 2d. |
| fileType | string | png | No | Type of the target file. - jpg: JPG image - png: PNG image |
| quality | number | - | No | Image quality. Currently, this parameter is valid only for JPG images. The value range is (0, 1]. If the value is not in the range, the value 1.0 is used. |
| success | function | - | - | Callback triggered when the API call is successful. |
| fail | function | - | - | Callback triggered when the API call fails. |
| complete | function | - | - | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| tempFilePath | string | Temporary path (local path) of the generated file. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | applicationContext is undefined. |
| 102 | 1. Fail to init save canvas infos, params error. 2. Invalid dimensions. 3. initSaveCanvasInfo -> CanvasParams is undefined. |
| 13000401 | 1. Fail to create file. 2. canvasToTempFilePath -> Save canvas as file failed. |
Example:
has.canvasToTempFilePath({
x: 10,
y: 20,
width: 50,
height: 50,
destWidth: 80,
destHeight: 80,
canvasId: 'firstCanvas',
success: (res) => {
console.info('canvasToTempFilePath success', res);
},
fail: (err) => {
console.error('canvasToTempFilePath fail', err);
},
complete: (res) => {
console.info('canvasToTempFilePath complete', res);
}
}); has.canvasPutImageData(Object object, Object this)
Draws pixel data to the canvas.
Since: 1.0.3
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| object | Object | Yes | Pixel data of the image. |
| this | Object | No | this of the current component instance operates the component canvas under custom components. Since: 1.0.13 |
object parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| canvasId | string | Yes | Canvas ID, which is the canvas-id passed in the canvas component. |
| data | Uint8ClampedArray | Yes | Image pixel data, one-dimensional array. Every four values indicate the RGBA of a pixel. |
| x | number | Yes | Position offset of the source image data in the target canvas on the X axis. |
| y | number | Yes | Position offset of the source image data in the target canvas on the Y axis. |
| width | number | Yes | Width of the source image data rectangle. |
| height | number | Yes | Height of the source image data rectangle. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | 1. canvas is empty. 2. params is illegal. 3. params is illegal. The pixel data does not match the number of pixels. pixel data length. |
| 13000601 | canvasPutImageData -> fail to put image data. |
Example:
const data = new Uint8ClampedArray([255, 0, 0, 1]);
has.canvasPutImageData({
canvasId: 'myCanvas',
x: 0,
y: 0,
width: 1,
height: 1,
data: data,
success: (res) => {
console.info('canvasPutImageData success', res);
},
fail: (err) => {
console.error('canvasPutImageData fail', err);
},
complete: (res) => {
console.info('canvasPutImageData complete', res);
}
}); has.canvasGetImageData(Object object, Object this)
Obtains the hidden pixel data in the canvas area.
Since: 1.0.3
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| object | Object | Yes | Pixel data of the image. |
| this | Object | No | this of the current component instance operates the component canvas under custom components. Since: 1.0.13 |
object parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| canvasId | string | Yes | Canvas ID, which is the canvas-id passed in the canvas component. |
| x | number | Yes | Top-left X coordinate of the image data rectangle to be extracted. |
| y | number | Yes | Top-left Y coordinate of the image data rectangle to be extracted. |
| width | number | Yes | Width of the image data rectangle to be extracted. |
| height | number | Yes | Height of the image data rectangle to be extracted. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
ImageData object.
Error code information
| Error Code | Error Message |
|---|---|
| 102 | 1. canvas is empty. 2. params is illegal. |
| 13000501 | canvasGetImageData -> fail to get image data from canvas. |
Example:
has.canvasGetImageData({
canvasId: 'myCanvas',
x: 0,
y: 0,
width: 100,
height: 100,
success: (res) => {
console.info(res.width);
console.info(res.height);
console.info(res.data instanceof Uint8ClampedArray);
console.info(res.data.length);
},
fail: (err) => {
console.error('canvasGetImageData fail', err);
},
complete: (res) => {
console.info('canvasGetImageData complete', res);
}
});