js-components-canvas-canvasgradient.md 1.4 KB
Newer Older
E
ester.zhou 已提交
1
# CanvasGradient
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

E
ester.zhou 已提交
7
**CanvasGradient**  provides a gradient object.
Z
zengyawen 已提交
8

E
ester.zhou 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
## addColorStop

addColorStop(offset: number, color: string): void

Adds a color stop for the **CanvasGradient** object based on the specified offset and gradient color.

**Parameters**

| Name   | Type   | Description                                                  |
| ------ | ------ | ------------------------------------------------------------ |
| offset | number | Proportion of the distance between the color stop and the start point to the total length. The value ranges from 0 to 1. |
| color   | string | Gradient color to set.                                       |

**Example** 
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
  <input type="button" style="width: 180px; height: 60px;" value="fillStyle"onclick="handleClick" />
</div>
  ```

  ```js
// xxx.js
export default {
  handleClick() {
    const el =this.$refs.canvas;
    const ctx =el.getContext('2d');
    const gradient = ctx.createLinearGradient(0,0,100,0);
    gradient.addColorStop(0,'#00ffff');
    gradient.addColorStop(1,'#ffff00');
  }
}
  ```

  ![en-us_image_0000001152610806](figures/en-us_image_0000001152610806.png)