title: CanvasGradient

sidebar_label: CanvasGradient

The gradient object.

Reference

Methods

addColorStop

Adds color gradient points. Areas less than the minimum stop are rendered with the color of the minimum stop. Areas greater than the maximum stop are rendered with the color of the maximum stop.

Reference

  1. (stop: number, color: string) => void
Property Type Description
stop number Represents a position between the start and end of the gradient. Value range: 0-1.
color string The color of the gradient point.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // Create circular gradient
  3. const grd = ctx.createLinearGradient(30, 10, 120, 10)
  4. grd.addColorStop(0, 'red')
  5. grd.addColorStop(0.16, 'orange')
  6. grd.addColorStop(0.33, 'yellow')
  7. grd.addColorStop(0.5, 'green')
  8. grd.addColorStop(0.66, 'cyan')
  9. grd.addColorStop(0.83, 'blue')
  10. grd.addColorStop(1, 'purple')
  11. // Fill with gradient
  12. ctx.setFillStyle(grd)
  13. ctx.fillRect(10, 10, 150, 80)
  14. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasGradient.addColorStop ✔️

API Support

API WeChat Mini-Program H5 React Native
CanvasGradient.addColorStop ✔️