• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CanvasGradient对象
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @sd-wu-->
5<!--Designer: @sunbees-->
6<!--Tester: @liuli0427-->
7<!--Adviser: @HelloCrease-->
8
9>  **说明:**
10>  从API version 4开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
11
12渐变对象。
13
14
15## addColorStop
16
17addColorStop(offset: number, color: string): void
18
19设置渐变断点值,包括偏移和颜色。
20
21**参数:**
22
23| 参数     | 类型     | 描述                           |
24| ------ | ------ | ---------------------------- |
25| offset | number | 设置渐变点距离起点的位置占总体长度的比例,范围为0到1。 |
26| color  | string | 设置渐变的颜色。                     |
27
28**示例:**
29
30  ```html
31<!-- xxx.hml -->
32<div>
33  <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
34</div>
35  ```
36
37  ```js
38// xxx.js
39export default {
40    onShow() {
41        const el = this.$refs.canvas;
42        const ctx = el.getContext('2d');
43        const gradient = ctx.createLinearGradient(50, 0, 300, 100);
44        gradient.addColorStop(0.0, '#ff0000')
45        gradient.addColorStop(0.5, '#ffffff')
46        gradient.addColorStop(1.0, '#00ff00')
47        ctx.fillStyle = gradient
48        ctx.fillRect(0, 0, 300, 300)
49    }
50}
51  ```
52
53  ![zh-cn_image_0000001152610806](figures/zh-cn_image_0000001152610806.png)
54