Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 377 of 534
Change every letter to next letter - JavaScript
We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element. For example: If the string is − const str = 'how are you'; Then the output should be − const output = 'ipx bsf zpv'; How It Works The algorithm processes each character by checking if it's a letter using ASCII codes. For letters 'a-y' and 'A-Y', it moves to the next letter. For 'z' and 'Z', it wraps around to 'a' and 'A' respectively. Non-alphabetic ...
Read MoreDetect area of a PNG that is not transparent with HTML
To detect the non-transparent area of a PNG image in HTML, you need to use the Canvas API to access pixel data and scan for opaque pixels. This technique is useful for cropping images, collision detection, or optimizing sprite sheets. How It Works The process involves loading the image onto a canvas, getting the pixel data as a buffer, and scanning for non-transparent pixels to find the bounding box: Load the PNG image onto a canvas element Get the 32-bit RGBA pixel data using getImageData() Scan from ...
Read MoreApply gravity between two or more objects in HTML5 Canvas
To apply gravity between two or more objects in HTML5 Canvas, you need to calculate the gravitational force between each pair of objects and update their velocities accordingly. Gravitational Force Formula The gravitational force between two objects is calculated using Newton's law of universal gravitation. In our simplified 2D canvas implementation, we use: F = G * (m1 * m2) / r² Where F is the force, G is the gravitational constant, m1 and m2 are the masses, and r is the distance between objects. Basic Gravity Calculation Here's how to calculate ...
Read MoreHTML5 tag
The HTML5 tag is used to create radial gradients in SVG graphics. It defines a smooth color transition that radiates from a center point outward in a circular pattern, making it perfect for creating lighting effects, shadows, and visual depth in SVG elements. Syntax Key Attributes cx, cy: Center point coordinates of the gradient (default: 50%) r: Radius of the gradient (default: 50%) fx, fy: Focal point coordinates for gradient focus gradientUnits: Coordinate system ("objectBoundingBox" or "userSpaceOnUse") Example: ...
Read MoreRepeating letter string - JavaScript
We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times. For example: If the string is − const str = 'how are you' And the number n is 2 Then the output should be − const output = 'hhooww aarree yyoouu' Example Following is the code − const str = 'how are you'; const repeatNTimes ...
Read MoreDifference between Session Storage and Local Storage in HTML5
HTML5 provides two web storage mechanisms: Session Storage and Local Storage. Both allow web applications to store data locally in the user's browser, but they differ in scope and persistence. Session Storage Session Storage is designed for scenarios where the user is carrying out a single transaction but could be carrying out multiple transactions in different windows at the same time. Data stored in Session Storage is only available for the duration of the page session. // Store data in Session Storage sessionStorage.setItem("username", "john_doe"); sessionStorage.setItem("theme", "dark"); // Retrieve data from Session Storage console.log("Username:", sessionStorage.getItem("username")); ...
Read MoreFinding missing letter in a string - JavaScript
We have a string of length m that contains first m letters of the English alphabets, but somehow, one element went missing from the string. So, now the string contains, m-1 letters We are required to write a function that takes in one such string and returns the missing element from the string. Example Following is the code − const str = "acdghfbekj"; const missingCharacter = str => { // to make the function more consistent const s = str.toLowerCase(); ...
Read MoreHow can I handle Server-Sent Events in HTML5?
Server-Sent Events (SSE) in HTML5 allow web pages to receive automatic updates from a server. This creates a persistent connection where the server can push data to the client in real-time. What are Server-Sent Events? Server-Sent Events provide a way for a server to send data to a web page automatically. Unlike WebSockets, SSE is unidirectional - only the server can send data to the client. Creating an EventSource Connection Use the EventSource object to establish a connection to the server: Server-Sent Events Demo ...
Read MoreHow to send a cross-document message with HTML?
Cross-document messaging allows secure communication between different browsing contexts (windows, iframes, or tabs) using the postMessage() API, even when they have different origins. Syntax targetWindow.postMessage(message, targetOrigin); Parameters message − The data to send (string, object, or any serializable value) targetOrigin − The origin URL of the target window (use "*" for any origin, but specify for security) Example: Sending Message from Parent to Iframe Parent window HTML: Send Message to Iframe ...
Read MoreOdd even sort in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Then the output should be − const output = [2, 2, 6, 8, 1, 5, 7, 9]; Approach The solution uses a custom comparator function that: Places even numbers ...
Read More