W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
- @Entry
- @Component
- struct Card {
- private canvasWidth: number = 0;
- private canvasHeight: number = 0;
- // 初始化CanvasRenderingContext2D和RenderingContextSettings
- private settings: RenderingContextSettings = new RenderingContextSettings(true);
- private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
- build() {
- Column() {
- Row() {
- Canvas(this.context)
- .margin('5%')
- .width('90%')
- .height('90%')
- .onReady(() => {
- console.info('[ArkTSCard] onReady for canvas draw content');
- // 在onReady回調(diào)中獲取畫布的實(shí)際寬和高
- this.canvasWidth = this.context.width;
- this.canvasHeight = this.context.height;
- // 繪制畫布的背景
- this.context.fillStyle = 'rgba(203, 154, 126, 1.00)';
- this.context.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
- // 在畫布的中心繪制一個(gè)紅色的圓
- this.context.beginPath();
- let radius = this.context.width / 3
- let circleX = this.context.width / 2
- let circleY = this.context.height / 2
- this.context.moveTo(circleX - radius, circleY);
- this.context.arc(circleX, circleY, radius, 2 * Math.PI, 0, true);
- this.context.closePath();
- this.context.fillStyle = 'red';
- this.context.fill();
- // 繪制笑臉的左眼
- let leftR = radius / 4
- let leftX = circleX - (radius / 2)
- let leftY = circleY - (radius / 3.5)
- this.context.beginPath();
- this.context.arc(leftX, leftY, leftR, 0, Math.PI, true);
- this.context.strokeStyle = '#ffff00'
- this.context.lineWidth = 10
- this.context.stroke()
- // 繪制笑臉的右眼
- let rightR = radius / 4
- let rightX = circleX + (radius / 2)
- let rightY = circleY - (radius / 3.5)
- this.context.beginPath();
- this.context.arc(rightX, rightY, rightR, 0, Math.PI, true);
- this.context.strokeStyle = '#ffff00'
- this.context.lineWidth = 10
- this.context.stroke()
- // 繪制笑臉的嘴巴
- let mouthR = radius / 2.5
- let mouthX = circleX
- let mouthY = circleY + (radius / 3)
- this.context.beginPath();
- this.context.arc(mouthX, mouthY, mouthR, Math.PI, 0, true);
- this.context.strokeStyle = '#ffff00'
- this.context.lineWidth = 10
- this.context.stroke()
- })
- }
- }.height('100%').width('100%')
- }
- }
運(yùn)行效果如下圖所示。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: