1283549784 Firefoxpandaroux thumb Use The Canvas Element in HTML To create Graphics I often see articles, tutorials and info sites focusing HTML5 and from those articles I see a lot of benefits to HTML5. At the same time, I get clients and friends asking me about what’s HTML and do they do they need it on their sites. To be honest,  you do not really or at least not yet. Its still under development and not all browsers support HTML5. Once HTML5 is fully out of testing more browsers will support HTML5. When that happens, it may be time to upgrade your site. It will be the new standard that has features such as video play back, drag-and-drop, and a variety other integrated multimedia features. What this means is, that users that are on systems such as iPads and other non flash using hardware, will be able to view embedded videos or mp3s embedded in websites with out a plugin. Of course this is just my understanding of what HTML5 is and what is possible. I am still researching this topic and so far I love it. As I learn more, I will share my findings with you.

HTML5 Can Create Images

Over the last few weeks, I have stumbled across several short and sweet code snippets you can use on your site or just to play around with it. All the code can be copied into your own projects. The code was put together from various sites and modification by me.

NOTE: Not all browsers support HTML5/CSS3 including Windows Explore. But there are work a rounds for them.

Over the next few posts, I will share some examples of my favorite code snippets and I did to them or how its used on a project. Also, keep in mind some of the more graphic intensive code can slow your load time.

Canvas Element

canvas thumb3 Use The Canvas Element in HTML To create Graphics

Check out the live Demo.

One of the new elements of HTML5 is the canvas element which allows for dynamic scriptable rendering of 2D shapes and bitmap images. Canvas is a low level procedural model that will update a bitmap. The canvas element has a drawable  region that is defined in the HTML code using the height and width attributes. To access the drawable area, JavaScript is used.

The above image is an animated graphic created by using the canvas element and JavaScript. Below is the code I used to create the image. To modify it, you can change the #hex color codes and the dimensions.

Code Only

 <canvas id="demoCanvas" width="1200" height="400"></canvas>
<script type="text/javascript">
var canvas, graphics;
function draw_init() {
 canvas = document.getElementById('demoCanvas');
 if (canvas.getContext) {
 graphics = canvas.getContext('2d');
 }
 draw();
}
function draw() {
 var theme = ["#0042B2","#007DB2","#0006B2","#FFFFFF","#000000"];
 var x = Math.random() * 1200;
 var y = Math.random() * 400;
 var size = (Math.random() * 100) + 20;
 var num_circles = (Math.random() * 10) + 2;
 for (var i = size; i > 0; i-= (size/num_circles)) {
 graphics.fillStyle = theme[ (Math.random() * 5 >> 0)];
 graphics.beginPath();
 graphics.arc(x, y, i * .5, 0, Math.PI*2, true);
 graphics.closePath();
 graphics.fill();
 }
 t = setTimeout('draw()',1000);
}
draw_init();
</script>

Demo Site Code

<html>
 <head>
<title>HTML5/CSS3 Canvas Demo and Tutorial</title>
</head>

<body>
<H1 align="center">HTML5/CSS3 Canvas Demo and Tutorial</H2>

  <canvas id="demoCanvas" width="1200" height="400"></canvas>
<script type="text/javascript">
var canvas, graphics;
function draw_init() {
	canvas = document.getElementById('demoCanvas');
	if (canvas.getContext) {
		graphics = canvas.getContext('2d');
	}
	draw();
}
function draw() {
	var theme = ["#0042B2","#007DB2","#0006B2","#FFFFFF","#000000"];
	var x = Math.random() * 1200;
	var y = Math.random() * 400;
	var size = (Math.random() * 100) + 20;
	var num_circles = (Math.random() * 10) + 2;
	for (var i = size; i > 0; i-= (size/num_circles)) {
		graphics.fillStyle = theme[ (Math.random() * 5 >> 0)];
		graphics.beginPath();
		graphics.arc(x, y, i * .5, 0, Math.PI*2, true);
		graphics.closePath();
		graphics.fill();
	}
	t = setTimeout('draw()',1000);
}
draw_init();
</script>

</body>
</html>
 Use The Canvas Element in HTML To create Graphics
If you enjoyed this post, make sure you subscribe to my RSS feed!