Saturday, March 5, 2016

HTML5 - Text

We can write font and set many different properties like:
font, alignment and measure text length

Properties

font - sets the font properties using the same values as CSS
textAlign

  • start
  • end
  • left
  • right
  • center

textBaseline - sets vertical alignment

  • top
  • hanging
  • middle
  • alphabetic
  • ideographic
  • bottom

measureText(text) - measure string and get size back in pixels


Code Snippet
HTML
<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
         <script src="script.js"></script>
        <title>Demo HTML5 Text</title>
    </head>
    <body>
        <section id="CanvasSection">
            <canvas id="canvas" width="500" height="300"></canvas>
        </section>
    </body>
</html> 
 
Javascript - script.js

function textdemo() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
 
    canvas.font = "bold 20px segoe UI, sans-serif";
    canvas.textAlign = "start";
    canvas.textBaseline = "bottom";
 
    var message = "Hello Html5!";
    canvas.fillText(message,200,220);
 
    var textSize = canvas.measureText(message);
    canvas.strokeRect(200, 200, textSize.width, 20);
}
addEventListener("load", textdemo);
 
Output

HTML5 - Canvas In Action !

HTML5 - Canvas Demo

Please read my article that describes various canvas objects and their usage
http://hetalparikh.blogspot.com/2016/03/html5-canvas.html

I created an HTML page with a simple canvas used to draw objects as described below. Note that I have included a seperate javascript file which does the work of drawing the canvas.

An event listener is added that loads the javascript on page load to draw the canvas.

HTML Source

<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
         <script src="script.js"></script>
        <title>Demo HTML5 Canvas</title>
    </head>
    <body>
        <section id="CanvasSection">
            <canvas id="canvas" width="500" height="300"></canvas>
        </section>
    </body>
</html>

script.js source code

Demo 1: Draw Rectangle
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.moveTo(200, 200);
    canvas.lineTo(200, 100);
    canvas.lineTo(400, 200);
    canvas.closePath(); 
 
    /*Rectangle Demo*/
    canvas.fillStyle = "#0000ff"; /*fills main canvas blue*/
    canvas.strokeStyle = "#ff0000";/*stroke/draws canvas red*/
 
    canvas.strokeRect(100, 100, 120, 120);/*draws main rectangle*/
    canvas.fillRect(110, 110, 100, 100);/*fills rectangle*/
    canvas.clearRect(130, 130, 30, 30);/*fills rectangle with clear/white space*/
}
addEventListener("load", initiate);
Output 

Demo 2: Draw triangle
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.moveTo(200, 200);
    canvas.lineTo(200, 100);
    canvas.lineTo(400, 200);
    canvas.closePath();
    canvas.stroke();
}
addEventListener("load", initiate);
Output
 
Demo 3: Fill triangle with default solid
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.moveTo(200, 200);
    canvas.lineTo(200, 100);
    canvas.lineTo(400, 200);
    canvas.closePath();
    canvas.fill();
}
addEventListener("load", initiate);
Output 
Demo 4: Clip Region
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.moveTo(200, 200);
    canvas.lineTo(200, 100);
    canvas.lineTo(400, 200);
    canvas.closePath();
    /*Clipping Demo*/
    canvas.clip();
    canvas.beginPath();
    for (var i = 0; i < 400; i = i + 5)
    {
        canvas.moveTo(0, i);
        canvas.lineTo(500, i); 
    }
    canvas.stroke();
}
addEventListener("load", initiate);
Output
 
Demo 5: Gradient Rectangle Demo
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.moveTo(200, 200);
    canvas.lineTo(200, 100);
    canvas.lineTo(400, 200);
    canvas.closePath();
   
    /*Linear Gradient Rectangle Demo*/
    var gradient = canvas.createLinearGradient(0, 0, 10, 100);
    gradient.addColorStop(0.5,'#0088FF');
    gradient.addColorStop(1,'#ff0000');
    canvas.fillStyle = gradient;
 
    canvas.fillRect(150, 10, 200, 100);
}
addEventListener("load", initiate); 
Output
























Demo 6 : Arc - Circle
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.arc(200, 200, 75, 0, Math.PI * 2, false);
    canvas.stroke();
}
addEventListener("load", initiate);
Output

Demo 7 : Arc - 60 Degrees Arc Demo
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    
    /*Arc Demo*/
    //We draw a 60 degrees arc
    //pi = circle. 
    //circle/180 = 1 degree. 
    //1 degree * 60 = 60 degrees
    var radiansFromDegrees = Math.PI / 180 * 60; 
    canvas.arc(200, 200, 75, 0, radiansFromDegrees, false);
    canvas.stroke();
}
addEventListener("load", initiate);
Output

Demo 8 : Quadratic Curve Demo
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    /*Quadratic Curve Demo*/
    canvas.moveTo(100, 100);
    canvas.quadraticCurveTo(150, 200, 100, 300);
    canvas.stroke();
}
addEventListener("load", initiate);
Output 

Demo 9 : Bezier Curve Demo
function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.moveTo(150, 150);
    canvas.bezierCurveTo(50, 100, 200, 150, 100, 250);
    canvas.stroke();
}
addEventListener("load", initiate);
Output 
 
 
 Demo 9 : Bezier Curve Demo - With Line
This draws a square shaped line, dimension 5 units

function initiate() 
{
    var elem = document.getElementById('canvas');
    var canvas = elem.getContext('2d');
    canvas.beginPath();
    canvas.moveTo(150, 150);
    canvas.bezierCurveTo(50, 100, 200, 150, 100, 250);
    canvas.lineWidth = 5;
    canvas.lineCap = "square";
    canvas.stroke();
}
addEventListener("load", initiate);
Output


HTML5 - Canvas

One of goals of HTML5 was to make web independant of plugins 
like flash, silverlight and other plugins

One of the key features were media and graphics

To handle this HTML5 introduces "Canvas" API

It creates drawing surface, a blank area in browser
It marks X,Y coordinates

With this you can achieve precise control over any object

2d vs 3d (webgl)
webgl only supported in browsers that implement WebGL library

Rectangle

Shape fully supported by canvas

Rectangle Methods
Stroke - draws border
Fill - fills solid color
Clear - remove solid / puts white fill

Gradients
createLinearGradient(x1,y1,x2,y2)
createRadialGradient(x1,y1,r1,x2,y2,r2)
addColorStop(position,color)

Drawing
Usually we don't draw directly to canvas
A path is a pattern for the "pen" to follow
Once the path is set, it is sent to the canvas

Paths

  • moveTo(x,y) - moves pen to x,y without drawing
  • lineTo(x,y) - draws line to x,y by drawing and pen moves
  • closePath - draws line from wherever pen is to start of path
  • clip - create region outside which region is clipped away
  • rect(x,y,width,height)
  • arc(x,y,radius,startAngle,endAngle,direction) - creates circle or arc of circle
  • quadraticCurveTo(cpx,cpy,x,y)
  • bezierCurveTo(cp1x,cp1y,cp,2x,cp2y,x,y)
Arc
You can draw curves and circles using "Arc"

Method:
canvas.arc(param1,param2,param3,param4,param5,param6);

Parameters are as follows:
param1:offset
param2:offset
param3:radius
param4:starting angle
param5:no.of degrees in radients
param6:draw clockwise/counter-clockwise

i.e.
canvas.arc(offset,offset,radius,starting angle,degrees,direction);



Thursday, March 3, 2016

HTML5 - Video & Audio

Video
HTML5 videos are not very sophisticated in functionality
However, your page does not need any plugin to run videos

No standard currently available to run videos across browsers
MP4 is not free - there are patent/license issues
WEBM - current available alternative

<video> element
Attribute includes:

  • src(takes a url)
  • controls
  • autoplay
  • loop
  • poster(takes a url)
  • preload - includes values none, metadata, auto (default)

HTML5 Input Types

HTML5 New Input Types

We will take a look at creating forms with HTML5 
with following additional input types

New input types


Date input types are covered in a separate article here: http://hetalparikh.blogspot.com/2016/03/html5-date-input-caveats.html
  • Date
  • Time
  • Month
  • Week

Other input types

Range - The <input type="range"> is used for input fields that should contain a value within a range. Depending on browser support, the input field can be displayed as a slider control. 

Search - The <input type="search"> is used for search fields (a search field behaves like a regular text field).
Telephone - The <input type="tel"> is used for input fields that should contain a telephone number

Email - The <input type="email"> is used for input fields that should contain an e-mail address. Depending on browser support, the e-mail address can be automatically validated when submitted. Some smartphones recognize the email type, and adds ".com" to the keyboard to match email input.

Color - The <input type="color"> is used for input fields that should contain a color.
Depending on browser support, a color picker can show up in the input field.

Data List - Specifies a list of pre-defined options for input controls

Progress - Represents the progress of a task
 

Meter - Defines a scalar measurement within a known range (a gauge)

Form Output (As displayed in Chrome)

You can save the source code below as html page  to see the code in action!

In Chrome, a nice picker control is displayed for date controls.
Also, for controls marked as required, browser automatically adds validation support to display a nice popup to show "required field" validation message.

 

Code Snippet


FormsDemo.html 
<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="MyStyles.css" />
        <title>Demo HTML5 Form Controls</title>
    </head>
    <body>
        <form id="myform">
            <!--Input Type:Date-->
            <label for="date-input">Date:</label>
            <input id="date-input" type="date" /><br/><br/>
            <label for="time-input">Time:</label>
            <input id="time-input" type="time" /><br/><br/>
            <label for="month-input">Month:</label>
            <input id="month-input" type="month" /><br/><br/>
            <label for="week-input">Week:</label>
            <input id="week-input" type="week" /><br/><br/>
 
            <!--Input Type: Range-->
            <label for="range-data">Range:</label>
            <input id="range-data" type="range" min="1" max="10" /><br/><br/>
 
            <!--Input Type: Search-->
            <label for="search-data">Search:</label>
            <input id="search-data" type="search" placeholder="Enter Search Term Here"/><br/><br/>
 
            <!--Input Type: Email-->
            <label for="email-data">Email:</label>
            <input id="email-data" type="email" required multiple /><br/><br/>
 
            <!--Input Type: Color-->
            <label for="color-data">Color:</label>
            <input id="color-data" type="color" /><br/><br/>
 
             <!--Input Type: Telephone-->
            <!--We have made telephone a required form field. So if you 
            click submit button without entering telephone, it will prompt you-->
            <label for="tel-data">Telephone:</label>
            <input id="tel-data" type="tel" required /><br/><br/>
 
            <!--Input Type: DataList-->
            <label>DataList:</label>
            <input type="text" list="myDataList">
            <datalist id="myDataList">
                <option label="Mr." value="Mr."></option>
                <option label="Mrs." value="Mrs."></option>
                <option label="Ms." value="Ms."></option>
            </datalist><br/><br/>
            
             <!--Input Type: Progress Bar-->
            <label for="progress-data">Progress Bar:</label>
            <progress id="progress-data" value="5" max="10"></progress><br/><br/>
 
            <!--Input Type: Meter-->
            <label for="meter-data">Meter:</label>
            <meter id="meter-data" value="75" min="10" max="100" optimum="50" low="20" high="70"></meter><br/><br/>
 
            <!--Form Submit Button-->
            <input type="submit" value="Submit" /><br/><br/>
        </form>
        
        <!--Input Type: Outside Form-->
        <!--Even though this control is outside the form, form property specifies which 
        form this control is associated with and the data is posted when the form is submitted-->
        <label for="outside-data">Outside Text:</label>
        <input id="outside-data" type="text" form="myform" /><br/><br/>
    </body>
</html> 
 
MyStyles.css file (added to the project directory for Range Control)
input[type=range]::before{content: attr(min);}
input[type=range]::after{content: attr(max);}
input[type=range] {width:250px; color:blue; font-family: 'Segoe UI'; font-size: small}