Javascript Articles

Page 384 of 534

Function that parses number embedded in strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 169 Views

JavaScript's built-in functions like parseInt() and parseFloat() only parse numbers from the beginning of a string, stopping when they encounter non-numeric characters. When you need to extract all digits from anywhere within a string, you need a custom solution. The Problem with Built-in Methods Standard parsing methods fail with embedded numbers: console.log(parseInt('454ffdg54hg53')); // 454 (stops at first non-digit) console.log(parseFloat('12.34abc56.78')); // 12.34 (stops at 'a') 454 12.34 Method 1: Loop Through Characters This approach iterates through each character, extracting only digits: const numStr = '454ffdg54hg53'; ...

Read More

Draw part of an image inside HTML5 canvas

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

Drawing part of an image in HTML5 canvas is useful for creating sprites, animations, or displaying specific portions of larger images. The key is using the extended drawImage() method with clipping parameters. Syntax context.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); Parameters Parameter Description sx, sy Starting coordinates in source image sWidth, sHeight Width and height of clipped area in source dx, dy Destination coordinates on canvas dWidth, dHeight Size to draw on canvas Example: Drawing Part of an Image ...

Read More

How to detect point on canvas after canvas rotation in HTML5

V Jyothi
V Jyothi
Updated on 15-Mar-2026 329 Views

When working with HTML5 canvas rotation, detecting the correct coordinates of points becomes challenging because the canvas coordinate system changes. This article shows how to create a transform class to handle point detection after canvas rotation. The Problem When you rotate a canvas, the coordinate system rotates with it. A point at (5, 6) on the original canvas will have different coordinates after rotation. You need to transform these coordinates to match the rotated canvas. Creating a Transform Class Here's a complete Transform class that handles canvas rotations and point transformations: ...

Read More

Matching strings for similar characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 666 Views

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false. Example Following is the code − const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { ...

Read More

Circle Collision Detection HTML5 Canvas

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

Circle collision detection in HTML5 Canvas involves checking whether two circles intersect by calculating the distance between their centers and comparing it to the sum of their radii. How Circle Collision Detection Works Two circles collide when the distance between their centers is less than or equal to the sum of their radii. If the distance is greater, they don't collide. distance r1 r2 Basic Formula The collision detection formula uses the Pythagorean theorem to calculate distance: distance = Math.sqrt((x2 - x1)² + (y2 - y1)²) collision = distance

Read More

HTML5 using src using raw binary data

Govinda Sai
Govinda Sai
Updated on 15-Mar-2026 1K+ Views

HTML5 allows you to embed binary data directly into src attributes using Data URLs. This is particularly useful when working with files stored in databases or when you need to display content without making separate HTTP requests. Data URL Syntax Data URLs follow this format: data:[mediatype][;base64], data Where: mediatype: MIME type (image/png, audio/mp3, etc.) base64: Optional encoding specification data: The actual binary data (base64 encoded if specified) Using Binary Data with Images For images, you can embed binary data directly in ...

Read More

Splitting last n digits of each value in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 159 Views

We have an array of numbers and need to extract the last n digits from each value. If a number has fewer than n digits, it remains unchanged. const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; console.log("Original array:", arr); Original array: [ 56768, 5465, 5467, 3, 878, 878, 34435, 78799 ] We need a function that takes an array and a number n, then returns the last n digits of each element. If an element has fewer than n digits, it stays the same. Example with n = 2 ...

Read More

Uniquely identify files before uploading with the HTML5 file API

Nitya Raut
Nitya Raut
Updated on 15-Mar-2026 267 Views

While making a file uploader using HTML5 file API, we want to be sure that no duplicate files are uploaded based on actual data. This prevents wasting storage space and bandwidth by uploading identical files multiple times. Calculating a hash with MD5 is not an efficient method as all that happens on the client side and is time-consuming. There is actually no perfect shortcut for this task. Method 1: Basic File Properties Check The simplest approach is to compare basic file properties like name, size, and last modified date: document.getElementById('fileInput').addEventListener('change', function(event) ...

Read More

Remove a FileList item from a multiple "input:file" in HTML5

V Jyothi
V Jyothi
Updated on 15-Mar-2026 2K+ Views

When working with HTML5 file inputs, you cannot directly modify the FileList object returned by input.files. The FileList is read-only, so removing items requires converting it to an array first. The Problem The FileList object from file inputs is immutable: Show Files Try Direct Removal function showFiles() { const files = document.getElementById('fileInput').files; document.getElementById('output').innerHTML = 'Files selected: ' + files.length; } function tryDirectRemoval() { const files = document.getElementById('fileInput').files; try { ...

Read More

HTML5 & JavaScript: resolution or size of

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

HTML5 provides two approaches for capturing photos from mobile devices with different levels of control over image resolution and size. HTML Media Capture The simplest approach uses HTML's capture attribute with accept="image/*" on input elements to access the device camera. HTML Media Capture Capture Photo with HTML document.getElementById('camera').addEventListener('change', function(e) { ...

Read More
Showing 3831–3840 of 5,340 articles
« Prev 1 382 383 384 385 386 534 Next »
Advertisements