目录

CanvasGradient

Provides a canvas gradient object.

CanvasGradient.addColorStop

CanvasGradient.addColorStop(number stop, string color)

Adds a color gradient stop. The part smaller than the minimum stop is rendered in the color of the minimum stop, and the part larger than the maximum stop is rendered in the color of the maximum stop.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
stop number Yes Position between the start and end in the gradient. The value ranges from 0 to 1.
color string Yes Color of the gradient stop.

Example:

const ctx = has.createCanvasContext('myCanvas');
// Create linear gradient
const grd = ctx.createLinearGradient(30, 10, 120, 10);
grd.addColorStop(0, 'red');
grd.addColorStop(0.2, 'white');
grd.addColorStop(0.4, 'blue');
grd.addColorStop(0.6, 'gray');
grd.addColorStop(0.8, 'green');
grd.addColorStop(1, 'black');
// Fill with gradient
ctx.setFillStyle(grd);
ctx.fillRect(10, 10, 150, 80);
ctx.draw();