Canvas instance, which can be obtained through SelectorQuery.
Since: 2.0.1
Attributes
| Parameter | Type | Description |
|---|---|---|
| width | number | Canvas width. |
| height | number | Canvas height. |
Canvas.getContext(string contextType)
Returns the drawing context of the canvas.
Since: 2.0.2
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| contextType | string | No | Context type. Valid value of contextType: 2d |
Returns:
RenderingContext object.
Example:
has.createSelectorQuery().select('#myCanvas')
.node((res) => {
const canvas = res.node;
const ctx = canvas.getContext('2d');
console.info('canvas context: ', ctx);
})
.exec(); Canvas.createImage()
Creates an image object. Only the 2D canvas type is supported.
Since: 2.0.2
Returns:
Image object.
Example:
has.createSelectorQuery().select('#myCanvas')
.node((res) => {
const canvas = res.node;
const ctx = canvas.getContext('2d');
const img = canvas.createImage();
img.src = 'https://www.example.com/xxxx.png'; // Replace the example with the actual image resource address.
img.onload = () => {
ctx.drawImage(img, 0, 10, 237, 133);
console.info('image width', img.width);
console.info('image height', img.height);
};
img.onerror = (err) => {
console.error('createImage err', err);
};
})
.exec(); Canvas.toDataURL(string type, number encoderOptions)
Returns a data URI containing image display information.
Since: 2.0.2
Parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| type | string | image/png | No | Image format. The default value is 'image/png'. Valid values: 'image/png', 'image/jpeg', and 'image/webp'. |
| encoderOptions | number | 0.92 | No | Image quality. The value ranges from 0 to 1. You can select the image quality from 0 to 1 only when the image format is set to 'image/jpeg' or 'image/webp'. |
Returns
| Type | Description |
|---|---|
| string | Data URI containing image display information. |
Example:
has.createSelectorQuery().select('#myCanvas')
.node((res) => {
const canvas = res.node;
const ctx = canvas.getContext('2d');
const base64 = canvas.toDataURL();
console.info('toDataURL res: ', base64);
})
.exec(); Canvas.createPath2D(string|Path2D path)
Creates a Path2D object.
Since: 2.0.2
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| path | string|Path2D | No | Source path used to create a new path. |
Returns:
Path2D object.
Example:
has.createSelectorQuery().select('#myCanvas')
.node((res) => {
const canvas = res.node;
const ctx = canvas.getContext('2d');
const path = canvas.createPath2D();
})
.exec(); Canvas.requestAnimationFrame(function callback)
Registers a function to be executed during the next redrawing.
Since: 2.0.2
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback to be called before the next redrawing. |
Returns:
requestId, which is used as the animation frame request ID and can be passed to Canvas.cancelAnimationFrame to cancel the animation frame request.
Example:
has.createSelectorQuery().select('#myCanvas')
.node((res) => {
const canvas = res.node;
const ctx = canvas.getContext('2d');
let x = 0;
const draw = () => {
ctx.clearRect(0, 0, 300, 300);
x = (x + 2) % 300;
ctx.fillStyle = 'red';
ctx.fillRect(x, 50, 50, 50);
const requestId = canvas.requestAnimationFrame(draw);
};
draw();
})
.exec(); Canvas.cancelAnimationFrame(number requestId)
Cancels an animation frame request added to the animation schedule by Canvas.requestAnimationFrame.
Since: 2.0.2
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| requestId | number | Yes | ID of an animation frame request, which is returned when Canvas.requestAnimationFrame is called. |
Example:
has.createSelectorQuery().select('#myCanvas')
.node((res) => {
const canvas = res.node;
canvas.cancelAnimationFrame(requestId); // requestId is the value returned when the Canvas.requestAnimationFrame API is called.
})
.exec();