目录

Drawing Geometric Shapes (Shape)

The drawing components are used to draw graphs on the page. The Shape component serves as the parent container for all drawing components and contains the common attributes shared by all drawing components. For details, see Shape.

Creating a Drawing Component

You can create a drawing component through either of the following approaches:

Shape Viewport

viewPort(value: { x?: number | string, y?: number | string, width?: number | string, height?: number | string })

Creates a viewport, which is a rectangle in the user space that maps to the view boundary established for the associated SVG element. Among the four optional parameters, x and y represent the coordinates of the upper left corner of the viewport, and width and height represent the size of the viewport.

The following examples demonstrate how to use viewPort:

Setting Styles

The example uses the commands parameter to define the path. For detailed syntax rules of this parameter, see SVG Path Syntax.

You can customize the component style by setting various style attributes.

Example Scenario

[h2]Drawing a Closed Path

Draw a closed path at the point (-80, -5) of the shape, with fill color rgb(213, 213, 213), line width 3, stroke color rgb(39, 135, 217), and miter join style (default value).

The example uses the commands parameter to define the path. For detailed syntax rules of this parameter, see SVG Path Syntax.

@Entry
@Component
struct ShapeExample {
  build() {
    Column({ space: 10 }) {
      Shape() {
        Path().width(200).height(60).commands('M0 0 L400 0 L400 150 Z')
      }
      .viewPort({
        x: -80,
        y: -5,
        width: 500,
        height: 300
      })
      .fill('rgb(213, 213, 213)')
      .stroke('rgb(39, 135, 217)')
      .strokeWidth(3)
      .strokeLineJoin(LineJoinStyle.Miter)
      .strokeMiterLimit(5)
    }.width('100%').margin({ top: 15 })
  }
}

[h2]Drawing a Circle and Ring

Draw a circle with a diameter of 150, and a ring with a diameter of 150 and a red dotted border. If the width and height differ, the drawing component will use the shorter side as the actual diameter.

In this example, the red dotted ring is implemented by setting the stroke dash pattern using the strokeDashArray attribute. For details about the strokeDashArray attribute, see strokeDashArray.

@Entry
@Component
struct CircleExample {
  build() {
    Column({ space: 10 }) {
      // Draw a circle whose diameter is 150.
      Circle({ width: 150, height: 150 })
      // Draw a ring with a diameter of 150 and a red dotted border.
      Circle()
        .width(150)
        .height(200)
        .fillOpacity(0)
        .strokeWidth(3)
        .stroke(Color.Red)
        .strokeDashArray([1, 2])
      // ...
    }.width('100%')
  }
}

[h2]UI Visual Attribute Effects

Universal attributes such as backgroundColor and linearGradient are applied to the component's background area, not its content area.

@Entry
@Component
struct CircleExample {
  build() {
    Column({ space: 10 }) {
      // ...
      // Draw a circle whose diameter is 150.
      Circle()
        .width(150)
        .height(200)
        .backgroundColor(Color.Pink) // This applies to 150 x 200 rectangular background, not the circular content area.
    }.width('100%')
  }
}