Showing posts with label html elements. Show all posts
Showing posts with label html elements. Show all posts

Thursday, 12 January 2012

HTML5 Forms


HTML5 New Input Types

HTML5 has several new input types for forms. These new features allow for better input control and validation.
This chapter covers the new input types:
  • email
  • url
  • number
  • range
  • Date pickers (date, month, week, time, datetime, datetime-local)
  • search
  • color

Input Type - email

The email type is used for input fields that should contain an e-mail address.
The value of the email field is automatically validated when the form is submitted.


Example
E-mail: <input type="email" name="user_email" />

Input Type - url

The url type is used for input fields that should contain a URL address.
The value of the url field is automatically validated when the form is submitted.


Example

Homepage: <input type="url" name="user_url" />


Input Type - number



The number type is used for input fields that should contain a numeric value.
You can also set restrictions on what numbers are accepted:

Example

Points: <input type="number" name="points" min="1" max="10" />

Use the following attributes to specify restrictions for the number type:





Attribute Value Description
max number Specifies the maximum value allowed
min number Specifies the minimum value allowed
step number Specifies legal number intervals (if step="3", legal numbers could be -3,0,3,6, etc)
value number Specifies the default value

EXAMPLE:


<!DOCTYPE html>
<html>
<body>


<form action="demo_form.asp" method="get">
<input type="number" name="points" min="0" max="10" step="3" value="6" />
<input type="submit" />
</form>


</body>
</html>



Input Type - range


The range type is used for input fields that should contain a value from a range of numbers.
The range type is displayed as a slider bar.
You can also set restrictions on what numbers are accepted:

Example

<input type="range" name="points" min="1" max="10" />

Use the following attributes to specify restrictions for the range type:





Attribute Value Description
max number Specifies the maximum value allowed
min number Specifies the minimum value allowed
step number Specifies legal number intervals (if step="3", legal numbers could be -3,0,3,6, etc)
value number Specifies the default value



Input Type - Date Pickers

HTML5 has several new input types for selecting date and time:
  • date - Selects date, month and year
  • month - Selects month and year
  • week - Selects week and year
  • time - Selects time (hour and minute)
  • datetime - Selects time, date, month and year (UTC time)
  • datetime-local - Selects time, date, month and year (local time)
The following example allows you to select a date from a calendar:



Example

Date: <input type="date" name="user_date" />



<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
</head>
<body>
    <form id="form1">

    <p>Email :</p>
    <input type="email" name="email" />

    <p>URL :</p>
    <input type="url" name="url" />

    <p>Telephone No. :</p>
    <input type="tel" name="tel" />

    <p>Number :</p>
    <input type="number" name="number" min="1" max="10" step="2"/>

    <p>Range :</p>
    <input type="range" name="range" min="1" max="10" step="2" />

    <p>Date :</p>
    <input type="date" name="date" />

    <p>Month :</p>
    <input type="month" name="month" />

    <p>Week :</p>
    <input type="month" name="week" />

    <p>UTC Date Time :</p>
    <input type="datetime" name="utcdatetime" />

    <p>Local Date Time :</p>
    <input type="datetime-local" name="localdatetime" />

    <p>Time :</p>
    <input type="time" name="time" />

    <input type="Submit" value="Submit" />
    </form>
</body>
</html>



Input Type - search

The search type is used for search fields, like a site search, or Google search.
The search field behaves like a regular text field.

Input Type - color

The color type is used for input fields that should contain a color.
The Opera browser will allow you to select a color from a color picker, Google's Chrome will only allow hexadecimal color values to be submitted:



Example

Color: <input type="color" name="user_color" />



Thursday, 5 January 2012

HTML5 Canvas


What is Canvas?

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. In order to leverage the HTML5 Canvas, you’ll need to place the canvas tag somewhere inside your HTML, create an initialize JavaScript function that accesses the canvas tag once the page loads, and then utilize the HTML5 Canvas API to draw your visualizations.


  • The HTML5 canvas element uses JavaScript to draw graphics on a web page.
  • A canvas is a rectangular area, and you control every pixel of it.
  • The canvas element has several methods for drawing paths, boxes, circles, characters, and adding images.

Create a Canvas Element

Add a canvas element to the HTML5 page.

Specify the id, width, and height of the element:

<html>
    <head>
        <script>
 
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
 
                // do stuff here
            };
 
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="578" height="200">
        </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.


1.1 HTML5 Canvas
1.2 Lines
1.3 Curves
1.4 Paths
1.5 Shapes
1.6 Fill Styles
1.7 Images
1.8 Text

Videos on the Web using html5


         Until now, there has not been a standard for showing a video/movie on a web page.
Today, most videos are shown through a plug-in (like flash). However, different browsers may have different plug-ins.
HTML5 defines a new element which specifies a standard way to embed a video/movie on a web page: the <video> element.

   HTML5 video is an element introduced in the HTML5 draft specification for the purpose of playing videos or movies, partially replacing the object element. HTML5 video is intended by its creators to become the new standard way to show video online, but has been hampered by lack of agreement as to which video formats should be supported in the video element.

How It Works

To show a video in HTML5, this is all you need:

Example
<video> element examples
The following HTML5 code fragment will embed a WebM video into a web page.
<video src="movie.webm" poster="movie.jpg" controls>
        This is fallback content to display if the browser
        does not support the video element.
</video>


Multiple sources

            Using any number of <source> elements, as shown below, the browser will choose automatically which file to download. Alternatively, the javascript canPlay() function can be used to achieve the same. The "type" attribute specifies the MIME type and possibly a list of codecs, which helps the browser to determine whether it can decode the file. Even with only one choice, such hints may be necessary to a hypothetical browser for querying its multimedia framework for third party codecs. Due to lack of a common video format, multiple sources is an important feature to avoid the need for browser sniffing, which is error prone: given that any web developer's knowledge of browsers will inevitably be incomplete, the browser in question knows best.


<video width="320" height="240" controls="controls">
  <source src="movie.mp4" type="video/mp4" />
  <source src="movie.ogg" type="video/ogg" />
  Your browser does not support the video tag.
</video>


The control attribute adds video controls, like play, pause, and volume.

It is also a good idea to always include width and height attributes. If height and width are set, the space required for the video is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the video, and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the video loads).

You should also insert text content between the <video> and </video> tags for browsers that do not support the <video> element.

The <video> element allows multiple <source> elements. <source> elements can link to different video files. The browser will use the first recognized format.

Video Formats and Browser Support

Currently, there are 3 supported video formats for the <video> element: MP4, WebM, and Ogg:

Browser MP4 WebM Ogg
Internet Explorer 9 YES NO NO
Firefox 4.0 NO YES YES
Google Chrome 6 YES YES YES
Apple Safari 5 YES NO NO
Opera 10.6 NO YES YES


HTML5 video Tags

Tag Description
<video> Defines a video or movie
<source> Defines multiple media resources for media elements, such as <video> and <audio>
<track> Defines text tracks in mediaplayers


Video courtesy of Big Buck Bunny.


HTML5 <video> - Methods, Properties, and Events

The table below lists the video methods, properties, and events supported by most browsers:



Methods Properties Events
play() currentSrc play
pause() currentTime pause
load() videoWidth progress
canPlayType videoHeight error
duration timeupdate
ended ended
error abort
paused empty
muted emptied
seeking waiting
volume loadedmetadata
height
width

Usage

      As of April 2010, in the wake of Apple iPad launch, a number of high-profile sites have started to serve H.264 HTML5 video instead of Flash for user-agents identifying as iPad.
As of May 2010, HTML5 video is not currently as widespread as Flash videos, though recent rollouts of experimental HTML5-based video players from Daily Motion(using Ogg Theora and Vorbis format), YouTube(using the H.264 and WebM formats) and Video (using the H.264 format) suggest that interest in adopting HTML5 video is increasing.
Some major video-providing websites have announced decisions to continue using technologies other than HTML5 video. According to a YouTube blog post, the <video> tag does not currently meet all the needs of a website like YouTube. The main reasons stated include the lack of a standard format, the absence of an effective and reliable means of delivering the video to the browser, JavaScript unable to display video fullscreen, and content protection issues. Hulu also has not adopted HTML5 video due to the inability of providing the user with adaptive bandwidth videos, securing the producer's content, and providing advertisers with data. Netflix stated that there are a number of issues preventing them from using HTML5 video: acceptable A/V container formats; acceptable audio and video codecs; streaming protocol; a way for the streaming protocol to adapt to available bandwidth; a way of conveying information about available streams and other parameters to the streaming player module; a way of supporting protected content; and a way of exposing all this functionality into HTML5.
On January 11, 2011, Google's Chromium Project announced on their blog that support for closed codecs (particularly H.264) would be removed from future releases of Chrome. The Chromium announcement specifically mentioned that this removal was an effort to increase the use of license-free HTML5 and the <video> tag, driving web-wide adoption of the open-source codecs VP8 and Theora. On February 2, 2011 Microsoft released the Windows Media Player HTML5 Extension for Chrome which added the ability to use the licensed H.264 player included with Windows to play back H.264 media content using Chrome
A number of advanced HTML5 video players and larger solutions have appeared. SublimeVideo Player was the first to demo how a custom player could provide a better viewing experience than the browser's native player, introducing features a unified player across all devices, flash fallback for older and incompatible devices and true HTML5 fullscreen video. Other popular choices include JW Player and Video JS.

New Elements in HTML5


           The internet has changed a lot since HTML 4.01 became a standard in 1999.
Today, some elements in HTML 4.01 are obsolete, never used, or not used the way they were intended to. These elements are deleted or re-written in HTML5.
To better handle today's internet use, HTML5 also includes new elements for better structure, drawing, media content, and better form handling.


New Markup Elements

New elements for better structure:


Tag Description
<article> Specifies independent, self-contained content, could be a news-article, blog post, forum post, or other articles which can be distributed independently from the rest of the site.
<aside> For content aside from the content it is placed in. The aside content should be related to the surrounding content
<bdi> For text that should not be bound to the text-direction of its parent elements
<command> A button, or a radiobutton, or a checkbox
<details> For describing details about a document, or parts of a document
<summary> A caption, or summary, inside the details element
<figure> For grouping a section of stand-alone content, could be a video
<figcaption> The caption of the figure section
<footer> For a footer of a document or section, could include the name of the author, the date of the document, contact information, or copyright information
<header> For an introduction of a document or section, could include navigation
<hgroup> For a section of headings, using <h1> to <h6>, where the largest is the main heading of the section, and the others are sub-headings
<mark> For text that should be highlighted
<meter> For a measurement, used only if the maximum and minimum values are known
<nav> For a section of navigation
<progress> The state of a work in progress
<ruby> For ruby annotation (Chinese notes or characters)
<rt> For explanation of the ruby annotation
<rp> What to show browsers that do not support the ruby element
<section> For a section in a document. Such as chapters, headers, footers, or any other sections of the document
<time> For defining a time or a date, or both
<wbr> Word break. For defining a line-break opportunity.

New Media Elements

HTML5 provides a new standard for media content:


The internet has changed a lot since HTML 4.01 became a standard in 1999.

To better handle today’s internet use, HTML5 also includes new elements for better structure, drawing, media content, and better form handling.
HTML5 provides a new standards for media contents:
TagDescription
<audio>For multimedia content, sounds, music or other audio streams
<video>For video content, such as a movie clip or other video streams
<source>For media resources for media elements, defined inside video or audio elements
<embed>For embedded content, such as a plug-in
<track>For text tracks used in mediaplayers
The Canvas Element
The canvas element uses JavaScript to make drawings on a web page.
TagDescription
<canvas>For making graphics with a script
New Form Elements
HTML5 offers more form elements, with more functionality:
TagDescription
<datalist>A list of options for input values
<keygen>Generate keys to authenticate users
<output>For different types of output, such as output written by a script

New Input Type Attribute Values
Also, the input element’s type attribute has many new values, for better input control before sending it to the server:
TypeDescription
telThe input value is of type telephone number
searchThe input field is a search field
urlThe input value is a URL
emailThe input value is one or more email addresses
datetimeThe input value is a date and/or time
dateThe input value is a date
monthThe input value is a month
weekThe input value is a week
timeThe input value is of type time
datetime-localThe input value is a local date/time
numberThe input value is a number
rangeThe input value is a number in a given range
colorThe input value is a hexadecimal color, like #FF8800
placeholderSpecifies a short hint that describes the expected value of an input field