【问题标题】:Connecting multiple circles using canvas使用画布连接多个圆圈
【发布时间】:2015-04-12 04:18:38
【问题描述】:

我正在尝试编写一个程序,该程序将在用户第一次单击时在屏幕上绘制一个圆圈,然后对于每次连续单击,它将绘制另一个圆圈并将第一个圆圈连接到新的圆圈直线。除了根据用户点击绘制圆圈之外,我有点卡住了。

这是我的代码

var app = angular.module('plunker', []);

app.controller('MainController', function($scope) {
  //alert("test");
 $scope.doClick = function(event){

 var x = event.clientX;
 var y = event.clientY;
 var offsetX = event.offsetX;
 var offsetY = event.offsetY;
 //alert(x, y, offsetX, offsetY);

 /// These are the 2 new lines, see you target the canvas element then apply it to getContext
 var canvasElement = document.getElementById("canvas");
 var ctx = canvasElement.getContext("2d");

  //draw a circle
  ctx.beginPath();
  ctx.arc(x, y, 5, 0, Math.PI*2, true); 
  ctx.closePath();
  ctx.fill();

};


});

这是 plnk 的链接

http://plnkr.co/edit/rYVLgB14IutNh1F4MN6T?p=preview

任何帮助表示赞赏

【问题讨论】:

    标签: javascript angularjs canvas


    【解决方案1】:

    你已经把圆圈画得很好......这是连接线的制作方法!

    您可以使用合成来绘制连接线在之前绘制的内容下

    特别是ctx.globalCompositeOperation='destination-over' 将导致您的新连接线之前绘制的圆圈(和线)下绘制。

    这是示例代码和演示:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    function reOffset(){
      var BB=canvas.getBoundingClientRect();
      offsetX=BB.left;
      offsetY=BB.top;        
    }
    var offsetX,offsetY;
    reOffset();
    window.onscroll=function(e){ reOffset(); }
    
    var isDown=false;
    var startX,startY;
    
    var radius=10;
    var lastX,lastY;
    
    ctx.fillStyle='red';
    
    $("#canvas").mousedown(function(e){handleMouseDown(e);});
    
    
    function drawCircle(cx,cy){
      if(lastX){
        ctx.globalCompositeOperation='destination-over';
        ctx.beginPath();
        ctx.moveTo(lastX,lastY);
        ctx.lineTo(cx,cy);
        ctx.stroke();
        ctx.globalCompositeOperation='source-over';
      }
      ctx.beginPath();
      ctx.arc(cx,cy,radius,0,Math.PI*2);
      ctx.closePath();
      ctx.fill();
    }
    
    function handleMouseDown(e){
      // tell the browser we're handling this event
      e.preventDefault();
      e.stopPropagation();
    
      mx=parseInt(e.clientX-offsetX);
      my=parseInt(e.clientY-offsetY);
    
      drawCircle(mx,my);
    
      lastX=mx;
      lastY=my;
    }
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <h4>Click on the canvas to draw connected circles</h4>
    <canvas id="canvas" width=300 height=300></canvas>

    [添加了所有新圈子都连接到第一个圈子的示例]

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    function reOffset(){
      var BB=canvas.getBoundingClientRect();
      offsetX=BB.left;
      offsetY=BB.top;        
    }
    var offsetX,offsetY;
    reOffset();
    window.onscroll=function(e){ reOffset(); }
    
    var isDown=false;
    var startX,startY;
    
    var radius=10;
    var lastX,lastY;
    
    ctx.fillStyle='red';
    
    
    $("#canvas").mousedown(function(e){handleMouseDown(e);});
    
    
    function drawCircle(cx,cy){
      if(lastX){
        ctx.globalCompositeOperation='destination-over';
        ctx.beginPath();
        ctx.moveTo(lastX,lastY);
        ctx.lineTo(cx,cy);
        ctx.stroke();
        ctx.globalCompositeOperation='source-over';
      }else{
        lastX=cx;
        lastY=cy;
      }
      ctx.beginPath();
      ctx.arc(cx,cy,radius,0,Math.PI*2);
      ctx.closePath();
      ctx.fill();
    }
    
    function handleMouseDown(e){
      // tell the browser we're handling this event
      e.preventDefault();
      e.stopPropagation();
    
      mx=parseInt(e.clientX-offsetX);
      my=parseInt(e.clientY-offsetY);
    
      drawCircle(mx,my);
    }
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <h4>Click on the canvas to draw connected circles</h4>
    <canvas id="canvas" width=300 height=300></canvas>

    【讨论】:

    • 这几乎只需要所有新圈子连接到第一个圈子。不是对彼此。
    • 只连接第一个圆圈其实比原来的Demo简单。有关将所有内容连接到第一个圆圈的第二个演示,请参阅我的答案。祝你的项目好运!
    • 抱歉,我找不到您将所有这些都连接到第一个圈子的答案。无论如何都非常感谢。
    • 这是底部代码...在 [添加所有新圆圈连接到第一个圆圈的示例] ;-)
    • 这在你的演示中看起来很神奇,但在我的 plnkr 上给出了稍微不同的行为。 plnkr.co/edit/rYVLgB14IutNh1F4MN6T?p=preview
    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多