1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import router from '@ohos.router'; 17 18@Entry 19@Component 20struct GradientExample { 21 private settings: RenderingContextSettings = new RenderingContextSettings(true) 22 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 23 24 build() { 25 Column() { 26 Canvas(this.context) 27 .width(600) 28 .height('40%') 29 .backgroundColor('#00ffff') 30 Row() { 31 Button('createRadialGradient').width(130).height(45) 32 .key('createRadialGradient') 33 .onClick(() => { 34 var grad = this.context.createRadialGradient(130, 130, 30, 130, 130, 60) 35 grad.addColorStop(0.0, 'red'); 36 grad.addColorStop(0.5, 'white'); 37 grad.addColorStop(1.0, 'green'); 38 this.context.fillStyle = grad; 39 this.context.fillRect(50, 30, 150, 200); 40 }) 41 Button('createLinearGradient').width(130).height(45) 42 .key('createLinearGradient') 43 .onClick(() => { 44 var grad = this.context.createLinearGradient(150, 0, 250, 300) 45 grad.addColorStop(0.0, 'red'); 46 grad.addColorStop(0.5, 'white'); 47 grad.addColorStop(1.0, 'green'); 48 this.context.fillStyle = grad; 49 this.context.fillRect(250, 30, 150, 200); 50 }) 51 }.margin({ top: 20 }) 52 .width('100%') 53 .justifyContent(FlexAlign.SpaceAround) 54 55 Button('back') 56 .width(130) 57 .height(45) 58 .key('back') 59 .backgroundColor(Color.Orange) 60 .onClick(() => { 61 router.back() 62 }) 63 .margin({ top: 10 }) 64 } 65 .width('100%') 66 .height('100%') 67 .alignItems(HorizontalAlign.Center) 68 .justifyContent(FlexAlign.Center) 69 } 70}