画布指向用户线

时间:2014-02-09 10:49:27

标签: javascript canvas line user-input

我一直在尝试创建绘图应用程序,并且作为其中一项功能,我希望用户绘制一条直线。我希望用户单击然后拖动鼠标,当他们放开时,将辅助点绘制为从它们开始和结束的位置绘制一条直线。

然而到目前为止,它只是从中心绘制一条对角线起点,设定长度可以帮助这是问题所在的代码

     var line = document.getElementById('line');

     // Function for the line to draw
    line.addEventListener('click', function () {
        // Removed extra code
        var putPoint2 = function (e) {
            if (dragging2) {
            var new_size = size/2;

              context.moveTo(e.offsetX, e.offsetY);


            }

        };
        engage2 = function (e) { // Made engage2() "global" to refer it in engage()
            dragging2 = true;
            putPoint2(e);
            disengage2(e);
            canvas.addEventListener('mousedown', putPoint2); // Repositioned event attachment
            //canvas.addEventListener('mousemove', disengage2); // Repositioned event attachment
        };
        var disengage2 = function (e) {
            dragging2 = false;
        context.lineTo(e.offsetX+10, e.offsetY+10);
context.stroke();
            context.beginPath();
            canvas.removeEventListener('mousedown', putPoint2); // Added event detachment
            canvas.removeEventListener('mouseup', disengage2); // Added event detachment
        };

1 个答案:

答案 0 :(得分:1)

您可以使用第二个临时画布让用户拖动您的线条。

如何做的概述:

创建第二个临时屏幕外画布,其尺寸与屏幕画布完全相同。

关于mousedown:

  • 将临时画布精确地移动到常规画布的顶部
  • 保存起始拖动XY
  • 设置一个标志,指示拖动已开始

关于mousemove:

  • 清除临时画布
  • 临时画布上的
  • :从起始拖动XY到当前鼠标XY
  • 绘制一条线

关于mouseup或mouseout:

  • 拖动已经结束了拖拽标志
  • 将临时画布移出屏幕
  • 主画布上的
  • :从起始dragXY到结束鼠标XY
  • 画一条线

以下是代码和演示: http://jsfiddle.net/m1erickson/2FaFw/

<!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; }
    #wrapper{
        position:relative;
        width:300px;
        height:200px;
    }
    #canvas,#canvasTemp{
        position:absolute; top:0px; left:0px;
        border:1px solid blue;
        width:100%;
        height:100%;
    }
    #canvasTemp{ border:1.5px solid green; }
    #canvas{ border:1px solid red; }
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var canvasTemp=document.getElementById("canvasTemp");
    var ctxTemp=canvasTemp.getContext("2d");
    var canvasOffset=$("#canvas").offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;

    var startX;
    var startY;
    var isDown=false;

    $("#canvasTemp").css({ left:-500, top:0 });

    function drawLine(toX,toY,context){
        context.beginPath();
        context.moveTo(startX, startY);
        context.lineTo(toX,toY);
        context.stroke();
    }

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

      // save drag-startXY, 
      // move temp canvas over main canvas,
      // set dragging flag
      startX=mouseX;
      startY=mouseY;
      ctxTemp.clearRect(0,0,canvasTemp.width,canvasTemp.height);
      $("#canvasTemp").css({ left:0, top:0 });
      isDown=true;
    }

    function handleMouseUp(e){
      e.preventDefault();
      if(!isDown){return;}
      // clear dragging flag
      // move temp canvas offscreen
      // draw the user's line on the main canvas
      isDown=false;
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      $("#canvasTemp").css({ left:-500, top:0 });
      drawLine(mouseX,mouseY,ctx);
    }

    function handleMouseMove(e){
      e.preventDefault();        
      if(!isDown){return;}
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      // clear the temp canvas
      // on temp canvas draw a line from drag-start to mouseXY
      ctxTemp.clearRect(0,0,canvasTemp.width,canvasTemp.height);
      drawLine(mouseX,mouseY,ctxTemp);
    }

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

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

</head>

<body>
    <div id="wrapper">
        <canvas id="canvasTemp" width=300 height=200></canvas>
        <canvas id="canvas" width=300 height=200></canvas>
    </div>
</body>
</html>