2. 水平(X)方向颜色渐进
Home >Web Front-end >H5 Tutorial >HTML5 Canvas progressive filling and transparency to achieve image Mask effect_html5 tutorial skills
Explain in detail the parameter setting and use of progressive filling in HTML5 Canvas, the setting and use of transparency in Canvas, and combine the progressive filling and transparency support to achieve the Mask effect of the image.
1: Gradient Fill (Gradient Fill)
Canvas supports two progressive filling methods, one is Line Gradient Fill (Line Gradient Fill), and the other is called
It is RadialGradient Fill. The APIs are:
createLinearGradient(x1, y1, x2, y2);
where x1, y1 are the coordinates of the first point, and x2, y2 are the coordinates of the second point.
createRadialGradient(x1, y1, r1, x2, y2, r2);
where x1, y1 are the coordinates of the first center point, r1 is the radius, x2, y2 are the coordinates of the second center point, and r2 is radius.
Set the color for each point
addColorStop(position, color);
where position represents the position, the size range [0~1], 0 represents the first point, 1 represents the coordinate of the second point
Color represents the color value, any CSS color value.
After the progressive fill object is created and configured, it can be used to set the strokeStyle and fillStyle of the context to implement text,
Gradual color filling of geometric shapes.
Code demonstration of linear gradient method:
1. Vertical (Y) direction color gradient