Javascript Articles

Page 362 of 534

Measure text height on an HTML5 canvas element

George John
George John
Updated on 15-Mar-2026 2K+ Views

To measure text height on an HTML5 canvas element, you can extract the font size from the canvas context's font property or use the TextMetrics object for more precise measurements. Method 1: Extracting Font Size from Font Property Set the font size in points and extract the numeric value: const canvas = document.getElementById('myCanvas'); const context = canvas.getContext('2d'); // Set font in points context.font = "15pt Calibri"; // Extract height using regex to match digits var height = parseInt(context.font.match(/\d+/), 10); console.log("Text height:", height, "points"); Text height: 15 ...

Read More

Add object to array in JavaScript if name does not already exist?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 994 Views

To add objects to an array only if they don't already exist, you can use push() combined with find() to check for duplicates. This prevents adding duplicate entries based on specific criteria. Basic Example Here's how to add objects to an array if the name property doesn't already exist: var details = [{name: "John"}, {name: "David"}]; var addObject = ["Mike", "Sam", "John"]; // "John" already exists addObject.forEach(obj1 => { if (!details.find(obj2 => obj2.name === obj1)) { details.push({name: obj1}); } ...

Read More

Playing a WAV file on iOS Safari

Nancy Den
Nancy Den
Updated on 15-Mar-2026 567 Views

Playing WAV files on iOS Safari requires specific HTTP headers to ensure proper audio streaming and playback compatibility. Required Headers for iOS Safari iOS Safari needs these HTTP headers to properly handle WAV file playback: Content-Range: bytes 0-1023/2048 Content-Type: audio/wav Accept-Ranges: bytes Content-Length: 2048 Complete Server Implementation Here's a Node.js example showing how to serve WAV files with proper headers: const express = require('express'); const fs = require('fs'); const path = require('path'); const app = express(); app.get('/audio/:filename', (req, res) => { const filename = req.params.filename; ...

Read More

How to ignore using variable name as a literal while using push() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 289 Views

To avoid using a variable name as a literal string when pushing objects to an array, use square brackets [variableName] for computed property names. This allows the variable's value to become the property key. The Problem with Literal Property Names When you use name: value in an object, "name" becomes a literal string property, not the variable's value: var name = "David"; var data = []; // This creates a property literally named "name" data.push({ name: "This will always be 'name' property" }); console.log(data); [ { name: 'This will always be 'name' ...

Read More

How can I make a div with irregular shapes with CSS3 and HTM5?

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

With CSS3, you can create various shapes including rectangles, triangles, circles, and more complex irregular shapes using properties like border-radius, transform, and clip-path. Basic Shapes Rectangle A simple rectangle is created using width and height properties: #rectangle { width: 300px; height: 150px; background: blue; } Triangle Triangles are created using border properties with transparent sides: #triangle { width: 0; height: 0; ...

Read More

Make first letter of a string uppercase in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 487 Views

To make first letter of a string uppercase, use toUpperCase() in JavaScript. With that, we will use charAt(0) since we need to only capitalize the 1st letter. Syntax string.charAt(0).toUpperCase() + string.slice(1) Example function replaceWithTheCapitalLetter(values){ return values.charAt(0).toUpperCase() + values.slice(1); } var word = "javascript" console.log(replaceWithTheCapitalLetter(word)); Javascript Multiple Examples function capitalizeFirst(str) { return str.charAt(0).toUpperCase() + str.slice(1); } console.log(capitalizeFirst("hello world")); console.log(capitalizeFirst("programming")); console.log(capitalizeFirst("a")); console.log(capitalizeFirst("")); Hello world Programming A How It Works The method breaks ...

Read More

Touchmove pointer-events: none CSS does not work on Chrome for Android 4.4 / ChromeView

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

The pointer-events: none CSS property doesn't work properly on Chrome for Android 4.4 (ChromeView) for touchmove events. This causes issues when trying to disable touch interactions on overlay elements. The Problem On Chrome for Android 4.4, elements with pointer-events: none still receive touchmove events, breaking the expected behavior where these elements should be non-interactive. Solution: Using touchstart with preventDefault() Instead of relying on CSS pointer-events: none, use JavaScript to handle and prevent touch events: Overlay Content const overlay = document.getElementById('overlay'); // Prevent all touch interactions overlay.addEventListener('touchstart', function(ev) ...

Read More

How to divide an unknown integer into a given number of even parts using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 451 Views

When dividing an integer into equal parts, you may need to distribute the remainder across some parts to ensure all values are used. This can be achieved using the modular operator and array manipulation. How It Works The algorithm divides the integer by the number of parts to get a base value. If there's a remainder, it distributes the extra values by adding 1 to some parts. Example function divideInteger(value, parts) { var baseValue; var remainder = value % parts; ...

Read More

Playing MP4 video in WebView using HTML5 in Android

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 1K+ Views

To play MP4 videos in Android WebView using HTML5, you need to enable JavaScript, DOM storage, and configure proper video handling. This requires both Android configuration and HTML5 video implementation. Android WebView Configuration First, configure your WebView settings to support HTML5 video playback: // Enable JavaScript and DOM storage WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setAllowFileAccess(true); webSettings.setAllowContentAccess(true); webSettings.setMediaPlaybackRequiresUserGesture(false); // Enable hardware acceleration webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); HTML5 Video Implementation Create an HTML page with HTML5 video element to play MP4 files: ...

Read More

In JavaScript, can be use a new line in console.log?

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

Yes, you can use newlines in console.log() using the escape character. This creates line breaks in console output. Basic Newline Usage console.log("First lineSecond lineThird line"); First line Second line Third line Multiple Ways to Add Newlines There are several approaches to include newlines in console output: // Method 1: Using escape character console.log("HelloWorld"); // Method 2: Multiple console.log() calls console.log("Hello"); console.log("World"); // Method 3: Template literals with actual line breaks console.log(`Hello World`); // Method 4: Combining text with newlines console.log("Name: JohnAge: 25City: New York"); ...

Read More
Showing 3611–3620 of 5,340 articles
« Prev 1 360 361 362 363 364 534 Next »
Advertisements