How to specify color and transparency in HTML5 canvas drawing

Time:2021-7-22

Specify the color

Black is the default color of canvas painting. If you want to change a color, you have to specify the color before the actual painting.

JavaScript CodeCopy content to clipboard
  1. ctx.strokeStyle = color   

Specify the color of the drawn line:

JavaScript CodeCopy content to clipboard
  1. ctx.fillStyle = color   

Specifies the color of the fill:
Let’s look at a practical example

JavaScript

JavaScript CodeCopy content to clipboard
  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:
2016325112217008.png (142×142)

Specify transparency

As in normal CSS, we can also specify the color with an alpha value (but not many, IE9 did not support it before). Look at the code:

JavaScript

JavaScript CodeCopy content to clipboard
  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:
2016325112248089.png (142×142)

Basically unchanged from the above code, that is, RGB (R, G, b) is changed into RGBA (R, G, B, a). The value of a is also 0 ~ 1, 0 means completely transparent, and 1 is completely opaque (so the value of alpha is actually “Opacity”).

Globally transparent globalalpha
This is also a very simple attribute. The default value is 1.0, which means completely opaque. The value range is 0.0 (completely transparent) ~ 1.0. This property is the same as the shadow setting. If you don’t want to set the opacity globally, you have to reset globalalpha before the next painting.

To summarize: what are the state based attributes?

——globalAlpha

——globalCompositeOpeartion

——strokeStyle

——textAlign,textBaseline

——lineCap,lineJoin,lineWidth,miterLimit

——fillStyle

——font

——shadowBlur,shadowColor,shadowOffsetX,shadowOffsetY
Let’s experience the magic of global alpha through a code~

JavaScript CodeCopy content to clipboard
  1. <!DOCTYPE html>   
  2. <html lang=“zh”>   
  3. <head>   
  4.     <meta charset=“UTF-8”>   
  5. < title > Global Transparency
  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. Your browser doesn’t support canvas?! Change one as soon as possible!!
  15.     </canvas>   
  16. </div>   
  17.   
  18. <script>   
  19.     window.onload = function(){   
  20.         var canvas = document.getElementById(“canvas”);   
  21.         canvas.width = 800;   
  22.         canvas.height = 600;   
  23.         var context = canvas.getContext(“2d”);   
  24.         context.fillStyle = “#FFF”;   
  25.         context.fillRect(0,0,800,600);   
  26.   
  27.         context.globalAlpha = 0.5;   
  28.   
  29.         for(var i=0; i<=50; i++){   
  30.             var R = Math.floor(Math.random() * 255);   
  31.             var G = Math.floor(Math.random() * 255);   
  32.             var B = Math.floor(Math.random() * 255);   
  33.   
  34.             context.fillStyle = “rgb(“ + R + “,” + G + “,” + B + “)”;   
  35.   
  36.             context.beginPath();   
  37.             context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, Math.PI * 2);   
  38.             context.fill();   
  39.         }   
  40.     };   
  41. </script>   
  42. </body>   
  43. </html>  

Results of operation:
2016325112320763.jpg (850×500)

Isn’t it really cool? It’s a bit of an artist at last.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]