Created
October 18, 2010 13:27
-
-
Save mrspeaker/632214 to your computer and use it in GitHub Desktop.
Code for the Global Composite Operation podcast
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Global Composite Operation</title> | |
<link rel="stylesheet" href="venn.css" type="text/css"> | |
</head> | |
<body> | |
<div id="container"> | |
<p>Canvas Global Composite Operation</p> | |
<canvas id="venn" width="1000" height="600"></canvas> | |
</div> | |
<script src="venn.js" type="text/javascript" charset="utf-8"></script> | |
<script type="text/javascript" charset="utf-8"> | |
window.addEventListener( 'load', function(){ | |
runVennDemo(); | |
}, false); | |
</script> | |
</body> | |
</html> |
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
body{ | |
background: #cacfb4; | |
} | |
#container{ | |
width: 1000px; | |
height: 100%; | |
text-align: center; | |
margin: 0px auto; | |
} | |
p{ | |
font-size: 36pt; | |
color: #fff; | |
text-shadow: 1px 1px #000; | |
padding: 10px; | |
background-color: rgba(0,0,0,0.4); | |
margin-top: -15px; | |
} | |
p span{ | |
color: #222; | |
} |
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
// Draw a circle to context | |
function drawCircle(ctx, x, y, radius, colour){ | |
ctx.fillStyle = colour; | |
ctx.beginPath(); | |
ctx.arc( x, y, radius, 0, Math.PI * 2, true); | |
ctx.closePath(); | |
ctx.fill(); | |
} | |
// Run our demo code | |
function runVennDemo(){ | |
// Get the 2D context | |
var ctx = document.getElementById("venn").getContext("2d"); | |
ctx.globalCompositeOperation = "darker"; | |
// Draw the "venn diagram" | |
drawCircle( ctx, 340, 400, 200, "#f00" ); | |
drawCircle( ctx, 620, 400, 200, "#00f" ); | |
ctx.globalCompositeOperation = "xor"; // Change the GCO mid-draw | |
drawCircle( ctx, 475, 200, 200, "#0f0" ); | |
// Draw some text | |
ctx.globalAlpha = 0.7; | |
ctx.globalCompositeOperation = "xor"; | |
ctx.save(); | |
ctx.rotate(-20 * Math.PI / 180); | |
ctx.fillStyle = "#0ff"; | |
ctx.font = "120pt Helvetica"; | |
ctx.fillText("HTML5!", 90, 450); | |
ctx.fillStyle = "#ff0"; | |
ctx.fillText("HTML5!", 95, 455); | |
ctx.restore(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment