我已经更新了你的脚本来做你想做的事。您可能需要进一步更新它。基本上,您需要存储鼠标单击事件并在创建新圆圈时添加菜单。然后检查所有新的点击,如果它们在圆圈中,它们会显示菜单,如果它们太近,则会发生警报,如果它们足够远,则会绘制一个新圆圈。
http://jsfiddle.net/sjjq5/1/
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var canvasOffset = $("#myCanvas").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var x;
var y;
var radius = 50;
var startAngle = 1.1 * Math.PI;
var endAngle = 1.9 * Math.PI;
var counterClockwise = false;
var circles = [];
function handleMouseDown(e) {
mouseX = parseInt(e.clientX - offsetX);
mouseY = parseInt(e.clientY - offsetY);
$("#downlog").html("Down: " + mouseX + " / " + mouseY);
//check all existing circles to see if we clicked on
var inCircle = false;
var tooClose = false;
for(var i =0;i<circles.length;i++){
if(circles.length > 0){
//checks if we clicked in a circle
if(Math.sqrt((mouseX-circles[i].x)*(mouseX-circles[i].x) + (mouseY-circles[i].y)*(mouseY-circles[i].y)) < radius+5){
console.log('in circle');
inCircle = i;
}
//checks if we clicked somewhere that would create an over lapping circle
else if(Math.sqrt((mouseX-circles[i].x)*(mouseX-circles[i].x) + (mouseY-circles[i].y)*(mouseY-circles[i].y)) < radius*2+5){
console.log('too close');
tooClose = true;
}
}
}
if(inCircle !== false){
//we clicked in a cirlce launch the menu
console.log('showing menu');
$('#circle-menu-'+inCircle).css({left:e.clientX,top:e.clientY});
$('#circle-menu-'+inCircle).show();
}
else if(tooClose){
alert('Cant create new circle, too close to existing one');
//hide all shown menus
$('.dropdown').hide();
}
else{
//hide all shown menus
$('.dropdown').hide();
console.log('creating new circle');
//Draw a new circle
context.beginPath();
context.arc(mouseX, mouseY, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'rgba(255, 255, 255, 0.5)';
// context.fillStyle = 'green';
context.fill();
context.lineWidth = 5;
context.strokeStyle = '#003300';
context.stroke();
//build a menu for it
$('#menus').append($('<div class="dropdown" id="circle-menu-'+circles.length+'"><a class="dropdown-toggle" id="dLabel" role="button" data-toggle="dropdown" data-target="#" href="/page.html">Dropdown<b class="caret"></b></a><ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"><li>Click Me</li></ul></div>'));
//store that data
circles.push({x:mouseX,y:mouseY});
}
}
$("#myCanvas").mousedown(function (e) {
handleMouseDown(e);
});