Discover the precise number of columns and rows that make up a circle

Is there a way to display a popup message when clicking on a circle in the array to see the number of rows and columns that circle is from the starting point (1,1)?

I'm looking for assistance with implementing this feature.

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var gCanvasElement = ctx;

ctx.strokeStyle="#FF0000";

ctx.strokeRect(20,20,800,600);

// Positions are hardcoded to make sure that circle starts from the right place
var startX = 55;
var startY = 55;

console.clear();

for(var i=1;i<=8;i++){
    console.group(i);
    for(var j=1;j<=i;j++){

        ctx.beginPath();
        ctx.strokeStyle='green';
        //radius is hardcoded to 30 for testing purpose
        ctx.arc(startX*j + (j-1)*10,startY*i + (i-1)*10,30,0,2*Math.PI);
        ctx.stroke();

        //console log
        console.group(j);    
        console.log(startX*j + (j-1)*10);
        console.log(startY*i + (i-1)*10);
        console.groupEnd(j);
    }

    console.groupEnd(i);
}

Answer №1

My explanation in the comment section emphasizes that canvas is essentially a raster image and does not retain shapes like vector graphics (SVG). Thus, determining if the user has clicked on the circle requires mathematical calculations.

http://jsfiddle.net/tarabyte/Jct6j/

function distance(x1, y1, x2, y2) { //a function to calculate the distance between two points
    return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

function center(i) { //a function to find the coordinates of the i-th circle
    return (i*2-1)*radius + (i-1)*step/2;
}

c.addEventListener('click', function(ev){ //click event listener
    var x = ev.pageX - c.offsetLeft - (startX - radius), //normalized coordinates
        y = ev.pageY - c.offsetTop - (startY - radius), //normalized coordinates
        grid = 2*radius + step/2,
        i, j, d;

    if(x < 0 || y < 0) { return;} //check if position is outside circles

    //determine the cell inside the grid
    i = Math.ceil(x/grid); 
    j = Math.ceil(y/grid);

    if(i > j) { return;} //check if position is outside circles

    d = distance(x, y, center(i), center(j));

    if(d > radius) {return;} //check if position is too far from the center


    console.log(i, j);
}, false);

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

"Which is better for handling form data: using $.ajax with hidden fields or PHP form

Creating a streamlined admin tool using PHP and JQuery to handle image data management is my current project. The main goal of this utility is to enable an admin to retrieve images from a database, view associated tags for each image, and add new tags as n ...

What is the best way to transform a JSON document collection into a GeoJSON FeatureCollection?

Using my RESTapi, I extract data from a mongoDB and pass it to my mapbox gl app as a JSON array with the help of the following code: $.ajax( { type: "GET", contentType: "application/json; charset=utf-8", ...

Tips for aligning text in the middle of a customizable and adjustable button with the use of only Bootstrap 4

I am encountering an issue with button text alignment. After customizing the width and height of a button, I noticed that its text does not remain centered on certain screen sizes while in debugging mode. Html <!doctype html> <html lang="en> ...

The outcome of a Wordpress AJAX submission redirects to the admin-ajax.php file rather than appearing in the designated content

I've encountered an issue with the AJAX handler in WordPress. Here is the form that's causing trouble: form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter" name="filter"> <input type="h ...

Retrieving decimal value from a given string

Currently, I am working with Google Maps and encountering an issue with distance values being returned as strings like 1,230.6 km. My goal is to extract the floating number 1230.6 from this string. Below is my attempted solution: var t = '1,234.04 km ...

Add objects to a web address that leads nowhere but can still be linked to

It has come to my realization that the vagueness of my question might be obstructing my search efforts online. Here is the scenario: I am aiming to create a URL structure like this: http://app.myurl.com/8hhse92 The identifier "8hhse92" does not correspo ...

Switching downlink to top link when scrolling downwards

I have a downward scrolling link on my homepage that moves the page down when clicked by the user. However, I am struggling to make it change to a "back to top" link as soon as the user scrolls 10 pixels from the top. Additionally, I am having trouble with ...

Steps to update XmlHttpRequest URL during image upload

I am currently facing an issue with updating images on my website. When I try to update an image, it redirects to the wrong URL instead of the intended one. The form is set to post data to this URL: POST http://127.0.0.1/mgt/upload/processImage/foodImage ...

How can a Chrome extension automatically send a POST request to Flask while the browser is reloading the page?

I am looking to combine the code snippets below in order to automatically send a post request (containing a URL) from a Chrome extension to Flask whenever a page is loading in Chrome, without needing to click on the extension's icon. Is this feasible? ...

Is there a way to ensure my function runs as soon as the page starts loading?

My goal is to have a function execute as soon as a person opens my page, without requiring any interaction. The function should trigger on page load, rather than waiting for a click event. Additionally, I want the function to repeat every 90 seconds. I&apo ...

Acquiring the `=` symbol within email HTML

Here is the HTML that I send from my ASP.NET application: <HTML> <BODY> <img src='http://10.1.1.42:8090/EmailLogo/8aaebe52-3758-485f-8c52-7782707a4c66.jpg' /> <br/> <div style='font-family:Arial, Tah ...

The background image positioned in the center may experience cropping in HTML

I am currently facing an issue with aligning a background image horizontally in a specific scenario: The background image is large and needs to be centered horizontally The page has a minimum width requirement of 960px which may result in a scrollbar on ...

I encountered an error in the Expo dashboard when attempting to upgrade from version 48 to 49 during the Expo Expo version change

For the image description, type in: "expo": "49", "react-native-reanimated": "~3.3.0", "expo-updates": "~0.18.19" I need assistance, can someone please help me out? ...

Ways to refresh dropdown list

I'm experiencing an issue with reloading the dropdownlist. Whenever I make a new selection on the dropdown list, the previous options are still visible. How can I fix this? <script type="text/javascript"> $(document).ready(function () { ...

determining the overall page displacement

I'm working with this code and I need help using the IF condition to check if the total page offset is greater-than 75%. How can I implement that here? function getLocalCoords(elem, ev) { var ox = 0, oy = 0; var first; var pageX, pageY; ...

Determining Text Color Based on Background Hue

Is it possible to create a div that dynamically changes text color based on the background color surrounding it? I want the text to switch between two different colors, with the font color being the opposite of the background color. For example, black text ...

What is the reasoning behind utilizing the "&" selector in CSS?

.navigation { position: fixed; top: 0; bottom: 0; left: 0; transform: translateX(-100%); transition: translation: all ease 0.25s; &.expand { transform: translateX(0); } } JavaScript file: $(document).ready(func ...

Using dynamic values in Angular functions

Here is the code snippet I am working with: $scope.testByValue = function() { $scope.fChanged($scope.lists.title); }; But I am facing an issue on the page where the value for: $scope.testValue = testValue; The variable testValue is being assigned a ...

What is the best way to extract a message as an object from a JSON data in discord.js?

Currently in the process of developing a discord bot with an election feature for my server's moderator. All the necessary election data is being saved in an external JSON file to ensure that if the bot crashes during an election, it can seamlessly re ...

When should separate controllers be created for a list of values in a Laravel REST API?

Imagine I have a straightforward API for user registration. This API collects basic information like Name, email, state, gender, and marital status for each user. I already have database tables pre-populated with ids for state, gender, and marital status o ...