• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CanvasGradient
2
3**CanvasGradient** provides a gradient object.
4
5## addColorStop
6
7addColorStop(offset: number, color: string): void
8
9Adds a color stop for the** CanvasGradient** object based on the specified offset and gradient color.
10
11- Parameters
12
13
14
15  | Name   | Type   | Description                                                  |
16  | ------ | ------ | ------------------------------------------------------------ |
17  | 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. |
18  | color  | string | Gradient color to set.                                       |
19
20- Example Code
21
22  ```
23  <!-- xxx.hml -->
24  <div>
25    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
26    <input type="button" style="width: 180px; height: 60px;" value="fillStyle" onclick="handleClick" />
27  </div>
28  ```
29
30  ```
31  // xxx.js
32  export default {
33    handleClick() {
34      const el =this.$refs.canvas;
35      const ctx =el.getContext('2d');
36      const gradient = ctx.createLinearGradient(0,0,100,0);
37      gradient.addColorStop(0,'#00ffff');
38      gradient.addColorStop(1,'#ffff00');
39    }
40  }
41  ```
42
43  ![img](figures/en-us_image_0000001152610806.png)