Canvas: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(33 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The [[wikipedia:Canvas element|canvas]] tag was introduced by Apple and has been [http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html standardized to become part of HTML5].
The [[wikipedia:Canvas element|canvas]] tag was introduced by Apple and has been [http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html standardized to become part of HTML5].


[http://pzwart3.wdka.hro.nl/~mmurtaugh/canvas.html A simple example page...]
To use it, you add the canvas tag to your HTML page, then use javascript draw on it.


A example for color cycling with the canvas element [[http://www.effectgames.com/demos/canvascycle/]]
<canvas id="example" showsrc>
function draw() {
  c = document.getElementById("example");
  p = c.getContext("2d");
  p.fillRect(50, 50, 150, 150);
}
</canvas>


== Template ==


Get the canvas tag:
A complete HTML page with canvas tag + javascript to draw in it, note how the draw function is first defined (in the script tag) and then finally called by the body "onload". This ensures that the page is fully loaded before the drawing is performed.


c=document.getElementById("c")
<source lang="javascript"><!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>
</source>


Get the "context" (like a paper):
== Links ==
 
* [[Looping with canvas]]
p=c.getContext("2d");
* [http://developer.mozilla.org/en-US/docs/Canvas_tutorial Mozilla canvas Tutorial]
 
* [http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html The official canvas specification]
Draw something!
* [http://www.effectgames.com/demos/canvascycle/ An example for color cycling with the canvas element]
 
* [http://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas Pixel manipulation with canvas]
p.fillRect(0, 0, 100, 100);

Latest revision as of 09:36, 30 September 2014

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="example" showsrc> function draw() {

 c = document.getElementById("example");
 p = c.getContext("2d");
 p.fillRect(50, 50, 150, 150);

} </canvas>

Template

A complete HTML page with canvas tag + javascript to draw in it, note how the draw function is first defined (in the script tag) and then finally called by the body "onload". This ensures that the page is fully loaded before the drawing is performed.

<!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>

Links