Craft a circular design with an Arc and a Pie using the DIV DOM element

After creating an ellipse using the div DOM element, here's how I did it:

var body = document.querySelector('body');
var div = document.createElement('div');
div.style.borderRadius = '50%';
div.style.border = '1px solid red'; 
div.style.left = '60px'; 
div.style.top =  '60px';
div.style.width = '100px';
div.style.height = '100px'; 
body.appendChild(div);

It's visualized in this image:

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

Next, I aim to create both an arc and a pie shape.

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

In the second illustration above, users can input the starting angle and the end angle of the arc. For our example, the starting angle is set at 180 degrees with an ending angle at 360 degrees.

Additonally, I plan on developing a pie chart where users provide the inside radius of the circle/ellipse along with the starting and ending angles.

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

For the third image shown above, the inside radius is calculated as 50% of the circle/ellipse width, while the starting angle and ending angle are 90 degrees and 360 degrees respectively.

Is this a feasible task?

P.S: I prefer not to utilize canvas or svg for this project.

Answer №1

Canvas is often suggested as a great solution for drawing these types of designs. However, if you prefer not to use canvas, you can achieve similar effects using border styles to create arcs. The angle of the arc should be in multiples of 90deg, such as 0 90 180 270 360. You can choose any starting angle like 0, 5, 10, or 7.

Here's an example:

To replicate the effect of the second arc you provided, set the start angle as 0 and the end angle as 180. Refer to the standard angle system shown in the following picture:

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

Execute this code:

var border = ['border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color'];
var circle;
var standeredRotation = 135;

function getinput() {
  circle = document.getElementById("circle");
  var startAngle = document.getElementById("startangle").value;
  var endAngle = document.getElementById("endtangle").value;
  var angle = endAngle - startAngle;
  if (angle % 90 != 0) {
    alert("Please enter angles that are multiples of 90 degrees.");
  } else {
    var parts = angle / 90;
    draw(parts, startAngle);
  }

}

function draw(parts, startAngle) {
  var style = "";
  for (var i = 0; i < parts; i++) {
    style += border[i] + ": #FF4E4E;";
  }
  circle.setAttribute("style", style);
  totalAngle = standeredRotation + parseInt(startAngle);
  circle.style.transform = 'rotate(' + totalAngle + 'deg)';
}
#circle {
  border-radius: 50%;
  border: 10px solid transparent;
  height: 100px;
  width: 100px;
  margin: 10px auto;
}
<body>
  <div class="user-input">
    <label>Start Angle:</label>
    <input type="text" id="startangle" value="0" />
    <label>End Angle:</label>
    <input type="text" id="endtangle" value="180" />
    <button onclick="getinput()">Draw</button>
  </div>
  <div id="circle">

  </div>
</body>

For the last circle with two overlapping circles, the angles are 0 and 270. To close the circles, you can position two divs and rotate them accordingly.

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

Guide on adjusting shipping costs in Stripe based on the customer's address using NodeJS

Utilizing Stripe's Checkout API, I am seeking to provide international shipping options with varying costs based on the country selected at checkout. Is there a method within Checkout that allows me to dynamically adjust shipping costs based on the us ...

What is the most effective way to transmit a conditional operator via a TypeScript boolean field?

Currently, as part of my transition to typescript, I am working on incorporating a conditional operator into the table component provided by Ant Design. const paginationLogic = props.data.length <= 10 ? false : true return ( <> ...

Having trouble with my intricate-box and grid layout - the images are all overlapping!

Could you please take a look at the image below and tell me what I might be doing incorrectly? When the page initially loads, pictures appear like this. However, if I refresh the page, the issue is resolved. https://i.sstatic.net/hIyA4.jpg If I remove th ...

How to Modify the Data in a Dynamic Object within the Vuex Store in a Nuxt Application

Within my Vue/Nuxt project, I have implemented a form where users can add and update dynamic fields for calculating price offers. Upon loading the form, one field is created using the beforeMount lifecycle, with the option for users to add more fields as n ...

To enhance user experience, consider incorporating a 'Next Page' feature after the completion of every four paragraphs,

Here is a code snippet that can be used to print 'n' number of paragraphs: <% while(rs.next()){ String para=rs.getString("poems"); %> <p> <%=para%> </p> <!-- n number of p tags are printe ...

Searching for a jQuery plugin that can dynamically rearrange tables while automatically updating their corresponding IDs

Is there a way in jQuery to dynamically move tables around on a webpage? Currently, I have implemented a button that clones a hidden table when needed and another button to delete unwanted tables. Now, I am looking to incorporate a feature that allows the ...

Node.js: Extracting parameters from the URL

When working with Rails, I make a POST request to my server: response = Typhoeus::Request.post("http://url.localtunnel.com/request?from=ola&to=ole") result = JSON.parse(response.body) Now in my Node.js application, I need to retrieve values for From ...

How do I fix the build error that says "Operator '+' cannot be used with types 'number[]'?

The function below is designed to generate unique uuidv4 strings. function uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => ( c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)) ...

Error in NodeJS: 'Cannot alter headers once they have been sent.'

My project involves developing an app with Express that fetches tweets from Twitter. Each API call retrieves 200 tweets (or fewer if there are less than 200) and in total, I need to retrieve 1800 tweets. To achieve this, I use a time interval to make multi ...

Decreasing the vertical size of the page

My mobile page was meant to be all black, but it's only half black now. I tried the following CSS: @media (max-width: 768px) {body, html.page-id- 28{background-color: black! important;}} @media (max-width: 768px) {. parallax-window {background ...

Angular and Firefox are flagging the response from my OAuth call as incorrect, however, Fiddler is showing a different result

Currently, I am in the process of developing a Cordova application and require OAuth authentication with my Drupal backend. My main issue lies in obtaining a request token for this purpose. Despite receiving a 200 response indicating success, when inspecti ...

Is it possible to incorporate a variable into my fetch request?

I am currently learning how to use react native and am studying some examples. Encountered a roadblock: While attempting to pass latitude in my fetch call, I encountered an error stating that "latitude" does not exist. class App extends React.Component ...

Using HTML symbols can alter the height of the text

It seems like I'm making a mistake - whenever I try to include an HTML symbol before a word, the following word without the HTML symbol ends up with a different height and is not on the same line. ...

Return to the initial stage of a multistep process in its simplest form following a setTimeout delay

I recently customized the stepsForm.js by Copdrops and made some modifications. Although everything works well, I'm struggling to navigate back to the initial step (first question) after submitting the form due to my limited knowledge of JavaScript. ...

Using AngularJS and Web API to generate a dropdown menu from a one-to-many relationship

I have two tables in my database: People and Payband. Let me simplify the relationship below: dbo.People PersonId : int (Primary Key) FirstName : string MiddleInitial: string LastName : string DateOfBirth: datetime PaybandId : int (Foreign Key) dbo.Payb ...

The value is undefined until a new Resource object is pushed with the item

I've encountered an issue while testing a factory and I can't seem to find anyone else with the same problem. Can someone help me figure out what's causing this strange error? TypeError: 'undefined' is not a function (evaluating & ...

The process of uploading a React App to Heroku resulted in a critical error: "FATAL ERROR: Heap limit reached, Allocation failed - JavaScript heap out of memory."

There's a puzzling issue that I believe I have the solution to (paying for more bandwidth on Heroku), but the root of the problem eludes me and it's truly frustrating. Any assistance in pinpointing the cause would be greatly appreciated! Hopefull ...

Exploring the power of AngularJS with ng-click functionality and utilizing services

As a beginner in AngularJS, I am facing a knowledge gap when it comes to integrating a barcode scan feature into my application. What I want to achieve is quite simple - a button in the view that, when clicked, triggers a barcode scan. Once the scan is com ...

How can I add 0-5 images to a column without causing any distortion in the row?

I'm having trouble adding a series of images to rows in my Bootstrap 4 project. The source assets are large by default and I'm struggling with scaling them properly without affecting other columns. Here's a visual representation of what I cu ...

The image is non-adjustable to different screen sizes

Currently, I am in the process of revamping a friend's website using Next.js and Tailwind. However, I've encountered a challenge with the background image specifically on iPhones. While it appears fine on browsers like Chrome and Safari as well a ...