Detailed description of how to specify the color and transparency when drawing HTML5 Canvas, html5canvas
Color
Black is the default color drawn by Canvas. To change the color, you must specify the color before the actual painting.
Copy the content to the clipboard using JavaScript Code
- Ctx. strokeStyle = color
Specify the line color:
Copy the content to the clipboard using JavaScript Code
- Ctx. fillStyle = color
Specify the fill color:
Let's take a look at the actual example:
JavaScript
Copy the content to the clipboard using JavaScript Code
- Onload = function (){
- Draw ();
- };
- Function draw (){
- Var canvas = document. getElementById ('c1 ');
- If (! Canvas |! Canvas. getContext) {return false ;}
- Var ctx = canvas. getContext ('2d ');
- Ctx. beginPath ();
- Ctx. fillStyle = 'rgb (192, 80, 77) '; // red
- Ctx. arc (70, 45, 35, 0, Math. PI * 2, false );
- Ctx. fill ();
- Ctx. beginPath ();
- Ctx. fillStyle = 'rgb (155,187, 89) '; // green
- Ctx. arc (45, 95, 35, 0, Math. PI * 2, false );
- Ctx. fill ();
- Ctx. beginPath ();
- Ctx. fillStyle = 'rgb (128,100,162) '; // purple
- Ctx. arc (95, 95, 35, 0, Math. PI * 2, false );
- Ctx. fill ();
- }
The effect is as follows:
Specify transparency
Like in common CSS, we can also include an alpha value when specifying the color (but not much, which is not supported before IE9 ). Check the Code:
JavaScript
Copy the content to the clipboard using JavaScript Code
- Onload = function (){
- Draw ();
- };
- Function draw (){
- Var canvas = document. getElementById ('c1 ');
- If (! Canvas |! Canvas. getContext) {return false ;}
- Var ctx = canvas. getContext ('2d ');
- Ctx. beginPath ();
- Ctx. fillStyle = 'rgba (192, 80, 77, 0.7 )';//
- Ctx. arc (70, 45, 35, 0, Math. PI * 2, false );
- Ctx. fill ();
- Ctx. beginPath ();
- Ctx. fillStyle = 'rgba (155,187, 89, 0.7 )';//
- Ctx. arc (45, 95, 35, 0, Math. PI * 2, false );
- Ctx. fill ();
- Ctx. beginPath ();
- Ctx. fillStyle = 'rgba (128,100,162, 0.7 )';//
- Ctx. arc (95, 95, 35, 0, Math. PI * 2, false );
- Ctx. fill ();
- }
The result is as follows:
The above code is basically unchanged, that is, the rgb (r, g, B) is changed to rgba (r, g, B, a), and the value of a is also 0 ~ 1. 0 indicates completely transparent, and 1 indicates completely opaque (so the alpha value is actually "opacity ").
Global Transparency globalAlpha
This is also a very simple property. The default value is 1.0, which indicates that it is completely opaque. The value range is 0.0 (completely transparent )~ 1.0. This attribute is the same as the shadow setting. If you do not want to Set opacity for the global image, you must reset globalAlpha before the next painting.
To sum up, what are the status-based attributes?
-- GlobalAlpha
-- GlobalCompositeOpeartion
-- StrokeStyle
-- TextAlign, textBaseline
-- LineCap, lineJoin, lineWidth, miterLimit
-- FillStyle
-- Font
-- ShadowBlur, shadowColor, shadowOffsetX, and shadowOffsetY
Let's use a code to experience the magic of globalAlpha ~
Copy the content to the clipboard using JavaScript Code
- <! DOCTYPE html>
- <Html lang = "zh">
- <Head>
- <Meta charset = "UTF-8">
- <Title> global transparency </title>
- <Style>
- Body {background: url ("./images/bg3.jpg") repeat ;}
- # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
- </Style>
- </Head>
- <Body>
- <Div id = "canvas-warp">
- <Canvas id = "canvas">
- Does your browser support Canvas ?! Just change one !!
- </Canvas>
- </Div>
- <Script>
- Window. onload = function (){
- Var canvas = document. getElementById ("canvas ");
- Canvas. width = 800;
- Canvas. height = 600;
- Var context = canvas. getContext ("2d ");
- Context. fillStyle = "# FFF ";
- Context. fillRect (0, 0, 800,600 );
- Context. globalAlpha = 0.5;
- For (var I = 0; I <= 50; I ++ ){
- Var R = Math. floor (Math. random () * 255 );
- Var G = Math. floor (Math. random () * 255 );
- Var B = Math. floor (Math. random () * 255 );
- Context. fillStyle = "rgb (" + R + "," + G + "," + B + ")";
- Context. beginPath ();
- Context. arc (Math. random () * canvas. width, Math. random () * canvas. height, Math. random () * 100, 0, Math. PI * 2 );
- Context. fill ();
- }
- };
- </Script>
- </Body>
- </Html>
Running result:
Is it really cool? Finally, let's look at the artist's fan.