Last active
December 14, 2015 00:59
-
-
Save syntagmatic/5003052 to your computer and use it in GitHub Desktop.
Canvas Compositing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas id="picture0" width=200 height=200></canvas> | |
<canvas id="picture1" width=200 height=200></canvas> | |
<canvas id="picture2" width=200 height=200></canvas> | |
<canvas id="picture3" width=200 height=200></canvas> | |
<script> | |
var compositing = ["source-over", "lighter", "darker", "xor"]; | |
for (var i=0; i<4; i++) { | |
render(i); | |
} | |
function render(i) { | |
var canvas = document.getElementById("picture" + i); | |
var ctx = canvas.getContext("2d"); | |
ctx.globalAlpha = 0.5; | |
ctx.globalCompositeOperation = compositing[i]; | |
ctx.font = "14pt sans-serif"; | |
ctx.fillText(compositing[i],40,16); | |
ctx.fillStyle = "red"; | |
ctx.fillRect(20,20,100,100); | |
ctx.fillStyle = "green"; | |
ctx.fillRect(60,60,100,100); | |
ctx.fillStyle = "blue"; | |
ctx.fillRect(100,10,80,80); | |
} | |
</script> | |
<style> | |
canvas { | |
float: left; | |
margin: 12px; | |
box-shadow: 0 0 4px #999; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment