HTML 5
HTML 5
<!DOCTYPE HTML>
<html>
<head>
<style>
#mycanvas{border:1px solid red;}
</style>
</head>
<body>
<canvas id = "mycanvas" width = "100" height = "100"></canvas>
</body>
</html>
Output
The Rendering Context
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
// drawing code here
} else {
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillRect(25,25,100,100);
ctx.clearRect(45,45,60,60);
ctx.strokeRect(50,50,50,50);
</script>
</body>
</html>
HTML5 Canvas - Drawing Paths
We require the following methods to draw paths on the canvas −
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
</script>
</body>
</html>
Question
Example
Sr.No. Examples & Description
1 Drawing Rectangles This method is used to draw rectangle using HTML5 <canvas> element
2 Drawing Paths This method is used to make shapes using paths in HTML5 <canvas> element
3 Drawing Lines This method is used to draw lines using HTML5 <canvas> element
4 Drawing Bezier This method is used to draw Bezier curve using HTML5 <canvas> element
5 Drawing Quadratic This method is used to draw quadratic curve using HTML5 <canvas> element
6 Using Images This method is used to use images with HTML5 <canvas> element
7 Create Gradients This method is used to create gradients using HTML5 <canvas> element
8 Styles and Colors This method is used to apply styles and colors using HTML5 <canvas> element
9 Text and Fonts This method is used to draw amazing text using different fonts and their size.
10 Pattern and Shadow This method is used to draw different patterns and drop shadows.
11 Canvas States This method is used to save and restore canvas states while doing complex drawings on a canvas.
12 Canvas Translation This method is used to move the canvas and its origin to a different point in the grid.
13 Canvas Rotation This method is used to rotate the canvas around the current origin.
14 Canvas Scaling This method is used to increase or decrease the units in a canvas grid.
15 Canvas Transform These methods allow modifications directly to the transformation matrix.
16 Canvas Composition This method is used to mask off certain areas or clear sections from the canvas.
17 Canvas Animation This method is used to create basic animation using HTML5 canvas and JavaScript.
HTML5 - Audio & Video
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
Video Attribute Specification
Sr.No. Attribute & Description
autoplay
1 This Boolean attribute if specified, the video will automatically begin to play back as soon as it can do so without
stopping to finish loading the data.
autobuffer
2 This Boolean attribute if specified, the video will automatically begin buffering even if it's not set to automatically
play.
controls
3 If this attribute is present, it will allow the user to control video playback, including volume, seeking, and
pause/resume playback.
height
4
This attribute specifies the height of the video's display area, in CSS pixels.
loop
5
This Boolean attribute if specified, will allow video automatically seek back to the start after reaching at the end.
preload
6
This attribute specifies that the video will be loaded at page load, and ready to run. Ignored if autoplay is present.
poster
7
This is a URL of an image to show until the user plays or seeks.
src
8 The URL of the video to embed. This is optional; you may instead use the <source> element within the video block
to specify the video to embed.
width
9
This attribute specifies the width of the video's display area, in CSS pixels.
Embedding Audio
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
Audio Attribute Specification
Sr.No. Attribute & Description
autoplay
1 This Boolean attribute if specified, the audio will automatically begin to play back as soon as it
can do so without stopping to finish loading the data.
autobuffer
2 This Boolean attribute if specified, the audio will automatically begin buffering even if it's not set
to automatically play.
controls
3 If this attribute is present, it will allow the user to control audio playback, including volume,
seeking, and pause/resume playback.
loop
4 This Boolean attribute if specified, will allow audio automatically seek back to the start after
reaching at the end.
preload
5 This attribute specifies that the audio will be loaded at page load, and ready to run. Ignored if
autoplay is present.
src
6 The URL of the audio to embed. This is optional; you may instead use the <source> element
within the video block to specify the video to embed.
HTML5 - Geolocation
The geolocation APIs work with a new property of the global navigator
object ie. Geolocation object which can be created as follows −
var geolocation = navigator.geolocation; The geolocation object is a
service object that allows widgets to retrieve information about the
geographic location of the device.
Check for Browser compatibility
The current location of the user can be obtained using the getCurrentPosition
function 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.
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. The location data will be available as a position object and the
function will be called with the position object as its input parameter. A
position object contains a timestamp property denoting the time at which
the location data is retrieved and a coords object. The coords object has the
following properties
Latitude, longitude – Geographic coordinates in decimal degrees
Accuracy – Accuracy level of the latitude and longitude coordinates in
meters. Bigger the number lesser is the accuracy
Altitude – Height of the position above the sea level in meters
AltitudeAccuracy – Denotes how far off the altitude position could be
from the actual attitude value obtained in meters. Bigger the number lesser
is the accuracy
Heading – Provides 360 degree heading information
Speed – Indicates relative speed in meters per second
Error callback function