W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Canvas提供畫布組件,用于自定義繪制圖形,開發(fā)者使用CanvasRenderingContext2D對(duì)象和OffscreenCanvasRenderingContext2D對(duì)象在Canvas組件上進(jìn)行繪制,繪制對(duì)象可以是基礎(chǔ)形狀、文本、圖片等。
- @Entry
- @Component
- struct CanvasExample1 {
- //用來配置CanvasRenderingContext2D對(duì)象的參數(shù),包括是否開啟抗鋸齒,true表明開啟抗鋸齒。
- private settings: RenderingContextSettings = new RenderingContextSettings(true)
- //用來創(chuàng)建CanvasRenderingContext2D對(duì)象,通過在canvas中調(diào)用CanvasRenderingContext2D對(duì)象來繪制。
- private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- //在canvas中調(diào)用CanvasRenderingContext2D對(duì)象。
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() => {
- //可以在這里繪制內(nèi)容。
- this.context.strokeRect(50, 50, 200, 150);
- })
- }
- .width('100%')
- .height('100%')
- }
- }
- @Entry
- @Component
- struct CanvasExample2 {
- //用來配置CanvasRenderingContext2D對(duì)象和OffscreenCanvasRenderingContext2D對(duì)象的參數(shù),包括是否開啟抗鋸齒。true表明開啟抗鋸齒
- private settings: RenderingContextSettings = new RenderingContextSettings(true)
- private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
- //用來創(chuàng)建OffscreenCanvasRenderingContext2D對(duì)象,width為離屏畫布的寬度,height為離屏畫布的高度。通過在canvas中調(diào)用OffscreenCanvasRenderingContext2D對(duì)象來繪制。
- private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- //可以在這里繪制內(nèi)容
- this.offContext.strokeRect(50, 50, 200, 150);
- //將離屏繪值渲染的圖像在普通畫布上顯示
- let image = this.offContext.transferToImageBitmap();
- this.context.transferFromImageBitmap(image);
- })
- }
- .width('100%')
- .height('100%')
- }
- }
在畫布組件中,通過CanvasRenderingContext2D對(duì)象和OffscreenCanvasRenderingContext2D對(duì)象在Canvas組件上進(jìn)行繪制時(shí)調(diào)用的接口相同,另接口參數(shù)如無特別說明,單位均為vp。
- import lottie from '@ohos/lottie'
具體接口和示例請(qǐng)參考Lottie。
onReady(event: () => void)是Canvas組件初始化完成時(shí)的事件回調(diào),調(diào)用該事件后,可獲取Canvas組件的確定寬高,進(jìn)一步使用CanvasRenderingContext2D對(duì)象和OffscreenCanvasRenderingContext2D對(duì)象調(diào)用相關(guān)API進(jìn)行圖形繪制。
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() => {
- this.context.fillStyle = '#0097D4';
- this.context.fillRect(50, 50, 100, 100);
- })
在Canvas組件生命周期接口onReady()調(diào)用之后,開發(fā)者可以直接使用canvas組件進(jìn)行繪制?;蛘呖梢悦撾xCanvas組件和onready生命周期,單獨(dú)定義Path2d對(duì)象構(gòu)造理想的路徑,并在onready調(diào)用之后使用Canvas組件進(jìn)行繪制。
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- this.context.beginPath();
- this.context.moveTo(50, 50);
- this.context.lineTo(280, 160);
- this.context.stroke();
- })
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- let region = new Path2D();
- region.arc(100, 75, 50, 0, 6.28);
- this.context.stroke(region);
- })
OffscreenCanvasRenderingContext2D對(duì)象和CanvasRenderingContext2D對(duì)象提供了大量的屬性和方法,可以用來繪制文本、圖形,處理像素等,是Canvas組件的核心。常用接口有fill(對(duì)封閉路徑進(jìn)行填充)、clip(設(shè)置當(dāng)前路徑為剪切路徑)、stroke(進(jìn)行邊框繪制操作)等等,同時(shí)提供了fillStyle(指定繪制的填充色)、globalAlpha(設(shè)置透明度)與strokeStyle(設(shè)置描邊的顏色)等屬性修改繪制內(nèi)容的樣式。將通過以下幾個(gè)方面簡(jiǎn)單介紹畫布組件常見使用方法:
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- //繪制矩形
- this.context.beginPath();
- this.context.rect(100, 50, 100, 100);
- this.context.stroke();
- //繪制圓形
- this.context.beginPath();
- this.context.arc(150, 250, 50, 0, 6.28);
- this.context.stroke();
- //繪制橢圓
- this.context.beginPath();
- this.context.ellipse(150, 450, 50, 100, Math.PI * 0.25, Math.PI * 0, Math.PI * 2);
- this.context.stroke();
- })
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- //繪制填充類文本
- this.context.font = '50px sans-serif';
- this.context.fillText("Hello World!", 50, 100);
- //繪制描邊類文本
- this.context.font = '55px sans-serif';
- this.context.strokeText("Hello World!", 50, 150);
- })
- @Entry
- @Component
- struct GetImageData {
- private settings: RenderingContextSettings = new RenderingContextSettings(true)
- private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
- private offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)
- private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- // 使用drawImage接口將圖片畫在(0,0)為起點(diǎn),寬高130的區(qū)域
- this.offContext.drawImage(this.img,0,0,130,130);
- // 使用getImageData接口,獲得canvas組件區(qū)域中,(50,50)為起點(diǎn),寬高130范圍內(nèi)的繪制內(nèi)容
- let imagedata = this.offContext.getImageData(50,50,130,130);
- // 使用putImageData接口將得到的ImageData畫在起點(diǎn)為(150, 150)的區(qū)域中
- this.offContext.putImageData(imagedata,150,150);
- // 將離屏繪制的內(nèi)容畫到canvas組件上
- let image = this.offContext.transferToImageBitmap();
- this.context.transferFromImageBitmap(image);
- })
- }
- .width('100%')
- .height('100%')
- }
- }
Canvas中還提供其他類型的方法。漸變(CanvasGradient對(duì)象)相關(guān)的方法:createLinearGradient(創(chuàng)建一個(gè)線性漸變色)、createRadialGradient(創(chuàng)建一個(gè)徑向漸變色)等。
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- //創(chuàng)建一個(gè)徑向漸變色的CanvasGradient對(duì)象
- let grad = this.context.createRadialGradient(200,200,50, 200,200,200)
- //為CanvasGradient對(duì)象設(shè)置漸變斷點(diǎn)值,包括偏移和顏色
- grad.addColorStop(0.0, '#E87361');
- grad.addColorStop(0.5, '#FFFFF0');
- grad.addColorStop(1.0, '#BDDB69');
- //用CanvasGradient對(duì)象填充矩形
- this.context.fillStyle = grad;
- this.context.fillRect(0, 0, 400, 400);
- })
- @Entry
- @Component
- struct ClearRect {
- 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('#F5DC62')
- .onReady(() =>{
- // 設(shè)定填充樣式,填充顏色設(shè)為藍(lán)色
- this.context.fillStyle = '#0097D4';
- // 以(50, 50)為左上頂點(diǎn),畫一個(gè)寬高200的矩形
- this.context.fillRect(50,50,200,200);
- // 以(70, 70)為左上頂點(diǎn),清除寬150高100的區(qū)域
- this.context.clearRect(70,70,150,100);
- })
- }
- .width('100%')
- .height('100%')
- }
- }
- @Entry
- @Component
- struct Path2d {
- private settings: RenderingContextSettings = new RenderingContextSettings(true);
- private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
- build() {
- Row() {
- Column() {
- Canvas(this.context)
- .width('100%')
- .height('100%')
- .backgroundColor('#F5DC62')
- .onReady(() =>{
- // 使用Path2D的接口構(gòu)造一個(gè)五邊形
- let path = new Path2D();
- path.moveTo(150, 50);
- path.lineTo(50, 150);
- path.lineTo(100, 250);
- path.lineTo(200, 250);
- path.lineTo(250, 150);
- path.closePath();
- // 設(shè)定填充色為藍(lán)色
- this.context.fillStyle = '#0097D4';
- // 使用填充的方式,將Path2D描述的五邊形繪制在canvas組件內(nèi)部
- this.context.fill(path);
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
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)系方式:
更多建議: