如何在画布上绘制一个矩形

如何在画布上绘制一个矩形

本文介绍了如何在画布上绘制一个矩形,就像我们在油漆上画一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想在画布上画一个矩形。我希望能够从用户的鼠标中获得坐标。理想情况是用户点击一点并拖拽到另一端,就像我们使用油漆绘制的那些矩形一样。如何通过拖动鼠标绘制像我们在绘画中的矩形? (当他点击鼠标并离开时,如何获得鼠标的坐标?)

Say i want to draw a rectangle on canvas. I want to be able to get the co-ordinates from user's mouse. Ideal scenario is user clicks at a point and drags down to another end like those rectangles we draw using paint. How can i draw a rectangle like we do in paint by dragging mouse? (how to get the co-ordinates of the mouse when he clicks mouse and leaves at?)

推荐答案

这是一个如何概述在画布上拖动一个矩形:

Here's a outline of how to drag-draw a rectangle on canvas:

在mousedown:

In mousedown:


  • 保存起始鼠标位置

  • 设置一个标志,指示拖动已经开始

在mousemove中:

In mousemove:


  • 清除前一个矩形的画布

  • 根据起始vs计算矩形宽度/高度当前鼠标位置

  • 从起始XY到当前鼠标位置绘制一个矩形

In mouseup:

In mouseup:


  • 清除拖动标记,因为拖动已结束

以下是示例代码和演示:

Here's example code and a Demo: http://jsfiddle.net/m1erickson/6E2yd/

<!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(){

    // get references to the canvas and context
    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    // style the context
    ctx.strokeStyle = "blue";
    ctx.lineWidth=3;

    // calculate where the canvas is on the window
    // (used to help calculate mouseX/mouseY)
    var $canvas=$("#canvas");
    var canvasOffset=$canvas.offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;
    var scrollX=$canvas.scrollLeft();
    var scrollY=$canvas.scrollTop();

    // this flage is true when the user is dragging the mouse
    var isDown=false;

    // these vars will hold the starting mouse position
    var startX;
    var startY;


    function handleMouseDown(e){
      e.preventDefault();
      e.stopPropagation();

      // save the starting x/y of the rectangle
      startX=parseInt(e.clientX-offsetX);
      startY=parseInt(e.clientY-offsetY);

      // set a flag indicating the drag has begun
      isDown=true;
    }

    function handleMouseUp(e){
      e.preventDefault();
      e.stopPropagation();

      // the drag is over, clear the dragging flag
      isDown=false;
    }

    function handleMouseOut(e){
      e.preventDefault();
      e.stopPropagation();

      // the drag is over, clear the dragging flag
      isDown=false;
    }

    function handleMouseMove(e){
      e.preventDefault();
      e.stopPropagation();

      // if we're not dragging, just return
      if(!isDown){return;}

      // get the current mouse position
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);

      // Put your mousemove stuff here

      // clear the canvas
      ctx.clearRect(0,0,canvas.width,canvas.height);

      // calculate the rectangle width/height based
      // on starting vs current mouse position
      var width=mouseX-startX;
      var height=mouseY-startY;

      // draw a new rect from the start position
      // to the current mouse position
      ctx.strokeRect(startX,startY,width,height);

    }

    // listen for mouse events
    $("#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>
    <h4>Drag the mouse to create a rectangle</h4>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>




  • 清除

  • 这篇关于如何在画布上绘制一个矩形,就像我们在油漆上画一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:37