简体   繁体   中英

Drawing on mouse move with Canvas, HMTL5?

What I am trying to achieve is a simple functionality to draw lines on a canvas when the mouse is clicked.

I have looked at code online and am trying to implement it myself but it won't work.

So far:

<html>
<canvas id="myCanvas" width="400" height="500"> </canvas>
</html>

<script type="text/javascript">
var el = document.getElementById('myCanvas');
    var ctx = el.getContext('2d');
    var isDrawing;

    el.onmousedown = function(e) {

      isDrawing = true;
      ctx.moveTo(e.clientX, e.clientY);
    };

    el.onmousemove = function(e) {
      if (isDrawing) {
        ctx.lineTo(e.clientX, e.clientY);
        ctx.stroke();
      }
    };
    el.onmouseup = function() {

      isDrawing = false;
    };

</script>

This doesn't output anything on the canvas however. I don't fully understand the event handling aspect of the code (ie, e.clientX, e.clientY) I suspect I must add to this code for these parts to have the desired effect?

粗略浏览一下,您会在第2行的末尾缺少“>”。

A couple of issues:

  • Adjust your mouse positions by the offset of the canvas (unless your canvas is at top-left of the browser)

  • do all drawing commands in mousemove (otherwise you will restroke the line with every ctx.stroke)

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

<!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 BB=canvas.getBoundingClientRect();
    var offsetX=BB.left;
    var offsetY=BB.top;

    var lastX,lastY;
    var isDown=false;

    canvas.onmousedown=handleMousedown;
    canvas.onmousemove=handleMousemove;
    canvas.onmouseup=handleMouseup;


    function handleMousedown(e){
         e.preventDefault();
         e.stopPropagation();
         lastX=e.clientX-offsetX;
         lastY=e.clientY-offsetY;
         isDown=true;
    }

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

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

         if(!isDown){return;}

         var mouseX=e.clientX-offsetX;
         var mouseY=e.clientY-offsetY;

         ctx.beginPath();
         ctx.moveTo(lastX,lastY);
         ctx.lineTo(mouseX,mouseY);
         ctx.stroke();

         lastX=mouseX;
         lastY=mouseY;
    }


}); // end $(function(){});
</script>
</head>
<body>
    <h4>Drag mouse to draw.</h4>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM