What is the best approach for adding a mark within a circle using React or vanilla JavaScript?

Has anyone come across an npm package that allows for marking a dot in a circle? I am looking to click on the mark and output the JSON object.
https://i.sstatic.net/u8FTY.png

Additionally, is there any option to achieve this with a simple onClick event?
https://i.sstatic.net/YzDhn.png

Answer №1

One way to achieve this is by determining the click position relative to the parent tag, and then adjusting the child tag's position using the 'left' and 'top' styles.

// Get references to the parent and child elements
let outer = document.getElementById("container");
let inner = document.getElementById("circle");

// Add a click event listener to the parent element
outer.addEventListener("click", function(event) {
    // Calculate the new position for the child element
    let result = {
        Azimut: {
            x: event.offsetX,
            y: event.offsetY,
        },
        Size: 10,
    }
  
    // Adjust the child element's position based on the click coordinates
    inner.style.top = (result.Azimut.y - result.Size / 2) + "px"; 
    inner.style.left = (result.Azimut.x - result.Size / 2) + "px";
    inner.style.width = result.Size + "px";
    inner.style.height = result.Size + "px";

})
<div id="container" style="
  width: 100px;
  height: 100px;
  border: 1px solid black;
  position: relative;">
  <div id="circle" style="
    border: 1px solid black;
    position: absolute;
  "></div>
<div>

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 verify the presence or absence of content in a DNN template editor token?

I created a form where individuals can input data, and each field is assigned a token. For instance, if someone enters their name in a textbox labeled as "name," using the [NAME] token will display the entered name on another page. Currently, I am working ...

Issue with npm install in React cloned repository

I am encountering an issue with my important GitHub repository (tutorial) when I try to clone it and run npm install - npm start. The Terminal displays the following error message: react-scripts start sh: react-scripts: command not found npm ERR! file s ...

What is the best way to conceal a section of a div using CSS/React?

I am attempting to create a design where the entire content of a div is displayed initially, and then after clicking a button labeled "show less", only 300px of the content will be shown with the button changing to "show more". <div className="bod ...

When I click on my navbar toggle button, why isn't my text displaying?

I'm experiencing an issue with my navbar toggle. The toggle button works fine, but when I click on it, the text inside the button doesn't show up. I've checked the Bootstrap docs to make sure I didn't miss any steps, but I can't se ...

Leveraging the functionalities of package-lock and npm link

Our organization specializes in creating addons that are essential components for implementation projects. The addon developers follow a specific process of linking their code to the implementation project using "npm link" and then installing it with "npm ...

Can you tell me if there is a switch available for hover or mouse enter/mouse leave functions?

I have a series of div elements, each containing several nested div elements. I would like to add hover effects to these div elements. However, I am unsure whether to use the hover or mouseenter function. For instance, when hovering over a div, I want it t ...

incorporating additional lines into the datatable with the help of jquery

I am attempting to directly add rows to the datatable by assigning values in the .js file through the Metronic theme. However, despite displaying item values during debugging, the values are not being added to the datatable row array and thus not reflect ...

React reducer being invoked twice

In my application, I am utilizing React and Redux. I am facing an issue where I receive my props twice - the first time as undefined and the second time correctly. This makes it impossible for me to use the props effectively. import ... class Home exte ...

When using JSON stringify, double quotes are automatically added around any float type data

When passing a float data from my controller to a JavaScript function using JSON, I encountered an issue with quotes appearing around the figure in the output. Here is the JS function: function fetchbal(){ $.ajax({ url: "/count/ew", dataType: "jso ...

React Native: implementing drawer labels with react-navigation

Is it possible to add drawer labels/separators to the drawer navigator? Looking for something similar to this Any suggestions on how I can achieve this? ...

Recurly.js: Enhancing PHP Integration with Advanced Digital Signatures

I've been working on setting up forms for a recurly implementation and using PHP to generate the signature. Despite following the documentation, searching for examples, and testing various combinations, I'm facing an issue where part of the PHP c ...

An error occurred stating 'TypeError: jsdom.jsdom is not a function' while using the paper-node.js in the npm paper module

Recently, I added the webppl-agents library to my project, including webppl and webppl-dp. However, when attempting to execute the command line test, I encountered some difficulties. It seems that there is a dependency problem with jsdom from the npm paper ...

What is the method to deactivate child elements within an element that possesses the disabled attribute in Polymer?

What are some effective methods for disabling children elements when at least one parent element is disabled? I have a form with nested groups and fields that can be disabled based on certain conditions. Different parent elements may be disabled independe ...

Is there a way to eliminate the shadow effect appearing on product photos in BIG CARTEL?

While using the Luna theme on my Big Cartel store, I noticed that the products are hard to see on mobile devices because of the box shadow effect. I found a solution on a Big Cartel help page recommending to add a specific code to the CSS, but when I tried ...

Generate unique avatars with a variety of predefined image sets

Instead of solving my problem, I am seeking guidance on it: I have a project to create an Avatar maker in C# using a vast collection of categorized images. The concept is simple, but I lack experience in image manipulation through code, so I need some ad ...

Toggling markers according to form selections is not functioning

My goal is to dynamically show or hide markers on my Google Map that represent houses and condos, based on the features selected by the user from a select box with id #features. For example, if a user picks 'Swimming Pool' as a feature and click ...

evaluation is not being executed in javascript

I am struggling to assign a value of 1 to the educationflag variable. I am trying to avoid calling the enableEdit.php file when the flag is set to 1. The issue arises when control reaches the if condition but fails to set the variable to 1. Here is my cod ...

What could be causing the issue with my code attempting to conceal a parent div?

I attempted to follow the guidance found here, tweaking it to suit my requirements, but unfortunately, it's not producing the desired result. I am endeavoring to conceal a parent div that contains two child elements. These parent divs are part of a li ...

Attempting to enlarge an image by hovering over it proves to be futile

Currently, I am exploring CSS and attempting to enlarge 2 images when they are hovered over. Unfortunately, the desired effect is not working. .imagezoom img { position: relative; float: left; margin: 50px 5px 0px 42px; border-color: rgba(143, 179, 218, ...

Failing to retain hyperlinks with ajax

Currently, I am utilizing ajax to transmit data from a sophisticated custom field wysiwyg editor. Within this setup, the specific div with the class 'bio' is what I'm addressing. The issue arises when the data is retrieved - all the original ...