Detailed description of how to specify the color and transparency when drawing HTML5 Canvas, html5canvas

Source: Internet
Author: User

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
  1. Ctx. strokeStyle = color

Specify the line color:

Copy the content to the clipboard using JavaScript Code
  1. 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
  1. Onload = function (){
  2. Draw ();
  3. };
  4. Function draw (){
  5. Var canvas = document. getElementById ('c1 ');
  6. If (! Canvas |! Canvas. getContext) {return false ;}
  7. Var ctx = canvas. getContext ('2d ');
  8. Ctx. beginPath ();
  9. Ctx. fillStyle = 'rgb (192, 80, 77) '; // red
  10. Ctx. arc (70, 45, 35, 0, Math. PI * 2, false );
  11. Ctx. fill ();
  12. Ctx. beginPath ();
  13. Ctx. fillStyle = 'rgb (155,187, 89) '; // green
  14. Ctx. arc (45, 95, 35, 0, Math. PI * 2, false );
  15. Ctx. fill ();
  16. Ctx. beginPath ();
  17. Ctx. fillStyle = 'rgb (128,100,162) '; // purple
  18. Ctx. arc (95, 95, 35, 0, Math. PI * 2, false );
  19. Ctx. fill ();
  20. }

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
  1. Onload = function (){
  2. Draw ();
  3. };
  4. Function draw (){
  5. Var canvas = document. getElementById ('c1 ');
  6. If (! Canvas |! Canvas. getContext) {return false ;}
  7. Var ctx = canvas. getContext ('2d ');
  8. Ctx. beginPath ();
  9. Ctx. fillStyle = 'rgba (192, 80, 77, 0.7 )';//
  10. Ctx. arc (70, 45, 35, 0, Math. PI * 2, false );
  11. Ctx. fill ();
  12. Ctx. beginPath ();
  13. Ctx. fillStyle = 'rgba (155,187, 89, 0.7 )';//
  14. Ctx. arc (45, 95, 35, 0, Math. PI * 2, false );
  15. Ctx. fill ();
  16. Ctx. beginPath ();
  17. Ctx. fillStyle = 'rgba (128,100,162, 0.7 )';//
  18. Ctx. arc (95, 95, 35, 0, Math. PI * 2, false );
  19. Ctx. fill ();
  20. }

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
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> global transparency </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. Context. globalAlpha = 0.5;
  26. For (var I = 0; I <= 50; I ++ ){
  27. Var R = Math. floor (Math. random () * 255 );
  28. Var G = Math. floor (Math. random () * 255 );
  29. Var B = Math. floor (Math. random () * 255 );
  30. Context. fillStyle = "rgb (" + R + "," + G + "," + B + ")";
  31. Context. beginPath ();
  32. Context. arc (Math. random () * canvas. width, Math. random () * canvas. height, Math. random () * 100, 0, Math. PI * 2 );
  33. Context. fill ();
  34. }
  35. };
  36. </Script>
  37. </Body>
  38. </Html>

Running result:

Is it really cool? Finally, let's look at the artist's fan.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.