You need to sign in or sign up before continuing.
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
## 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** 
E
ester.zhou 已提交
23

E
ester.zhou 已提交
24 25 26 27 28 29 30 31 32 33
  ```html
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
</div>
  ```

  ```js
// xxx.js
export default {
E
ester.zhou 已提交
34
  onShow() {
E
ester.zhou 已提交
35
    const el =this.$refs.canvas;
E
ester.zhou 已提交
36 37 38 39 40 41 42
    const ctx = el.getContext('2d');
    const gradient = ctx.createLinearGradient(50,0,300,100);
    gradient.addColorStop(0.0, 'red')
    gradient.addColorStop(0.5, 'white')
    gradient.addColorStop(1.0, 'green')
    ctx.fillStyle = gradient
    ctx.fillRect(0, 0, 300, 300)
E
ester.zhou 已提交
43 44 45 46 47
  }
}
  ```

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