The CanvasRenderingContext2D.globalAlpha property of the Canvas 2D API specifies the alpha value that is applied to shapes and images before they are drawn onto the canvas. The value is in the range from 0.0 (fully transparent) to 1.0 (fully opaque).
See also the chapter Applying styles and color in the Canvas Tutorial.
Syntax
ctx.globalAlpha = value;
Options
- value
- A number between 0.0 (fully transparent) and 1.0 (fully opaque). The default value is 1.0. Values outside the range, including InfinityandNaNwill not be set andglobalAlphawill retain its previous value.
Examples
Using the globalAlpha property
This is just a simple code snippet using the globalAlpha property to draw two semi transparent rectangles.
HTML
<canvas id="canvas"></canvas>
JavaScript
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.globalAlpha = 0.5;
ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 100, 100);
ctx.fillStyle = "red";
ctx.fillRect(50, 50, 100, 100);
Edit the code below and see your changes update live in the canvas:
Playable code
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code" style="height:120px;"> ctx.globalAlpha = 0.5; ctx.fillStyle = 'blue'; ctx.fillRect(10, 10, 100, 100); ctx.fillStyle = 'red'; ctx.fillRect(50, 50, 100, 100);</textarea>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var textarea = document.getElementById('code');
var reset = document.getElementById('reset');
var edit = document.getElementById('edit');
var code = textarea.value;
function drawCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  eval(textarea.value);
}
reset.addEventListener('click', function() {
  textarea.value = code;
  drawCanvas();
});
edit.addEventListener('click', function() {
  textarea.focus();
})
textarea.addEventListener('input', drawCanvas);
window.addEventListener('load', drawCanvas);
A globalAlpha example
In this example, a background of four different colored squares is drawn. On top of these, there is a set of semi-transparent circles. The globalAlpha property is set at 0.2 which will be used for all shapes from that point on. Every step in the for loop draws a set of circles with an increasing radius. The final result is a radial gradient. By overlaying ever more circles on top of each other, the transparency of the circles that have already been drawn are reduced. By increasing the step count and in effect drawing more circles, the background would completely disappear from the center of the image.
var ctx = document.getElementById('canvas').getContext('2d');
// draw background
ctx.fillStyle = '#FD0';
ctx.fillRect(0, 0, 75, 75);
ctx.fillStyle = '#6C0';
ctx.fillRect(75, 0, 75, 75);
ctx.fillStyle = '#09F';
ctx.fillRect(0, 75, 75, 75);
ctx.fillStyle = '#F30';
ctx.fillRect(75, 75, 75, 75);
ctx.fillStyle = '#FFF';
// set transparency value
ctx.globalAlpha = 0.2;
// Draw semi transparent circles
for (i = 0; i < 7; i++){
  ctx.beginPath();
  ctx.arc(75, 75, 10 + 10 * i, 0, Math.PI * 2, true);
  ctx.fill();
}
<canvas id="canvas" width="150" height="150"></canvas>
| Screenshot | Live sample | 
|---|---|
|  | 
Specifications
| Specification | Status | Comment | 
|---|---|---|
| WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D.globalAlpha' in that specification. | Living Standard | 
Browser compatibility
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari | 
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | 
| Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | 
|---|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | 
Gecko-specific notes
- Starting with Gecko 5.0, specifying invalid values for globalAlphano longer throws aSYNTAX_ERRexception; these are now correctly silently ignored.
WebKit/Blink-specific notes
- In WebKit- and Blink-based Browsers, a non-standard and deprecated method ctx.setAlpha()is implemented besides this property.
See also
- The interface defining it, CanvasRenderingContext2D
- CanvasRenderingContext2D.globalCompositeOperation