简体   繁体   English

我正在尝试通过使用鼠标事件在画布上画一条线

[英]I am trying to draw a line on canvas by using mouse events

I am trying to draw a line on canvas using mousedown , mousemove , mouseup events, but I am unable to set coordinates for drawing a line on Canvas. 我正在尝试使用mousedownmousemovemouseup事件在画布上画一条线,但是我无法设置在Canvas上画线的坐标。 Here I am using the following JavaScript code: 在这里,我使用以下JavaScript代码:

function line() 
{
    canvas = document.getElementById("drawingCanvas");
    context = canvas.getContext("2d");
    canvas.onmousedown = startLine;
    canvas.onmouseup =   drawLine;
    canvas.onmouseout = stopLine;
    //canvas.onmousemove =drawLine;
};

function startLine(e) 
{
    isLine = true;
    context.beginPath();
    context.moveTo(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop);

}

function drawLine(e) 
{
    if (isLine==true) 
    {
        var x = e.pageX - canvas.offsetLeft;
        var y = e.pageY - canvas.offsetTop;
        context.lineTo(x ,y);
        context.stroke();
        context.closePath();
    }
    cPush();
}  
function stopLine() 
{
    isLine = false;
}

When I am using mousemove event for drawLine() method it is drawing multiple lines. 当我将mousemove事件用于drawLine()方法时,它正在绘制多条线。 Can you lease tell me what wrong in my code? 您能告诉我我的代码有什么问题吗?

Basically, you need to refactor your code to do beginPath+moveTo+lineTo+stroke inside mousemove. 基本上,您需要重构代码以在mousemove内部执行beginPath + moveTo + lineTo + stroke。

Otherwise you will get those multiple lines... 否则,您将获得这些多行...

In mouseDown: Save the startX/startY and set the isDown flag to indicate the drag has started: 在mouseDown中:保存startX / startY并设置isDown标志以指示拖动已开始:

function handleMouseDown(e){
  e.preventDefault();
  startX=parseInt(e.clientX-offsetX);
  startY=parseInt(e.clientY-offsetY);
  isDown=true;
}

In mouseMove: Draw a new line from startX/Y to mouseX/Y and reset startX/Y=mouseX/Y 在mouseMove中:从startX / Y到mouseX / Y画一条新线,并重置startX / Y = mouseX / Y

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

  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  ctx.beginPath();  // use beginPath for every segment of the line
  ctx.moveTo(startX,startY);
  ctx.lineTo(mouseX,mouseY);
  ctx.stroke();
  startX=mouseX;
  startY=mouseY;
}

In mouseUP: Clear the isDown flag to indicate the drag has ended: 在mouseUP中:清除isDown标志以指示拖动已结束:

function handleMouseUp(e){
  e.preventDefault();
  isDown=false;
}

Here's code and a Fiddle: http://jsfiddle.net/m1erickson/hzNg4/ 这是代码和小提琴: http : //jsfiddle.net/m1erickson/hzNg4/

<!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; }
    #canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var $canvas=$("#canvas");
    var canvasOffset=$canvas.offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;
    var scrollX=$canvas.scrollLeft();
    var scrollY=$canvas.scrollTop();

    var isDown=false;
    var startX;
    var startY;


    function handleMouseDown(e){
      e.preventDefault();
      startX=parseInt(e.clientX-offsetX);
      startY=parseInt(e.clientY-offsetY);
      isDown=true;
    }

    function handleMouseUp(e){
      e.preventDefault();
      isDown=false;
    }

    function handleMouseOut(e){
      e.preventDefault();
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      isDown=false;
    }

    function handleMouseMove(e){
      if(!isDown){return;}
      e.preventDefault();
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);

      // Put your mousemove stuff here
      ctx.beginPath();
      ctx.moveTo(startX,startY);
      ctx.lineTo(mouseX,mouseY);
      ctx.stroke();
      startX=mouseX;
      startY=mouseY;

    }

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



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

</head>

<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

Try this way ( DEMO ) : 尝试这种方式( DEMO ):

function line() {
    canvas = document.getElementById("drawingCanvas");
    context = canvas.getContext("2d");
    canvas.onmousedown = startLine;
    canvas.onmouseup = canvas.onmouseout = stopLine;
    canvas.onmousemove = drawLine;
};

function startLine(e) {
    isLine = true;
    context.beginPath();
    context.moveTo(startX = (e.pageX - canvas.offsetLeft),
    startY = (e.pageY - canvas.offsetTop));
}

function drawLine(e) {
    if (isLine) {
        var x = e.pageX - canvas.offsetLeft;
        var y = e.pageY - canvas.offsetTop;
        context.clearRect(0, 0, 300, 150);    // width = 300, height = 150
        context.beginPath();
        context.moveTo(startX, startY);
        context.lineTo(x, y);
        context.stroke();
        context.closePath();
    }
    //cPush();
}

function stopLine() {
    isLine = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用JCanvas中的鼠标事件在画布上绘制线条和矩形 - I am trying to draw a line and rectangle on canvas using mouse events in JCanvas 我正在尝试使用jcanvas中的鼠标事件绘制形状 - I am trying to draw shapes using mouse events in jcanvas 如何在Javascript中使用鼠标事件在画布上绘画 - How to draw on canvas using mouse events with Javascript 画布绘制对象的鼠标事件 - Mouse events for canvas draw objects 无法使用鼠标事件在JavaScript中的画布上绘制一个完整的圆圈 - Not able to draw a full circle on canvas in JavaScript using mouse events 当我移动鼠标时,如何用 HTML 画布绘制一条简单的直线? - How to draw a simple straight line with HTML canvas as I move the mouse? 如何使用 html canvas 和 javascript 用鼠标绘制平滑的连续线 - How to draw a smooth continuous line with mouse using html canvas and javascript 我正在尝试创建一个鼠标事件,允许用户使用“ ctrl”键和Shift键绘制红色或蓝色 - I am trying to create a mouse event that allows the user to draw in red or blue using the “ctrl” key and the shift key 在JavaScript中的画布上使用鼠标事件绘制直线 - Drawing a straight line using mouse events on canvas in javascript 试图在画布上画一个精灵,但我错过了一些东西 - trying to draw a sprite onto canvas but I am missing something
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM