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

You Might Be Interested In :

| Print article | This entry was posted by James on September 3, 2010 at 3:56 pm, and is filed under web design. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |













about 1 year ago
Nice! Though I’ve read a few things about the canvas element, I haven’t seen it in quite this way. Appreciate you sharing the code so we can more easily play around with it ourselves.
HTML5fan recently posted..LavenderDays HTML5 and CSS3 Template
about 1 year ago
No problem, thanks for stopping by and commenting. I didn’t know you could do this either up till recently. Took me some time to play with the code and JavaScript. I got couple other examples I am working on as well. Should have them up real soon…
James recently posted..19 Color Palette Generators to Help You Design Like A Pro
about 1 year ago
I had no idea you could do this and it looks pretty cool as well
I’m seriously going to have to have a play around with this..
Karen recently posted..Happy Birthday! Blazing Minds is Two Today
about 1 year ago
Karen,
It is fun. Playing with the colors alone is fun. You can get some pretty cool colors designs out of it.
James recently posted..21 Killer Examples of Afro Samurai Fan Art
about 1 year ago
hey James this looks fun to use!
If it doesnt slow down my blog, I might give this a try in my pages. Thanks!
Cindy recently posted..Enhance WordPress Posts with Post Layout Pro plugin
about 1 year ago
Cindy,
I can’t wait to see what you can do with it!
James recently posted..Manually Install Digg This to Your WordPress Theme
about 1 year ago
Looking good! Nice to see how HTML is making progress.
Anne Moss recently posted..And… I’m back!
about 1 year ago
I know it looks good. I have few more examples I will post next week.
James recently posted..On Vacation For the Week