Draw rectangles with two different globalCompositeOperation values. - Javascript Canvas Reference

Javascript examples for Canvas Reference:globalCompositeOperation

Introduction

Red rectangles are destination images. Blue rectangles are source images.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="300" height="250" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 75, 50);/* ww w  . java  2  s  .c o  m*/
ctx.fillStyle = "blue";
ctx.globalCompositeOperation = "source-over";
ctx.fillRect(50, 50, 75, 50);
ctx.fillStyle = "red";
ctx.fillRect(150, 20, 75, 50);
ctx.fillStyle = "blue";
ctx.globalCompositeOperation = "destination-over";
ctx.fillRect(180, 50, 75, 50);

</script>

</body>
</html>

Related Tutorials