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 362 of 534
Measure text height on an HTML5 canvas element
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 MoreAdd object to array in JavaScript if name does not already exist?
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 MorePlaying a WAV file on iOS Safari
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 MoreHow to ignore using variable name as a literal while using push() in JavaScript?
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 MoreHow can I make a div with irregular shapes with CSS3 and HTM5?
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 MoreMake first letter of a string uppercase in JavaScript?
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 MoreTouchmove pointer-events: none CSS does not work on Chrome for Android 4.4 / ChromeView
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 MoreHow to divide an unknown integer into a given number of even parts using JavaScript?
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 MorePlaying MP4 video in WebView using HTML5 in Android
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 MoreIn JavaScript, can be use a new line in console.log?
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