Html5/Js canvas capturing mouse coords when it moves fast

后端 未结 1 357
攒了一身酷
攒了一身酷 2020-12-22 11:50

I have a canvas where I draw a curve. onmouseup lines are drawn based on that curve and connecting specific x-points for it.

The problem is that if the mouse is

相关标签:
1条回答
  • 2020-12-22 12:50

    You are not likely "missing" any mousemove events.

    Each operating system regulates (limits) how many mousemove events per second are emitted. So moving the mouse fast will cause more distance (less resolution) between mousemove events. There is no workaround to get more mousemove points per second.

    It looks like you are capturing points to create a spline. If so, Stackoverflow's Ken Fyrstenberg has created a nice script that will create a spline when fed a set of points. You can loosen the tension on Ken's spline which will cause your spline to become more smoothed relative to the waypoints. Loosening the tension will reduce the effects of having fewer than desired mousemove waypoints.

    how to draw smooth curve through N points using javascript HTML5 canvas?

    As far as capturing mouse events in a cross-browser compliant way...

    Here's a template for capturing mouse drag events in across browsers:

    window.onload=function(){
    
      // canvas related variables
      var canvas=document.getElementById("canvas");
      var ctx=canvas.getContext("2d");
      var BB,BBoffsetX,BBoffsetY;
      setBB();
    
      // a flag indicating the mouse is being dragged
      var isDown=false;
    
      // an array of points accumulated during mouse dragging
      var ppts=[];
    
      // listen for mouse events
      canvas.onmousedown=handleMousedown;
      canvas.onmousemove=handleMousemove;
      canvas.onmouseup=handleMouseup;
      canvas.onmouseout=handleMouseup;
    
      // recalculate the canvas offset if the window is scrolled
      window.onscroll=function(e){
        var BB=canvas.getBoundingClientRect();
        offsetX=BB.left;
        offsetY=BB.top;
      }
    
    
      function handleMousedown(e){
        // tell the browser we're handling this event
        e.preventDefault();
        e.stopPropagation();
        // get the mouse position relative to the canvas
        var mouseX=e.clientX-BBoffsetX;
        var mouseY=e.clientY-BBoffsetY;
        // start a new ppts array
        ppts=[];
        // set the mouse-is-down flag
        isDown=true;
      }
    
      function handleMouseup(e){
        // if the mouse isn't being dragged, just return
        if(!isDown){return;}
        // tell the browser we're handling this event
        e.preventDefault();
        e.stopPropagation();
        // clear the mouse-is-down flag
        isDown=false;           
        // get the mouse position relative to the canvas
        var mouseX=e.clientX-BBoffsetX;
        var mouseY=e.clientY-BBoffsetY;
        // add this point to ppts
        ppts.push({x:mouseX,y:mouseY});
    
        alert('You have accumulated '+ppts.length+' points.');
      }
    
      function handleMousemove(e){
        // if the mouse isn't being dragged, just return
        if(!isDown){return;}
        // tell the browser we're handling this event
        e.preventDefault();
        e.stopPropagation();
        // get the mouse position relative to the canvas
        var mouseX=e.clientX-BBoffsetX;
        var mouseY=e.clientY-BBoffsetY;
        // add this point to ppts
        ppts.push({x:mouseX,y:mouseY});
      }
    
      // calculate the canvas offset
      function setBB(){
        BB=canvas.getBoundingClientRect();
        BBoffsetX=BB.left;
        BBoffsetY=BB.top;
      }
    
    
    }; // end window.onload;
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
    <h4>Drag mouse to accumulate ppts</h4>
    <canvas id="canvas" width=300 height=300></canvas>

    0 讨论(0)
提交回复
热议问题