- HTML5 Canvas Tutorial
- HTML5 Canvas Overview
- HTML5 Canvas: Stroke and Fill
- HTML5 Canvas: Rectangles
- HTML5 Canvas: clearRect()
- HTML5 Canvas: Paths
- HTML5 Canvas: Gradients
- HTML5 Canvas: Shadows
- HTML5 Canvas: Text
- HTML5 Canvas: Images
- HTML5 Canvas: Composition
- HTML5 Canvas: Transformations
- HTML5 Canvas: State + State Stack
- HTML5 Canvas: toDataURL()
- HTML5 Canvas: Pixel Manipulation
- HTML5 Canvas: Animation
HTML5 Canvas: Composition
When drawing shapes on an HTML5 canvas, you can set how what you draw is composited with what is already drawn on the canvas. In other words, how what you draw is blended with what is already on the canvas.
The 2D Context has two attributes which control the composition mode of a canvas. These are:
globalAlpha attribute determines the transparency / opacity of what you draw.
It can take a value between 0 and 1. A value of 0 means that what you draw is completely transparent.
A value of 0.5 means that what you draw is semi-transparent.
A value of 1 means that what you draw is completely opaque. The default value is 1.
globalAlpha attribute is set like this:
context.globalAlpha = 0.5;
globalCompositeOperation attribute determines how what you draw is blended into
the existing graphics on the canvas.
globalCompositeOperation attribute is set like this:
context.globalCompositeOperation = "copy";
The values for
globalCompositeOperation refers to a "source" and a "destination", and
specifies how the source and destination is blended. The source is what you draw, and the destination
is what is already drawn. Here is a list of the possible values, and their meaning:
||Where source and destination overlaps, the source is displayed.|
||Where the source and destination overlap, and both are opaque, the destination is displayed. Where the destination is transparent, the source is displayed.|
||Where the source and destination overlaps, and both are opaque, the destination is displayed. The source is not displayed where no overlaps exists.|
||Displays the destination everywhere the source and destination do not overlap. Everywhere else, transparency is displayed.|
||Where source and destination overlaps, the destination is displayed. Where no overlap exists, the source is displayed.|
||Where source and destination overlaps, the source is displayed. Where no overlaps exists, or where the source is transparent, the destination is displayed.|
||Where source and destination overlaps and both are opaque, the source is displayed. Everywhere else transparency is displayed.|
||Where the source and destination do not overlap, the source is displayed. Everwhere else transparency is displayed.|
||Where the source is opaque, the source is displayed. The destination is displayed everywhere else.|
||The source and destination colors are added to each other, resulting in brighter colors, moving towards color values of 1 (maximum brightness for that color).|
An HTML5 Canvas Composition Example
Here is an example canvas where you can play around with the compositing modes, and alpha values. You can change the compositing mode by clicking the buttons. The alpha mode is changed by using the control just below the canvas.
If you see a text field for the
globalAlpha, type in values between 0 and 100.
The code converts the value to be between 0 and 1.0 .
Otherwise just use the slider.
Note: at the time of writing there is a difference between how Firefox and Chrome handles these compositing modes. It also seems to be different from e.g. rects and text how the modes work. Play around with the modes in the various browsers you plan to support, to see how they work.