制作点击事件并删除画布中的圆圈重叠

时间:2013-10-16 17:16:13

标签: javascript html5 twitter-bootstrap canvas

我写了这个html和javascript代码:

Demo fiddle

单击画布坐标时,绘制一个圆。 现在我想在代码中执行这两个操作:

1 - 创建点击事件,当我点击圈子时,打开下面的bootstrap下拉菜单:

<div class="dropdown">
  <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">
    ...
  </ul>
</div>

2 - 用户无法绘制圆圈重叠的2个圆圈。

1 个答案:

答案 0 :(得分:0)

我已更新您的脚本以执行您想要的操作。您可能需要进一步更新它。基本上,您需要存储鼠标单击事件并在创建新圆时添加菜单。然后检查所有新点击,如果他们在圈子中,他们会显示菜单,如果他们距离太近,则会发出警报,如果距离足够远,则会绘制新的圈子。

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);
});