Compositing

Next ❯ImageData

  • turned_in_notCompositing Related Properties
    • globalAlpha
    • globalCompositeOperation


globalAlpha

Used to set or get the alpha value of drawing onto the canvas. Default value is 1.0 (opaque)

  • Value range 0.0 (full transparent) - 1.0 (opaque)

Syntax - To set alpha value

ctx.globalAlpha=number

Syntax - To get alpha value

ctx.globalAlpha
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

ctx.fillStyle="red";ctx.fillRect(10,10,80,40);
ctx.globalAlpha=0.4;
ctx.fillStyle="blue";ctx.fillRect(30,30,80,40);
ctx.fillStyle="teal";ctx.fillRect(50,50,80,40);
globalAlphasubject
globalAlphaclose
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
</head>
<body>
  <canvas id="canvas" width="200" height="100" style="border:1px solid teal;"></canvas>
<script>
  var canvas = document.getElementById("canvas");
  var ctx = canvas.getContext("2d");

/* Now we code to draw on canvas */
  ctx.fillStyle="red";ctx.fillRect(10,10,80,40);
  ctx.globalAlpha=0.4;
  ctx.fillStyle="blue";ctx.fillRect(30,30,80,40);
  ctx.fillStyle="teal";ctx.fillRect(50,50,80,40);
</script>
</body>
</html>


globalCompositeOperation

Used to set or get how a new drawing are drawn onto an existing old drawing. Default value is 'source-over'

Syntax - To set global composite

ctx.globalCompositeOperation = "source-over|source-in|source-atop|source-out|destination-over|destination-in|destination-atop|destination-out|lighter|copy|xor"

Syntax - To get global composite

ctx.globalCompositeOperation

Below Red rectangle is old drawing (destination) & Green circle is new drawing (source)
ctx.globalCompositeOperation
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

ctx.fillStyle="red";ctx.fillRect(20,20,60,60);
ctx.globalCompositeOperation="source-over";
ctx.fillStyle="teal";ctx.arc(80,60,30,0,2*Math.PI);ctx.fill();
globalCompositeOperationsubject
globalCompositeOperationclose
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
</head>
<body>
  <canvas id="canvas" width="200" height="100" style="border:1px solid teal;"></canvas>
<script>
  var canvas = document.getElementById("canvas");
  var ctx = canvas.getContext("2d");

/* Now we code to draw on canvas */
  ctx.fillStyle="red";ctx.fillRect(20,20,60,60);
  ctx.globalCompositeOperation="source-over";
  ctx.fillStyle="teal";ctx.arc(80,60,30,0,2*Math.PI);ctx.fill();
</script>
</body>
</html>

source image - new drawing image
destination image - old drawing image

ValueDescription
source-over(Default) Displays the source image over the destination image
source-inDisplays only the source image part which is inside the destination image
source-atopDisplays the source image (only part under destination image area) with the destination image
source-outDisplays only the source image part which is outside the destination image
destination-overDisplays the destination image over the source image
destination-inDisplays only the destination image part which is inside the source image
destination-atopDisplays the destination image (only part under source image area) with the source image
destination-outDisplays only the destination image part which is outside the source image
lighterDisplays the combination of source & destination image
copyDisplays only the source image
xorDisplays the xor of source & destination image

  • ImageData
❮ Prev Effects
Next ❯ImageData
TryOut Examples"Learn to Explore..!"

TryOut Editor

receipt