Set color to transparency when drawing on canvas in JavaScript

Description

The following code shows how to set color to transparency when drawing on canvas.

Example


<!DOCTYPE HTML>
<html>
<head>
</head><!--from   w  w  w. j ava 2s .c o  m-->
<body>
<canvas id="canvas" width="800" height="820"></canvas>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
ctx.fillStyle = "lightgrey";
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.font = "100px sans-serif";
ctx.fillText("java2s.com", 10, 100);
ctx.strokeText("java2s.com", 10, 100);
ctx.fillStyle = "red";
ctx.globalAlpha = 0.5;
ctx.fillRect(100, 10, 150, 100);
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Set color to transparency when drawing on canvas in JavaScript
Home »
  Javascript Tutorial »
    Canvas »
      Canvas Draw
Javascript Tutorial Canvas Draw
Change the stroke style in JavaScript
Clip a region in JavaScript
Control line width in JavaScript
Create Radial Gradient on HTML5 canvas in J...
Create a rainbow radial gradient in JavaScr...
Create linear gradient in JavaScript
Draw an image on HTML5 Canvas in JavaScript
Draw transparent shapes on HTML5 Canvas in ...
Get canvas context in JavaScript
Restore a drawing state in JavaScript
Save and restore Canvas states with button ...
Set color to transparency when drawing on c...
Set shadow color and blur in JavaScript
Use image as the fill pattern in JavaScript