box-o-sand/html5/canvas/mdn-tutorial.html
2013-02-10 22:02:10 -05:00

76 lines
1.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>MDN Canvas Tutorial</title>
<style type="text/css">
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="tutorial0" width="150" height="150"></canvas>
<canvas id="tutorial1" width="150" height="150"></canvas>
<canvas id="tutorial2" width="150" height="150"></canvas>
<canvas id="tutorial3" width="150" height="150"></canvas>
<script type="text/javascript">
function draw0() {
var ctx = document.getElementById('tutorial0').getContext('2d');
ctx.fillStyle = 'rgb(200, 0, 0)';
ctx.fillRect(10, 10, 55, 50);
ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';
ctx.fillRect(30, 30, 55, 50);
}
function draw1() {
var ctx = document.getElementById('tutorial1').getContext('2d');
ctx.fillRect(25, 25, 100, 100);
ctx.clearRect(45, 45, 60, 60);
ctx.strokeRect(50, 50, 50, 50);
}
function draw2() {
var ctx = document.getElementById('tutorial2').getContext('2d');
ctx.beginPath();
ctx.arc(75, 75, 50, 0, Math.PI*2, true);
ctx.moveTo(110, 75);
ctx.arc(75, 75, 35, 0, Math.PI, false);
ctx.moveTo(65, 65);
ctx.arc(60, 65, 5, 0, Math.PI*2, true);
ctx.moveTo(95, 65);
ctx.arc(90, 65, 5, 0, Math.PI*2, true);
ctx.stroke();
}
function draw3() {
var ctx = document.getElementById('tutorial3').getContext('2d');
ctx.beginPath();
ctx.moveTo(25, 25);
ctx.lineTo(105, 25);
ctx.lineTo(25, 105);
ctx.fill();
ctx.beginPath();
ctx.moveTo(125, 125);
ctx.lineTo(125, 45);
ctx.lineTo(45, 125);
ctx.closePath();
ctx.stroke();
}
window.addEventListener('load', function() {
draw0();
draw1();
draw2();
draw3();
}, false);
</script>
</body>
</html>