How to use globalCompositeOperation to set the type of composite operation to apply when drawing shapes in JavaScript

2 Answers

0 votes
<canvas id="canvas"></canvas>
<script type="text/JavaScript">   
           
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

ctx.globalCompositeOperation = "xor";

ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = "red";
ctx.fillRect(40, 40, 100, 100);

</script>

 



answered May 27, 2016 by avibootz
0 votes
<canvas id="canvas"></canvas>
<script type="text/JavaScript">   
           
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

ctx.globalCompositeOperation = "lighter";

ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = "red";
ctx.fillRect(40, 40, 100, 100);

</script>

 



answered May 27, 2016 by avibootz
...