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 378 of 534
Two way communication between the browsing contexts in HTML5
Two-way communication between browsing contexts in HTML5 is achieved through Channel Messaging API. This allows secure communication between different origins, iframes, web workers, and windows. The MessageChannel creates two connected ports that can send messages to each other. When you create a MessageChannel, it internally generates two ports - one for sending data and another that gets forwarded to the target browsing context. Key Methods postMessage() − Posts the message through the channel start() − Starts listening for messages on the port close() − Closes the communication ports Basic Syntax // Create ...
Read MoreFeatures detected by Modernizr
Modernizr is a JavaScript library that detects HTML5 and CSS3 features in browsers. It provides both CSS classes and JavaScript properties to help developers create progressive enhancement strategies. How Feature Detection Works Modernizr adds CSS classes to the element and creates JavaScript properties on the Modernizr object. This allows you to apply styles or run scripts only when features are supported. // Check if canvas is supported if (Modernizr.canvas) { console.log("Canvas is supported!"); } else { console.log("Canvas not supported - provide fallback"); } Complete Feature ...
Read MoreLayers of canvas fabric.js
Fabric.js provides several methods to control the layering (z-index) of objects on the canvas. Understanding these methods is crucial for managing object visibility and creating complex compositions. Basic Layer Control Methods Fabric.js offers four primary methods to manipulate object layers: canvas.sendBackwards(myObject) // Move one layer back canvas.sendToBack(myObject) // Send to bottom layer canvas.bringForward(myObject) // Move one layer forward canvas.bringToFront(myObject) // Bring to top layer Example: Layer Manipulation ...
Read MoreDigit distance of two numbers - JavaScript
We are required to write a JavaScript function that takes in two numbers and returns their digit distance. What is Digit Distance? The digit distance of two numbers is the absolute sum of the difference between their corresponding digits from left to right. For example, if the numbers are: 345 678 Then the digit distance calculation will be: |3-6| + |4-7| + |5-8| = 3 + 3 + 3 = 9 Implementation Here's how to calculate digit distance in JavaScript: const num1 = 345; const num2 ...
Read MoreUpdate HTML5 canvas rectangle on hover
To update HTML5 canvas rectangle on hover, you need to handle mouse events and redraw the canvas. Here's how to create a rectangle that changes color when you hover over it. Basic Setup First, create a canvas element and get its context: var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); // Draw initial rectangle function drawRectangle(fillColor) { context.clearRect(0, 0, canvas.width, canvas.height); context.rect(20, 20, 150, 100); context.strokeStyle = "black"; context.stroke(); ...
Read MoreJavaScript - Find the smallest n digit number or greater
We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n digit number which is a multiple of all the elements specified in the array. If there exist no such n digit element then we should return the smallest such element. For example: If the array is: const arr = [12, 4, 5, 10, 9] For both n = 2 and n = 3, the output should be 180 Example Following ...
Read MoreHow to adopt a flex div's width to content in HTML?
By default, flex containers with display: flex take up the full width of their parent. To make a flex container shrink to fit its content, use display: inline-flex. The Problem with display: flex Regular flex containers expand to fill their parent's width, even when content is smaller: .flex-container { display: flex; padding: 10px; background-color: lightblue; border: 2px solid blue; margin: 10px 0; } ...
Read MoreDetermining rightness of a triangle – JavaScript
We are required to write a JavaScript function that takes in three numbers say a, b and c representing the length of three sides of a triangle. The function should return true if those three sides represent a right-angle triangle, false otherwise. Right Angle Triangle A triangle is a right-angle triangle if one of the three angles in the triangle is 90 degrees. And one angle in the triangle is 90 degrees when the square of the longest side is equal to the sum of squares of the other two sides. This is known as the Pythagorean ...
Read MoreHow to keep the image at the back of the HTML5 canvas when moving elements with fabric.js?
When working with fabric.js, you might want to keep a background image fixed while allowing other elements to move freely on top. By default, fabric.js may reorder objects during interactions, potentially bringing background elements to the front. The Solution: preserveObjectStacking To maintain the proper layering order, use the preserveObjectStacking property when creating your fabric.js canvas: // Create canvas with preserveObjectStacking enabled ...
Read MoreStrictly increasing or decreasing array - JavaScript
In Mathematics, a strictly increasing function is one where values always increase, while a strictly decreasing function has values that always decrease. In JavaScript, we can check if an array follows either pattern. We need to write a function that takes an array of numbers and returns true if it's either strictly increasing or strictly decreasing, otherwise returns false. Understanding the Logic The key insight is to check if consecutive elements maintain the same slope (all increasing or all decreasing). We use a helper function sameSlope that compares three consecutive numbers to ensure they follow the same ...
Read More