KEMBAR78
Working With Canvas | ODP
Working with Canvas
It's fun
 
<canvas> is an html tag
It is defined in HTML 5
and is already part of browsers
firefox
safari
chrome
opera
canvas is not in IE
but there are alternatives... canvas is not in IE
alternatives explorercanvas http://code.google.com/p/explorercanvas
alternatives explorercanvas http://code.google.com/p/explorercanvas
simple to use: just need to include a script tag on header of your html header <!--[if IE]><script src=&quot;excanvas.js&quot;></script><![endif]-->
alternatives Chrome Frame http://code.google.com/chrome/chromeframe
alternatives Chrome Frame http://code.google.com/chrome/chromeframe simple use: put meta tag in your html file <meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;chrome=1&quot;>
alternatives Chrome Frame http://code.google.com/chrome/chromeframe simple use: put meta tag in your html file <meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;chrome=1&quot;>
But... it's a plugin...
a little bit of history
<canvas></canvas> First introduced in browsers by Apple
<canvas></canvas> First introduced in browsers by Apple Powerup dashboard and Safari
<canvas></canvas> First introduced in browsers by Apple Powerup dashboard and Safari Later on Gecko 1.8 based browsers
<canvas></canvas> First introduced in browsers by Apple Powerup dashboard and Safari Later on Gecko 1.8 based browsers Firefox 1.5
Canvas HTML Element <canvas></canvas> width attribute
height attribute
all global attributes ex.:
<canvas id=”elm” width=”10” height=”10”></canvas>
Canvas HTMLElement Js interface HTMLCanvasElement : HTMLElement { attribute unsigned long width;
attribute unsigned long height; DOMString toDataURL(in optional DOMString type, in any... args); Object getContext(in DOMString contextId); };
Canvas JavaScript API Starting with
<canvas id=”canvas” width=”200” height=”300”></canvas>
Canvas JavaScript API Getting the Context
Canvas JavaScript API Getting the Context
<script type=”text/javascript”> var elm = document.getElementById(“canvas”);
var context = elm.getContext(“2d”); </script>
Canvas JavaScript API Simple example
Canvas JavaScript API Simple example Creating a gradient background
Canvas JavaScript API Simple example Creating a gradient background
Draw a rectangle over it
Canvas JavaScript API Simple example Creating a gradient background
Draw a rectangle over it
Apply stroke to rectangule
Canvas JavaScript API Simple example Creating a gradient background
Draw a rectangule over it
Stroke the rectangule
Insert Text
Creating a gradient background var gradient =
context.createLinearGradient(0, 0, 200, 200);
Creating a gradient background var gradient =
context.createLinearGradient(0, 0, 200, 200);
gradient.addColorStop(0, &quot;#f00&quot;);
Creating a gradient background var gradient =
context.createLinearGradient(0, 0, 200, 200);
gradient.addColorStop(0, &quot;#f00&quot;);
gradient.addColorStop(1, &quot;#fff&quot;);
Creating a gradient background var gradient =
context.createLinearGradient(0, 0, 200, 200);
gradient.addColorStop(0, &quot;#f00&quot;);
gradient.addColorStop(1, &quot;#fff&quot;);
context.fillStyle = gradient;
Creating a gradient background var gradient =
context.createLinearGradient(0, 0, 200, 200);
gradient.addColorStop(0, &quot;#f00&quot;);
gradient.addColorStop(1, &quot;#fff&quot;);
context.fillStyle = gradient;
context.fillRect(0, 0, 200, 200);
Creating a Rectangle /* Setting fill  */
Creating a Rectangle /* Setting fill  */
context.fillStyle = &quot;#0f0&quot;;
Creating a Rectangle /* Setting fill  */
context.fillStyle = &quot;#0f0&quot;;
context.fillRect(20, 20, 100, 100);
Creating a Rectangle /* Setting fill  */
context.fillStyle = &quot;#0f0&quot;;
context.fillRect(20, 20, 100, 100);
/* Setting stroke */
Creating a Rectangle /* Setting fill  */
context.fillStyle = &quot;#0f0&quot;;
context.fillRect(20, 20, 100, 100);
/* Setting stroke */
context.strokeStyle = &quot;#00f&quot;;
Creating a Rectangle /* Setting fill  */
context.fillStyle = &quot;#0f0&quot;;
context.fillRect(20, 20, 100, 100);
/* Setting stroke */
context.strokeStyle = &quot;#00f&quot;;
context.strokeRect(20, 20, 100, 100);
Show Demo Source file http://dicode.org/2009/codebits.html
Canvas JavaScript API http://html5demos.com/video-canvas
by @rem

Working With Canvas