CSS resizing causing delays in canvas mouse interactions

Currently, I am engaging in a project to enhance my knowledge of node.js and html canvases.

Within my project, there is a canvas that I wish to maintain at a fixed bitmap size while occupying its containing div and preserving its aspect ratio.

I have set the canvas element size to 500x500, and then applied the following CSS style:

canvas {
  display: block;
  width:100%;
  height:100%;
  object-fit: contain;
  background-color: lightgrey;
}

Initially, within the JavaScript code, I fill the canvas with white color resulting in an image like the following:

https://i.sstatic.net/eI4F7.png

By capturing mouse events, I draw lines on the canvas using a function that correctly scales the events on the canvas:

function getMousePos(evt) {
  var rect = canvas.getBoundingClientRect();

  var raw_x = evt.clientX || evt.touches[0].clientX;
  var raw_y = evt.clientY || evt.touches[0].clientY;

  var min_dimension = Math.min(rect.width, rect.height);
  var x_offset = 0.5 * (rect.width - min_dimension);
  var y_offset = 0.5 * (rect.height - min_dimension);

  return {
    x: ((raw_x - rect.left - x_offset) / min_dimension) * canvas.width,
    y: ((raw_y - rect.top - y_offset) / min_dimension) * canvas.height
  }
}

Although this technique works, I noticed that when drawing on the canvas and moving the mouse over a specific band on the right side, the updates are delayed until the mouse exits the band. The size of this band matches the space on the left side of the canvas, suggesting a connection that I'm unsure how to explore further. Interestingly, I do not encounter any issues when resizing the window to eliminate spaces around the canvas (enhancing performance significantly). You can view a clearer demonstration through the gif below:

https://i.sstatic.net/Zg4Qq.gif

If you have any suggestions on what might be causing this behavior or a more efficient method to achieve the same outcome, please share your insights.

Note: My browser version is Chrome 80.0.3987.149

Answer №1

After extensive searching, I was unable to locate a satisfactory resolution to this issue other than implementing a JavaScript function that adjusts the element size when the window is resized.

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

Is there a way to create an input field that accepts only numbers and behaves like a password field simultaneously?

I am attempting to develop an input field for entering a PIN. I would like the PIN to be masked on mobile devices, similar to how passwords are obscured in password input fields. I came across a suggestion on stackoverflow regarding this, but unfortunately ...

Exploring the wonders of Onsen UI's ng-repeat feature combined

Facing a major issue with my app, and I suspect it's due to the asynchronous call. The HTML section looks like this: <ons-list ng-repeat="item in newsEventi.items"> <ons-list-header class="news_titolo_lista">{{item.categoria}}</ons ...

When I checked the error kind in MongoDB, I encountered an undefined value

After entering a valid ID following /user/ like 'http://localhost:5000/api/profile/user/5ee9fbe0e82023146c8a5230', the expected result is returned. However, in cases where an invalid ID is provided, I need to identify the type of error. To test t ...

When selecting a dropdown in Angular 5, the aria-expanded attribute remains unchanged and the 'show' class is not applied. This behavior is specific to Bootstrap dropdowns

Having an issue with the bootstrap dropdown in my Angular project. When I click on it, the dropdown-menu does not show up. The 'show' class is not being added to the dropdown and the 'aria-expanded="false"' attribute does not change to ...

Requires a minimum of two page refreshes to successfully load

Our website is currently hosted on Firebase. However, there seems to be an issue as we have to refresh the website at least twice in order for it to load when visiting www.website.com. Update: We are unsure of what could be causing this problem. W ...

Tips for utilizing jquery.load for fetching a section of an html document or an entire html file

I'm experimenting with jquery's load function to dynamically fetch and display HTML content, rather than using $.ajax(). I enjoy exploring the various features that jQuery offers and want to understand how each one works. Below is the code I am ...

Is there a way to resolve the issue of my CSS menu dropdown getting blocked by the jQuery slideshow on my website?

Hey there, I'm currently assisting a friend in the development of a website, but I am quite new to this myself. Any advice or tips would be greatly appreciated. The website is being built using asp.net. My sub-menu is getting blocked by images. Is th ...

Ways to retrieve the date of the chosen <td> cell in a calendar

Looking for a way to extract dates from the <td> elements selected by mouse? Here is my code snippet that highlights the TD upon selection: $(function () { var isMouseDown = false, isHighlighted; $("#schichtplan td") .moused ...

A JavaScript function that reverses content within a specified range

As I adjust the values in the number input boxes, the "total" should change based on whether the value increases or decreases (it's deducting from the total points). However, when toggling between "9" and "10", it behaves the opposite way (lowering t ...

Utilizing CSS styling in PHP as a functional approach

In my journey of learning CSS, HTML5, and jQuery, I have found a unique way to organize my CSS code into different chapters. I simply call them into the head section when needed. This method allows me to have only the necessary CSS on each page, making it ...

Ways to alter the color of an icon when hovering over it

Is there a way to change the color of a material icon inside an IconButton material component when hovering over the IconButton? I've tried adding a class directly to the icon, but it only works when hovering over the icon itself and not the IconButto ...

Having trouble with Javascript functionality while testing wkhtmltopdf in the terminal

When using the PDF generator wkhtmltopdf (ver. 0.12.5), I am encountering an issue where it does not display any results with JavaScript. I have been attempting to get the PDF generator wkhtmltopdf to work on my Mac. It successfully renders the PDF with s ...

Is there a way to create a new prettyphoto window by clicking on a link within the original prettyphoto window?

I have an HTML table that is dynamically constructed on the server side using AJAX. The table is displayed using prettyphoto, everything works fine up to this point. However, the last column of the table contains a link that should open an image using pret ...

Basic HTML code that displays either one or two columns based on the width of the screen

My monitoring website displays dynamic rrd graphs in png format with fixed dimensions. I am looking to adjust the layout based on browser window size - showing the graphs in 1 column if the width is below a certain threshold, and in 2 columns if it exceed ...

Is it possible to access a function on the main page from a plugin?

In my plugin, I need to call a function that is located on the main page, not within the plugin's .js file. EDIT I am currently working with jQuery to parse a very large XML file and generate a substantial list (resulting in a 1.1 MB HTML file when ...

The use of *ngFor in Angular allows for a form array to be dynamically bound, enabling users to input one character at a time

This field is connected to the formBuilder input interests: formBuilder.array([ ['', Validators.required], ]) It seems like there may be an issue with how my form is linked in the template. Is this method incorrect? How should a Form Array ...

angular js sorting JSON objects by ID

I am having trouble sorting and displaying data with AngularJS. I have added a sort option to my table, but it does not seem to be working properly. Could you please review my JSON data? [ { "id":143, "companyName":"XC", "dividendIn ...

How can one update transitive dependencies to a specific newer version in npm to address CVE vulnerabilities?

Here are the packages that require upgrading. I attempted to update the version and signature of the packages listed in package-lock.json. However, whenever I run npm i after modifying package-lock.json, the changes made to package-lock.json disappear. ...

What are some techniques for locating an element in Selenium when it has no tags?

I'm facing a challenge in my code where I need to ensure that the message "Product successfully added to your shopping cart" appears after adding an item to the cart. Here, there is no specific tag enclosing the text, making it tricky to target for ve ...

Transferring data from the backend to the frontend in Node.js through Express

How can I pass the IP variable retrieved from request.headers['x-forwarded-for'] in my server.js file to display it on my webpage index.html? I've attempted passing it within the <script> tag and even inside the <a> tag for immed ...