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 */ 15import router from '@ohos.router'; 16 17@Entry 18@Component 19struct TransformExample { 20 private settings: RenderingContextSettings = new RenderingContextSettings(true) 21 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 22 private Scroller = new Scroller() 23 24 build() { 25 Column() { 26 Canvas(this.context) 27 .width('100%') 28 .height('50%') 29 .backgroundColor('#00ffff') 30 .padding({left: 12, right: 12}) 31 Row() { 32 Button('translate') 33 .width(130).height(45) 34 .key('translate') 35 .onClick(() => { 36 this.context.fillRect(90, 90, 100, 100); 37 this.context.translate(90, 90) 38 this.context.fillRect(90, 90, 100, 100); 39 }) 40 Button('transform') 41 .width(130).height(45) 42 .key('transform') 43 .onClick(() => { 44 this.context.fillStyle = 'rgb(0,0,0)'; 45 this.context.fillRect(0, 0, 100, 100); 46 this.context.transform(1, 0.5, -0.5, 1, 10, 10); 47 this.context.fillStyle = 'rgb(255,0,0)'; 48 this.context.fillRect(0, 0, 100, 100); 49 this.context.transform(1, 0.5, -0.5, 1, 10, 10); 50 this.context.fillStyle = 'rgb(0,0,255)'; 51 this.context.fillRect(0, 0, 100, 100); 52 }) 53 Button('setTransform') 54 .width(130).height(45) 55 .key('setTransform') 56 .onClick(() => { 57 this.context.fillStyle = 'rgb(0,0,255)'; 58 this.context.fillRect(20, 20, 100, 100); 59 this.context.setTransform(1, 0.5, -0.5, 1, 10, 10); 60 }) 61 }.margin({ top: 10 }) 62 .width('100%') 63 .justifyContent(FlexAlign.SpaceAround) 64 65 Row() { 66 Button('rotate').width(130).height(45) 67 .key('rotate') 68 .onClick(() => { 69 this.context.rotate(45 * Math.PI / 180); 70 this.context.fillRect(300, 100, 100, 100); 71 }) 72 Button('back') 73 .width(130).height(45).backgroundColor(Color.Orange) 74 .key('back') 75 .onClick(() => { 76 router.back() 77 }) 78 }.margin({ top: 10 }) 79 .width('100%') 80 .justifyContent(FlexAlign.SpaceAround) 81 } 82 .alignItems(HorizontalAlign.Center) 83 .justifyContent(FlexAlign.Center) 84 } 85}