1e41f4b71Sopenharmony_ci# CanvasGradient
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci**CanvasGradient** provides a canvas gradient object.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>  **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>  The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## addColorStop
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciaddColorStop(offset: number, color: string): void
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciAdds a color stop for the **CanvasGradient** object based on the specified offset and gradient color.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 9.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Parameters**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description |
26e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------------------------- |
27e41f4b71Sopenharmony_ci| offset | number | Yes | Relative position of the gradient stop along the gradient vector. The value ranges from 0 to 1.            |
28e41f4b71Sopenharmony_ci| color  | string | Yes | Gradient color to set. For details about the color notation, see the description of the string type in [ResourceColor](ts-types.md#resourcecolor). |
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**Example**
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci  ```ts
34e41f4b71Sopenharmony_ci  // xxx.ets
35e41f4b71Sopenharmony_ci  @Entry
36e41f4b71Sopenharmony_ci  @Component
37e41f4b71Sopenharmony_ci  struct AddColorStop {
38e41f4b71Sopenharmony_ci    private settings: RenderingContextSettings = new RenderingContextSettings(true)
39e41f4b71Sopenharmony_ci    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci    build() {
42e41f4b71Sopenharmony_ci      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
43e41f4b71Sopenharmony_ci        Canvas(this.context)
44e41f4b71Sopenharmony_ci          .width('100%')
45e41f4b71Sopenharmony_ci          .height('100%')
46e41f4b71Sopenharmony_ci          .backgroundColor('#ffff00')
47e41f4b71Sopenharmony_ci          .onReady(() => {
48e41f4b71Sopenharmony_ci            let grad = this.context.createLinearGradient(50, 0, 300, 100)
49e41f4b71Sopenharmony_ci            grad.addColorStop(0.0, '#ff0000')
50e41f4b71Sopenharmony_ci            grad.addColorStop(0.5, '#ffffff')
51e41f4b71Sopenharmony_ci            grad.addColorStop(1.0, '#00ff00')
52e41f4b71Sopenharmony_ci            this.context.fillStyle = grad
53e41f4b71Sopenharmony_ci            this.context.fillRect(0, 0, 400, 400)
54e41f4b71Sopenharmony_ci          })
55e41f4b71Sopenharmony_ci      }
56e41f4b71Sopenharmony_ci      .width('100%')
57e41f4b71Sopenharmony_ci      .height('100%')
58e41f4b71Sopenharmony_ci    }
59e41f4b71Sopenharmony_ci  }
60e41f4b71Sopenharmony_ci  ```
61e41f4b71Sopenharmony_ci  ![en-us_image_0000001194032516](figures/en-us_image_0000001194032516.jpeg)
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci 
64