Determining the XY position of the wheel data

In my attempt to develop a lucky draw wheel using reactjs, I needed to position all the input data at specific XY coordinates. Below is an example of the expected output XY positions that I require.

var renderData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
React.createElement('div', { className: '_data'},
    renderData.map((index,item) => {
        var itemPosition = index / renderData.length * 360;
        var itemX = itemPosition * Math.PI/180;
        var itemY = itemPosition * Math.PI/180;
        return React.createElement('div', { className: '_items',
            style:{top:itemX,left:itemY}
        }, item);
    })
)

Using createElement, I created a div for each piece of data and set the XY position using the top and left attributes.

To calculate the XY position for each div:

Update

After following @keikai's suggestion:

var renderData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
const r = 350;
const len = renderData.length;
const radiusList = renderData.map(x => (360 / len) * (x - 1));
const positionPairList = radiusList.map(x => ({
        x: Math.sin((Math.PI * x) / 180) * r,
        y: Math.cos((Math.PI * x) / 180) * r
}));
React.createElement('div', { className: '_data'},
    renderData.map((item, index) => {
        return React.createElement('div', { className: `_items`,
            style:{top:`${positionPairList[index].x.toFixed(2)}px`, left:`${positionPairList[index].y.toFixed(2)}px`}
        }, item);
    })
)

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

  1. All data are rotated at 0 degrees.
  2. The child divs are still not positioned correctly inside the parent div.
  3. For clockwise rotation, does it start from 10?

Answer №1

Update: display rotation with various clock styles

https://i.sstatic.net/A0TTg.jpg?s=256

https://codesandbox.io/s/elastic-easley-1p5rj?fontsize=14&hidenavigation=1&theme=dark

import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";

function Display() {
  const numPoints = 12;
  const radius = 500;

  const anglesList = Array.from(Array(numPoints).keys()).map(x => (360 / numPoints) * x);
  const pointCoordsList = anglesList.map(angle => ({
    x: Math.sin((Math.PI * angle) / 180) * radius,
    y: Math.cos((Math.PI * angle) / 180) * radius
  }));
  
  return (
    <div className="Display">
      {pointCoordsList.map((coords, index) => {
        const offset = index === 0 ? numPoints : 0;
        return (
          <div
            className="Point"
            style={{
              top: `${radius - coords.y.toFixed(0)}px`,
              right: `${radius + 200 - coords.x.toFixed(0)}px`,
              transform: `rotate(${anglesList[index]}deg)`
            }}
          >
            {index + offset}
          </div>
        );
      })}
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Display />, rootElement);

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

Picture spills over onto the following row

Here is the HTML code snippet I have: <div id="thumbs"> ...8 image tags with width and height of images set to 100 x 100px </div> Below is the corresponding CSS: #thumbs { overflow:hidden; float:left; posi ...

Unchecked checkbox displays as checked in UI

I am facing an issue with checking checkboxes using JavaScript. Even though the checkbox appears to be checked in the program, it does not reflect the updates on the user interface. If anyone has a solution for this, please share. <!DOCTYPE html> ...

Looking to deactivate a particular checkbox in a chosen mode while expanding the tree branches

I encountered an issue with a checkbox tree view where I needed to disable the first two checkboxes in selected mode. While I was able to achieve this using the checked and readonly properties, I found that I could still uncheck the checkboxes, which is no ...

Generating PDF files from HTML documents using Angular

I am currently working on an Angular 11 application and I have a specific requirement to download a PDF file from a given HTML content. The challenge is that the HTML content exists independent of my Angular app and looks something like this: < ...

Issues with CSS z-index in negative values not functioning correctly

Help! I'm encountering a perplexing z-index issue on my website. Check out the problem here: Upon closer inspection of each step's header, you'll see that only step 3 features a ribbon while steps 1 and 2 do not (if this isn't visible, ...

How Python Flask sends a string as &#34 to an HTML page

I am working on a Flask app and I need to send simple JSON data from the app.py file to an HTML page. Here is the relevant code in my app.py: jsonArr = [{"type": "circle", "label": "New York"}, {"type": "circle", "label": "New York"}] return ...

Retrieve the value of a specific key from the previous state object using the useState hook

I've searched exhaustively on stack overflow, but couldn't find a similar solution. I need to update an object key-value pair without using the old value. Let's consider a graph declared using useState: const [graphBounds, setGraphBounds] ...

Generating dynamic forms using JSON Schema in Angular 8 can greatly streamline the form creation process

Struggling with generating dynamic forms from JSON Schema in Angular 8, I stumbled upon a couple of libraries. One seemed outdated with its last commit around 2 years ago, while the other appeared to be a more recent fork of it for Angular 8. The first li ...

Chrome Canary's behavior with CSS border-radius on tiny objects

Is it possible to achieve CSS border-radius on really small objects in Chrome Canary? This experiment with a 3px high line and a 2px border radius, when zoomed in at 600%, doesn't show much difference: It works perfectly fine in regular Google Chrom ...

Activate GTM data layer variable upon certain historical changes

In my react project, I have been utilizing the react-gtm-module to implement Google Tag Manager. Previously, I was triggering the dataLayer variable when needed using the code snippet below: window.dataLayer = window.dataLayer || [] window.dataL ...

Is it possible to vertically align variable length text within a container of fixed height?

One common method for centering text inside a div is to use the following CSS trick: .center-div { display:table-cell; vertical-align: middle; } The issue arises when the text within the div varies in length and I require the container to have fi ...

Tips for utilizing the Delete function in Python Flask

Need help with the Delete method in Python Flask. I'm encountering some difficulties in my code and it keeps displaying Method Not Allowed error when trying to load the URL http://localhost:8082/api/users/remove_group/5. @app.route('/api/users/re ...

What could be the reason behind the application experiencing crashes while utilizing express-rate-limit?

I am utilizing version 6.0.1 of the express-rate-limit package to control the number of request hits, and I referenced the express-rate-limit documentation available at https://www.npmjs.com/package/express-rate-limit However, I am encountering a crash in ...

A guide on accessing JavaScript files from the components directory in a React Native iOS application

I am encountering difficulty in accessing the components folder within my React Native Project on IOS. The error message I am receiving is: Unable to resolve module ./Login from ....../ReactNative/ReactNativeProject/components/App.js: Unable to find ...

Tips for validating form input upon submission in Angular 6

Within my Angular application, I have successfully implemented form validators. However, I am aiming to trigger form validation specifically upon submission. This means that when the user clicks on the submit button and the form is invalid, the errors indi ...

Tips on how to update the styling of an active link

http://jsfiddle.net/G8djC/2/ Looking to create a tabbed area where content changes based on the tab clicked. The Javascript function switches the link class to active upon clicking. However, struggling to change the color of the active tab beyond the firs ...

Exploring nested arrays within JSON objects - techniques for accessing specific properties

Having some difficulty with the JSON data I received. The structure is as follows: [ [ { "items": [ { "id": "xxxxxxx", "name": "xxxxxxxxxx", "description": "xxxxxxxxxxx" } ...

Tips for uploading files in asp.net using an ajax call

I have developed a small asp.net web forms application for managing emails. The interface allows users to input mandatory information such as sender, recipient, and subject. I am now looking to implement file attachments in the emails using the asp.net fil ...

Utilizing a try/catch block for validating a JSON file is ineffective

I'm attempting to verify if a received string is JSON and I experimented with the code below: try { JSON.parse(-10); // Also tried with "-10" }catch(e) { console.log('inside catch'); } Surprisingly, the code never enters the catch ...

Adjust google maps to fill the entire vertical space available

I found this helpful resource for integrating Google Maps into my Ionic app. Everything is working smoothly, but I am trying to make the map fill the remaining height below the header. Currently, I can only set a fixed height in pixels like this: .angula ...