> 文档中心 > HarmonyOS/OpenHarmony应用开发-ArkTS画布组件CanvasRenderingContext2D对象(十三)

HarmonyOS/OpenHarmony应用开发-ArkTS画布组件CanvasRenderingContext2D对象(十三)


lineTo

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

从当前点到指定点进行路径连接。

参数:

参数

类型

必填

默认值

描述

x

number

0

指定位置的x坐标

y

number

0

指定位置的y坐标。

示例

.// xxx.ets.@Entry.@Component.struct LineTo {.  private settings: RenderingContextSettings = new RenderingContextSettings(true).  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)..  build() {.    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {.      Canvas(this.context). .width('100%'). .height('100%'). .backgroundColor('#ffff00'). .onReady(() =>{.   this.context.beginPath().   this.context.moveTo(10, 10).   this.context.lineTo(280, 160).   this.context.stroke(). }).    }.    .width('100%').    .height('100%').  }.}

closePath

closePath(): void

结束当前路径形成一个封闭路径。

示例:

.// xxx.ets.@Entry.@Component.struct ClosePath {.  private settings: RenderingContextSettings = new RenderingContextSettings(true).  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)..  build() {.    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {.      Canvas(this.context). .width('100%'). .height('100%'). .backgroundColor('#ffff00'). .onReady(() =>{.     this.context.beginPath().     this.context.moveTo(30, 30).     this.context.lineTo(110, 30).     this.context.lineTo(70, 90).     this.context.closePath().     this.context.stroke(). }).    }.    .width('100%').    .height('100%').  }.}