Css 3

Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Vous êtes sur la page 1sur 62

Overview of HTML5

• History, Vision & Future of HTML5

• Structure of a Web Page

• HTML5 Mark-up

• Browser Support

• Forms

• Audio and Video

• Canvas

• SVG

• Geo location
The <input> element in HTML4
The <input> element in HTML4
The <input> element in HTML4
<html>
<form action = "http://example.com/cgiscript.pl" method = "post">
<p>
<label for = "firstname">first name: </label>
<input type = "text" id = "firstname"><br />

<label for = "lastname">last name: </label>


<input type = "text" id = "lastname"><br />

<label for = "email">email: </label>


<input type = "text" id = "email"><br>

<input type = "radio" name = "sex" value = "male"> Male<br>


<input type = "radio" name = "sex" value = "female"> Female<br>
<input type = "submit" value = "send"> <input type = "reset">
</p>
</form>
</html>
The <input> element in HTML5
The <input> element in HTML5
The <input> element in HTML5
<!DOCTYPE HTML>
<html>
<head>
<script type = "text/javascript">
function showResult()
{
x = document.forms["myform"]["newinput"].value;
document.forms["myform"]["result"].value = x;
}
</script>
</head>
<body>

<form action = "/cgi-bin/html5.cgi" method = "get" name = "myform">


Enter a value : <input type = "text" name = "newinput" />
<input type = "button" value = "Result" onclick = "showResult();" />
<output name = "result"></output>
</form>

</body>
</html>
The <input> element in HTML5

The placeholder attribute


- HTML5 introduced a new attribute called placeholder.

- This attribute on <input> and <textarea> elements provide a hint to the user
of what can be entered in the field.

-The placeholder text must not contain carriage returns or line-feeds.

Here is the simple syntax for placeholder attribute −

<input type = "text" name = "search" placeholder = "search the web"/>


The <input> element in HTML5

<!DOCTYPE HTML>

<html>
<body>

<form action = "/cgi-bin/html5.cgi" method = "get">

Enter email :
<input type = "email" name = "newinput” placeholder = "[email protected]"/>
<input type = "submit" value = "submit" />

</form>

</body>
</html>
The <input> element in HTML5

The autofocus attribute


- This is a simple one-step pattern, easily programmed in JavaScript at the time
of document load, automatically focus one particular form field.

HTML5 introduced a new attribute called autofocus which would be used as


follows −

<input type = "text" name = "search" autofocus/>


The <input> element in HTML5

<!DOCTYPE HTML>

<html>
<body>

<form action = "/cgi-bin/html5.cgi" method = "get">

Enter email : <input type = "text" name = "newinput" autofocus/>


<p>Try to submit using Submit button</p>
<input type = "submit" value = "submit" />

</form>

</body>
</html>
The <input> element in HTML5

The required attribute


- No need to have JavaScript for client-side validations like empty text box
would never be submitted because HTML5 introduced a new attribute
called required

- which would be used as follows and would insist to have a value −

<input type = "text" name = "search" required/>


The <input> element in HTML5

<!DOCTYPE HTML>

<html>
<body>

<form action = "/cgi-bin/html5.cgi" method = "get">

Enter email : <input type = "text" name = "newinput" required/>


<p>Try to submit using Submit button</p>
<input type = "submit" value = "submit" />

</form>

</body>
</html>
HTML5 Graphics Elements
HTML5 offers new semantic elements to define different parts of a web page:
• <Canvas>
- The <canvas> tag is used to draw graphics, on the fly, via scripting (usually
JavaScript).
- The <canvas> tag is only a container for graphics, you must use a script to
actually draw the graphics.
HTML5 Graphics Elements
HTML5 offers new semantic elements to define different parts of a web page:
• <Canvas>
- The <canvas> element must have an id attribute so it can be referred to by
JavaScript.
- The width and height attribute is necessary to define the size of the canvas.
<canvas id="myCanvas" width="200" height="100"></canvas>

By default, the <canvas> element has no border and no content.

<canvas id="myCanvas" width="200" height="100" style="border:1px solid


#000000;"></canvas>
HTML5 Graphics Elements
Step 1: Find the Canvas Element

First of all, you must find the <canvas> element.


This is done by using the HTML DOM method getElementById():

var canvas = document.getElementById("myCanvas");

Step 2: Create a Drawing Object

Secondly, you need a drawing object for the canvas.


The getContext() is a built-in HTML object, with properties and methods for
drawing:

var ctx = canvas.getContext("2d");


HTML5 Graphics Elements
Step 3: Draw on the Canvas

Finally, you can draw on the canvas.

Set the fill style of the drawing object to the color red:

ctx.fillStyle = "#FF0000";

The fillStyle property can be a CSS color, a gradient, or a pattern.


The default fillStyle is black.

The fillRect(x,y,width,height) method draws a rectangle, filled with the fill style, on
the canvas:

ctx.fillRect(0, 0, 150, 75);


HTML5 Graphics Elements
Draw a Circle

To draw a circle on a canvas, use the following methods:

beginPath() - begins a path


arc(x,y,r,startangle,endangle) - creates an arc/curve.

To create a circle with arc( ): Set start angle to 0 and end angle to 2*Math.PI.

The x and y parameters define the x- and y-coordinates of the center of the circle.

The r parameter defines the radius of the circle.


HTML5 Graphics Elements
<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="200" height="100“ style="border:1px solid


#d3d3d3;">
Your browser does not support the canvas element.
</canvas>

<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
</script>

</body>
</html>
HTML5 Graphics Elements

Drawing Text on the Canvas

To draw text on a canvas, the most important property and methods are:

font - defines the font properties for the text

fillText(text,x,y) - draws "filled" text on the canvas (x, y – positions on canvas)

strokeText(text,x,y) - draws text on the canvas (no fill)


<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="200" height="100“ style="border:1px solid #d3d3d3;">


Your browser does not support the canvas element.
</canvas>

<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillStyle = "red"; // Text color
ctx.textAlign = "center"; // TextAlignment
ctx.fillText("Hello World",10,50);
OR
ctx.strokeText("Hello World", 10, 50);

</script>

</body>
</html>
Drawing a Line

Sr.No. Method and Description


beginPath()
1 This method resets the current path.
moveTo(x, y)
2 This method creates a new subpath with the given point.

closePath()
3 This method marks the current subpath as closed, and starts a new subpath with a
point the same as the start and end of the newly closed subpath.
fill()
4 This method fills the subpaths with the current fill style.

stroke()
5 This method strokes the subpaths with the current stroke style.

lineTo(x, y)
6 This method adds the given point to the current subpath, connected to the
previous one by a straight line.
Drawing a Line
HTML5 canvas provides the following two important properties to apply colors to a
shape −
Sr.No. Method and Description
fillStyle
1 This attribute represents the color or style to use inside the shapes.

strokeStyle
This attribute represents the color or style to use for the lines around
2
shapes.
HTML5 Graphics Elements
Canvas – Gradients

Gradients can be used to fill rectangles, circles, lines, text, etc. Shapes on the canvas
are not limited to solid colors.

There are two different types of gradients:

createLinearGradient(x,y,x1,y1) - creates a linear gradient


createRadialGradient(x,y,r,x1,y1,r1) - creates a radial/circular gradient
Once we have a gradient object, we must add two or more color stops.

The addColorStop() method specifies the color stops, and its position along the
gradient. Gradient positions can be anywhere between 0 to 1.

To use the gradient, set the fillStyle or strokeStyle property to the gradient, then draw
the shape (rectangle, text, or a line).
<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="200" height="100"


style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// Create gradient
var grd = ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");

// Fill with gradient


ctx.fillStyle = grd;
ctx.fillRect(10,10,150,80);
</script>

</body>
</html>
Create Gradient
HTML5 canvas allows us to fill and stroke shapes using linear and radial gradients using
the following methods −`
Sr.No. Method and Description
addColorStop(offset, color)
This method adds a color stop with the given color to the gradient at the given
1 offset. Here 0.0 is the offset at one end of the gradient, 1.0 is the offset at the other
end.

createLinearGradient(x0, y0, x1, y1)


This method returns a CanvasGradient object that represents a linear gradient that
2 paints along the line given by the coordinates represented by the arguments.
The four arguments represent the starting point (x0,y0) and end point (x1,y1) of the
gradient.

createRadialGradient(x0, y0, r0, x1, y1, r1)


This method returns a CanvasGradient object that represents a radial gradient that
3 paints along the cone given by the circles represented by the arguments.
The first three arguments define a circle with coordinates (x0,y0) and radius r0 and
the second a circle with coordinates (x1,y1) and radius r1.
SVG

• SVG stands for Scalable Vector Graphics


• Used to define vector-based graphics for the Web
• Defines the graphics in XML format
• Every element and every attribute in SVG files can be animated
• SVG images can be created and edited with any text editor
• SVG images can be searched, indexed, scripted, and compressed
• SVG images are scalable
SVG
<!DOCTYPE html>
<html>
<body>

<svg width="100" height="100">

<circle cx="50" cy="50" r="40"


stroke="green" stroke-width="4" fill="yellow" />

Sorry, your browser does not support inline SVG.


</svg>

</body>
</html>
SVG
SVG Code explanation:
• An SVG image begins with an <svg> element
• The width and height attributes of the <svg> element define the width and
height of the SVG image
• The <circle> element is used to draw a circle
• The cx and cy attributes define the x and y coordinates of the center of the
circle. If cx and cy are not set, the circle's center is set to (0, 0)
• The r attribute defines the radius of the circle
• The stroke and stroke-width attributes control how the outline of a shape
appears.
• We set the outline of the circle to a 4px green "border"
• The fill attribute refers to the color inside the circle. We set the fill color to
yellow
• The closing </svg> tag closes the SVG image
SVG
SVG Shapes

SVG has some predefined shape elements that can be used by developers:

• Rectangle <rect>
• Circle <circle>
• Ellipse <ellipse>
• Line <line>
• Polyline <polyline>
• Polygon <polygon>
• Path <path>
SVG
<!DOCTYPE html>
<html>
<body>

<svg width="400" height="100">


<rect width="400" height="100"
style="fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)" />
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>
SVG
<!DOCTYPE html>
<html>
<body>

<svg width="400" height="180">


<rect x="50" y="20" rx="20" ry="20" width="150" height="150"
style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>

The rx and the ry attributes rounds the corners of the rectangle


SVG
<!DOCTYPE html>
<html>
<body>

<svg height="140" width="500">


<ellipse cx="200" cy="80" rx="100" ry="50"
style="fill:yellow;stroke:purple;stroke-width:2" />
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>
•The cx attribute defines the x coordinate of the center of the ellipse
• The cy attribute defines the y coordinate of the center of the ellipse
• The rx attribute defines the horizontal radius
• The ry attribute defines the vertical radius
SVG
<!DOCTYPE html>
<html>
<body>

<svg height="150" width="500">


<ellipse cx="240" cy="100" rx="220" ry="30" style="fill:purple" />
<ellipse cx="220" cy="70" rx="190" ry="20" style="fill:lime" />
<ellipse cx="210" cy="45" rx="170" ry="15" style="fill:yellow" />
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>
SVG
<!DOCTYPE html>
<html>
<body>

<svg height="210" width="500">


<line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-
width:2" />
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>

•The x1 attribute defines the start of the line on the x-axis


•The y1 attribute defines the start of the line on the y-axis
•The x2 attribute defines the end of the line on the x-axis
•The y2 attribute defines the end of the line on the y-axis
SVG
<!DOCTYPE html>
<html>
<body>

<svg height="210" width="500">


<polygon points="100,10 40,198 190,78 10,78 160,198"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;"/>
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>
Differences between SVG and Canvas

SVG Canvas
Vector based (composed of shapes) Raster based (composed of pixel)
Multiple graphical elements, which become the Single element similar to <img> in behavior.
part of the page's DOM tree Canvas diagram can be saved to PNG or JPG
format
Modified through script and CSS Modified through script only
Good text rendering capabilities Poor text rendering capabilities
Give better performance with smaller number Give better performance with larger number of
of objects or larger surface, or both objects or smaller surface, or both
Better scalability. Poor scalability.
Can be printed with high quality at any Not suitable for printing on higher resolution.
resolution. Pixelation does not occur Pixelation may occur
Audio / Video

• The controls attribute adds audio controls, like play, pause, and volume.

• The <source> element allows you to specify alternative audio files which the browser may

choose from.

• The browser will use the first recognized format.

•The text between the <audio> and </audio> tags will only be displayed in browsers that do

not support the <audio> element.


Audio / Video
Sr. Attribute & Description
No.
autoplay
This Boolean attribute if specified, the video will automatically begin to play back as
1
soon as it can do so without stopping to finish loading the data.

autobuffer
This Boolean attribute if specified, the video will automatically begin buffering even
2
if it's not set to automatically play.

controls
If this attribute is present, it will allow the user to control video playback, including
3
volume, seeking, and pause/resume playback.

height
4 This attribute specifies the height of the video's display area, in CSS pixels.

loop
This Boolean attribute if specified, will allow video automatically seek back to the
5
start after reaching at the end.
Audio / Video

<!DOCTYPE html>
<html>
<body>

<video width="320" height="240" controls>


<source src="mov_bbb.mp4" type="video/mp4">
<source src="mov_bbb.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>

</body>
</html>
HTML5 Local Storage

• Cookies are small pieces of data which a server can store in the browser.

• The cookie is sent by the browser along with all future HTTP requests to the

server that set the cookie.

• Cookies cannot be bigger than 4KB in total.


HTML5 Local Storage

• HTML5 local storage is set via JavaScript executed in the browser.


• HTML5 local storage properties are never sent to any server - unless you
explicitly copy them out of the local storage and appends them to an AJAX
request.

• HTML5 local storage can store somewhere between 2MB and 10MB data in
the browser (per origin - domain name).

- Exactly how much data is allowed depends on the browser.


- A limit of 5MB to 10MB is most common.
HTML5 Local Storage
• Each piece of data is stored in a key/value pair.
• The key identifies the name of the information

<script> // Check if the localStorage object exists


if(localStorage)
{ // Store data
localStorage.setItem("first_name", “ABC"); // Retrieve data
alert("Hi, " + localStorage.getItem("first_name"));
} else
{
alert("Sorry, your browser do not support local storage.");
}
</script>
HTML5 Local Storage

• localStorage.setItem(key, value) stores the value associated with a key.

• localStorage.getItem(key) retrieves the value associated with the key.

• You can also remove a particular item from the storage if it exists, by passing the key
name to the removeItem() method, like localStorage.removeItem("first_name").

• However, if you want to remove the complete storage use the clear() method, like
localStorage.clear().

• The clear() method takes no arguments, and simply clears all key/value pairs from
localStorage at once, so think carefully before you using it.
HTML5 Local Storage

• HTML5 local storage offers a simple key - value store, like a hash table or
dictionary object.

•The local storage object looks very similar to a regular JavaScript object, with
the exception that it is stored in the browser, even if the page is unloaded.

Object Available to Lifetime

All windows or tabs


localStorage Permanent
using the same domain

A particular window Till the end of the


sessionStorage or tab and its popups session
HTML5 Local Storage
<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter()
{
if (typeof(Storage) !== "undefined")
{
if (localStorage.clickcount)
{
localStorage.clickcount = Number(localStorage.clickcount)+1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
localStorage.clickcount + " time(s).";
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not
support web storage...";
}
}
</script>
</head>
HTML5 Local Storage

<body>

<p><button onclick="clickCounter()" type="button">Click me!</button></p>


<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will
continue to count (is not reset).</p>

</body>
</html>
HTML5 SessionStorage
<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter() {
if (typeof(Storage) !== "undefined") {
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
} else {
sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the
button " + sessionStorage.clickcount + " time(s) in this session.";
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does
not support web storage...";
}
}
</script>
</head>
HTML5 SessionStorage

<body>

<p><button onclick="clickCounter()" type="button">Click me!</button></p>


<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will
continue to count (is not reset).</p>

</body>
</html>
HTML5 Geolocation

•The Geolocation API of HTML5 helps in identifying the user’s location, which
can be used to provide location specific information or route navigation details
to the user.

Check for Browser compatibility

• The geolocation property of the global navigator object helps in detecting the
browser support for the Geolocation API.

if (navigator.geolocation)
{
// Get the user's current position
} else
{
alert('Geolocation is not supported in your browser');
}
HTML5 Geolocation
Get the user’s current location

• The current location of the user can be obtained using the getCurrentPosition
of the navigator.geolocation object.

• This function accepts three parameters – Success callback function, Error


callback function and position options.

• If the location data is fetched successfully, the success callback function will be
invoked with the obtained position object as its input parameter.

• Otherwise, the error callback function will be invoked with the error object as its
input parameter.
HTML5 Geolocation

Get the user’s current location

if (navigator.geolocation)
{
// Get the user's current position
navigator.geolocation.getCurrentPosition(showPosition, showError, optn);
} else
{
alert('Geolocation is not supported in your browser');
}
HTML5 Geolocation

Get the user’s current location

1. Success callback function


- This callback function is invoked only when the user accepts to share the
location information and the location data is successfully fetched by the
browser.

- A position object contains a timestamp property denoting the time at which


the location data is retrieved and a coords object.
- The coords object contains latitude, longitude, accuracy, altitude.
2. Error
3. Position
HTML5 Drag and Drop

• HTML Drag and Drop (DnD) is a feature of HTML5.

• It is a powerful user interface concept which is used to copy, reorder and


delete items with the help of mouse.

• You can hold the mouse button down over an element and drag it to another
location. If you want to drop the element there, just release the mouse button.

1. Make an Element Draggable


2. What to Drag - ondragstart and setData()
3. Where to Drop - ondragover
4. Do the Drop - ondrop
HTML5 Drag and Drop

Step 1 - Making an Object Draggable

• If you want to drag an element, you need to set the draggable attribute to
true for that element.

• Set an event listener for dragstart that stores the data being dragged.

• The event listener dragstart will set the allowed effects (copy, move,
link, or some combination).
HTML5 Drag and Drop

Step 2 - Dropping the Object


• To accept a drop, the drop target has to listen to at least three events.

• The dragenter event, which is used to determine whether or not the drop
target is to accept the drop. If the drop is to be accepted, then this event
has to be canceled.

• The dragover event, which is used to determine what feedback is to be


shown to the user. If the event is canceled, then the feedback (typically
the cursor) is updated based on the dropEffect attribute's value.

• Finally, the drop event, which allows the actual drop to be performed.
HTML5 Drag and Drop

• Make an Element Draggable

First of all: To make an element draggable, set the draggable attribute to true:

<img draggable="true">

• What to Drag - ondragstart and setData()


Then, specify what should happen when the element is dragged.

In the example above, the ondragstart attribute calls a function, drag(event), that
specifies what data to be dragged.

The dataTransfer.setData() method sets the data type and the value of the
dragged data:

function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
HTML5 Drag and Drop

Where to Drop – ondragover

• The ondragover event specifies where the dragged data can be dropped.

• By default, data/elements cannot be dropped in other elements.

• To allow a drop, we must prevent the default handling of the element.

• This is done by calling the event.preventDefault() method for the


ondragover event:

event.preventDefault()
HTML5 Drag and Drop

• Do the Drop – ondrop

When the dragged data is dropped, a drop event occurs.

In the example above, the ondrop attribute calls a function, drop(event):

function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
HTML5 Drag and Drop
Events for Drag and Drop feature

Event Description
Drag It fires every time when the mouse is moved while the object is being
dragged.
Dragstart It is a very initial stage. It fires when the user starts dragging object.
Dragenter It fires when the user moves his/her mouse cursur over the target
element.
Dragover This event is fired when the mouse moves over an element.
Dragleave This event is fired when the mouse leaves an element.
Drop Drop It fires at the end of the drag operation.
Dragend It fires when user releases the mouse button to complete the drag
operation.
HTML5 Drag and Drop
Drag and Drop Events

Event Description
ondragstart Fires when the user starts dragging an element.
ondragenter Fires when a draggable element is first moved into a drop listener.
ondragover Fires when the user drags an element over a drop listener.
ondragleave Fires when the user drags an element out of drop listener.
ondrag Fires when the user drags an element anywhere; fires constantly but can
give X and Y coordinates of the mouse cursor.
ondrop Fires when the user drops an element into a drop listener successfully.
ondragend Fires when the drag action is complete, whether it was successful or not.
This event is not fired when dragging a file to the browser from the
desktop.
References

• https://www.dcpehvpm.org/E-Content/BCA/BCA-II/Web%20Technology/the-
complete-reference-html-css-fifth-edition.pdf

• https://www.studytonight.com/html5-references/

Vous aimerez peut-être aussi