中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
怎样使用 CanvasRenderingContext2D 在特定形状内填充颜色,如:圆角矩形 ,贝塞尔曲线和其它直线组合的区域。
微信扫码分享
@Entry @Component struct BezierCurveTo { 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) .backgroundColor('#ffff00') .width('100%') .height('100%') .onReady(() => { this.context.strokeStyle = "#333" this.context.fillStyle = '#ffe514d0' this.context.lineWidth = 2 this.context.beginPath() this.context.moveTo(30, 30) this.context.lineTo(30, 150) this.context.lineTo(100, 150) this.context.lineTo(100, 80) this.context.bezierCurveTo(75, 20, 75, 20, 30, 30) this.context.stroke() this.context.fill() this.context.beginPath() this.context.moveTo(100, 80) this.context.lineTo(100, 150) this.context.lineTo(150, 150) this.context.lineTo(150, 120) this.context.bezierCurveTo(130, 90, 160, 30, 100, 80) this.context.stroke() this.context.fillStyle = '#ff1f7fe5' this.context.fill() }) } .width('100%') .height('100%') } }