Cross-Browser HTML5 Canvas

Canvas is a relatively new HTML5 technology that provides a "scriptable" image. You can add a <canvas> element to your page and draw on its surface using JavaScript commands.
The HTML5 Canvas element is an HTML tag similar to the <div>, <a>, or <table> tag, with the exception that its contents are rendered with JavaScript.
A "canvas" tag can be placed on an HTML5 page with the following code:
  1. <canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
  2. Your browser does not support the HTML5 canvas tag.
  3. </canvas>
Assuming that the canvas tag is supported, JavaScript can be used to draw directly onto the canvas.
First, we will find the "<canvas>" element:
var c=document.getElementById("myCanvas");
Then, call its "getContext()" method (you must the string "2d" to the "getContext()" method):
var ctx=c.getContext("2d");
The getContext("2d") object is a built-in HTML5 object, with many properties and methods for drawing paths, boxes, circles, text, images, etc.
ctx.fillStyle="#FF0000"; ctx.fillRect(0,0,150,75);
The fillStyle property can be a CSS color, a gradient, or a pattern. The default fillStyle is #000000 (black).
The fillRect(x,y,width,height) method draws a rectangle filled with the current fill style.
Step 1:
Beginning with the code, our HTML5 head contains a small script that declares a canvas element.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Canvas Example</title>
  6. <script>
  7. document.createElement("canvas");
  8. </script>
Step 2:

We can now define CSS styles for our canvas tag.
  1. <style>
  2. #mycanvas {
  3. float: left;
  4. width: 300px;
  5. height: 300px;
  6. margin: 0 20px 0 0;
  7. background-image: url(rain.jpg);
  8. border: 1px solid #000;
  9. }
  10. #mycanvas.active {
  11. background-image: none;
  12. }
  13. </style>
Step 3:
We can now place a canvas tag on the page.
  1. </head>
  2. <body>
  3. <h1>HTML Canvas Example with Image Fall Back</h1>
  4. <canvas id="mycanvas" width="300" height="300"></canvas>
Step 4:
The first few lines check whether canvas is supported, and applies a class of "active" to the element to remove the static background:
  1. var canvas = document.getElementById("mycanvas");
  2. if (canvas.getContext) {
  3. // canvas supported
  4. canvas.className = "active";
Step 5:
The following will start the animation:
  1. // start animation
  2. var cxt = canvas.getContext("2d");
  3. cxt.fillStyle = "rgba(255,255,255,0.5)";
  4. setInterval(function() {
  5. var x = Math.round(Math.random()*canvas.width),
  6. y = Math.round(Math.random()*canvas.height),
  7. e = 20 + Math.round(Math.random()*30),
  8. s = 0;
  9. (function() {
  10. s++;
  11. if (s <= e) {
  12. setTimeout(arguments.callee,s);
  13. var c = 255-(e-s)*3;
  14. cxt.strokeStyle = "rgb("+c+","+c+","+c+")";
  15. cxt.beginPath();
  16. cxt.arc(x,y,s,0,Math.PI*2,true);
  17. cxt.fill();
  18. cxt.stroke();
  19. }
  20. })();
  21. },100);
  22. }
Example:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Canvas Example</title>
  6. <script>
  7. document.createElement("canvas");
  8. </script>
  9. <style>
  10. body {
  11. font-family: sans-serif;
  12. margin: 20px;
  13. color: #333;
  14. background-color: #fff;
  15. }
  16. #mycanvas {
  17. float: left;
  18. width: 300px;
  19. height: 300px;
  20. margin: 0 20px 0 0;
  21. background-image: url(rain.jpg);
  22. border: 1px solid #000;
  23. }
  24. #mycanvas.active {
  25. background-image: none;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <h1>HTML Canvas Example</h1>
  31. <canvas id="mycanvas" width="300" height="300"></canvas>
  32. <p>This page works in any browser.</p>
  33. <script>
  34. var canvas = document.getElementById("mycanvas");
  35. if (canvas.getContext) {
  36. // canvas supported
  37. canvas.className = "active";
  38. // start animation
  39. var cxt = canvas.getContext("2d");
  40. cxt.fillStyle = "rgba(255,255,255,0.5)";
  41. setInterval(function () {
  42. var x = Math.round(Math.random() * canvas.width),
  43. y = Math.round(Math.random() * canvas.height),
  44. e = 20 + Math.round(Math.random() * 30),
  45. s = 0;
  46. (function () {
  47. s++;
  48. if (s <= e) {
  49. setTimeout(arguments.callee, s);
  50. var c = 255 - (e - s) * 3;
  51. cxt.strokeStyle = "rgb(" + c + "," + c + "," + c + ")";
  52. cxt.beginPath();
  53. cxt.arc(x, y, s, 0, Math.PI * 2, true);
  54. cxt.fill();
  55. cxt.stroke();
  56. }
  57. })();
  58. }, 100);
  59. }
  60. </script>
  61. </body>
  62. </html>
Output
rain.jpg