• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(600).height('50%').backgroundColor('#00ffff')
28      Row() {
29        Button('translate')
30          .width(130).height(45)
31          .key('translate')
32          .onClick(() => {
33            this.context.fillRect(90, 90, 100, 100);
34            this.context.translate(90, 90)
35            this.context.fillRect(90, 90, 100, 100);
36          })
37        Button('transform')
38          .width(130).height(45)
39          .key('transform')
40          .onClick(() => {
41            this.context.fillStyle = 'rgb(0,0,0)';
42            this.context.fillRect(0, 0, 100, 100);
43            this.context.transform(1, 0.5, -0.5, 1, 10, 10);
44            this.context.fillStyle = 'rgb(255,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(0,0,255)';
48            this.context.fillRect(0, 0, 100, 100);
49          })
50        Button('setTransform')
51          .width(130).height(45)
52          .key('setTransform')
53          .onClick(() => {
54            this.context.fillStyle = 'rgb(0,0,255)';
55            this.context.fillRect(20, 20, 100, 100);
56            this.context.setTransform(1, 0.5, -0.5, 1, 10, 10);
57          })
58      }.margin({ top: 10 })
59      .width('100%')
60      .justifyContent(FlexAlign.SpaceAround)
61
62      Row() {
63        Button('rotate').width(130).height(45)
64          .key('rotate')
65          .onClick(() => {
66            this.context.rotate(45 * Math.PI / 180);
67            this.context.fillRect(300, 100, 100, 100);
68          })
69        Button('back')
70          .width(130).height(45).backgroundColor(Color.Orange)
71          .key('back')
72          .onClick(() => {
73            router.back()
74          })
75      }.margin({ top: 10 })
76      .width('100%')
77      .justifyContent(FlexAlign.SpaceAround)
78    }
79    .alignItems(HorizontalAlign.Center)
80    .justifyContent(FlexAlign.Center)
81  }
82}