Represents the drawing context of the canvas component.
Attributes
| Parameter | Type | Description |
|---|---|---|
| strokeStyle | string|CanvasGradient | Stroke color. The usage is the same as that of CanvasContext.setStrokeStyle. Since: 1.0.15 |
| shadowOffsetX | number | Horizontal offset of the shadow relative to the shape. Since: 1.0.15 |
| shadowOffsetY | number | Vertical offset of the shadow relative to the shape. Since: 1.0.15 |
| shadowColor | string | Shadow color. Since: 1.0.15 |
| shadowBlur | number | Blur level of the shadow. Since: 1.0.15 |
| lineWidth | number | Line width. The usage is the same as that of CanvasContext.setLineWidth. Since: 1.0.15 |
| lineCap | string | End-cap style of a line. Valid values: - 'butt': adds a straight edge to each end of a line. - 'round': adds a round cap to each end of a line. - 'square': adds a square cap to each end of a line. The usage is the same as that of CanvasContext.setLineCap. Since: 1.0.15 |
| lineJoin | string | Corner style where lines meet. Valid values: - 'bevel': bevel angle. - 'round': rounded corner. - 'miter': sharp corner. The usage is the same as that of CanvasContext.setLineJoin. Since: 1.0.15 |
| miterLimit | number | Maximum miter length. The usage is the same as that of CanvasContext.setMiterLimit. Since: 1.0.15 |
| lineDashOffset | number | Dashed line offset. The initial value is 0. Since: 1.0.15 |
| globalCompositeOperation | string | Global composite operation to use when drawing a new shape. The following operations are supported: - 'source-over', - 'source-atop', - 'source-in', - 'source-out', - 'destination-over', - 'destination-atop', - 'destination-in', - 'destination-out', - 'lighter', - 'copy', - 'xor' Since: 1.0.15 |
| globalAlpha | number | Global alpha value of the brush. The value ranges from 0 (completely transparent) to 1 (completely opaque). Since: 1.0.15 |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.lineWidth = 10;
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.draw(); CanvasContext.arc(number x, number y, number r, number sAngle, number eAngle, boolean counterclockwise)
Creates a circular arc.
Sets the start radian to 0 and the end radian to 2 × Math.PI to create a circle.
Uses the stroke method to draw a circular arc on the canvas.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | X coordinate of the circle center. |
| y | number | Yes | Y coordinate of the circle center. |
| r | number | Yes | Radius of a circle. |
| sAngle | number | Yes | Start radian at the direction of 3 o'clock. |
| eAngle | number | Yes | End radian. |
| counterclockwise | boolean | No | Whether the radian direction is anticlockwise. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.setFillStyle('#EEEEEE');
ctx.fill();
ctx.draw(); CanvasContext.arcTo(number x1, number y1, number x2, number y2, number radius)
Draws a circular arc based on the control point and radius.
Moves a brush to a specified start point using the moveTo method.
Specifies the control point and radius of a circular arc using the arcTo method.
Uses the stroke method to draw a circular arc on the canvas.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x1 | number | Yes | X coordinate of the first control point. |
| y1 | number | Yes | Y coordinate of the first control point. |
| x2 | number | Yes | X coordinate of the second control point. |
| y2 | number | Yes | Y coordinate of the second control point. |
| radius | number | Yes | Arc radius. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(100, 20);
ctx.arcTo(150, 20, 150, 70, 50);
ctx.lineTo(150, 120);
ctx.stroke();
ctx.draw(); CanvasContext.beginPath()
Starts to create a path. The path is used for filling or stroking only when fill or stroke is called.
If setFillStyle, setStrokeStyle, and setLineWidth are set for multiple times in the same path, the last setting takes effect.
Since: 1.0.0
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.setStrokeStyle('#ff0000');
ctx.moveTo(0, 75);
ctx.lineTo(250, 75);
ctx.stroke();
ctx.beginPath();
ctx.setStrokeStyle('#0000ff');
ctx.moveTo(50, 0);
ctx.lineTo(150, 130);
ctx.draw(); CanvasContext.bezierCurveTo(number cp1x, number cp1y, number cp2x, number cp2y, number x, number y)
Creates a cubic Bezier curve path. The start point of the curve is the last point on the path.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| cp1x | number | Yes | X coordinate of the first Bezier control point. |
| cp1y | number | Yes | Y coordinate of the first Bezier control point. |
| cp2x | number | Yes | X coordinate of the second Bezier control point. |
| cp2y | number | Yes | Y coordinate of the second Bezier control point. |
| x | number | Yes | X coordinate of the end point. |
| y | number | Yes | Y coordinate of the end point. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.bezierCurveTo(20, 100, 200, 100, 200, 20);
ctx.stroke();
ctx.draw(); CanvasContext.clip()
Cuts out shapes of any size from the original canvas.
Once an area is cut, you can only draw within that area on the canvas. (Other areas in the canvas cannot be drawn).
Before using the clip method, you can save the current canvas area with the save method and restore it at any time later using the restore method.
Since: 1.0.0
Example:
Uses the draw method to draw the result.
Cut an image:
const ctx = has.createCanvasContext('myCanvas');
has.downloadFile({
url: 'https://www.example.com/xxx.png', // Replace the example with the actual image resource address.
success: (res) => {
ctx.save();
ctx.beginPath();
ctx.arc(50, 50, 25, 0, 2 * Math.PI);
ctx.clip();
ctx.drawImage(res.tempFilePath, 25, 25);
ctx.restore();
ctx.draw();
}
}); Cut a figure:
const ctx = has.createCanvasContext('myCanvas');
ctx.arc(50, 50, 50, 0, 2 * Math.PI);
ctx.save();
ctx.clip();
ctx.fillRect(0, 0, 20, 200);
ctx.restore();
ctx.draw(); CanvasContext.closePath()
Closes a path. The start point and end point are connected. If fill or stroke is not called and a new path is opened after the path is closed, the previous path will not be rendered.
Since: 1.0.0
Example:
Uses the draw method to draw the result.
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(10, 10);
ctx.lineTo(100, 10);
ctx.lineTo(100, 100);
ctx.closePath();
ctx.stroke();
ctx.draw(); CanvasContext.createCircularGradient(number x, number y, number r): CanvasGradient
Creates a gradient in a circle. The start point is at the center of the circle, and the end point is on the circumference.
The returned CanvasGradient object needs to use CanvasGradient.addColorStop() to specify at least two gradient points.
Since: 1.0.4
Parameters
Parameter x and y can be empty; parameter r must be entered. If parameter x and y are not transferred, an example is as follows:
CanvasContext.createCircularGradient(undefined, undefined, 50)
In this case, parameter transfer is as follows:
CanvasContext.createCircularGradient(0, 0, 50)
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| x | number | 0 | No | X coordinate of the circle center. |
| y | number | 0 | No | Y coordinate of the circle center. |
| r | number | - | Yes | Radius of a circle. |
Returns:
CanvasGradient object.
Example:
const ctx = has.createCanvasContext('myCanvas');
const grd = ctx.createCircularGradient(75, 50, 50);
grd.addColorStop(0, 'red');
grd.addColorStop(1, 'black');
ctx.setFillStyle(grd);
ctx.fillRect(10, 10, 150, 80);
ctx.draw(); CanvasContext.createLinearGradient(number x0, number y0, number x1, number y1): CanvasGradient
Creates a linear gradient. The returned CanvasGradient object needs to use CanvasGradient.addColorStop() to specify at least two gradient points.
Since: 1.0.0
Parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| x0 | number | 0 | No | X coordinate of the start point. |
| y0 | number | 0 | No | Y coordinate of the start point. |
| x1 | number | 0 | No | X coordinate of the end point. |
| y1 | number | 0 | No | Y coordinate of the end point. |
Returns:
CanvasGradient object.
Example:
const ctx = has.createCanvasContext('myCanvas');
const grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, 'red');
grd.addColorStop(1, 'green');
ctx.setFillStyle(grd);
ctx.fillRect(10, 10, 150, 80);
ctx.draw(); CanvasContext.draw(boolean reserve, function callback)
Draws the description (path, deformation, and style) in the drawing context to the canvas.
Since: 1.0.0
Parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| reserve | boolean | false | No | Whether the current drawing continues from the previous one. If the reserve parameter is set to false, the native layer clears the canvas before drawing. If the reserve parameter is set to true, the content on the current canvas is retained and overwritten by the content drawn by calling drawCanvas. |
| callback | function | - | No | Callback function executed after the drawing is complete. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(10, 10);
ctx.lineTo(100, 10);
ctx.lineTo(100, 100);
ctx.fill();
ctx.draw(); CanvasContext.drawImage(string imageResource, number sx, number sy, number sWidth, number sHeight, number dx, number dy, number dWidth, number dHeight)
Draws an image to the canvas.
Since: 1.0.4
Parameters
Currently, three input parameter modes are at your service.
CanvasContext.drawImage(string imageResource, number dx, number dy)
CanvasContext.drawImage(string imageResource, number dx, number dy, number dWidth, number dHeight)
CanvasContext.drawImage(string imageResource, number sx, number sy, number sWidth, number sHeight, number dx, number dy, number dWidth, number dHeight)
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| imageResource | string | Yes | Resource of the image to be drawn. (The network image must be downloaded through getImageInfo/downloadFile.) |
| sx | number | No | Top-left X coordinate of a rectangle in the source image to be drawn to the canvas. |
| sy | number | No | Top-left Y coordinate of a rectangle in the source image to be drawn to the canvas. |
| sWidth | number | No | Width of a rectangle in the source image to be drawn to the canvas. |
| sHeight | number | No | Height of a rectangle in the source image to be drawn to the canvas. |
| dx | number | Yes | X coordinate of the top-left corner of the image on the target canvas. |
| dy | number | Yes | Y coordinate of the top-left corner of the image on the target canvas. |
| dWidth | number | No | Width of the image to be drawn on the target canvas. The image can be scaled. |
| dHeight | number | No | Height of the image to be drawn on the target canvas. The image can be scaled. |
Example:
const ctx = has.createCanvasContext('myCanvas');
has.chooseImage({
success: (res) => {
ctx.drawImage(res.tempFilePaths[0], 0, 0, 150, 100);
ctx.draw();
}
}); CanvasContext.fill()
Fills the content in the current path. The default fill color is black.
Since: 1.0.0
Example:
Uses the draw method to draw the result.
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(10, 10);
ctx.lineTo(100, 10);
ctx.lineTo(100, 100);
ctx.fill();
ctx.draw(); CanvasContext.fillRect(number x, number y, number width, number height)
Fills a rectangle. To set the fill color of the rectangle, use setFillStyle. If no fill color is specified, the default color is black.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | Top-left X coordinate of the rectangle path. |
| y | number | Yes | Top-left Y coordinate of the rectangle path. |
| width | number | Yes | Width of the rectangle path. |
| height | number | Yes | Height of the rectangle path. |
Example:
Uses the draw method to draw the result.
const ctx = has.createCanvasContext('myCanvas');
ctx.setFillStyle('blue');
ctx.fillRect(10, 10, 150, 75);
ctx.draw(); CanvasContext.fillText(string text, number x, number y, number maxWidth)
Draws the filled text on the canvas.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| text | string | Yes | Text to be output on the canvas. |
| x | number | Yes | Top-left X coordinate of the text to be drawn. |
| y | number | Yes | Top-left Y coordinate of the text to be drawn. |
| maxWidth | number | No | Maximum width of a text to be drawn. |
Example:
Uses the draw method to draw the result.
const ctx = has.createCanvasContext('myCanvas');
ctx.setFontSize(20);
ctx.fillText('Hello', 20, 20);
ctx.fillText('Webapp', 100, 100);
ctx.draw(); CanvasContext.lineTo(number x, number y)
Adds a point and creates a line from the last specified point to the target point. You need to use the stroke method to draw lines.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | X coordinate of the target position. |
| y | number | Yes | Y coordinate of the target position. |
Example:
Uses the draw method to draw the result.
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(10, 10);
ctx.lineTo(210, 60);
ctx.stroke();
ctx.draw(); CanvasContext.measureText(string text): number
Measures the text size. Currently, only the text width is returned. This API returns the result synchronously.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| text | string | Yes | Text to be measured. |
Returns
| Parameter | Type | Description |
|---|---|---|
| width | number | Width of the text. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setFontSize(20);
let metrics = ctx.measureText('hello world'); CanvasContext.moveTo(number x, number y)
Moves a path to a specified point in the canvas without creating a line. You need to use the stroke method to draw lines.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | X coordinate of the target position. |
| y | number | Yes | Y coordinate of the target position. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(10, 10);
ctx.lineTo(100, 10);
ctx.moveTo(10, 50);
ctx.lineTo(100, 50);
ctx.stroke();
ctx.draw(); CanvasContext.quadraticCurveTo(number cpx, number cpy, number x, number y)
Creates a quadratic Bezier curve path. The start point of the curve is the last point on the path.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| cpx | number | Yes | X coordinate of the Bezier control point. |
| cpy | number | Yes | Y coordinate of the Bezier control point. |
| x | number | Yes | X coordinate of the end point. |
| y | number | Yes | Y coordinate of the end point. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.quadraticCurveTo(110, 100, 200, 20);
ctx.stroke();
ctx.draw(); CanvasContext.rect(number x, number y, number width, number height)
Creates a rectangle path. You need to use the CanvasContext.fill or CanvasContext.stroke method to draw the rectangle to the canvas.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | Top-left X coordinate of the rectangle path. |
| y | number | Yes | Top-left Y coordinate of the rectangle path. |
| width | number | Yes | Width of the rectangle path. |
| height | number | Yes | Height of the rectangle path. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.rect(10, 10, 150, 75);
ctx.setFillStyle('blue');
ctx.fill();
ctx.draw(); CanvasContext.restore()
Restores the previously saved drawing context.
Since: 1.0.0
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.save();
ctx.setFillStyle('blue');
ctx.fillRect(10, 10, 150, 100);
ctx.restore();
ctx.fillRect(50, 50, 150, 100);
ctx.draw(); CanvasContext.rotate(number rotate)
Rotates the coordinate axis clockwise around the origin. The rotation angles are overlapped when the rotation function is called for multiple times. The origin can be modified using the CanvasContext.translate method.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| rotate | number | Yes | Rotation angle, in radians (degrees × Math.PI/180. The value of degrees ranges from 0 to 360. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.strokeRect(100, 10, 150, 100);
ctx.rotate(20 * Math.PI / 180);
ctx.strokeRect(100, 10, 150, 100);
ctx.rotate(20 * Math.PI / 180);
ctx.strokeRect(100, 10, 150, 100);
ctx.draw(); CanvasContext.save()
Saves the drawing context.
Since: 1.0.0
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.save();
ctx.setFillStyle('blue');
ctx.fillRect(10, 10, 150, 100);
ctx.restore();
ctx.fillRect(50, 50, 150, 100);
ctx.draw(); CanvasContext.scale(number scaleWidth, number scaleHeight)
After the API is called, X and Y coordinates of the path created later are scaled. The coordinate scaling multiples are multiplied when the API is repeatedly called.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| scaleWidth | number | Yes | X coordinate scaling multiples (1 = 100%, 0.5 = 50%, 2 = 200%). |
| scaleHeight | number | Yes | Y coordinate scaling multiples (1 = 100%, 0.5 = 50%, 2 = 200%). |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(100, 20);
ctx.arcTo(150, 20, 150, 70, 50);
ctx.lineTo(150, 120);
ctx.stroke();
ctx.scale(2, 2);
ctx.strokeRect(20,20,30,15);
ctx.draw(); CanvasContext.setFillStyle(string | CanvasGradient color)
Sets the fill color.
Since: 1.0.0
Parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| color | string | CanvasGradient | black | No | Fill color. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setFillStyle('blue');
ctx.fillRect(10, 10, 150, 75);
ctx.draw(); CanvasContext.setFontSize(number fontSize)
Sets the font size.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| fontSize | number | Yes | Font size. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setFontSize(20);
ctx.fillText('20', 20, 20);
ctx.setFontSize(30);
ctx.fillText('30', 40, 40);
ctx.draw(); CanvasContext.setGlobalAlpha(number alpha)
Sets the global brush opacity.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| alpha | number | Yes | Opacity. The value ranges from 0 to 1. 0: completely transparent 1: completely opaque |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setFillStyle('blue');
ctx.fillRect(10, 10, 150, 100);
ctx.setGlobalAlpha(0.5);
ctx.setFillStyle('red');
ctx.fillRect(50, 50, 150, 100);
ctx.setFillStyle('red');
ctx.fillRect(100, 100, 150, 100);
ctx.draw(); CanvasContext.setLineCap(string lineCap)
Sets the end-cap style of a line.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| lineCap | string | Yes | End-cap style of a line. - butt: adds a straight edge to each end of a line. - round: adds a round cap to each end of a line. - square: adds a square cap to each end of a line. |
Example:
const ctx = has.createCanvasContext('firstCanvas');
ctx.beginPath();
ctx.moveTo(10, 10);
ctx.lineTo(150, 10);
ctx.stroke();
ctx.beginPath();
ctx.setLineCap('square');
ctx.setLineWidth(10);
ctx.moveTo(10, 70);
ctx.lineTo(150, 70);
ctx.stroke();
ctx.beginPath();
ctx.setLineCap('round');
ctx.setLineWidth(10);
ctx.moveTo(10, 50);
ctx.lineTo(150, 50);
ctx.stroke();
ctx.beginPath();
ctx.setLineCap('butt');
ctx.setLineWidth(10);
ctx.moveTo(10, 30);
ctx.lineTo(150, 30);
ctx.stroke();
ctx.draw(); CanvasContext.setLineDash(Array<number> pattern, number offset)
Sets the dashed line style.
Since: 1.0.4
Parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| pattern | Array<number> | - | Yes | A group of digits that describe the lengths of alternatively drawn line segments and spacing (coordinate space unit). |
| offset | number | 0 | No | Offset of the dashed line. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setLineDash([10, 20], 5);
ctx.beginPath();
ctx.moveTo(0,100);
ctx.lineTo(500, 100);
ctx.stroke();
ctx.draw(); CanvasContext.setLineJoin(string lineJoin)
Sets the corner style where lines meet.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| lineJoin | string | Yes | Corner style where lines meet. - bevel: bevel angle. - round: rounded corner. - miter: sharp corner. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.moveTo(10, 10);
ctx.lineTo(100, 50);
ctx.lineTo(10, 90);
ctx.stroke();
ctx.beginPath();
ctx.setLineJoin('bevel');
ctx.setLineWidth(10);
ctx.moveTo(50, 10);
ctx.lineTo(140, 50);
ctx.lineTo(50, 90);
ctx.stroke();
ctx.beginPath();
ctx.setLineJoin('round');
ctx.setLineWidth(10);
ctx.moveTo(90, 10);
ctx.lineTo(180, 50);
ctx.lineTo(90, 90);
ctx.stroke();
ctx.beginPath();
ctx.setLineJoin('miter');
ctx.setLineWidth(10);
ctx.moveTo(130, 10);
ctx.lineTo(220, 50);
ctx.lineTo(130, 90);
ctx.stroke();
ctx.draw(); CanvasContext.setLineWidth(number lineWidth)
Sets the width of a line.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| lineWidth | number | Yes | Line width, in pixels. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.moveTo(10, 10);
ctx.lineTo(150, 10);
ctx.stroke();
ctx.beginPath();
ctx.setLineWidth(5);
ctx.moveTo(10, 30);
ctx.lineTo(150, 30);
ctx.stroke();
ctx.beginPath();
ctx.setLineWidth(10);
ctx.moveTo(10, 50);
ctx.lineTo(150, 50);
ctx.stroke();
ctx.draw(); CanvasContext.setMiterLimit(number miterLimit)
Sets the maximum miter length. The miter length is the distance between the inner corner and the outer corner where two lines meet.
This API is valid only when CanvasContext.setLineJoin is set to miter. When the miter length exceeds the maximum value, the intersection point of lines will be displayed using the bevel style.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| miterLimit | number | Yes | Maximum miter length. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.beginPath();
ctx.setLineWidth(10);
ctx.setLineJoin('miter');
ctx.setMiterLimit(1);
ctx.moveTo(10, 10);
ctx.lineTo(100, 50);
ctx.lineTo(10, 90);
ctx.stroke();
ctx.beginPath();
ctx.setLineWidth(10);
ctx.setLineJoin('miter');
ctx.setMiterLimit(2);
ctx.moveTo(50, 10);
ctx.lineTo(140, 50);
ctx.lineTo(50, 90);
ctx.stroke();
ctx.beginPath();
ctx.setLineWidth(10);
ctx.setLineJoin('miter');
ctx.setMiterLimit(3);
ctx.moveTo(90, 10);
ctx.lineTo(180, 50);
ctx.lineTo(90, 90);
ctx.stroke();
ctx.beginPath();
ctx.setLineWidth(10);
ctx.setLineJoin('miter');
ctx.setMiterLimit(4);
ctx.moveTo(130, 10);
ctx.lineTo(220, 50);
ctx.lineTo(130, 90);
ctx.stroke();
ctx.draw(); CanvasContext.setShadow(number offsetX, number offsetY, number blur, string color)
Sets the shadow style.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| offsetX | number | Yes | Horizontal offset of the shadow relative to the shape. |
| offsetY | number | Yes | Vertical offset of the shadow relative to the shape. |
| blur | number | Yes | Shadow blur level. Larger values represent a greater blur effect. The value ranges from 0 to 100. |
| color | string | Yes | Shadow color. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setFillStyle('blue');
ctx.setShadow(10, 50, 50, 'blue');
ctx.fillRect(10, 10, 150, 75);
ctx.draw(); CanvasContext.setStrokeStyle(string | CanvasGradient color)
Sets the stroke color.
Since: 1.0.4
Parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| color | string | CanvasGradient | black | No | Stroke color. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setStrokeStyle('blue');
ctx.strokeRect(10, 10, 150, 75);
ctx.draw(); CanvasContext.setTextAlign(string align)
Sets the text alignment mode.
Sets the vertical alignment mode of the text using the setTextAlign method. The mode value must be valid.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| align | string | Yes | Text alignment mode. |
Valid values of align
| Value | Description |
|---|---|
| left | Left-aligned. |
| center | Centered. |
| right | Right-aligned. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(150, 20);
ctx.lineTo(150, 170);
ctx.stroke();
ctx.setFontSize(15);
ctx.setTextAlign('left');
ctx.fillText('textAlign=left', 150, 60);
ctx.setTextAlign('right');
ctx.fillText('textAlign=right', 150, 100);
ctx.setTextAlign('center');
ctx.fillText('textAlign=center', 150, 80);
ctx.draw(); CanvasContext.setTransform(number scaleX, number skewX, number skewY, number scaleY, number translateX, number translateY)
Resets (overwrites) the current transformation using a matrix.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| scaleX | number | Yes | Horizontal scaling. |
| skewX | number | Yes | Horizontal skew. |
| skewY | number | Yes | Vertical skew. |
| scaleY | number | Yes | Vertical scaling. |
| translateX | number | Yes | Horizontal translation. |
| translateY | number | Yes | Vertical translation. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.fillStyle = '#FFFF00';
ctx.fillRect(0, 0, 250, 100);
ctx.setTransform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = 'blue';
ctx.fillRect(0, 0, 250, 100);
ctx.setTransform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, 250, 100);
ctx.draw(); CanvasContext.stroke()
Draws the border of the current path. The default color is black.
Since: 1.0.4
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(10, 10);
ctx.lineTo(100, 10);
ctx.lineTo(100, 100);
ctx.stroke();
ctx.draw(); CanvasContext.strokeRect(number x, number y, number width, number height)
Draws a non-filling rectangle. Use CanvasContext.setStrokeStyle to set the color of the rectangle line. If no color is specified, the default color is black.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | Top-left X coordinate of the rectangle path. |
| y | number | Yes | Top-left Y coordinate of the rectangle path. |
| width | number | Yes | Width of the rectangle path. |
| height | number | Yes | Height of the rectangle path. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setStrokeStyle('blue');
ctx.strokeRect(10, 10, 150, 75);
ctx.draw(); CanvasContext.strokeText(string text, number x, number y, number maxWidth)
Draws text strokes in the specified (x, y) position.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| text | string | Yes | Text to be drawn. |
| x | number | Yes | X coordinate of the start point of a text. |
| y | number | Yes | Y coordinate of the start point of a text. |
| maxWidth | number | No | Maximum width of a text to be drawn. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.setFontSize(20);
ctx.strokeText('Hello Web!', 10, 50);
ctx.setFontSize(30);
let gradient = ctx.createLinearGradient(0, 0, 200, 0);
gradient.addColorStop(0, 'magenta');
gradient.addColorStop(0.5, 'blue');
gradient.addColorStop(1.0, 'black');
ctx.strokeStyle = gradient;
ctx.strokeText('atomicservice.com', 10, 90);
ctx.draw(); CanvasContext.transform(number scaleX, number skewX, number skewY, number scaleY, number translateX, number translateY)
Concatenates multiple transformations to the current transformation matrix.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| scaleX | number | Yes | Horizontal scaling. |
| skewX | number | Yes | Horizontal skew. |
| skewY | number | Yes | Vertical skew. |
| scaleY | number | Yes | Vertical scaling. |
| translateX | number | Yes | Horizontal translation. |
| translateY | number | Yes | Vertical translation. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.fillStyle = '#FFFF00';
ctx.fillRect(0, 0, 250, 100);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = 'blue';
ctx.fillRect(0, 0, 250, 100);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, 250, 100);
ctx.draw(); CanvasContext.translate(number x, number y)
Transforms the origin (0, 0) of the current coordinate system. The default origin of the coordinate system is in the top-left corner of the page.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | X coordinate translation quantities. |
| y | number | Yes | Y coordinate translation quantities. |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.strokeRect(10, 10, 150, 100);
ctx.translate(20, 20);
ctx.strokeRect(10, 10, 150, 100);
ctx.translate(20, 20);
ctx.strokeRect(10, 10, 150, 100);
ctx.draw(); CanvasContext.setTextBaseline(string textBaseline)
Sets the vertical alignment mode of the text.
Sets the vertical alignment mode of the text using the setTextBaseline method. The mode value must be valid.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| textBaseline | string | Yes | Vertical alignment mode of the text. - top: align top - bottom: align bottom - middle: align center - normal: align with the standard alphabetic baseline (default alignment mode) |
Example:
const ctx = has.createCanvasContext('myCanvas');
ctx.moveTo(5, 75);
ctx.lineTo(295, 75);
ctx.stroke();
ctx.setFontSize(20);
ctx.setTextBaseline('normal');
ctx.fillText('normal', 200, 75);
ctx.setTextBaseline('top');
ctx.fillText('top', 5, 75);
ctx.setTextBaseline('middle');
ctx.fillText('middle', 50, 75);
ctx.setTextBaseline('bottom');
ctx.fillText('bottom', 120, 75);
ctx.draw(); CanvasContext.clearRect(number x, number y, number width, number height)
Clears content in the rectangle on the canvas.
Since: 1.0.3
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| x | number | Yes | Top-left X coordinate of the rectangle path. |
| y | number | Yes | Top-left Y coordinate of the rectangle path. |
| width | number | Yes | Width of the rectangle path, in vp. |
| height | number | Yes | Height of the rectangle path, in vp. |
Example:
Uses the draw method to draw the result.
const ctx = has.createCanvasContext('myCanvas');
ctx.setFillStyle('red');
ctx.fillRect(0, 0, 150, 200);
ctx.setFillStyle('blue');
ctx.fillRect(150, 0, 150, 200);
ctx.clearRect(10, 10, 150, 75);
ctx.draw(); CanvasContext.createPattern(string image, string repetition);
Creates a mode for a specified image. The meta image can be repeated in the specified direction.
Since: 1.0.3
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| image | string | Yes | Duplicate image sources, whose paths can be code package path and local temporary path (local path). |
| repetition | string | Yes | How to repeat an image. - repeat: repeat an image in both horizontal and vertical directions. - repeat-x: repeat an image in the horizontal direction. - repeat-y: repeat an image in the vertical direction. - no-repeat: not to repeat an image. |
Example:
const ctx = has.createCanvasContext('myCanvas');
const pattern = ctx.createPattern('internal://files/startIcon.png', 'repeat');
ctx.setFillStyle(pattern);
ctx.fillRect(10, 10, 100, 100);
ctx.draw();