• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CanvasGradient对象
2
3渐变对象。
4
5>  **说明:**
6>
7>  从 API Version 8 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10
11## addColorStop
12
13addColorStop(offset: number, color: string): void
14
15设置渐变断点值,包括偏移和颜色。
16
17
18**参数:**
19
20| 参数     | 类型     | 必填   | 默认值       | 描述                           |
21| ------ | ------ | ---- | --------- | ---------------------------- |
22| offset | number | 是    | 0         | 设置渐变点距离起点的位置占总体长度的比例,范围为0到1。 |
23| color  | string | 是    | '#ffffff' | 设置渐变的颜色。                     |
24
25
26**示例:**
27
28  ```ts
29// xxx.ets
30@Entry
31@Component
32struct Page45 {
33  private settings: RenderingContextSettings = new RenderingContextSettings(true)
34  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
35
36  build() {
37    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
38      Canvas(this.context)
39        .width('100%')
40        .height('100%')
41        .backgroundColor('#ffff00')
42        .onReady(() => {
43          var grad = this.context.createLinearGradient(50, 0, 300, 100)
44          grad.addColorStop(0.0, '#ff0000')
45          grad.addColorStop(0.5, '#ffffff')
46          grad.addColorStop(1.0, '#00ff00')
47          this.context.fillStyle = grad
48          this.context.fillRect(0, 0, 500, 500)
49        })
50    }
51    .width('100%')
52    .height('100%')
53  }
54}
55  ```
56  ![zh-cn_image_0000001194032516](figures/zh-cn_image_0000001194032516.png)
57
58
59