How to specify the color and transparency when drawing HTML5Canvas _ html5 tutorial tips-

Source: Internet
Author: User
This article describes how to specify the color and transparency when creating HTML5Canvas, including the introduction of the Global Transparency globalAlpha attribute. For more information, see 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. Global Transparency
  2. Does your browser support Canvas ?! Just change one !!
  3. Script
  4. Window. onload = function (){
  5. Var canvas = document. getElementById ("canvas ");
  6. Canvas. width = 800;
  7. Canvas. height = 600;
  8. Var context = canvas. getContext ("2d ");
  9. Context. fillStyle = "# FFF ";
  10. Context. fillRect (0, 0, 800,600 );
  11. Context. globalAlpha = 0.5;
  12. For (var I = 0; I <= 50; I ++ ){
  13. Var R = Math. floor (Math. random () * 255 );
  14. Var G = Math. floor (Math. random () * 255 );
  15. Var B = Math. floor (Math. random () * 255 );
  16. Context. fillStyle = "rgb (" + R + "," + G + "," + B + ")";
  17. Context. beginPath ();
  18. Context. arc (Math. random () * canvas. width, Math. random () * canvas. height, Math. random () * 100, 0, Math. PI * 2 );
  19. Context. fill ();
  20. }
  21. };
  22. Script

Running result:

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

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.