Nested element position within a resizable div being influenced by viewport dimensions

Within the application I developed, using Vue and OpenLayers for creating animations, an img element with position: relative is positioned at the top right corner of a canvas element with resize: both; overflow: auto;. The issue (CodePen example) arises when this element is utilized in creating an animation loop where at each iteration, the canvas element functions as a frame that gets updated and rendered. During each step, a new canvas is composed by combining the current canvas element and another hidden img tag (with similar properties to the overlaying canvas). This hidden img tag is then accessed using .getElementById(...) and drawn onto the frame canvas using .getContext("2d").drawImage(...). Strangely enough, the img element tends to move towards the center of the canvas element under certain circumstances such as resizing the viewport or utilizing multiple screens with uncommon aspect ratios, despite all elements being fixed in size before the loop begins.

The question at hand is what could be influencing the calculated widths determining the position of the img element (as shown in the JS code) that is linked to multi-screen setups, viewports, or other factors, while performing perfectly on more standard configurations?

if (showLegendFlag === true) {
  const mapLegend = document.getElementById("mapLegend");
  mapCnv
    .getContext("2d")
    .drawImage(
      mapLegend,
      mapCnv.width - mapLegend.width,
      0,
      mapLegend.width,
      mapLegend.height
    ); // drawImage(image, dx, dy, dWidth, dHeight)
}
<div id="map" class="map">
  <img id="legendMapOverlay" src="https://geo.weather.gc.ca/geomet?version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=GDPS.CONV_KINDEX.PT3H&format=image/png&STYLE=KINDEX&lang=en"/>
</div>

<img id="mapLegend" :src="mapLegendURL" style="display: none;" v-if="displayMapLegend" crossorigin="anonymous" />

https://i.sstatic.net/7vTPv.jpg

Answer №1

OpenLayers may apply a transform to the canvas based on the device pixel ratio. For this issue, there are two possible solutions:

Method 1 Include pixelRatio: 1, in the ol.Map options to override the device pixel ratio and eliminate the need for transformation.

Method 2 Before drawing the image, reset the transform while using save() and restore() around the function calls.

const mapCtx = mapCnv.getContext("2d")
mapCtx.save();
mapCtx.resetTransform()
mapCtx.drawImage(
  mapLegend,
  mapCnv.width - mapLegend.width,
  0,
  mapLegend.width,
  mapLegend.height
);
mapCtx.restore();

Please be aware that with OpenLayers versions 6 and 7, there is no longer a single map canvas, so this method may not be effective.

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

Space around the flex container

I am facing an issue with a flex display that splits the screen into two sections: one for login information and the other for a background picture. When setting up the flex layout, I notice unwanted margins on both sides (highlighted as orange bars in the ...

Ensure that text is justified to the left both above and below inline elements contained within the parent div

I am attempting to align text, generated dynamically, on the left side of some images that are positioned next to each other. The image on the far left should also be aligned to the left. .inline-attachments { display:inline; vertical-align:top; } ...

Run an anonymous function when the page is loaded

When it comes to loading a JavaScript function upon page load, there are various methods such as onLoad(), $( document ).ready(), and more. However, I am facing an issue with a function that does not have a specific name. This unnamed function is used to ...

Discover the method used to determine the vertical dimensions of a button

Check out my code snippet (here): <div class="modal-body"> <div class="container"> <div class="row"> <div class="col-8 p-0"> <form class="form-group"> <form> <input type="t ...

Preserving the "height" declaration in jQuery post-Ajax request (adjusting height after dropdown selection loads product information, resetting heights of other page elements)

Looking for a way to set a height on product descriptions using jQuery? Check out the solution below: https://www.example.com/product-example Here is the code snippet that can help you achieve this feature: $(document).ready(function() { var $dscr = $ ...

Issues arise when attempting to affix the navigation bar to the top of the webpage

After running the code below, I noticed that my navbar is not behaving as expected and seems to be stuck in the middle of the screen instead of being fixed to the top. This is confusing for me because I have created navbars before that worked fine. Any ass ...

Looking to modify the contents of a shopping cart by utilizing javascript/jQuery to add or remove items?

I'm dealing with a challenge on my assignment. I've been tasked with creating a Shopping Cart using Javascript, HTML5, and JQuery. It needs to collect all the items from the shop inside an Array. While I believe I have most of it figured out, I a ...

Can you explain the distinction between Declared Values and Specified Values?

I found myself confused by the distinction between Declared Values and Specified Values. Initially, I believed that the Declared Values were set by the author. These values then filter down to a single value, known as the "winning value", which becomes t ...

Creating an image dynamically at runtime from a section of HTML code as it appears in the browser

Is there a way to use Java API or Jquery library to create an image from input HTML code? Alternatively, how can I capture a screenshot of a section of HTML code as it appears in the browser? For example: If I have the following HTML code: <h1>Logo& ...

Disable form input fields while keeping all other elements enabled

Currently, I am facing a dilemma where I must deactivate specific input fields within a form. Given that there are numerous fields to consider, individually disabling each one seems impractical. Can anyone offer advice on how to disable a set of elements ...

Learn how to uninstall Vue.js from your Laravel project and seamlessly integrate React.js into your Laravel workflow

Did you know that in Laravel, you can easily replace Vue.js with React.js using just one command? To do this, simply open Git Bash and enter the following command: $php artisan preset React ...

Rewrite: "Rewriting URL POST requests in Node.js

Within my JavaScript file, I have this function: exports.authentication = function(req, res) { // .. // user validation // .. res.redirect('/login'); }; I'm looking to modify all POST requests to a different path, such as / ...

The challenge of navigating CSS specificity guidelines

In my CSS file, I have defined two styles for a table with the class "myTable". The first style sets the background color of header cells to gray, while the second style sets the background color of odd rows to blue: .myTable th { background-color: gr ...

Having difficulty saving data in database using Ajax request from a Chrome extension

I am currently working on an extension that captures the page URL and saves it in a database once the user clicks the submit button. However, I have encountered a roadblock and believe there might be a missing piece in the extension setup that I cannot pi ...

Expanding the functionality of Three.js classes

I need help figuring out how to extend the Object3D class in Three.js. I've been trying to follow a Stackoverflow question on this topic, but despite multiple attempts, I haven't been successful. Is there a way to extend a ThreeJS object? If a ...

What is the best way to position buttons at the bottom of a material ui list and checkboxes at the top of the list?

Struggling with my CSS skills while working on a React TODO app using Material-UI. I need help in displaying TODO items in a Material-UI list format - where the list should have a checkbox as the first item, the TODO text as the second item, and edit/delet ...

Having issues with the functionality of jQuery. It needs to be able to override existing

I am trying to make it so that if yes2 == none, the element with class .bottomandroid is removed and instead, the element with class .cta is displayed. However, it doesn't seem to be working as expected. How can I fix this issue? else if (isAndroid) ...

Should all images be set to 100% width with a standard Bootstrap configuration?

For my website project, I decided to incorporate the Bootstrap carousel into the header using version 3.0.3 of Bootstrap. Everything seemed to be functioning correctly until I noticed a strange issue with the image sizes on the page. Initially, all the ima ...

What causes the parent and grandparent elements to shift when adjusting the margin of a header tag?

I'm facing an issue with the h1 tag where changing its margin-top property also affects the parent and grandparent elements. Can someone shed light on why this is happening and provide a solution? If you observe, setting the h1 margin to 0 auto creat ...

Nesting functionality in TailwindCSS is currently experiencing issues when used in conjunction with tailwindcss/nesting or postcss

PostCSS Configuration module.exports = { plugins: { "postcss-import": {}, "tailwindcss/nesting": {}, tailwindcss: {}, autoprefixer: {}, }, }; Global CSS Styles .form-control { @apply w-full px-3; & p { @ ...