Latest web development tutorials

HTML canvas globalAlpha property

HTML canvas Reference Manual HTML canvas Reference Manual

Examples

First, draw a red rectangle, and then the transparency (globalAlpha) is set to 0.5, and then draw a blue and a green rectangle:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(20,20,75,50);
// Turn transparency on
ctx.globalAlpha=0.2;
ctx.fillStyle="blue";
ctx.fillRect(50,50,75,50);
ctx.fillStyle="green";
ctx.fillRect(80,80,75,50);

try it"

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support globalAlpha property.

Note: 8 and earlier versions of Internet Explorer do not support the <canvas> element.


Definition and Usage

globalAlpha property sets or returns the current drawing transparent value (alpha or transparency).

globalAlpha attribute value must be between 0.0 (fully transparent) Digital (opaque) and 1.0.

Defaults: 1.0
JavaScript syntax: context.globalAlpha = number;

Property Value

描述
number 透明值。必须介于 0.0(完全透明) 与 1.0(不透明) 之间。


HTML canvas Reference Manual HTML canvas Reference Manual