JSFiddle - React, Tailwind, and code Playground
by satgam
HTML
<div id="container" class="text-center">
<canvas id="canvas" width=400 height=400 style="border:1px solid #d3d3d3;"></canvas>
</div>
<script>
const view=document.getElementById('canvas');
var ctx1=view.getContext('2d');
var hCentre=view.width/2;
var vCentre=view.height/2;
ctx1.translate(hCentre,vCentre);
const family=["yellow","red","lime","cyan","magenta"];
const twoShades=["white","gray"];
const startAngle=Math.PI*1.2;
const numberOfButtons=5;
const pathRadius=103;
const buttonRadius=61;
var x1;// button x & y
var y1;// coordinates
var xButtonCoords=new Array(numberOfButtons); // button arrays
var yButtonCoords=new Array(numberOfButtons);
const mouse=document.getElementById('canvas');
var ctx2=mouse.getContext('2d');
var x2; // click x
var y2; // click y
rosette (numberOfButtons, pathRadius, buttonRadius);
var clickRing=function(e){
x2=e.offsetX - hCentre;
y2=e.offsetY - vCentre;
var clickRadius=buttonRadius * 0.85;
// show all Mouse hits including misses and near misses
ctx2.beginPath();
ctx2.arc(x2, y2, clickRadius * 0.85, 0, Math.PI * 2);
ctx2.strokeStyle="black";
ctx2.stroke();
for (i = 0; i < numberOfButtons; i++) {
x1=xButtonCoords[i];
y1=yButtonCoords[i];
// ctx1 ctx2
var xSquared=Math.pow((x1 - x2),2);
var ySquared=Math.pow((y1 - y2),2);
// find distance from button centre to clickpoint
var hypotenuse=Math.sqrt(xSquared+ySquared);
var distance=parseFloat(hypotenuse).toFixed(0);
// clicks won't trigger adjacent buttons
if (distance<(buttonRadius-clickRadius)) {
alert("centre of button"+(i+1)+ " to mouse is "+distance);
}
}
}
document.addEventListener('mousedown', clickRing);
function rosette (numberOfButtons, pathRadius, buttonRadius){
var theta=2*Math.PI/numberOfButtons;
var rotateAngle=startAngle;
x1= pathRadius*Math.cos(rotateAngle); // button centre
y1= pathRadius*Math.sin(rotateAngle); // coordinates
for...