Keeping the Aspect Ratio of HTML5 Video in Sync with Div Dimensions

Using react with typescript, my project involves JSON data containing information about faces detected on a video. I am currently working on creating bounding boxes around the faces in the video; however, I have encountered an issue. The bounding box information is based on a video resolution of 3840 x 2160, but the div I am working with has a height of 500px and width of 800px, utilizing the object-fill property for filling. As a result, when I render the bounding boxes on the video, they extend beyond the div.

How can I adjust this to ensure that the bounding boxes are accurately positioned within the div?

A snippet of the sample JSON data:

[
  { "frame": 2, "x": 2943, "y": 503, "width": 266, "height": 354 }

]

Here is a portion of my code:

<div ref={divRef}>
        <Video videoSource={source} ref={vRef}/>
        <svg id="svg" ref={sRef}>
        }
        </svg>
      </div>

Answer №1

It appears that a simple math problem may be the solution here, but I could be overlooking something.

1) Determine the dimensions of the video container (500 X 800). 2) Calculate how much smaller it is compared to the original size (3840 X 2160).

small video width / big video width = X ratio
    
small video height / big video height = Y ratio

Multiply these ratios with X and Y values to locate the correct position on the smaller video.

For frame 0, X should be: Old X * X aspect ratio

For frame 0, Y should be: Old Y * Y aspect ratio

Apply the same principle to width and height. Iterate through the JSON object to generate a new object with desired values and display it using SVG mapping.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Does applying a CSS class to a <b> element adhere to proper HTML/CSS guidelines?

Is it considered valid HTML/CSS to apply a CSS class to a <b> tag? For instance, is the following code acceptable: <b class="myclass"> Foo Bar </b> Is this appropriate in terms of HTML/CSS standards? I am looking to assign a class t ...

Upon clicking the button, an event listener is attached to the window

I have an image with an onClick function attached to it. <img className="pointer pipette-icon" src={pipetteIcon} alt="" onClick={handlePipetteClick} /> My objective is as follows: when clicking on this image, nothing should happe ...

Is there a way to adjust the border color of my <select> element without disrupting the existing bootstrap CSS styling?

In my Bootstrap (v4.5.3) form, the select options appear differently in Firefox as shown in the image below: https://i.sstatic.net/3suke.png Upon form submission, I have JavaScript code for validation which checks if an option other than "-- Select an Op ...

Frequently, cypress encounters difficulty accessing the /auth page and struggles to locate the necessary id or class

When trying to navigate to the /auth path and log in with Cypress, I am using the following code: Cypress.Commands.add('login', (email, password) => { cy.get('.auth').find('.login').should(($login) => { expect($log ...

Incorporate an HTML form with ever-changing attribute values

I am currently working on a webpage that has an option to "add another" which will insert a fieldset with a title and three input boxes. Each input box needs to have a unique name for form submission, such as "field1_1" or "field1_2". I am aware that I ...

Comparing Jquery's smoothscroll feature with dynamic height implementation

Recently I launched my own website and incorporated a smoothscroll script. While everything seems to be working smoothly, I encountered an issue when trying to adjust the height of the header upon clicking on a menu item. My dilemma is as follows: It appe ...

Learn how to incorporate a HTML page into a DIV by selecting an Li element within a menu using the command 'include('menu.html')'

I have a website with both 'guest' and 'user' content. To handle the different menus for logged-in and logged-out users, I created menuIn.html and menuOut.html files. These menus are included in my MainPage.php using PHP logic as sugges ...

Alert! Server node encountered an issue while handling the response: process.nextTick(function(){throw err;});

Currently, I am working on a simple application to familiarize myself with Mongo, Express, and Node. An issue arises when I attempt to utilize res.json(docs) in the success conditional during the GET request, generating an error process.nextTick(function( ...

Tips for utilizing a dropdown menu in Angular

<div> <label> Categories </label> <select> <option *ngFor = "let category of categories" [value] = "category" (click) = "onCategorySelected( category.name )"> ...

How to align navbar elements of different sizes using only CSS

Apologies if this question has already been asked. I've searched for solutions to this problem, but they all involve Bootstrap or similar frameworks, not pure CSS. The issue I'm facing is with a navbar that contains three elements: https://i.sst ...

Challenge with Angular date selection component

I am currently utilizing ngbdatepicker in my form. I want to save the date separately and retrieve it as shown below. <input class="form-control ngbfield custom-form-control" required [ngClass]="{'is-invalid':f.touched && birthDay.inv ...

Creating a Next.js dynamic route that takes in a user-submitted URL for customization

Currently, I have implemented the Next.js Router to facilitate the display of different dashboards based on the URL slug. While this functionality works seamlessly when a button with the corresponding link is clicked (as the information is passed to the Ne ...

Create a new project in Express 4 with necessary dependencies that are reliant on having Express

Currently, I am working on a Node.js project that utilizes the latest version of express (4.3). However, I have come across a situation where one of my dependencies relies on express 3.3. Will this setup work or do I need to ensure all submodules are using ...

Passing the output of a function as an argument to another function within the same HTTP post request

I have a situation where I am working with two subparts in my app.post. The first part involves returning a string called customToken which I need to pass as a parameter into the second part of the process. I'm struggling with identifying where I m ...

Opening external stylesheets in a separate window

Objective: Create a new window, add elements to it, and then apply CSS styles to the elements in the new window using an external style sheet. Issue: While I am able to add elements to the new window and link it to an external style sheet in the head sect ...

Adjust the size of the logo picture

Hey there, I have a question that may seem a bit silly, but I'm just starting out with coding. I'm trying to resize and center my logo image (which is currently taking up the whole page in preview), but no matter what I try with CSS, nothing see ...

Sharing data across multiple paths

route.post('/register',function(req,res){ //completed registration process // token value assigned as 'abc' }) route.post('/verify',function(req,res){ // How can I retrieve the token ('abc') here? }) I' ...

Struggling to make a basic ajax request function correctly

I've been experimenting with this code snippet in my browser's console. $.ajax({ type: 'GET', url: 'http://stackoverflow.com/', dataType: 'html', success: function() { console.log("Yes, t ...

Best practices for including jQuery in ASP.NET (or any other external JavaScript libraries)

Can you explain the distinctions among these three code samples below? Is there a preferred method over the others, and if so, why? 1.Page.ClientScript.RegisterClientScriptInclude(typeof(demo), "jQuery", Re ...

Creating a variable amount of datasets depending on database values in an easy way

I am striving to create a dynamic graph in PHP using CanvasJS with multiple datasets generated based on values from a database. Generating the graph with only one dataset was successful, but making it "dynamic" has proven to be a challenge. For a single d ...