Creating Circular Divisions with HTML5 and CSS3

Creating segments inside a circle based on input is completely achievable. I am currently attempting to illustrate the fraction value using a circular representation by generating these segments. For example:

Imagine there is a container div like this:

<div class="circle">
</div>

    The circle has a width and height of 150px with a border radius set at 50%;

I would like to receive input values for the numerator and denominator, and then show that number of segments within the circle div.

This can look something like this:

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

Answer №1

If you are dealing with complex angles, utilizing a canvas is highly recommended. Here is a sample code snippet to demonstrate how you can achieve your desired output:

// Code for canvas setup
var canvas = document.getElementById('fraction');
var context = canvas.getContext('2d');

// Define initial coordinates and radius of the circle segment
var x = 80;
var y = 80;
var radius = 75;

// Customize fraction values here 
var numerator = 1;
var denominator = 4;
var fraction = numerator / denominator; 

// Calculate start and end angles for segment plotting
var startAngle = Math.PI;
var endAngle = (1 + (2 * fraction)) * startAngle;

// Specify clockwise or anti-clockwise drawing direction
var drawClockwise = false;

// Draw the segment
context.beginPath();
context.arc(x, y, radius, startAngle, endAngle, drawClockwise);
context.lineTo(x, y);
context.closePath();
context.fillStyle = 'yellow';
context.fill();

//***************** Edit *******************

// Add outline around the segment
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2, drawClockwise);
context.closePath();
context.strokeStyle = '#000';
context.stroke();
<div>
  <canvas id="fraction" width="200" height="200"></canvas>
</div>

Adjust the variables like numerator, denominator, and fraction to suit your needs. These control the appearance of the segment based on the specified fraction.

You have the flexibility to modify other parameters such as size, position, and direction of the shape. Feel free to experiment with different settings to enhance your design!

For more details on drawing shapes and setting colors in canvas, refer to this link. Additionally, learn about canvas basics from tutorials like this and here.

Hope this information proves helpful! Good luck with your implementation :)

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

What is the best way to create an express route for a delayed string response that is loaded only after an API call promise is fulfilled?

app.get(`/${sumName}`, async (req, res) => { const link = `https://na1.api.riotgames.com/lol/league/v4/challengerleagues/by-queue/RANKED_SOLO_5x5?api_key=${RiotKey}` const response = await fetch(link); let data = await response.j ...

Retrieve a file from a webpage that lacks a designated download option

Currently, I am attempting to utilize Python to download an Excel file. You can find the Excel file on the right side of the web page within a box labeled "Top Turnovers - All Market" by visiting this link. https://i.sstatic.net/Zaayr.png Typically, fil ...

Updating AngularJS: Removing the hashtag using history.pushState()

Struggling to enhance the aesthetics of the URLs in my AngularJS application, I am facing some challenges. While I can access the "/" route without any issues, the "/about" route seems to be out of reach. Please note that the project is situated at (loc ...

The template language is failing to render within the HTML code

Currently exploring Django for the first time and I'm following a tutorial on YouTube. However, when attempting to replicate an example demonstrated by the instructor in my own environment, it did not yield the expected outcome. Below is the HTML file ...

Cannot transmit JSON API information to Chart JS

Having trouble fetching and displaying data in a chart? I've been struggling with it for the past few days—any help would be appreciated. My code fetches and reads data perfectly fine, passing it to an HTML table, but I can't seem to pass it t ...

What is the best way to retrieve text that is viewable to the user when there is a text overflow situation

When using ellipsis in a text input to indicate overflow, is there a way to determine what text is visible to the user versus hidden behind the ellipsis? Thank you for any help! https://i.stack.imgur.com/8iLBQ.png In my scenario, I need to display a list ...

How can I use JavaScript to display every line individually in a textarea?

I am trying to display the content of a textarea in my div with the ID #result. I want to maintain the line breaks from the textarea and append 1 at the end of each line. Here's my current attempt, but it only adds 1 to the last line. <!DOCTY ...

How can JSON attribute/value pairs be rearranged using JavaScript?

Looking at the JSON data below: [ { 'Album': 'Dearest', 'Artist': 'Theresa Fu', 'Year': '2009'}, { 'Album': 'To be Free', 'Artist': 'Arashi', &apo ...

Choose the designated radio button option automatically depending on the value received from the server

In the process of creating a quiz engine using angularjs, I have successfully loaded questions with options and implemented the NEXT and BACK buttons. However, I am facing a challenge with pre-selecting the previously chosen option when the user clicks the ...

Tips for toggling the display of multiple ion-input fields based on the selected value from an ion-select dropdown

I am working with an ion-select element that contains options from 1 to 10. <ion-label> Select how many input fields</ion-label> <ion-select> <ion-option value="0"> Zero</ion-option> <ion-option value="1"> One</ion- ...

Invoke message embedding feature to mention the user above by pinging them on Discord

I was wondering how to add a ping above the user. I attempted this code and it did not show any errors. client.on("guildMemberAdd", (member) => { let embed = new Discord.MessageEmbed(); member.roles.add(member.guild.roles.cache.find( ...

Make sure to load the HTML content before requesting any input from the user through the prompt function

Could you please help me with a question? I'm looking to load HTML content before prompting the user for input using JavaScript's prompt() function. However, currently the HTML is only loaded after exiting the prompt. Below is the code that I hav ...

The process of embedding a .js file into an HTML document involves

The question appears to be straightforward. The common approach is to simply utilize this code snippet: <script src='https://code.angularjs.org/1.0.0rc9/angular-1.0.0rc9.js' type='text/javascript'/> Alternatively, in Jsfiddle, we ...

Loading a webpack bundled ngModule dynamically to handle a route efficiently

In an effort to make working on our large frontend projects more manageable, we are looking to split them into multiple independently deployed projects. I am attempting to integrate a bundled ngModule to handle a route from within another app. It is crucia ...

Eliminating blank elements from arrays using JavaScript

I'm looking for some assistance in deciphering the functionality of my code. I'm trying to create a function that will take a string as input and eliminate all letters, leaving only numbers behind. The goal is to have this function return an arra ...

Retrieving document attributes from a Mongoose Model with the help of Typescript

Incorporating Typescript with Mongoose, my aim is to retrieve properties from a Model. Taking the illustrated UserModel as an example import mongoose, { Schema } from 'mongoose'; const userSchema: Schema = new mongoose.Schema({ _id: mongoos ...

Transfer Data from Ajax Response to an Array

I am struggling to properly assign a value from an ajax call to an array and then pass that array. It seems like the variable 'options' is not being recognized as valid. This is the code I have: $.ajax({ url: url, success: function(data){ ...

Tips for adjusting the maximum characters per line in tinyMCE 5.0.11

I have an angular 8 application that utilizes tinyMCE, and I am looking to limit the maximum number of characters per line in the textArea of tinyMCE. My search for a solution has been unsuccessful so far despite extensive googling efforts. (image link: [ ...

Column flexbox self is not functioning properly with text ellipsis

Is there a way to apply text-overflow: ellipsis to a flexbox element? I know I need to set the width and overflow: hidden, but how can I achieve the ellipsis effect within the grey block when the text overflows? Removing the flexbox property from the < ...

Retrieve a random element from a selection of objects within an Array using JavaScript

Hey fellow Developers, I'm having trouble getting a random piece of data from some objects in an Array. When I try to display it, all I see is [object Object] instead of the actual text from the object. I've tried various methods but nothing seem ...