1e41f4b71Sopenharmony_ci# CanvasGradient
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>  **NOTE**
4e41f4b71Sopenharmony_ci>
5e41f4b71Sopenharmony_ci>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**CanvasGradient** provides a canvas gradient object.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## addColorStop
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciaddColorStop(offset: number, color: string): void
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ciAdds a color stop for the **CanvasGradient** object based on the specified offset and gradient color.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci**Parameters**
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci| Name    | Type    | Description                          |
19e41f4b71Sopenharmony_ci| ------ | ------ | ---------------------------- |
20e41f4b71Sopenharmony_ci| offset | number | Relative position of the gradient stop along the gradient vector, represented by the ratio of the distance between the gradient stop and the start point to the total length. The value ranges from 0 to 1.|
21e41f4b71Sopenharmony_ci| color  | string | Gradient color to set.                    |
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Example**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci  ```html
26e41f4b71Sopenharmony_ci<!-- xxx.hml -->
27e41f4b71Sopenharmony_ci<div>
28e41f4b71Sopenharmony_ci  <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
29e41f4b71Sopenharmony_ci</div>
30e41f4b71Sopenharmony_ci  ```
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci  ```js
33e41f4b71Sopenharmony_ci// xxx.js
34e41f4b71Sopenharmony_ciexport default {
35e41f4b71Sopenharmony_ci    onShow() {
36e41f4b71Sopenharmony_ci        const el = this.$refs.canvas;
37e41f4b71Sopenharmony_ci        const ctx = el.getContext('2d');
38e41f4b71Sopenharmony_ci        const gradient = ctx.createLinearGradient(50, 0, 300, 100);
39e41f4b71Sopenharmony_ci        gradient.addColorStop(0.0, '#ff0000')
40e41f4b71Sopenharmony_ci        gradient.addColorStop(0.5, '#ffffff')
41e41f4b71Sopenharmony_ci        gradient.addColorStop(1.0, '#00ff00')
42e41f4b71Sopenharmony_ci        ctx.fillStyle = gradient
43e41f4b71Sopenharmony_ci        ctx.fillRect(0, 0, 300, 300)
44e41f4b71Sopenharmony_ci    }
45e41f4b71Sopenharmony_ci}
46e41f4b71Sopenharmony_ci  ```
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci  ![en-us_image_0000001152610806](figures/en-us_image_0000001152610806.png)
49