How do I get the coordinates of a mouse click on a canvas element?

后端 未结 22 2375
忘掉有多难
忘掉有多难 2020-11-21 23:56

What\'s the simplest way to add a click event handler to a canvas element that will return the x and y coordinates of the click (relative to the canvas element)?

No

相关标签:
22条回答
  • 2020-11-22 00:38

    I was creating an application having a canvas over a pdf, that involved a lot of resizes of canvas like Zooming the pdf-in and out, and in turn on every zoom-in/out of PDF I had to resize the canvas to adapt the size of the pdf, I went through lot of answers in stackOverflow, and didn't found a perfect solution that will eventually solve the problem.

    I was using rxjs and angular 6, and didn't found any answer specific to the newest version.

    Here is the entire code snippet that would be helpful, to anyone leveraging rxjs to draw on top of canvas.

      private captureEvents(canvasEl: HTMLCanvasElement) {
    
        this.drawingSubscription = fromEvent(canvasEl, 'mousedown')
          .pipe(
            switchMap((e: any) => {
    
              return fromEvent(canvasEl, 'mousemove')
                .pipe(
                  takeUntil(fromEvent(canvasEl, 'mouseup').do((event: WheelEvent) => {
                    const prevPos = {
                      x: null,
                      y: null
                    };
                  })),
    
                  takeUntil(fromEvent(canvasEl, 'mouseleave')),
                  pairwise()
                )
            })
          )
          .subscribe((res: [MouseEvent, MouseEvent]) => {
            const rect = this.cx.canvas.getBoundingClientRect();
            const prevPos = {
              x: Math.floor( ( res[0].clientX - rect.left ) / ( rect.right - rect.left ) * this.cx.canvas.width ),
              y:  Math.floor( ( res[0].clientY - rect.top ) / ( rect.bottom - rect.top ) * this.cx.canvas.height )
            };
            const currentPos = {
              x: Math.floor( ( res[1].clientX - rect.left ) / ( rect.right - rect.left ) * this.cx.canvas.width ),
              y: Math.floor( ( res[1].clientY - rect.top ) / ( rect.bottom - rect.top ) * this.cx.canvas.height )
            };
    
            this.coordinatesArray[this.file.current_slide - 1].push(prevPos);
            this.drawOnCanvas(prevPos, currentPos);
          });
      }
    

    And here is the snippet that fixes, mouse coordinates relative to size of the canvas, irrespective of how you zoom-in/out the canvas.

    const prevPos = {
      x: Math.floor( ( res[0].clientX - rect.left ) / ( rect.right - rect.left ) * this.cx.canvas.width ),
      y:  Math.floor( ( res[0].clientY - rect.top ) / ( rect.bottom - rect.top ) * this.cx.canvas.height )
    };
    const currentPos = {
      x: Math.floor( ( res[1].clientX - rect.left ) / ( rect.right - rect.left ) * this.cx.canvas.width ),
      y: Math.floor( ( res[1].clientY - rect.top ) / ( rect.bottom - rect.top ) * this.cx.canvas.height )
    };
    
    0 讨论(0)
  • 2020-11-22 00:39

    You could just do:

    var canvas = yourCanvasElement;
    var mouseX = (event.clientX - (canvas.offsetLeft - canvas.scrollLeft)) - 2;
    var mouseY = (event.clientY - (canvas.offsetTop - canvas.scrollTop)) - 2;
    

    This will give you the exact position of the mouse pointer.

    0 讨论(0)
  • 2020-11-22 00:39

    Here is a simplified solution (this doesn't work with borders/scrolling):

    function click(event) {
        const bound = event.target.getBoundingClientRect();
    
        const xMult = bound.width / can.width;
        const yMult = bound.height / can.height;
    
        return {
            x: Math.floor(event.offsetX / xMult),
            y: Math.floor(event.offsetY / yMult),
        };
    }
    
    0 讨论(0)
  • 2020-11-22 00:40

    I'm not sure what's the point of all these answers that loop through parent elements and do all kinds of weird stuff.

    The HTMLElement.getBoundingClientRect method is designed to to handle actual screen position of any element. This includes scrolling, so stuff like scrollTop is not needed:

    (from MDN) The amount of scrolling that has been done of the viewport area (or any other scrollable element) is taken into account when computing the bounding rectangle

    Normal image

    The very simplest approach was already posted here. This is correct as long as no wild CSS rules are involved.

    Handling stretched canvas/image

    When image pixel width isn't matched by it's CSS width, you'll need to apply some ratio on pixel values:

    /* Returns pixel coordinates according to the pixel that's under the mouse cursor**/
    HTMLCanvasElement.prototype.relativeCoords = function(event) {
      var x,y;
      //This is the current screen rectangle of canvas
      var rect = this.getBoundingClientRect();
      var top = rect.top;
      var bottom = rect.bottom;
      var left = rect.left;
      var right = rect.right;
      //Recalculate mouse offsets to relative offsets
      x = event.clientX - left;
      y = event.clientY - top;
      //Also recalculate offsets of canvas is stretched
      var width = right - left;
      //I use this to reduce number of calculations for images that have normal size 
      if(this.width!=width) {
        var height = bottom - top;
        //changes coordinates by ratio
        x = x*(this.width/width);
        y = y*(this.height/height);
      } 
      //Return as an array
      return [x,y];
    }
    

    As long as the canvas has no border, it works for stretched images (jsFiddle).

    Handling CSS borders

    If the canvas has thick border, the things get little complicated. You'll literally need to subtract the border from the bounding rectangle. This can be done using .getComputedStyle. This answer describes the process.

    The function then grows up a little:

    /* Returns pixel coordinates according to the pixel that's under the mouse cursor**/
    HTMLCanvasElement.prototype.relativeCoords = function(event) {
      var x,y;
      //This is the current screen rectangle of canvas
      var rect = this.getBoundingClientRect();
      var top = rect.top;
      var bottom = rect.bottom;
      var left = rect.left;
      var right = rect.right;
      //Subtract border size
      // Get computed style
      var styling=getComputedStyle(this,null);
      // Turn the border widths in integers
      var topBorder=parseInt(styling.getPropertyValue('border-top-width'),10);
      var rightBorder=parseInt(styling.getPropertyValue('border-right-width'),10);
      var bottomBorder=parseInt(styling.getPropertyValue('border-bottom-width'),10);
      var leftBorder=parseInt(styling.getPropertyValue('border-left-width'),10);
      //Subtract border from rectangle
      left+=leftBorder;
      right-=rightBorder;
      top+=topBorder;
      bottom-=bottomBorder;
      //Proceed as usual
      ...
    }
    

    I can't think of anything that would confuse this final function. See yourself at JsFiddle.

    Notes

    If you don't like modifying the native prototypes, just change the function and call it with (canvas, event) (and replace any this with canvas).

    0 讨论(0)
  • 2020-11-22 00:42

    So this is both simple but a slightly more complicated topic than it seems.

    First off there are usually to conflated questions here

    1. How to get element relative mouse coordinates

    2. How to get canvas pixel mouse coordinates for the 2D Canvas API or WebGL

    so, answers

    How to get element relative mouse coordinates

    Whether or not the element is a canvas getting element relative mouse coordinates is the same for all elements.

    There are 2 simple answers to the question "How to get canvas relative mouse coordinates"

    Simple answer #1 use offsetX and offsetY

    canvas.addEventListner('mousemove', (e) => {
      const x = e.offsetX;
      const y = e.offsetY;
    });
    

    This answer works in Chrome, Firefox, and Safari. Unlike all the other event values offsetX and offsetY take CSS transforms into account.

    The biggest problem with offsetX and offsetY is as of 2019/05 they don't exist on touch events and so can't be used with iOS Safari. They do exist on Pointer Events which exist in Chrome and Firefox but not Safari although apparently Safari is working on it.

    Another issue is the events must be on the canvas itself. If you put them on some other element or the window you can not later choose the canvas to be your point of reference.

    Simple answer #2 use clientX, clientY and canvas.getBoundingClientRect

    If you don't care about CSS transforms the next simplest answer is to call canvas. getBoundingClientRect() and subtract the left from clientX and top from clientY as in

    canvas.addEventListener('mousemove', (e) => {
      const rect = canvas.getBoundingClientRect();
      const x = e.clientX - rect.left;
      const y = e.clientY - rect.top;
    });
    

    This will work as long as there are no CSS transforms. It also works with touch events and so will work with Safari iOS

    canvas.addEventListener('touchmove', (e) => {
      const rect = canvas. getBoundingClientRect();
      const x = e.touches[0].clientX - rect.left;
      const y = e.touches[0].clientY - rect.top;
    });
    

    How to get canvas pixel mouse coordinates for the 2D Canvas API

    For this we need to take the values we got above and convert from the size the canvas is displayed to the number of pixels in the canvas itself

    with canvas.getBoundingClientRect and clientX and clientY

    canvas.addEventListener('mousemove', (e) => {
      const rect = canvas.getBoundingClientRect();
      const elementRelativeX = e.clientX - rect.left;
      const elementRelativeY = e.clientY - rect.top;
      const canvasRelativeX = elementRelativeX * canvas.width / rect.width;
      const canvasRelativeY = elementRelativeY * canvas.height / rect.height;
    });
    

    or with offsetX and offsetY

    canvas.addEventListener('mousemove', (e) => {
      const elementRelativeX = e.offsetX;
      const elementRelativeX = e.offsetY;
      const canvasRelativeX = elementRelativeX * canvas.width / canvas.clientWidth;
      const canvasRelativeY = elementRelativeX * canvas.height / canvas.clientHeight;
    });
    

    Note: In all cases do not add padding or borders to the canvas. Doing so will massively complicate the code. Instead of you want a border or padding surround the canvas in some other element and add the padding and or border to the outer element.

    Working example using event.offsetX, event.offsetY

    [...document.querySelectorAll('canvas')].forEach((canvas) => {
      const ctx = canvas.getContext('2d');
      ctx.canvas.width  = ctx.canvas.clientWidth;
      ctx.canvas.height = ctx.canvas.clientHeight;
      let count = 0;
    
      function draw(e, radius = 1) {
        const pos = {
          x: e.offsetX * canvas.width  / canvas.clientWidth,
          y: e.offsetY * canvas.height / canvas.clientHeight,
        };
        document.querySelector('#debug').textContent = count;
        ctx.beginPath();
        ctx.arc(pos.x, pos.y, radius, 0, Math.PI * 2);
        ctx.fillStyle = hsl((count++ % 100) / 100, 1, 0.5);
        ctx.fill();
      }
    
      function preventDefault(e) {
        e.preventDefault();
      }
    
      if (window.PointerEvent) {
        canvas.addEventListener('pointermove', (e) => {
          draw(e, Math.max(Math.max(e.width, e.height) / 2, 1));
        });
        canvas.addEventListener('touchstart', preventDefault, {passive: false});
        canvas.addEventListener('touchmove', preventDefault, {passive: false});
      } else {
        canvas.addEventListener('mousemove', draw);
        canvas.addEventListener('mousedown', preventDefault);
      }
    });
    
    function hsl(h, s, l) {
      return `hsl(${h * 360 | 0},${s * 100 | 0}%,${l * 100 | 0}%)`;
    }
    .scene {
      width: 200px;
      height: 200px;
      perspective: 600px;
    }
    
    .cube {
      width: 100%;
      height: 100%;
      position: relative;
      transform-style: preserve-3d;
      animation-duration: 16s;
      animation-name: rotate;
      animation-iteration-count: infinite;
      animation-timing-function: linear;
    }
    
    @keyframes rotate {
      from { transform: translateZ(-100px) rotateX(  0deg) rotateY(  0deg); }
      to   { transform: translateZ(-100px) rotateX(360deg) rotateY(720deg); }
    }
    
    .cube__face {
      position: absolute;
      width: 200px;
      height: 200px;
      display: block;
    }
    
    .cube__face--front  { background: rgba(255, 0, 0, 0.2); transform: rotateY(  0deg) translateZ(100px); }
    .cube__face--right  { background: rgba(0, 255, 0, 0.2); transform: rotateY( 90deg) translateZ(100px); }
    .cube__face--back   { background: rgba(0, 0, 255, 0.2); transform: rotateY(180deg) translateZ(100px); }
    .cube__face--left   { background: rgba(255, 255, 0, 0.2); transform: rotateY(-90deg) translateZ(100px); }
    .cube__face--top    { background: rgba(0, 255, 255, 0.2); transform: rotateX( 90deg) translateZ(100px); }
    .cube__face--bottom { background: rgba(255, 0, 255, 0.2); transform: rotateX(-90deg) translateZ(100px); }
    <div class="scene">
      <div class="cube">
        <canvas class="cube__face cube__face--front"></canvas>
        <canvas class="cube__face cube__face--back"></canvas>
        <canvas class="cube__face cube__face--right"></canvas>
        <canvas class="cube__face cube__face--left"></canvas>
        <canvas class="cube__face cube__face--top"></canvas>
        <canvas class="cube__face cube__face--bottom"></canvas>
      </div>
    </div>
    <pre id="debug"></pre>

    Working example using canvas.getBoundingClientRect and event.clientX and event.clientY

    const canvas = document.querySelector('canvas');
    const ctx = canvas.getContext('2d');
    ctx.canvas.width  = ctx.canvas.clientWidth;
    ctx.canvas.height = ctx.canvas.clientHeight;
    let count = 0;
    
    function draw(e, radius = 1) {
      const rect = canvas.getBoundingClientRect();
      const pos = {
        x: (e.clientX - rect.left) * canvas.width  / canvas.clientWidth,
        y: (e.clientY - rect.top) * canvas.height / canvas.clientHeight,
      };
      ctx.beginPath();
      ctx.arc(pos.x, pos.y, radius, 0, Math.PI * 2);
      ctx.fillStyle = hsl((count++ % 100) / 100, 1, 0.5);
      ctx.fill();
    }
    
    function preventDefault(e) {
      e.preventDefault();
    }
    
    if (window.PointerEvent) {
      canvas.addEventListener('pointermove', (e) => {
        draw(e, Math.max(Math.max(e.width, e.height) / 2, 1));
      });
      canvas.addEventListener('touchstart', preventDefault, {passive: false});
      canvas.addEventListener('touchmove', preventDefault, {passive: false});
    } else {
      canvas.addEventListener('mousemove', draw);
      canvas.addEventListener('mousedown', preventDefault);
    }
    
    function hsl(h, s, l) {
      return `hsl(${h * 360 | 0},${s * 100 | 0}%,${l * 100 | 0}%)`;
    }
    canvas { background: #FED; }
    <canvas width="400" height="100" style="width: 300px; height: 200px"></canvas>
    <div>canvas deliberately has differnt CSS size vs drawingbuffer size</div>

    0 讨论(0)
  • 2020-11-22 00:42

    Hey, this is in dojo, just cause it's what I had the code in already for a project.

    It should be fairly Obvious how to convert it back to non dojo vanilla JavaScript.

      function onMouseClick(e) {
          var x = e.clientX;
          var y = e.clientY;
      }
      var canvas = dojo.byId(canvasId);
      dojo.connect(canvas,"click",onMouseClick);
    

    Hope that helps.

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