Showing posts with label HTML 5. Show all posts
Showing posts with label HTML 5. Show all posts

Jul 3, 2012

HTML5 Air Hockey Game Using Canvas

Air Hockey, my first HTML5 game

In this tutorial you will be learning the importance of canvas tag.


The canvas element is part of HTML5 and allows for dynamic, scriptable rendering of 2D shapes and bitmap images. It is a low level, procedural model that updates a bitmap and does not have a built-in scene graph.


Working : A puck(ball) will be freely moving in the table, we just need to stop it from touching the horizontal sides of table by using two mallets.

Let's break our game into pieces, which will help us to code easily. We will be using following javascripts

  • jquery.hotkeys.js - Helps for keyboard events

  • key_status.js - Helps to call required event for our game

  • util.js - Helps to define the mallet position

  • game.js - Our main file which will contain the game functionality



First we will make the canvas in our HTML document.



[html]
<canvas id="canvas" width="480" height="320" tabindex="1"></canvas>
[/html]



Now javascript - game.js


[js]
var canvasElement = document.getElementById("canvas");
var canvas = canvasElement.getContext("2d");

var canvas_width = canvasElement.width;
var canvas_height =canvasElement.height;
var botX = 2;
var botY = 2;
var mx = 2;
var my = 4;
var points = 0;

var init = {};
var FPS = {};

FPS.set = 30;
[/js]



Above, first we created a canvas in our HTML document, and then in game.js we are initializing it, storing its height and width in a variable, declaring some more constant variables.

Let's make our players(mallets) and our ball(puck).



[js]
var player = {
color: "#000",
x: 220,
y: 310,
width: 50,
height: 10,
draw: function() {
canvas.fillStyle = this.color;
canvas.fillRect(this.x, this.y, this.width, this.height);
}
};

var player2 = {
color: "#000",
x: 220,
y: 0,
width: 50,
height: 10,
draw: function() {
canvas.fillStyle = this.color;
canvas.fillRect(this.x, this.y, this.width, this.height);
}
};

var ball = {
x : 2,
y : 2,
r : 5,
draw: function() {
canvas.beginPath();
canvas.arc(this.x, this.y, this.r, 0, Math.PI*2, true);
canvas.fill();
}

};
[/js]



We are defining player1 and player2 and setting its height, width, default position and finally giving it a shape.

HTML5 Air hockey

At this stage if you run the code then you will just find a blank canvas element with no players and no ball. Because we just created the players and ball, we have not set them to do something in canvas. So let's do that now.




[js]

init.interval = setInterval(function(){
update();
draw();
}, 1000/FPS.set);


function update() {
if (keydown.left) {
player.x -= 5;
player2.x -= 5;
}

if (keydown.right) {
player.x += 5;
player2.x += 5;
}

player.x = player.x.clamp(0, canvas_width - player.width);

player2.x = player.x.clamp(0, canvas_width - player.width);

}

function draw() {
canvas.clearRect(0, 0, canvas_width, canvas_height);
player.draw();
player2.draw();
ball.draw();

}
[/js]



Now if you run the file then you'll find that in our canvas a ball and two players will present. If you try to press left and right arrow players will move left/right.

HTML5 Air hockey

In our init() function we call two functions draw() and update() in interval. Update function is used to check any movements or changes in our canvas and it sets the value of players and ball. Draw function will take those values and draw it on canvas.

So if suppose you pressed left arrow once, the update function will take the X position of the player, it will update the new position in the player parameters.

Then the draw function will take the new value from player's parameters and then it will update the canvas, so it appears that the player is actually moving.

And also we are limiting our players to go outside the canvas. We use clamp function to check whether the player has touched the boundaries of the canvas.

Now let's move the ball.



[js]
function draw() {
canvas.clearRect(0, 0, canvas_width, canvas_height);
player.draw();
player2.draw();
ball.draw();

if (ball.x + mx > canvas_width || ball.x + mx < 0){
mx = -mx;
}
if (ball.y + my > canvas_height || ball.y + my < 0){
my = -my;
}

ball.x += mx;
ball.y += my;

}

[/js]



We updated our draw function. Here we use if-else conditions to check the position of the ball. If its touching the boundary then throw it back. mx and my are the variables which helps the ball to give X, Y position. And with its help we change the X, Y co-ordinates of the ball every time.

Now if you run the code, the ball will be moving.

HTML5 Air hockey

But it won't be reacting with the players. Now we will update our draw function one more time to check whether our ball is colliding with player1 or player2.



[js]

function collides(a, b) {
if(a.y == b.y || a.y <= b.height){
if(a.x >= b.x && a.x <= b.x+b.width){
return true;
}else{
return false;
}
}

}

function draw() {
canvas.clearRect(0, 0, canvas_width, canvas_height);
player.draw();
player2.draw();
ball.draw();

if (collides(ball, player)) {

my = -my;

}else if (collides(ball, player2)) {

my = +my;

}else
{
if (ball.x + mx > canvas_width || ball.x + mx < 0){
mx = -mx;
}
if (ball.y + my > canvas_height || ball.y + my < 0){
my = -my;
}


}

ball.x += mx;
ball.y += my;

}


[/js]



As you can see we have created one more function collides(), which takes two parameters. In our draw function we are checking whether ball and player1, ball and player2 are colliding with each other. In our collides() function we are checking position of ball and players. If it returns true, then we change the mx and my value.

Now try running the game again, when the ball hits the players it will return back.

HTML5 Air hockey

Last we will code the game over section. If the ball touched the horizontal boundary of canvas then we end our game.



[js]
function gameOver(){
canvas.fillStyle = '#000';
canvas.font = '18px verdana';
canvas.textBaseline = 'top';
canvas.fillText('Game Over, refresh the page to restart game', 50, 150);

clearInterval(init.interval);

$('#level').removeAttr('disabled');
}

if (ball.y + my > canvas_height || ball.y + my < 0){
my = -my;
gameOver();
}
[/js]



We call gameover() function in our draw function. With help of clearInterval() function we stop the game.


HTML5 Air hockey


So this was the basic game, it can't be completely called Air Hockey but after creating it I found that word to be the best. Still there are many bugs. I just started learning HTML5 and this was my first attempt on a game. You can download this from github, suggestions are always welcomed and fork me if you liked it.

May 26, 2012

HTML5 CSS3 Admin Panel | Dashboard Template

Now a days I go to dribble and see the amazing designs created by users. From their I get amazing ideas and today I ll share my HTML5 and CSS3 coded Admin panel with you all.

This tutorial will consist with :-

Here's the final output of our template:

HTML5-CSS3 Template
Fixed Menubar

Menu bar code is simple, li list with a drop down in it. We will be using box-shadow to give the gradient effect.

[html]
<ul id="admin-bar">
<li title="Home">H</li>
<li title="Users">A</li>
<li title="Add New Post">D</li>
<li title="Settings">G</li>
<li title="Videos">V</li>
<li title="Stats" class="active">v</li>
<li title="More">d
<ul id="drop-down">
<li title="Media Gallery">m<span>Media Gallery</span></li>
<li title="Comments">b<span>Comments</span></li>
<li title="Plugins">C<span>Plugins</span></li>
</ul>
</li>
</ul>
[/html]

Like my previous CSS3 tutorials here also I ll be using fonts instead of images to show the correct icons in menu.

[css]
#menu{
position:fixed;
top:0;
left:0;
width:100%;
background-color: #151515;
background-size: 6px 6px;
border-radius: 2px;
box-shadow: 0 25px 0 0 rgba(255, 255, 255, 0.15) inset;
height: 50px;
z-index: 1;
}

#admin-bar {
float: left;
margin: 0 auto;
padding: 0;
position: relative;
}

#admin-bar li {
color: #FFFFFF;
cursor: default;
display: inline;
float: left;
font-family: icons;
font-size: 25px;
list-style-type: none;
padding: 7px 10px 14px;
position: relative;
}

#admin-bar li:hover, .active{
text-shadow:0 0 5px #fff;
background:-moz-linear-gradient(center top , #1B1B1B 0%, #262626 100%) repeat scroll 0 0 transparent;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1b1b1b), color-stop(100%,#262626));
}

#admin-bar li ul {
-webkit-transition: all 1s ease 0s;
-moz-transition: all 1s ease 0s;
-o-transition: all 1s ease 0s;
-ms-transition: all 1s ease 0s;
transition: all 1s ease 0s;

background: -moz-linear-gradient(center top , #1B1B1B 0%, #262626 100%) repeat scroll 0 0 transparent;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1b1b1b), color-stop(100%,#262626)); /* Chrome,Safari4+ */
border-radius: 0 0 10px 10px;
display: block;
height: 0;
left: 0;
margin: 0;
opacity: 0;
overflow: hidden;
padding: 0;
position: absolute;
top: 50px;
width: 135px;
}

#admin-bar li:hover ul{
height:auto;
overflow:visible;
opacity:1;
}

#drop-down li {
display: block;
float: none;
background-image:url(divider.png);
background-repeat:no-repeat;
}

#drop-down li:hover{
background: -moz-linear-gradient(center top , #1B1B1B 0%, #262626 100%) repeat scroll 0 0 transparent;
}

#drop-down li:last-child {
border-radius: 0 0 10px 10px;
}
[/css]

To show the menu gradient I am using box-shadow: 0 25px 0 0 rgba(255, 255, 255, 0.15) inset; and for drop down menu the logic is really simple. By default overflow to be set as hidden, opacity as 0 and height to 0 and on mouse hover we change these values as visible, 1, auto.

The icons are not images but fonts. And using text-shadow they appear to glow on mouse hover.




Search Box

Its really simple if you have knowledge in CSS3 gradients. A textbox with black blackground color and CSS3 gradient effect to change the looks.

[html]
<input type="text" placeholder="Search" id="df">
[/html]

 

[css]
input[type="text"] {
background: -moz-linear-gradient(center top , #1B1B1B 0%, #262626 100%) repeat scroll 0 0 transparent;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1b1b1b), color-stop(100%,#262626)); /* Chrome,Safari4+ */
border: 1px solid #000000;
border-radius: 5px 5px 5px 5px;
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2), 0 2px 0 rgba(0, 0, 0, 0.5) inset;
color: #FFFFFF;
float: left;
font-size: 11px;
height: 26px;
padding-left: 10px;
padding-right: 65px;
text-shadow: 0 0 3px #FFFFFF;
width: 130px;
margin-left: 50px;
margin-top: 10px;
}
input[type="text"]:hover, input[type="text"]:active{
background:#1B1B1B;
}
[/css]




Log out Button
Using the search box idea here also we will make use of CSS3 gradients.

[html]
<input type="submit" id="logout" value="Logout" />
[/html]

 

[css]
#logout {
background: -moz-linear-gradient(center top , #CD2525 0%, #B11B1C 50%, #940F10 50%, #861213 100%) repeat scroll 0 0 #CD2525;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#CD2525), color-stop(50%,#B11B1C), color-stop(50%,#940F10), color-stop(100%,#861213)); /* Chrome,Safari4+ */

border-radius: 3px;
color: #FFFFFF;
float: right;
font-family: arial;
font-size: 14px;
margin: 12px;
padding: 5px 15px;
text-decoration: none;
border:0px;
}

#logout:active{
background:#861213;
}
[/css]




Animated Graphs
The logic to design the graph is really simple, make 6-7 div's with same height and width. The height should be longer and width to be thin so that it looks like a standing pole. Then with the help of simple CSS3 gradients and CSS3 animations we have the make it animated so that it looks like animated bar graph.

[html]
<div id="graph">
<div class="progress">
<div style="height: 40%;" class="bar"></div>
<p class="stats-info"><span class="tooltip"></span>Monday</p>
</div>
<div class="progress">
<div style="height: 70%;" class="bar"></div>
<p class="stats-info"><span class="tooltip"></span>Tuesday</p>
</div>
<div class="progress">
<div style="height: 72%;" class="bar"></div>
<p class="stats-info"><span class="tooltip"></span>Wednesday</p>
</div>
<div class="progress">
<div style="height: 80%;" class="bar"></div>
<p class="stats-info"><span class="tooltip"></span>Thursday</p>
</div>
<div class="progress">
<div style="height: 75%;" class="bar"></div>
<p class="stats-info"><span class="tooltip"></span>Friday</p>
</div>
<div class="progress">
<div style="height: 65%;" class="bar"></div>
<p class="stats-info"><span class="tooltip"></span>Saturday</p>
</div>
<div class="progress">
<div style="height: 45%;" class="bar"></div>
<p class="stats-info"><span class="tooltip"></span>Sunday</p>
</div>
</div>
[/html]

To show the tooltip we are using the same logic as drop down menu. Here the tooltip is the class stats-info. On hover we are changing the opacity to give the fade in effect with the help of css3 transition effect

To show the graph animation we will be using animation effect and the css3 gradient:

[css]
.bar {
animation: 2s linear 0s normal none infinite progress-bar-stripes;
background-image: -moz-linear-gradient(-134deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
}

@keyframes "progress-bar-stripes" {
from {
background-position: 0 0;
}
to {
background-position: 40px 0;
}

}

[/css]

Tooltip hover effect

[css]
.stats-info {
-webkit-transition: all 0.3s linear 0s;
-moz-transition: all 0.3s linear 0s;
-o-transition: all 0.3s linear 0s;
-ms-transition: all 0.3s linear 0s;
transition: all 0.3s linear 0s;

background-image: -moz-linear-gradient(center top, #CD2525 0%, #B11B1C 50%, #940F10 50%, #861213 100%);
background-image: -webkit-linear-gradient(-90deg, #CD2525 0%, #B11B1C 50%, #940F10 50%, #861213 100%);

background-repeat:repeat;
background-color:#CD2525;
border-radius: 5px 5px 5px 5px;
bottom: -45px;
color: #FFFFFF;
left: -18px;
opacity: 0;
padding: 5px;
position: absolute;
font-size: 12px;
}

.bar:hover + .stats-info{
opacity:1;
}

.tooltip {
border-bottom: 10px solid #C02020;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
display: block;
height: 0;
left: 17px;
position: absolute;
top: -9px;
width: 0;
z-index: 1;
}
[/css]




Notification messages
If you have gone through all the above parts then I am sure you can guess how the notification messages are coded.

By using CSS3 gradients and fonts we can make it.

[html]
<div id="messages">

<div class="error">Error</div>
<div class="notification">Notification</div>
<div class="success">Success</div>

</div>
[/html]

 

[css]
/* messages */
#messages{
float:left;
}

#messages div{
display:block;
margin:20px;
cursor: pointer;
}

/* Error */
.error {
background-image: -moz-linear-gradient(center top , #CB1634 0%, #C41330 50%, #BC102C 50%, #B70D2A 100%);
background-image: -webkit-linear-gradient(-90deg , #CB1634 0%, #C41330 50%, #BC102C 50%, #B70D2A 100%);
background-color:#CB1634;
background-repeat:repeat;
border-radius: 8px 8px 8px 8px;
color: #FFFFFF;
display: block;
font-size: 18px;
padding: 5px 30px;
position: relative;
width: 200px;
}

.error:before {
content: "X";
font-family: icons;
left: 4px;
position: absolute;
font-size: 18px;
}

/* Notification */

.notification {
background-image: -moz-linear-gradient(center top , #e4b023 0%, #e0ab1b 50%, #dca512 50%, #d79f08 100%);
background-image: -webkit-linear-gradient(-90deg , #e4b023 0%, #e0ab1b 50%, #dca512 50%, #d79f08 100%);
background-color:#e4b023;
background-repeat:repeat;
border-radius: 8px 8px 8px 8px;
color: #FFFFFF;
display: block;
font-size: 18px;
padding: 5px 30px;
position: relative;
width: 200px;
}

.notification:before {
content: "l";
font-family: icons;
font-size: 28px;
left: 0;
position: absolute;
top: -1px;
}

/* Success */

.success {
background-image: -moz-linear-gradient(center top , #6da920 0%, #62991b 50%, #558614 50%, #4d7a10 100%);
background-image: -webkit-linear-gradient(-90deg , #6da920 0%, #62991b 50%, #558614 50%, #4d7a10 100%);
background-color:#6da920;
background-repeat:repeat;
border-radius: 8px 8px 8px 8px;
color: #FFFFFF;
display: block;
font-size: 18px;
padding: 5px 30px;
position: relative;
width: 200px;
}

.success:before {
content: "O";
font-family: icons;
left: 4px;
position: absolute;
}

[/css]




Dialog box

[html]
<div id="dialogbox">

<div id="box-header">Are You Sure?<a id="close">X</a></div>
<div id="box-content">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>

<p style="position: relative; left: 20%; margin: 12px 0pt;">
<a class="yes">Yes</a>
<a class="no">No</a>
</p>
</div>

</div>
[/html]

We are using the div structure to build the dialog box. Again with the help of CSS3 gradients, fonts and other effects we will be making it to look like a Dialog Box.

[css]
/* Modal box */

#dialogbox{
float:left;
width:344px;
padding:5px;
background:rgba(255, 255, 255, 0.2);
border-radius:10px;
}

#box-header {
background-image: -moz-linear-gradient(center top , #D2DEE3 0%, #C7D6DB 50%, #B8CBD1 50%, #BBCDD3 100%);
background-image: -webkit-linear-gradient(-90deg , #D2DEE3 0%, #C7D6DB 50%, #B8CBD1 50%, #BBCDD3 100%);
background-color:#D2DEE3;
background-repeat:repeat;
border-radius: 5px 5px 0 0;
color: #324044;
font-size: 17px;
padding: 15px;
position: relative;
display: block;
cursor: default;
}

#close {
color: #314248;
cursor: pointer;
font-family: icons;
font-size: 26px;
position: absolute;
right: 5px;
top: 10px;
}

/* */
#box-content{
text-align:center;
background:#fff;
color:#7c7c7c;
font-size:13px;
float:left;
padding: 10px 5px;
}

.yes{
background-image: -moz-linear-gradient(center top , #c1e087 0%, #c3e092 50%, #bbdd83 50%, #b7db7a 100%);
background-image: -webkit-linear-gradient(-90deg , #c1e087 0%, #c3e092 50%, #bbdd83 50%, #b7db7a 100%);
background-color:#c1e087;
background-repeat:repeat;
font-size:12px;
padding:10px 35px;
border:1px solid #81aa3c;
border-radius:5px;
margin:2px 5px;
float:left;
cursor:pointer;
}

.yes:hover{
box-shadow: 0 0 5px #a7bc84;
}

.no{
background-image: -moz-linear-gradient(center top , #f6b2b2 0%, #f7bfbf 50%, #f7c1c1 50%, #f5b1b1 100%);
background-image: -webkit-linear-gradient(-90deg , #f6b2b2 0%, #f7bfbf 50%, #f7c1c1 50%, #f5b1b1 100%);
background-repeat:repeat;
background-color:#f6b2b2;
font-size:12px;
padding:10px 35px;
border:1px solid #d38e8e;
color:#582121;
cursor:pointer;
border-radius:5px;
float:left;
margin:2px 5px;
}

.no:hover{
box-shadow: 0 0 5px #B98B8B;
}
[/css]

I hope that with the help of this tutorial you learned a lot, you can use this for free in any of your creative work(don't forget to give me a back-link :P). Tweet, FB Shares and other suggestions are always welcomed at WT.

May 15, 2012

CSS3 & HTML 5 Login Form With Validation

Today's tutorial is based on simple and attractive CSS3 HTML5 login and registering form with fields validation. In html5 we can actually do validation without using any external JQuery or other scripts.

In this tutorial we would:

  • Design a toggle functionality using checkbox.

  • Make two forms with HTML5 validation inputs

  • And CSS3 toppings


css3-login-form
So the logic for toggle is simple, if checked then show the form else hide the form. With CSS3 we can give transition effects.

So here is the HTML markup for Toggle:

[html]
<input type="checkbox" id="login_toggle" checked=""/>
<label id="toggle" for="login_toggle">Click here to login</label>
[/html]

CSS3 Markup for its effect and styling:

[css]
#login_toggle:checked + #login-form {
top: -210px;
}

#toggle {
background-image: url("bg.png");
border: 3px solid #FFFFFF;
float: right;
padding: 5px 50px;
position: relative;
right: 80px;
top: 210px;
color: #fff;
border-top: 0px;
text-shadow: 0 0 1px #000000;

border-radius: 0 0 4px 4px;

-webkit-box-shadow: 1px 4px 4px #000000;
-moz-box-shadow: 1px 4px 4px #000000;
box-shadow: 1px 4px 4px #000000;
cursor: pointer;
}
[/css]

In html5 we can set a new attribute to input type named as required. When this attribute is inserted then the form won't process until its validated.

HTML5 form markup

[html]
<form id="loginForm" class="right">
<div id="input">
<input class="input username" type="text" placeholder="Username" name="username" required />
<span>A</span>
</div>

<div id="input">
<input class="input password" type="password" placeholder="Password" name="password" required />
<span>L</span>
</div>

<div id="input">
<input type="submit" class="submit" value="Login">
</div>
</form>
[/html]

Simple isn't it? Just try the demo and have fun with CSS3, there's so much creativity you can do with it.

Apr 5, 2012

Range Slider | Range Input Using HTML5 & CSS3 | NO JAVASCRIPT

A regular website coder will face a situation where they will have to use a range slider to fulfill some requirements. I regularly used JQuery UI range slider, but thanks to HTML5 that they made us our life easy. HTML5 has a new input tag, its type is defined as "range". Using this, a range slider will be formed, but for now this is only supported by -webkit- supported browsers. You can read more on new HTML5 input tags over here.

Range Slider

In todays tutorial we will explore range slider.

Basic syntax:

[html]
<input type="range" id="rangeinput" value="50"/>
[/html]

You can edit the CSS and change the slider icon.

[css]
input[type="range"] {
-webkit-appearance: none;
background-color: black;
height: 2px;
}

input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
position: relative;
top: -1px;
z-index: 1;
width: 11px;
height: 11px;

-webkit-border-radius: 40px;
-moz-border-radius: 40px;
border-radius: 40px;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ebf1f6), color-stop(50%,#abd3ee), color-stop(51%,#89c3eb), color-stop(100%,#d5ebfb));
}
[/css]

You can check the demo and download the files. Demo will be only supported in WEBKIT browsers

Feb 16, 2012

HTML5 & CSS3 Toggle slideup and slidedown | NO JAVASCRIPT

While reading more about HTML5 & CSS3 I just found an amazing css3 button styling. This time I wanted to do something which I was doing from past 2 years using JQuery. In this tutorial I am going to teach you how to make a toggle slideDown and slideUp function using CSS3 and HTML5.

html5-css3

To achieve this we will have to know the pseudo element :Target.

Target element takes the href value and finds the element with its ID(value).

In this tutorial I am going to take the CSS3 styling from Tympanus and add my CSS3 function of toggle.

So here is the HTML structure

[html]
<div class="container">
<section>
<div id="container_buttons">
<h1>
HTML5 & CSS3 Toggle slideup and slidedown using NO JAVASCRIPT.
</h1>

<div style="clear:both"></div>
<ul>
<li class="toggle">
<a href="#One">A clock made in CSS3 | NO Javascript</a>
<p id="One">
Lets start learning CSS3 before its too late, I just saw a website which amazed me with its animation effects. First I thought that the site is using some JQuery libraries but when I actually went into the code, I found it is CSS3. So I started learning it, and today I made my first <a href="http://webstutorial.com/css3-clock/css3">Read more...</a>
</p>
</li>
<li class="toggle">
<a href="#Two">Learn CakePHP From Novice to Professional : Part 2</a>
<p id="Two">
Model-View-Controller Cake enforces an MVC structure for your web applications. Basically, it effectively separates typical operations into specific areas: MODELS : for all your database interaction VIEWS : for all your output and displays CONTROLLERS : for all your commands/scripts for input and program flow The typical PHP application mixes each of these three functions <a href="http://webstutorial.com/learn-cakephp-novice-professional-part-2/framework">Read more...</a>
</p>
</li>
<li class="toggle">
<a href="#Three">Learn CakePHP From Novice to Professional : Part 1</a>
<p id="Three">
This guide is for beginners to CakePHP. Whether or not you have much experience with the PHP scripting language, working in Cake will require some new methods you may or may not have tried before. If you don't know what a "has-and-belongs-to-many" relationship is, don't know how to build your own class object, or don't <a href="http://webstutorial.com/learn-cakephp-novice-professional-part-1/framework">Read more...</a>
</p>
</li>

<li class="toggle">
<a href="#Four">Google Maps Server Side Geocoding Using PHP and Infobox Fully AJAX</a>
<p id="Four">
Recently I got a new project of real estate where I had to display about 1000 of projects on the google map. I was having the address of 1000 projects, so I started with my php and js code. When I coded and started testing, I found that on the map only 20-30 markers were <a href="http://webstutorial.com/google-server-side-geocoding-php-infobox/website-tweaks/google">Read more...</a>
</p>
</li>

<li class="toggle">
<a href="#Five">Learn Codeigniter | Codeigniter Tutorial | Codeigniter Lessons | Part 1 | Webs Tutorial</a>
<p id="Five">
What is CodeIgniter? CodeIgniter is an open source PHP framework. This framework is used to build web applications and websites. CodeIgniter is best for developers who are into front-end development. It's easy to learn and it's fully flexibe. To learn CI (CodeIgniter), you should know PHP, MYSQL, OOPs and MVC. If you are new to <a href="http://webstutorial.com/codeigniter-tutorials-codeigniter-lessons-part-1/programming/php">Read more...</a>
</p>
</li>
</ul>

<em style="float: right;margin:10px 0;">Credits: <a href="webstutorial.com">WebsTutorial</a> & <a href="http://tympanus.net/codrops/2012/01/11/css-buttons-with-pseudo-elements/" rel="nofollow" target="_blank">Tympanus</a></em>

</div>
</section>
</div>
[/html]

Here we are making a simple unordered list structure, each list consits an anchor tag and a paragraph tag. Anchor will have a href pointing to ID of a paragraph tag.

And the final CSS to toggle the paragraph tag is

[css]
li p {
display: none;
padding: 10px 20px;
}

li p:target{
display:block;
}
[/css]

Aug 11, 2011

How To Make A GAME In HTML 5 | How To Build A Game With HTML 5

download



Final Output:


Running Ball

HTML 5 is growing at the lightning speed, none of the languages in web development has grew up in this speed. Today we are going to make a simple game in HTML 5 by using Box2D and HTML 5 Canvas.


But before that, the question is



What is BOX2D?
Box2D is an open source and popular engine that simulates 2D physics for making games and applications. Primarily written in C++, it has been converted to numerous languages by community contributors.

This tutorial is divided into 5 simple steps, so lets get started



STEP - 1 Folder Structure


First of all we need to download the box2d engine for our html 5, you can download it from here. Next we create the file and folder structure. Just follow the image of file folder structure and copy paste them in respective places.
Folder Structure


After the setup of files, we will add necessary files to the html file that is index.html



[html]
<!--[if IE]><script src="lib/excanvas.js"></script><![endif]--><script type="text/javascript" src="lib/prototype-1.6.0.2.js"></script>
<!-- box2djs -->
<script type="text/javascript" src="js/box2d/common/b2Settings.js"></script><script type="text/javascript" src="js/box2d/common/math/b2Vec2.js"></script>
<script type="text/javascript" src="js/box2d/common/math/b2Mat22.js"></script><script type="text/javascript" src="js/box2d/common/math/b2Math.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2AABB.js"></script><script type="text/javascript" src="js/box2d/collision/b2Bound.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2BoundValues.js"></script><script type="text/javascript" src="js/box2d/collision/b2Pair.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2PairCallback.js"></script><script type="text/javascript" src="js/box2d/collision/b2BufferedPair.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2PairManager.js"></script><script type="text/javascript" src="js/box2d/collision/b2BroadPhase.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2Collision.js"></script><script type="text/javascript" src="js/box2d/collision/Features.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2ContactID.js"></script><script type="text/javascript" src="js/box2d/collision/b2ContactPoint.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2Distance.js"></script><script type="text/javascript" src="js/box2d/collision/b2Manifold.js"></script>
<script type="text/javascript" src="js/box2d/collision/b2OBB.js"></script><script type="text/javascript" src="js/box2d/collision/b2Proxy.js"></script>
<script type="text/javascript" src="js/box2d/collision/ClipVertex.js"></script><script type="text/javascript" src="js/box2d/collision/shapes/b2Shape.js"></script>
<script type="text/javascript" src="js/box2d/collision/shapes/b2ShapeDef.js"></script><script type="text/javascript" src="js/box2d/collision/shapes/b2BoxDef.js"></script>
<script type="text/javascript" src="js/box2d/collision/shapes/b2CircleDef.js"></script><script type="text/javascript" src="js/box2d/collision/shapes/b2CircleShape.js"></script>
<script type="text/javascript" src="js/box2d/collision/shapes/b2MassData.js"></script><script type="text/javascript" src="js/box2d/collision/shapes/b2PolyDef.js"></script>
<script type="text/javascript" src="js/box2d/collision/shapes/b2PolyShape.js"></script><script type="text/javascript" src="js/box2d/dynamics/b2Body.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/b2BodyDef.js"></script><script type="text/javascript" src="js/box2d/dynamics/b2CollisionFilter.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/b2Island.js"></script><script type="text/javascript" src="js/box2d/dynamics/b2TimeStep.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/contacts/b2ContactNode.js"></script><script type="text/javascript" src="js/box2d/dynamics/contacts/b2Contact.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/contacts/b2ContactConstraint.js"></script><script type="text/javascript" src="js/box2d/dynamics/contacts/b2ContactConstraintPoint.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/contacts/b2ContactRegister.js"></script><script type="text/javascript" src="js/box2d/dynamics/contacts/b2ContactSolver.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/contacts/b2CircleContact.js"></script><script type="text/javascript" src="js/box2d/dynamics/contacts/b2Conservative.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/contacts/b2NullContact.js"></script><script type="text/javascript" src="js/box2d/dynamics/contacts/b2PolyAndCircleContact.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/contacts/b2PolyContact.js"></script><script type="text/javascript" src="js/box2d/dynamics/b2ContactManager.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/b2World.js"></script><script type="text/javascript" src="js/box2d/dynamics/b2WorldListener.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2JointNode.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2Joint.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2JointDef.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2DistanceJoint.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2DistanceJointDef.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2Jacobian.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2GearJoint.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2GearJointDef.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2MouseJoint.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2MouseJointDef.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2PrismaticJoint.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2PrismaticJointDef.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2PulleyJoint.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2PulleyJointDef.js"></script>
<script type="text/javascript" src="js/box2d/dynamics/joints/b2RevoluteJoint.js"></script><script type="text/javascript" src="js/box2d/dynamics/joints/b2RevoluteJointDef.js"></script>
[/html]

Next, we will create two more scripts file inside the /js/ folder directory, named as  "box2dutils.js" and "game.js" respectively




  • box2dutils.js – This files is a copy and paste from some other online demos that come with box2dlib, and it is very important for drawing functions.

  • game.js – By the name it will be easy to recognize, it the actual game, where platform, keyboard input etc functions are set.


Copy paste the following code in "box2dutils.js" file



[html]
function drawWorld(world, context) {
for (var j = world.m_jointList; j; j = j.m_next) {
drawJoint(j, context);
}
for (var b = world.m_bodyList; b; b = b.m_next) {
for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {
drawShape(s, context);
}
}
}
function drawJoint(joint, context) {
var b1 = joint.m_body1;
var b2 = joint.m_body2;
var x1 = b1.m_position;
var x2 = b2.m_position;
var p1 = joint.GetAnchor1();
var p2 = joint.GetAnchor2();
context.strokeStyle = '#00eeee';
context.beginPath();
switch (joint.m_type) {
case b2Joint.e_distanceJoint:
context.moveTo(p1.x, p1.y);
context.lineTo(p2.x, p2.y);
break;

case b2Joint.e_pulleyJoint:
// TODO
break;

default:
if (b1 == world.m_groundBody) {
context.moveTo(p1.x, p1.y);
context.lineTo(x2.x, x2.y);
}
else if (b2 == world.m_groundBody) {
context.moveTo(p1.x, p1.y);
context.lineTo(x1.x, x1.y);
}
else {
context.moveTo(x1.x, x1.y);
context.lineTo(p1.x, p1.y);
context.lineTo(x2.x, x2.y);
context.lineTo(p2.x, p2.y);
}
break;
}
context.stroke();
}
function drawShape(shape, context) {
context.strokeStyle = '#000000';
context.beginPath();
switch (shape.m_type) {
case b2Shape.e_circleShape:
{
var circle = shape;
var pos = circle.m_position;
var r = circle.m_radius;
var segments = 16.0;
var theta = 0.0;
var dtheta = 2.0 * Math.PI / segments;
// draw circle
context.moveTo(pos.x + r, pos.y);
for (var i = 0; i < segments; i++) {
var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
var v = b2Math.AddVV(pos, d);
context.lineTo(v.x, v.y);
theta += dtheta;
}
context.lineTo(pos.x + r, pos.y);

// draw radius
context.moveTo(pos.x, pos.y);
var ax = circle.m_R.col1;
var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
context.lineTo(pos2.x, pos2.y);
}
break;
case b2Shape.e_polyShape:
{
var poly = shape;
var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));
context.moveTo(tV.x, tV.y);
for (var i = 0; i < poly.m_vertexCount; i++) {
var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
context.lineTo(v.x, v.y);
}
context.lineTo(tV.x, tV.y);
}
break;
}
context.stroke();
}

function createWorld() {
var worldAABB = new b2AABB();
worldAABB.minVertex.Set(-1000, -1000);
worldAABB.maxVertex.Set(1000, 1000);
var gravity = new b2Vec2(0, 300);
var doSleep = true;
var world = new b2World(worldAABB, gravity, doSleep);
return world;
}

function createGround(world) {
var groundSd = new b2BoxDef();
groundSd.extents.Set(1000, 50);
groundSd.restitution = 0.2;
var groundBd = new b2BodyDef();
groundBd.AddShape(groundSd);
groundBd.position.Set(-500, 340);
return world.CreateBody(groundBd)
}

function createBall(world, x, y) {
var ballSd = new b2CircleDef();
ballSd.density = 1.0;
ballSd.radius = 20;
ballSd.restitution = 1.0;
ballSd.friction = 0;
var ballBd = new b2BodyDef();
ballBd.AddShape(ballSd);
ballBd.position.Set(x,y);
return world.CreateBody(ballBd);
}

function createBox(world, x, y, width, height, fixed, userData) {
if (typeof(fixed) == 'undefined') fixed = true;
var boxSd = new b2BoxDef();
if (!fixed) boxSd.density = 1.0;

boxSd.userData = userData;

boxSd.extents.Set(width, height);
var boxBd = new b2BodyDef();
boxBd.AddShape(boxSd);
boxBd.position.Set(x,y);
return world.CreateBody(boxBd)
}
[/html]

Don't worry I ll explain the code further.


Now we move on to our next step



Step 2 - Game Development


Open the index.html file, which we created in the first step, after adding the head part, insert the canvas code between the body tag



[html]<canvas id="game" width="600" height="400"></canvas>[/html]

Next we have to add the reference of the two javascripts file which we created just now. So in index.html, within the head tag, add the following code in it.



[html]
<script type="text/javascript" src="js/box2dutils.js"></script><script type="text/javascript" src="js/game.js"></script>
[/html]

Now lets start developing our game, so open game.js file and add the following code in it.



[html]
// some variables that we gonna use in this demo
var initId = 0;
var player = function(){
this.object = null;
this.canJump = false;
};
var world;
var ctx;
var canvasWidth;
var canvasHeight;
var keys = [];

// HTML5 onLoad event
Event.observe(window, 'load', function() {
world = createWorld(); // box2DWorld
ctx = $('game').getContext('2d'); // 2
var canvasElm = $('game');
canvasWidth = parseInt(canvasElm.width);
canvasHeight = parseInt(canvasElm.height);
initGame(); // 3
step(); // 4

// 5
window.addEventListener('keydown',handleKeyDown,true);
window.addEventListener('keyup',handleKeyUp,true);
});
[/html]

OK, now its time for explanation of code now


BOX2DWorld is the available class from the core of box2d.Its major and simple function is to combine everything into one class. In this box2DWorld, we have the bodies definition and collisions manager of our game.


Now open the game.js and box2dutils.js and search for the function createWorld() in box2dutils.js file.



[html]
function createWorld() {
// here we create our world settings for collisions
var worldAABB = new b2AABB();
worldAABB.minVertex.Set(-1000, -1000);
worldAABB.maxVertex.Set(1000, 1000);
// set gravity vector
var gravity = new b2Vec2(0, 300);
var doSleep = true;
// init our world and return its value
var world = new b2World(worldAABB, gravity, doSleep);
return world;
}
[/html]

Its really simple to make such box2DWorld


Now again back to game.js file.


Now copy paste the following code in game.js file



[html]
function initGame(){
// create 2 big platforms
createBox(world, 3, 230, 60, 180, true, 'ground');
createBox(world, 560, 360, 50, 50, true, 'ground');

// create small platforms
for (var i = 0; i < 5; i++){
createBox(world, 150+(80*i), 360, 5, 40+(i*15), true, 'ground');
}

// create player ball
var ballSd = new b2CircleDef();
ballSd.density = 0.1;
ballSd.radius = 12;
ballSd.restitution = 0.5;
ballSd.friction = 1;
ballSd.userData = 'player';
var ballBd = new b2BodyDef();
ballBd.linearDamping = .03;
ballBd.allowSleep = false;
ballBd.AddShape(ballSd);
ballBd.position.Set(20,0);
player.object = world.CreateBody(ballBd);

}

Inside <code>box2dutils.js</code>, we've created a function, called <code>createBox</code>. This creates a static rectangle body.

function createBox(world, x, y, width, height, fixed, userData) {
if (typeof(fixed) == 'undefined') fixed = true;
//1
var boxSd = new b2BoxDef();
if (!fixed) boxSd.density = 1.0;
//2
boxSd.userData = userData;
//3
boxSd.extents.Set(width, height);

//4
var boxBd = new b2BodyDef();
boxBd.AddShape(boxSd);
//5
boxBd.position.Set(x,y);
//6
return world.CreateBody(boxBd)
}
[/html]

A Box2DBody has some unique characteristics:




  • It can be static (not affected by collisions impacts), kinematic (it isn't affected by collisions, but it can be moved by your mouse, for example), or dynamic (interacts with everything)

  • Must have a shape definition, and should indicate how the object appears

  • May have more than one fixture, which indicates how the object will interact with collisions

  • Its position is set by the center of your object, not the left top edge as many other engines do.


Reviewing the above code:



  1. Here we are creating a shape, which can be a square or rectangle or any other geometry shape. And also we setup the density(the actual movement of the object)

  2. We setup the userData, usually we setup graphics objects here, but for this game, we just setup strings that will be the identifier of the type of the object for collisions.

  3. Setup half of the size of our box.

  4. We create the body definition, and add to it the box shape definition.

  5. Setup the position.

  6. Create the body in the world and return its value.


Creating The Player Object


Paste the following code in game.js file



[html]
var ballSd = new b2CircleDef();
ballSd.density = 0.1;
ballSd.radius = 12;
ballSd.restitution = 0.5;
ballSd.friction = 1;
ballSd.userData = 'player';
var ballBd = new b2BodyDef();
ballBd.linearDamping = .03;
ballBd.allowSleep = false;
ballBd.AddShape(ballSd);
ballBd.position.Set(20,0);
player.object = world.CreateBody(ballBd);
[/html]

So now we have created body.
We have used simple steps




  1. Created Shape, Fixture and Sensor Definition.

  2. Created Body Definition

  3. Added body into the shape

  4. Created body in the world.


Simple isn't it?


So Now if we run our code, it will give us a blank white screen.


Don't panic, till now we have just setup Box2, and always remember



Box2D doesn't render; it only calculates physics.

Step 3 - Rendering Time


Now we will render the box2dworld.


Open game.js file and add the following code in it:



[html]
function step() {

var stepping = false;
var timeStep = 1.0/60;
var iteration = 1;
// 1
world.Step(timeStep, iteration);
// 2
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
drawWorld(world, ctx);
// 3
setTimeout('step()', 10);
}
[/html]

In the above code we have intructed box2dworld to achieve the following things




  1. To perform physics calculations.

  2. To clear the canvas screen and draw again on it.

  3. And to run the step() function in every 10 milliseconds.


Now if you run the code, you will see the canvas with a running ball.


Running Ball


drawWorld in box2dutils.js


Open box2dutils.js file and add the following code in it.



[html]
function drawWorld(world, context) {
for (var j = world.m_jointList; j; j = j.m_next) {
drawJoint(j, context);
}
for (var b = world.m_bodyList; b; b = b.m_next) {
for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {
drawShape(s, context);
}
}
}
[/html]

The above written function is a debug function which will draw our world into the canvas, by using the graphics API provided by HTML5's Canvas API.


The very first loop will draw all joints.


The second loop will draw all bodies.



[html]
function drawShape(shape, context) {
context.strokeStyle = '#000000';
context.beginPath();
switch (shape.m_type) {
case b2Shape.e_circleShape:
{
var circle = shape;
var pos = circle.m_position;
var r = circle.m_radius;
var segments = 16.0;
var theta = 0.0;
var dtheta = 2.0 * Math.PI / segments;
// draw circle
context.moveTo(pos.x + r, pos.y);
for (var i = 0; i < segments; i++) {
var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
var v = b2Math.AddVV(pos, d);
context.lineTo(v.x, v.y);
theta += dtheta;
}
context.lineTo(pos.x + r, pos.y);

// draw radius
context.moveTo(pos.x, pos.y);
var ax = circle.m_R.col1;
var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
context.lineTo(pos2.x, pos2.y);
}
break;
case b2Shape.e_polyShape:
{
var poly = shape;
var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));
context.moveTo(tV.x, tV.y);
for (var i = 0; i < poly.m_vertexCount; i++) {
var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
context.lineTo(v.x, v.y);
}
context.lineTo(tV.x, tV.y);
}
break;
}
context.stroke();
}
[/html]

Here we're looping through every vertices of the object and drawing it with lines.



Step 4 - Interactivity


In this step we will instruct the game by our keyboard events.
Add the following code to game.js file



[html]
function handleKeyDown(evt){
keys[evt.keyCode] = true;
}

function handleKeyUp(evt){
keys[evt.keyCode] = false;
}

// disable vertical scrolling from arrows</pre>
<img src="http://net.tutsplus.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" />
<pre>
document.onkeydown=function(){return event.keyCode!=38 && event.keyCode!=40}
[/html]

With the help of an array we are handling the UpKey and the DownKey event. With document.onkeydown, we disable the browser's native vertical scrolling function for up and down arrows.
If you have played a html5 game and jumped during the game, the game goes off screen, but in our game, it won't



[html]handleInteractions(); [/html]

And outside, declare the function:



[html]
function handleInteractions(){
// up arrow
// 1
var collision = world.m_contactList;
player.canJump = false;
if (collision != null){
if (collision.GetShape1().GetUserData() == 'player' || collision.GetShape2().GetUserData() == 'player'){
if ((collision.GetShape1().GetUserData() == 'ground' || collision.GetShape2().GetUserData() == 'ground')){
var playerObj = (collision.GetShape1().GetUserData() == 'player' ? collision.GetShape1().GetPosition() :  collision.GetShape2().GetPosition());
var groundObj = (collision.GetShape1().GetUserData() == 'ground' ? collision.GetShape1().GetPosition() :  collision.GetShape2().GetPosition());
if (playerObj.y < groundObj.y){
player.canJump = true;
}
}
}
}
// 2
var vel = player.object.GetLinearVelocity();
// 3
if (keys[38] && player.canJump){
vel.y = -150;
}

// 4
// left/right arrows
if (keys[37]){
vel.x = -60;
}
else if (keys[39]){
vel.x = 60;
}

// 5
player.object.SetLinearVelocity(vel);
}
[/html]

The most complicated code in the above function is the first one, where we are checking the collision and writing some if else condition to determine the shape one or shape two.


On the second commented line (2), we retrieve the LinearVelocity of the player.


The third and forth commented sections verify if arrows are being pressed, and adjust the velocity vector(speed), accordingly.


In the fifth section, we setup the player with the new velocity vector.


Till now interactions are done, but if you run it now, it will only keep on jumping.



Step 5 - "You Win" Message


Add the code below to the beginning of your LinearVelocity function:



[html]
if (player.object.GetCenterPosition().y > canvasHeight){
player.object.SetCenterPosition(new b2Vec2(20,0),0)
}
else if (player.object.GetCenterPosition().x > canvasWidth-50){
showWin();
return;
}
[/html]

In this the first condition determines that if the player falls then it should be placed agin to the starting point.


The second condition is used to state if the player is in the final platform then display the winning function, which is below



[html]
function showWin(){
ctx.fillStyle = '#000';
ctx.font = '30px verdana';
ctx.textBaseline = 'top';
ctx.fillText('Yes! you made it!', 30, 0);
ctx.fillText('Thank You, www.webstutorial.com', 30, 30);
ctx.fillText('HTML 5 Rocks', 30, 60);
}
[/html]

And that's it, you have just made your own simple adventure game in HTML5.


Credit:NetTuts

download

Jul 10, 2011

Wordpress Make HTML5 theme

HTML 5 is the latest and the hottest topic in the web development.
Now a days everybody wants there blogs theme to be in HTML5, even the latest WP-3.2 default theme twenty eleven is also in HTML-5.

So why to wait, convert our current blog old html theme to brand new HTML-5

In this tutorial, you are going to learn the conversion of old HTML to HTML-5

1. Basic Document’s structure

[php]
<!doctype html>
<head>
</head>
<body>
<header>
</header>
<section id="content">
<article>
<header>
</header>
</article>
</section>
<aside>
<nav>
</nav>
</aside>
<footer>
</footer>
</body>
</html>
[/php]

As we can see, that the tags are very new to us. If we add following CSS:
article, aside, figure, footer, header, hgroup, menu, nav, section { display:block; }
we are free to format them like other block elements, e.g.


  • Tag Header, as the name itself indicates that all the head contents like, logo, navigation, search etc will come into it. The usage of this tag inside
    will be presented below.



  • tag used to keep the order in document. We can divide the website into sections, e.g. content, comments, gallery etc.



  • can be used to store our entries – each entry in separate tag! The title and meta of entry should be inside
    – it will have positive impact on our SEO.



  • Our sidebar should be inside






  • If we have footer it is a good practice to put it inside
    . We can have more than one
    , but each should be inside separate
    and one can be directly inside


Tags  like article, header, footer, section can be used more than one time, but the structure has to be maintained.

[php]
<section id="content">
<article>
<header>
</header>
<p>
</p>
<footer>
</footer>
</article>
</section>
[/php]

In header, we can put h3 tag, which is commonly used in wordpress themes content.
If you want to use more then one time hx tags, then it should be wrapped inside hgroup tag.

[php]
<hgroup>
<h3>Main Title</h3>
<h4>Subtitle</h4>
</hgroup>
[/php]

The meta of the post can be wrapped by
, what is a convenient solution.


When it comes to the comments we can treat them like posts and put inside
with >header>,


,
etc. inside.


When we want to refresh our comment form we should definitely use HTML5. Some new input features have been added. Let’s take a look on this:

[php]
<input type="text" name="author" id="comment-form-author" placeholder="Your name" value="" size="22" tabindex="1" required="required">

<input type="email" name="email" id="comment-form-email" placeholder="Your email" pattern="[^ @]*@[^ @]*" value="" size="22" tabindex="2" required="required">

<input type="text" name="url" id="comment-form-url" placeholder="Your website" size="22" tabindex="3" />
[/php]

As a value of placeholder we can specify a hint for user, it will appear as a text inside the input and after clicking on it will disappear.
pattern attribute allows us to specify a patter than must be followed. Here we have an example of pattern for email address.

HTML5 is powerful tool, but it’s not completely supported by all browsers.
More cool features we can find on http://html5demos.com/