Loading...

Go Back

Next page
Go Back Course Outline

Html5 Full Course


HTML5 APIs and Advanced Features

HTML5 APIs and Advanced Features

1. Canvas Element

Explanation: The <canvas> element allows you to draw graphics on the webpage using JavaScript. It is widely used for creating animations, games, and dynamic graphics.

Basic Usage of the <canvas> Tag

<canvas id="myCanvas" width="500" height="500" style="border:1px solid #000000;"></canvas>

Output: This draws a green rectangle on the canvas.

Drawing Shapes, Lines, and Text using JavaScript

<canvas id="myCanvas" width="500" height="500" style="border:1px solid #000000;"></canvas>

Output: A blue rectangle is drawn, followed by a line and text ("Hello, Canvas!") on the canvas.

Animation and Game Development Basics with <canvas>

<canvas id="myCanvas" width="500" height="500" style="border:1px solid #000000;"></canvas>

Output: The red square moves across the canvas continuously. It resets once it moves out of view.



2. Geolocation API

Explanation: The Geolocation API allows web applications to access the geographical location of the user’s device. It is commonly used in maps and location-based services.

Accessing User Location

<button onclick="getLocation()">Get My Location</button>
<p id="location"></p>

Output: Clicking the button will fetch and display the user's latitude and longitude.

3. Local Storage and Session Storage

Explanation: These allow storing data on the client side. localStorage persists data even after closing the browser, while sessionStorage clears the data once the session ends.

Storing Data with Local Storage and Session Storage

<button onclick="saveData()">Save Data</button>
<button onclick="loadData()">Load Data</button>
<p id="output"></p>

Output: Clicking "Save Data" stores data, and "Load Data" retrieves and displays it from both localStorage and sessionStorage.

4. Web Workers

Explanation: Web Workers allow you to run JavaScript in the background, without blocking the UI thread. They are great for performing time-consuming tasks like data processing.

Introduction to Web Workers

<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>

Output: Clicking "Start Worker" creates a background task (worker) to process data, while "Stop Worker" terminates it.



5. WebSockets

Explanation: WebSockets provide real-time communication between the client and server. They allow you to send and receive messages over a persistent connection.

Real-Time Communication Using WebSockets

<button onclick="startSocket()">Start WebSocket</button>
<p id="message"></p>

Output: A WebSocket connection is established, and messages are sent and received in real-time.

6. Drag and Drop API

Explanation: The Drag and Drop API allows you to create draggable elements and define drop zones on your web page.

Enabling Drag-and-Drop Functionality

<div id="drag" draggable="true" ondragstart="dragStart(event)">
  Drag me!
</div>

<div id="dropZone" ondrop="drop(event)" ondragover="allowDrop(event)">
  Drop here!
</div>

Output: Drag the "Drag me!" element and drop it into the "Drop here!" zone.

Drag me!

Go Back

Next page