openharmony 鸿蒙 js-components-canvas-path2d

2025-06-12 浏览 (1)

Path2D

NOTE

This component is supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version.

Path2D allows you to describe a path through an existing path. This path can be drawn through the stroke API of Canvas.

addPath

addPath(path: Object): void

Adds a path to this path.

Parameters

NameTypeDescription
pathObjectPath to be added to this path.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path1 = ctx.createPath2D("M250 150 L150 350 L350 350 Z");
      var path2 = ctx.createPath2D();
      path2.addPath(path1);
      ctx.stroke(path2);
  }
}

en-us_image_0000001173164873

setTransform

setTransform(scaleX: number, skewX: number, skewY: number, scaleY: number, translateX: number, translateY: number): void

Sets the path transformation matrix.

Parameters

NameTypeDescription
scaleXnumberScale ratio of the x-axis.
skewXnumberSkew angle of the x-axis.
skewYnumberSkew angle of the y-axis.
scaleYnumberScale ratio of the y-axis.
translateXnumberTranslation distance along the x-axis.
translateYnumberTranslation distance along the y-axis.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D("M250 150 L150 350 L350 350 Z");
      path.setTransform(0.8, 0, 0, 0.4, 0, 0);
      ctx.stroke(path);
  }
}

en-us_image_0000001127125208

closePath

closePath(): void

Moves the current point of the path back to the start point of the path, and draws a straight line between the current point and the start point. If the shape has already been closed or has only one point, this method does nothing.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.moveTo(200, 100);
      path.lineTo(300, 100);
      path.lineTo(200, 200);
      path.closePath();
      ctx.stroke(path);
  }
}

en-us_image_0000001127125202

moveTo

moveTo(x: number, y: number): void

Moves the current coordinate point of the path to the target point, without drawing a line during the movement.

Parameters

ParameterTypeDescription
xnumberX-coordinate of the target point.
ynumberY-coordinate of the target point.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.moveTo(50, 100);
      path.lineTo(250, 100);
      path.lineTo(150, 200);
      path.closePath();
      ctx.stroke(path);
  }
}

en-us_image_0000001173164869

lineTo

lineTo(x: number, y: number): void

Draws a straight line from the current point to the target point.

Parameters

NameTypeDescription
xnumberX-coordinate of the target point.
ynumberY-coordinate of the target point.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 400px; height: 450px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.moveTo(100, 100);
      path.lineTo(100, 200);
      path.lineTo(200, 200);
      path.lineTo(200, 100);
      path.closePath();
      ctx.stroke(path);
  }
}

en-us_image_0000001127285024

bezierCurveTo

bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void

Draws a cubic bezier curve on the canvas.

Parameters

NameTypeDescription
cp1xnumberX-coordinate of the first parameter of the bezier curve.
cp1ynumberY-coordinate of the first parameter of the bezier curve.
cp2xnumberX-coordinate of the second parameter of the bezier curve.
cp2ynumberY-coordinate of the second parameter of the bezier curve.
xnumberX-coordinate of the end point on the bezier curve.
ynumberY-coordinate of the end point on the bezier curve.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.moveTo(10, 10);
      path.bezierCurveTo(20, 100, 200, 100, 200, 20);
      ctx.stroke(path);
  }
}

en-us_image_0000001173324783

quadraticCurveTo

quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void

Draws a quadratic curve on the canvas.

Parameters

NameTypeDescription
cpxnumberX-coordinate of the bezier curve parameter.
cpynumberY-coordinate of the bezier curve parameter.
xnumberX-coordinate of the end point on the bezier curve.
ynumberY-coordinate of the end point on the bezier curve.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.moveTo(10, 10);
      path.quadraticCurveTo(100, 100, 200, 20);
      ctx.stroke(path);
  }
}

en-us_image_0000001173164871

arc

arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise: number): void

Draws an arc on the canvas.

Parameters

NameTypeDescription
xnumberX-coordinate of the center point of the arc.
ynumberY-coordinate of the center point of the arc.
radiusnumberRadius of the arc.
startAnglenumberStart radian of the arc.
endAnglenumberEnd radian of the arc.
anticlockwisebooleanWhether to draw the arc counterclockwise.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.arc(100, 75, 50, 0, 6.28);
      ctx.stroke(path);
  }
}

en-us_image_0000001173164867

arcTo

arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void

Draws an arc based on the radius and points on the arc.

Parameters

ParameterTypeDescription
x1numberX-coordinate of the first point on the arc.
y1numberY-coordinate of the first point on the arc.
x2numberX-coordinate of the second point on the arc.
y2numberY-coordinate of the second point on the arc.
radiusnumberRadius of the arc.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 300px; height: 250px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.arcTo(150, 20, 150, 70, 50);
      ctx.stroke(path);
  }
}

en-us_image_0000001127125204

ellipse

ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise: number): void

Draws an ellipse in the specified rectangular region on the canvas.

Parameters

NameTypeDescription
xnumberX-coordinate of the ellipse center.
ynumberY-coordinate of the ellipse center.
radiusXnumberEllipse radius on the x-axis.
radiusYnumberEllipse radius on the y-axis.
rotationnumberRotation angle of the ellipse, in radians.
startAnglenumberAngle of the start point for drawing the ellipse, in radians.
endAnglenumberAngle of the end point for drawing the ellipse, in radians.
anticlockwisenumberWhether to draw the ellipse counterclockwise. The value 0 means clockwise, and 1 means counterclockwise. This parameter is optional. The default value is 0.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 450px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.ellipse(200, 200, 50, 100, Math.PI * 0.25, Math.PI * 0.5, Math.PI, 1);
      ctx.stroke(path);
  }
}

en-us_image_0000001173324787

rect

rect(x: number, y: number, width: number, height: number): void

Creates a rectangle on the canvas.

Parameters

NameTypeDescription
xnumberX-coordinate of the upper left corner of the rectangle.
ynumberY-coordinate of the upper left corner of the rectangle.
widthnumberWidth of the rectangle.
heightnumberHeight of the rectangle.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 450px; background-color: #ffff00;"></canvas>
</div>
// xxx.js
export default {
  onShow() {
      const el = this.$refs.canvas;
      const ctx = el.getContext('2d');
      var path = ctx.createPath2D();
      path.rect(20, 20, 100, 100);
      ctx.stroke(path);
  }
}

en-us_image_0000001127125212

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Full)

harmony 鸿蒙Data Type Attributes

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙divider

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙label

harmony 鸿蒙marquee

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/rD1hy4