Canvas: Difference between revisions
Line 19: | Line 19: | ||
== Examples == | == Examples == | ||
Here is a complete HTML page with a working canvas: | |||
<source lang="javascript"><!DOCTYPE html> | <source lang="javascript"><!DOCTYPE html> | ||
Line 26: | Line 28: | ||
<script> | <script> | ||
function draw(){ | function draw(){ | ||
c = document.getElementById("c") | c = document.getElementById("c") | ||
//console.log("c is",c) | //console.log("c is",c) | ||
p = c.getContext("2d") | p = c.getContext("2d") | ||
x = 10 | x = 10 | ||
while (x< | while (x<300) { | ||
p.strokeRect(x,100,20,40) | p.strokeRect(x,100,20,40) | ||
x+=60 | x+=60 | ||
} | } | ||
} | } | ||
</script> | </script> | ||
Line 42: | Line 44: | ||
</body> | </body> | ||
</html> | </html> | ||
</source> | </source> | ||
<canvas width="300" height="300" showsrc="true"> | |||
function draw(){ | |||
c = document.getElementById("c") | |||
//console.log("c is",c) | |||
p = c.getContext("2d") | |||
x = 10 | |||
while (x<300) { | |||
p.strokeRect(x,100,20,40) | |||
x+=60 | |||
} | |||
} | |||
</canvas> | |||
When the loop reaches the end of page it breaks the line. | When the loop reaches the end of page it breaks the line. | ||
It redefines the "x" value to the begin of the line and adds 40px to "y". | It redefines the "x" value to the begin of the line and adds 40px to "y". | ||
< | <canvas width="300" height="300" showsrc="true"> | ||
<script> | <script> | ||
function draw(){ | function draw(){ | ||
Line 69: | Line 84: | ||
} | } | ||
} | } | ||
</ | </canvas> | ||
== Tutorials & Reference == | == Tutorials & Reference == |
Revision as of 14:13, 25 September 2012
The canvas tag was introduced by Apple and has been standardized to become part of HTML5.
A example for color cycling with the canvas element [[1]]
Get the canvas tag:
c=document.getElementById("c")
Get the "context" (like a paper):
p=c.getContext("2d");
Draw something!
p.fillRect(0, 0, 100, 100);
Examples
Here is a complete HTML page with a working canvas:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
function draw(){
c = document.getElementById("c")
//console.log("c is",c)
p = c.getContext("2d")
x = 10
while (x<300) {
p.strokeRect(x,100,20,40)
x+=60
}
}
</script>
</head>
<body onload="draw()">
<h1>Hello canvas</h1>
<canvas id="c" width="640" height="640" style="border: 3px dotted red"></canvas>
</body>
</html>
<canvas width="300" height="300" showsrc="true"> function draw(){
c = document.getElementById("c") //console.log("c is",c) p = c.getContext("2d") x = 10 while (x<300) { p.strokeRect(x,100,20,40) x+=60 }
} </canvas>
When the loop reaches the end of page it breaks the line.
It redefines the "x" value to the begin of the line and adds 40px to "y".
<canvas width="300" height="300" showsrc="true"> <script> function draw(){ c = document.getElementById("c"); console.log("c is ", c); p = c.getContext("2d"); hands = 1; x = 20; y = 200;
while (hands < 100) { console.log(hands); p.strokeRect(x,y,20,20); x = x + 50; hands++; if (x > 600) { x = 20; y = y + 40; } } } </canvas>