Canvas: Difference between revisions
Line 26: | Line 26: | ||
//console.log("c is",c) | //console.log("c is",c) | ||
p = c.getContext("2d") | p = c.getContext("2d") | ||
p.fillRect(50, 50, 150, 150); | |||
} | } | ||
</script> | </script> | ||
Line 36: | Line 32: | ||
<!-- The body tag then will call draw() when the page has loaded --> | <!-- The body tag then will call draw() when the page has loaded --> | ||
<body onload="draw()"> | <body onload="draw()"> | ||
<canvas id="canvas" width=" | <canvas id="canvas" width="250" height="250" style="border: 1px solid black"></canvas> | ||
</body> | </body> | ||
</html> | </html> |
Revision as of 16:05, 25 September 2012
The canvas tag was introduced by Apple and has been standardized to become part of HTML5.
To use it, you add the canvas tag to your HTML page, then use javascript draw on it.
<canvas id="canvas" showsrc> function draw() {
c = document.getElementById("canvas"); p = c.getContext("2d"); p.fillRect(50, 50, 150, 150);
} </canvas>
Template
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
// Define a draw function that contains your
// drawing code
function draw(){
c = document.getElementById("canvas")
//console.log("c is",c)
p = c.getContext("2d")
p.fillRect(50, 50, 150, 150);
}
</script>
</head>
<!-- The body tag then will call draw() when the page has loaded -->
<body onload="draw()">
<canvas id="canvas" width="250" height="250" style="border: 1px solid black"></canvas>
</body>
</html>