openharmony 鸿蒙 ts-drawingrenderingcontext

2025-06-12 浏览 (1)

DrawingRenderingContext

DrawingRenderingContext provides a rendering context for drawing rectangles, text, images, and other objects on a canvas.

NOTE

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

APIs

DrawingRenderingContext(unit?: LengthMetricsUnit)

Widget capability: This API can be used in ArkTS widgets since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
unit12+LengthMetricsUnitNoUnit mode of the DrawingRenderingContext object. The value cannot be changed once set. The configuration method is the same as that of CanvasRenderingContext2D.
Default value: DEFAULT

Attributes

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeRead OnlyOptionalDescription
sizeSizeNoNoWidth and height of the context.
Default unit: vp
canvasCanvasNoNoCanvas object. For details, see Canvas.

Size

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeRead OnlyOptionalDescription
widthnumberNoNoWidth of the DrawingRenderingContext object, which corresponds to the width of the associated Canvas component.
heightnumberNoNoHeight of the DrawingRenderingContext object, which corresponds to the height of the associated Canvas component.

Methods

invalidate

invalidate(): void

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Invalidates the component and triggers re-rendering of the component.

Example

This example shows how to use the APIs in DrawingRenderingContext for drawing.

// xxx.ets
@Entry
@Component
struct CanvasExample {
  private context: DrawingRenderingContext = new DrawingRenderingContext()

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() => {
          this.context.canvas.drawCircle(200, 200, 100)
          this.context.invalidate()
        })
    }
    .width('100%')
    .height('100%')
  }
}

en-us_image_0000001194032666

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

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