Comments: This article will introduce in detail how to set and use the transparency in the Canvas. Combined with incremental filling and transparency Support, the image Mask effect can be achieved. The Code of linear progressive mode is demonstrated as follows, for more information about how to set and use parameters of progressive padding in HTML5 Canvas, and how to set and use opacity in Canvas, combined with incremental filling and transparency Support, the image's Mask effect is achieved.
1. Progressive filling (Gradient Fill)
Canvas supports two progressive filling modes: Line Gradient Fill and
Fill the radial gradient (RadialGradient Fill ). The APIs are:
CreateLinearGradient (x1, y1, x2, y2 );
X1 and y1 are the first coordinate, x2 and y2 are the Second coordinate.
CreateRadialGradient (x1, y1, r1, x2, y2, r2 );
X1 and y1 are the coordinates of the first center, r1 is the radius, x2, y2 is the coordinates of the second center, and r2 is the radius.
Set color for each vertex
AddColorStop (position, color );
Position indicates the position. The size range is [0 ~ 1] 0 indicates the first vertex, and 1 indicates the Second coordinate.
Color indicates the Color value, which is the Color value of any CSS.
After the incremental filling object is created and configured, it can be used to set the strokeStyle and fillStyle of context to implement text,
Fill in the progressive color of the geometric shape.
Code demonstration of linear progressive mode:
1. Gradual color in the vertical (Y) Direction
The Code is as follows:
// Vertical/Y direction
Var lineGradient = ctx. createLinearGradient (50, 0, 50,200 );
LineGradient. addColorStop (0, 'rgba (255, 0, 0, 1 )');
LineGradient. addColorStop (1, 'rgba (255,255, 0, 1 )');
Ctx. fillStyle = lineGradient;
Ctx. fillRect (0, 0,300,300 );
2. Horizontal (X) Direction gradual color
The Code is as follows:
// Horizontal/X direction
Var lineGradient = ctx. createLinearGradient (0, 50,200, 50 );
LineGradient. addColorStop (0, 'rgba (255, 0, 0, 1 )');
LineGradient. addColorStop (1, 'rgba (255,255, 0, 1 )');
Ctx. fillStyle = lineGradient;
Ctx. fillRect (0, 0,300,300 );
3. vertical and horizontal (XY direction) colors gradually
The Code is as follows:
// Vertical and horizontal ction
Var lineGradient = ctx. createLinearGradient (0, 0,200,200 );
LineGradient. addColorStop (0, 'rgba (255, 0, 0, 1 )');
LineGradient. addColorStop (1, 'rgba (255,255, 0, 1 )');
Ctx. fillStyle = lineGradient;
Ctx. fillRect (0, 0,300,300 );
2. Transparency)
The transparency settings in Canvas can be divided into global and local transparency settings.
Context. globalAlpha. You can use fillStyle to set the alpha channel in the color value.
. The Code is as follows:
// Change global alpha value
Ctx. globalAlpha = 0.5;
Ctx. fillRect (50,50, 75,50 );
// Change fill style color's alphachannel
Ctx. fillStyle = 'rgba (225,225,225, 0.5 )';
Ctx. fillRect (50,50, 75,50 );
The two effects are the same.
3. Transparent and progressive photo Mask effect
The translucent mask effect on the image is achieved through radial color gradient and transparency change. The script running effect is as follows:
The Code is as follows:
Var myImage = document. createElement ('img ');
MyImage. src = "../test.png ";
MyImage. onload = function (){
Ctx. drawImage (myImage, 80, 30, myImage. naturalWidth, myImage. naturalHeight );
Var radialGradient = ctx. createRadialGradient (canvas. width/2, canvas. height/2, 10, canvas. width/2, canvas. height/2,200 );
RadialGradient. addColorStop (0, 'rgba (247,247,247, 0 )');
RadialGradient. addColorStop (0.7, 'rgba (120,120,120, 0.5 )');
RadialGradient. addColorStop (0.9, 'rgba (0, 0, 0, 0.8 )');
RadialGradient. addColorStop (1, 'rgba (238,238,238, 1 )');
Ctx. beginPath ();
Ctx. arc (canvas. width/2, canvas. height/2,300, 0, Math. PI * 2, true );
Ctx. closePath ();
Ctx. fillStyle = radialGradient;
Ctx. fill ();
}