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

Thursday, March 1, 2012

Basic of Canvas Element in HTML5



Solid Understanding of the Basics of Canvas Element in HTML5




HTML5 Canvas

Canvas element is useful for draw graphics on a web pages. With the help of canvas element we can draw rectangular area, and also control each and every pixels. It have various methods to draw any graphics.
Whenever we are going to draw any graphics first of all we should create Canvas element like:
< canvas id="CanvasID" width="100" height="100">< /canvas>
Canvas element have no drawing capability. So we must be done it in javaScript method like:



< script type="text/javascript">
var canId=document.getElementById("CanvasID");
var cantext= canId.getContext("2d");
cantext.fillStyle="#FF0000";
cantext.fillRect(0,0,180,95);
< /script>
Basically JavaScript uses id to find the canvas element:

Solid Understanding of the Basics of HTML5 Part 1



Solid Understanding of the Basics of HTML5 | Let us play with HTML5




HTML5 Introduction

HTML5 is advanced of HTML. It is the new standared for HTML. Now days most modern browsers have support. Example, Crome, Mozila 10.0.2, IE 9.
There are following Rules for HTML 5, which is given bellow:-
1) There are some new features based on HTML, CSS, DOM, JavaScript
2) Best way to Error handling
3) HTML5, device independent
4) Reduce external Plugins

What new features is in HTML 5?

There are some following new features in HTML5:
1) Canvas element for drawing charts according to user preference.
2) The most powerful feature audio and video elements for media.
3) Best support for local offline storage.
4) Add new tag elements.
5) Add new form controls.

List of new tag elements

Media Elements Tags in HTML 5
1)< audio >
2)< video >
3)< source >
4)< embed >
5)< track >
Canvas Element Tags in HTML 5
1)< canvas>
Form Elements Tags in HTML 5
1)< datalist>
2)< keygen>
3)< output>
Input Type Attribute Values in HTML 5
1) tel
2) search
3) url
4) email
5) datetime
6) date
7) month
8) week
9) time
10) datetime-local
11) number
12) range
13) color
14) placeholder
< video> - Methods, Properties, and Events Tags in HTML 5
1) play()
2) pause()
3) load()
4) canPlayType
< video> Properties in HTML 5
1) currentSrc
2) currentTime
3) videoWidth
4) videoHeight
5) duration
6) ended
7) error
8) paused
9) muted
10) seeking
11) volume
12) height
13) width
< video> Events in HTML 5
1) play
2) pause
3) progress
4) error
5) timeupdate
6) ended
7) abort
8) empty
9) emptied
10) waiting
11)oadedmetadata

Example:















Thursday, December 1, 2011

Let us play with HTML 5, specially HTML 5 Canvas



<br /> HTML 5<br />



Prerequisites:
All you need to get started with Basic is a modern browser such as Google Chrome, Firefox, Safari, Opera, or IE9, a good working knowledge of JavaScript, and a simple text editor like notepad. 
The HTML5 Canvas element is very similar to HTLM tag like <table>, <span>, <div> with exception that it is contents are rendered with JavaScript. 
<canvas id="myCanvas"></canvas> 

HTML5 Canvas Template 
<!DOCTYPE HTML>
<html>
    <head>
        <script> 
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d"); 
                // do your action here
            }; 
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="600">
        </canvas>
    </body>
</html> 

HTML5 Canvas Element Explanation 
The code above will be the base template for all of your future HTML5 Canvas projects.
We can define the height and width of the canvas tag using the height and width attributes,
just like we would with any other HTML tag.
Inside the initializer function we can access the canvas DOM object by its id,
and then get a 2-d context using the getContext() method.

HTML5 Canvas Line Example with Explanation
<!DOCTYPE HTML>
<html>
    <head>
        <style>
            body {
                margin: 0px;
                padding: 0px;
            }
           
            #myCanvas {
                border: 1px solid #9C9898;
            }
        </style>
        <script>
           
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
               
                context.moveTo(100, 150);
                context.lineTo(450, 50);
context.strokeStyle = "#ff0000"; // line color
context.lineWidth = 10;
                context.stroke();
            };
        </script>
    </head>
    <body onmousedown="return false;">
        <canvas id="myCanvas" width="578" height="200">
        </canvas>
    </body>
</html>

The moveTo(): It takes two argument, moveTo() method creates a new subpath for the given point. This point becomes the new context point. You can think of the moveTo() method as a way to position your drawing cursor.
The lineTo():It takes two argument, lineTo() method draws a line from the context point to the given point.
The stroke() method assigns a color to the line and makes it visible. Unless otherwise specified, the default stroke color is black.
The strokeStyle = “” To set the color of an HTML5 Canvas line, we can use the strokeStyle property of the canvas context.