Tips for arranging elements in proper order following a rotation

Having trouble aligning rotated divs? Let's say we rotate .straight by 30deg, and now we want to find the new offset coordinates of its bottom right corner. This way, we can perfectly match up the bottom left corners of .curve with this new coordinate.

To calculate the offset coordinates of the bottom right corner of .straight, the following code is used:

var straight_width = $(".straight").width();
var br_corner_x = straight_width*Math.cos(30*Math.PI/180); //60.62
var br_corner_y = straight_width*Math.sin(30*Math.PI/180); //35

While this method works well, there is a problem when setting the left and top of .curve using these values:

$(".curve").css({"left": br_corner_x, "top": br_corner_y})

The browser rounds the pixels, resulting in left: 61px and leaving a small gap between the two divs. Although it may seem insignificant, for specific purposes, this difference matters.

For a demonstration of this issue, refer to the following fiddle: https://jsfiddle.net/39q5m71u/2/

If rotating a wrapper containing both divs or adjusting the transform-origin coordinates won't work due to handling multiple rotated divs with varying angles, what alternative approach can be taken to align the divs after rotation?

Answer №1

The issue at hand is due to the browser performing pixel rounding on the left property:

<div class="curve" style="left: 60.6218px;top: 35px;"></div>

To eliminate the gap, manually adjusting the value of left to something like 60.49 resolves the issue.

I successfully closed the gap by rounding down the value of br_corner_x to the nearest whole number:

var br_corner_x = Math.floor(straight_width*Math.cos(30*Math.PI/180));

To address some of the rough edges, I enclosed both existing divs in a .container div and included the following CSS:

.container {
  backface-visibility: hidden;
}

https://i.stack.imgur.com/ln9H9.png

https://jsfiddle.net/ytzw2bzf/2/

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

My Node.js update query is not retrieving the correct values

Having some trouble with an update query on my nodejs server. It seems to be functioning properly, but I'm only getting the ID and not the values. Can't seem to figure out how to fix it. Any help would be appreciated! Server-side: app.post(&apo ...

Sort Messages By Date Created

While using Vue, I encountered a general JavaScript question. I am fetching cat messages from an API for a chat interface. The initial call returns an array of objects where each object represents a chat message: data: [ {id: 1, created_at: "2022-05 ...

Encountered an issue while attempting to make a GET request using the fetch API

Currently, I am attempting to retrieve some data from the server using react.js and the fetch API. However, I keep encountering this error: SyntaxError: Unexpected token < in JSON at position 0. This is the code snippet I am using to fetch the data: ...

Why does the onBlur event function in Chrome but fails to work in Safari?

I've encountered a problem with the onBlur event in react-typescript. To replicate the issue, I clicked the upButton repeatedly to increase the number of nights to 9 or more, which is the maximum allowed. Upon further clicking the upButton, an error m ...

Tips on appending a parameter to an image URL using JavaScript

On my website, I am able to insert images with specified width and height using this code: <img src="image.php?w=100&h=100"> I would like the image size to change based on the device screen width. For screens smaller than 600px, I want to displa ...

Changing the disabled textfield colour in Material-UI

I am looking to enhance the visibility of disabled text in a textfield by making it darker than the current light color: I have attempted to achieve this using the following code snippet: import { withStyles } from '@material-ui/styles'; import ...

What is the best way to incorporate a search feature within Bootstrap cards?

I've been struggling to incorporate a search feature within my bootstrap cards. Despite trying various online methods, none have proven successful for me so far. Below is my HTML code - any assistance in implementing a search filter into my app would ...

Exploring each item within oData: A guide

After writing the code statement above, I am able to see the attached image. Now, my challenge is accessing the "label" property inside each object. How can I iterate through these objects and retrieve their "label" properties? item.getModel().oData; I a ...

What is the solution to the question: "Hey there, time traveler. We are currently in an era where CSS prefixes are no longer necessary, thanks to the advances in prefix-less CSS

I'm having an issue in my Next.JS app with importing my stylesheet in _app.js. Here is how I currently import it: import '../public/css/Index.css'; The content of Index.css looks like this: .index-container { margin: 20px auto 0; } Wha ...

To activate a function, simply click anywhere on the body: instruction

One of my latest projects involved creating a directive that allows users to click on a word and edit it in a text box. Once edited, clicking anywhere on the body should return the word back to its updated form. html <div markdown>bineesh</div& ...

Tips for modifying the href attribute when a user clicks

Is there a way to dynamically change the value of this link <a href="/Countries/388/10">next</a> without having to refresh the page? For example, if a user clicks on the link, it should update to <a href="/Countries/388/20">next</a&g ...

How can parameters be included in ng-href when using ng-click?

So I have this list of products and I want to pass the current product id when clicking on ng-href by using a function with ng-click in the ng-href. This is what my html file looks like: <div class="agile_top_brands_grids" ng-model="products" ng-repe ...

The project is not being recognized by 'webpack' when running within it

Every time I attempt to execute 'webpack' in my project, the command line shows me this error message: 'webpack' is not recognized as an internal or external command, operable program or batch file. I have installed webpack using th ...

Library of CSS styling information

Has anyone come across a reliable javascript library that can retrieve the original style (not computed) of a specific element in the DOM? Essentially, I am looking for something that can provide results similar to what is displayed in Firebug's style ...

When using jQuery to select elements of a specific class, make sure to exclude the element that triggered the

A dynamic number of divs are generated from a data source. Each div contains an image button and another div with text. While the actual scenario is more complex, we can present a simplified version: <div id="main"> <div id="content_block_1" ...

Discovering all class names following the same naming convention and storing them in an array through Javascript

Hey everyone, I could use some assistance with a coding challenge. I'm aiming to extract all class names from the DOM that share a common naming convention and store them in an array. For instance: <div class="userName_342">John</div> & ...

Selecting items from a list at random using the power of math.random() and math.floor()

When attempting to randomize the popping of elements in a list stored in the baby variable, only baby[1] is being popped. How can I make it pop random elements? <body> <h1 id="1">bebe</h1> <h1 id="2">t ...

Activate on click using JavaScript

When a link with the class .active is clicked, I want to use a JavaScript function to deactivate any other active links. The structure of the code should be as follows: <ul class="product"> <li><a href="#myanmar" class="active">Mya ...

Implement a jQuery loading animation triggered by scrolling down the page

Can anyone offer guidance on how to trigger an animation as you scroll down a webpage? I've come across this feature while browsing through this website: I would love to include code examples, but I'm unsure of where to start with implementing t ...

Include the component with the function getStaticProps loaded

I am currently working on a project using NextJs and I have created a component to load dynamic data. The component works fine when accessed via localhost:3000/faq, but I encounter an error when trying to import the same component into index.js. It seems l ...