目录

Path2D

Path2D API of the Canvas 2D API is used to declare a path, which can be used by the CanvasRenderingContext2D object. The path methods of the CanvasRenderingContext2D API are also available in the Path2D API, allowing you to create paths that can be preserved and reused in the canvas as needed.

Path2D.addPath

Path2D.addPath(Path2D path)

Adds a path to the current path.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
path Path2D Yes Path2D path to be added.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.rect(30, 30, 100, 100);
    const path1 = canvas.createPath2D();
    path1.rect(40, 40, 50, 50);
    path.addPath(path1);
    ctx.stroke(path);
  })
  .exec();

Path2D.arc

Path2D.arc(number x, number y, number radius, number startAngle, number endAngle, boolean counterclockwise)

Adds an arc path.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
x number Yes X coordinate of the circle center.
y number Yes Y coordinate of the circle center.
radius number Yes Radius of the circle, which must be a positive number.
startAngle number Yes Start angle of the arc.
endAngle number Yes End angle of the arc.
counterclockwise boolean No Whether to draw the arc counterclockwise. If true is passed, the arc is drawn from the end angle to the start angle.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.arc(75, 75, 50, 0, Math.PI * 2, true);
    ctx.stroke(path);
  })
  .exec();

Path2D.arcTo

Path2D.arcTo(number x1, number y1, number x2, number y2, number radius)

Adds an arc based on the given control points and radius.

Since: 2.0.2

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 Radius of the arc, which must be a non-negative value.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.moveTo(200, 20);
    path.arcTo(200, 130, 50, 20, 40);
    ctx.stroke(path);
  })
  .exec();

Path2D.bezierCurveTo

Path2D.bezierCurveTo(number cp1x, number cp1y, number cp2x, number cp2y, number x, number y)

Draws a cubic Bezier curve.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
cp1x number Yes X coordinate of the first control point.
cp1y number Yes Y coordinate of the first control point.
cp2x number Yes X coordinate of the second control point.
cp2y number Yes Y coordinate of the second control point.
x number Yes X coordinate of the end point.
y number Yes Y coordinate of the end point.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.bezierCurveTo(30, 100, 200, 100, 200, 20);
    ctx.stroke(path);
  })
  .exec();

Path2D.closePath

Path2D.closePath()

Closes the current subpath. The start point and the last point of the current subpath are connected by a straight line.

Since: 2.0.2

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.bezierCurveTo(20, 100, 200, 100, 200, 20);
    path.closePath();
    ctx.stroke(path);
  })
  .exec();

Path2D.ellipse

Path2D.ellipse(number x, number y, number radiusX, number radiusY, number rotation, number startAngle, number endAngle, boolean counterclockwise)

Adds an elliptical arc path.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
x number Yes X coordinate of the ellipse center.
y number Yes Y coordinate of the ellipse center.
radiusX number Yes Radius of the major axis of the ellipse, which must be a non-negative number.
radiusY number Yes Radius of the minor axis of the ellipse, which must be a non-negative number.
rotation number Yes Rotation angle of the ellipse.
startAngle number Yes Start angle of the arc.
endAngle number Yes End angle of the arc.
counterclockwise boolean No Whether to draw the arc counterclockwise. If true is passed, the arc is drawn from the end angle to the start angle.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.ellipse(80, 50, 60, 30, 0, 0, Math.PI * 2, false);
    ctx.stroke(path);
  })
  .exec();

Path2D.lineTo

Path2D.lineTo(number x, number y)

Adds a point and creates a line from the last specified point to the target point.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
x number Yes X coordinate of the end point.
y number Yes Y coordinate of the end point.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.moveTo(10, 10);
    path.lineTo(90, 120);
    ctx.stroke(path);
  })
  .exec();

Path2D.moveTo

Path2D.moveTo(number x, number y)

Moves a path to a specified point in the canvas without creating a line.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
x number Yes X coordinate of a point.
y number Yes Y coordinate of a point.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.moveTo(10, 10);
    path.lineTo(90, 120);
    ctx.stroke(path);
  })
  .exec();

Path2D.quadraticCurveTo

Path2D.quadraticCurveTo(number cpx, number cpy, number x, number y)

Draws a quadratic Bezier curve.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
cpx number Yes X coordinate of the control point.
cpy number Yes Y coordinate of the control point.
x number Yes X coordinate of the end point.
y number Yes Y coordinate of the end point.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.moveTo(50, 120);
    path.quadraticCurveTo(50, 80, 150, 100);
    ctx.stroke(path);
  })
  .exec();

Path2D.rect

Path2D.rect(number x, number y, number width, number height)

Draws a rectangle path.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
x number Yes X coordinate of the start point of the rectangle.
y number Yes Y coordinate of the start point of the rectangle.
width number Yes Width of the rectangle. A positive value extends the rectangle to the right, and a negative value extends it to the left.
height number Yes Height of the rectangle. A positive value extends the rectangle downwards and to the right, and a negative value extends it upwards.

Example:

has.createSelectorQuery().select('#myCanvas')
  .node((res) => {
    const canvas = res.node;
    const ctx = canvas.getContext('2d');
    const path = canvas.createPath2D();
    path.rect(10, 10, 100, 100);
    ctx.fill(path);
  })
  .exec();