Javascript Articles

Page 378 of 534

Two way communication between the browsing contexts in HTML5

Rishi Rathor
Rishi Rathor
Updated on 15-Mar-2026 425 Views

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 More

Features detected by Modernizr

Jennifer Nicholas
Jennifer Nicholas
Updated on 15-Mar-2026 649 Views

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 More

Layers of canvas fabric.js

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 1K+ Views

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 More

Digit distance of two numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

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 More

Update HTML5 canvas rectangle on hover

Daniol Thomas
Daniol Thomas
Updated on 15-Mar-2026 691 Views

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 More

JavaScript - Find the smallest n digit number or greater

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

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 More

How to adopt a flex div's width to content in HTML?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 15-Mar-2026 353 Views

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 More

Determining rightness of a triangle – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

How to keep the image at the back of the HTML5 canvas when moving elements with fabric.js?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 403 Views

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 More

Strictly increasing or decreasing array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 486 Views

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
Showing 3771–3780 of 5,340 articles
« Prev 1 376 377 378 379 380 534 Next »
Advertisements