0

大家,早安。

我正在为我工​​作的企业系统构建一个图表,但我遇到了一个问题,我需要插入某种标识,我可能会被JavaScript名称、id、标签……)拯救。

有人可以告诉我如何分别识别图表的每个元素?更准确地说,我想知道创建的每个元素


如果有人想看代码更好地理解它,我会把链接放在这里:
  - JS Bin

4

1 回答 1

2

您在画布上绘制的任何内容都不会自动记住或标有 id。

所有画布绘图都成为被遗忘和无法访问的像素。但是,有一些方法可以跟踪您的各种图纸。

与其将 posX、posY 和颜色信息保存在单独的并行数组中,不如为每个节点创建一个对象。

然后您可以将 id 属性添加到每个节点对象:

var nodes=[];

nodes.push({id:"sun", x:100, y:100, color:"yellow"});
nodes.push({id:"earth", x:50, y:50, color:"blue"});
nodes.push({id:"moon", x:50, y:60, color:"gray"});

而且,当然,从每个节点中提取图形信息......

您可以在函数内绘制图形并在需要时应用 scaleFactor。

    function drawGraph(){
        context.clearRect(0,0,canvas.width,canvas.height);
        context.save();
        context.scale(scaleFactor,scaleFactor);
        for(var i=0; i<nodes.length;i++){
            var node=nodes[i];
            context.beginPath();
            context.moveTo(centerCanvasX,centerCanvasY);
            context.lineTo(node.x,node.y);
            context.stroke();
            context.beginPath();
            context.arc(node.x,node.y,node.radius,0,Math.PI*2,false);
            context.closePath();
            context.stroke();
            context.fillStyle=node.color;
            context.fill();
        }
        context.restore();
    }

对于拖动/单击/等,您可以点击测试每个节点数组元素,直到找到匹配项。

function hit(x,y){
    for(var i=0;i<nodes.length;i++){
        var node=nodes[i];
        var dx=node.x-x;
        var dy=node.y-y;
        var rr=node.radius;
        if(dx*dx+dy*dy<rr*rr){
            return(i);
        }
    }
    return(-1);
}

匹配的元素将具有您需要的 id。

如果您的图表已缩放并且您使用鼠标坐标拖动,则必须通过图表的当前 scaleFactor 调整浏览器为您提供的坐标:

mouseX=parseInt(event.clientX-offsetX)/scaleFactor;
mouseY=parseInt(event.clientY-offsetY)/scaleFactor;

这是代码和小提琴:http: //jsfiddle.net/m1erickson/c4hsW/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; padding:20px; }
    #canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var context=canvas.getContext("2d");
    var canvasOffset=$("#canvas").offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;
    var centerCanvasX=canvas.width/2;
    var centerCanvasY=canvas.height/2;

    var startX;
    var startY;
    var isDown=false;

    var nodes=[];
    var dragNode;

    var scaleFactor=1.00;

    nodes.push({id:"sun", x:centerCanvasX, y:centerCanvasY, radius:20, color:"yellow"});
    nodes.push({id:"earth", x:50, y:50, radius:5, color:"blue"});
    nodes.push({id:"moon", x:50, y:65, radius:3, color:"gray"});

    drawGraph();

    function drawGraph(){
        context.clearRect(0,0,canvas.width,canvas.height);
        context.save();
        context.scale(scaleFactor,scaleFactor);
        for(var i=0; i<nodes.length;i++){
            var node=nodes[i];
            context.beginPath();
            context.moveTo(centerCanvasX,centerCanvasY);
            context.lineTo(node.x,node.y);
            context.stroke();
            context.beginPath();
            context.arc(node.x,node.y,node.radius,0,Math.PI*2,false);
            context.closePath();
            context.stroke();
            context.fillStyle=node.color;
            context.fill();
        }
        context.restore();
    }

    function hit(x,y){
        for(var i=0;i<nodes.length;i++){
            var node=nodes[i];
            var dx=node.x-x;
            var dy=node.y-y;
            var rr=node.radius;
            if(dx*dx+dy*dy<rr*rr){
                return(i);
            }
        }
        return(-1);
    }


    function handleMouseDown(e){
        mouseX=parseInt(e.clientX-offsetX)/scaleFactor;
        mouseY=parseInt(e.clientY-offsetY)/scaleFactor;

        var i=hit(mouseX,mouseY);
        if(i>=0){ 
            startX=mouseX;
            startY=mouseY;
            isDown=true;
            dragNode=nodes[i];
        }
    }

    function handleMouseUp(e){
      isDown=false;
    }

    function handleMouseOut(e){
      isDown=false;
    }

    function handleMouseMove(e){
        if(!isDown){return;}

        // get the current mouse position
        mouseX=parseInt(e.clientX-offsetX)/scaleFactor;
        mouseY=parseInt(e.clientY-offsetY)/scaleFactor;
        // reposition the dragged node
        dragNode.x+=(mouseX-startX);
        dragNode.y+=(mouseY-startY);
        startX=mouseX;
        startY=mouseY;
        // redraw the graph
        drawGraph();
    }

    $("#canvas").mousedown(function(e){handleMouseDown(e);});
    $("#canvas").mousemove(function(e){handleMouseMove(e);});
    $("#canvas").mouseup(function(e){handleMouseUp(e);});
    $("#canvas").mouseout(function(e){handleMouseOut(e);});

    $("#bigger").click(function(){
        scaleFactor+=0.20;
        drawGraph();
    });

    $("#smaller").click(function(){
        scaleFactor-=0.20;
        drawGraph();
    });

}); // end $(function(){});
</script>

</head>

<body>
    <button id="bigger">Scale Up</button>
    <button id="smaller">Scale Down</button><br>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>
于 2013-09-12T15:11:38.630 回答