globalAlpha Property - Javascript Canvas Reference

Javascript examples for Canvas Reference:globalAlpha


The globalAlpha property sets or gets the current alpha or transparency value, which is a number between 0.0 (fully transparent) and 1.0 (no transparency).

JavaScript syntax

context.globalAlpha = number;

Property Values

Value Description
number The transparency value. Must be a number between 0.0 (fully transparent) and 1.0 (no transparency)


The following code shows how to draw a red rectangle, set transparency (globalAlpha) to 0.5, and then draw a blue and a green rectangle:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<canvas id="myCanvas" width="300" height="250" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>


var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 55, 50);//ww  w . j ava2 s  . c  o  m

//Turn transparency on
ctx.globalAlpha = 0.2;
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 75, 50);
ctx.fillStyle = "green";
ctx.fillRect(80, 100, 75, 50);


Related Tutorials