# How to make an object face the mouse in Canvas?

**URL:** <https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330>\
**Category:** web dev\
**Created:** [December 13, 2019, 6:30pm UTC](https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330 "2019-12-13T18:30:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brilliant](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/brilliant/32/10552_2.png) [@brilliant](https://forum.kirupa.com/u/brilliant)\
**Post date:** [December 13, 2019, 6:30pm UTC](https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330/1 "2019-12-13T18:30:55Z")

</div>

Hello there. I am wondering how to make an object rotate to face the current position of the mouse in a canvas. I am not sure if this is a question yet but I am keen to knowing how to do this.  
For example, in [agar.io](https://agar.io), you are a circle which follows your mouse.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [December 14, 2019, 2:47am UTC](https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330/3 "2019-12-14T02:47:47Z")

</div>

This is a great question! It is one I don’t have an answer for right now, and I’m currently fiddling with a solution for you. I did a quick search, and this may help you in the interim: [https://stackoverflow.com/questions/40120470/javascript-making-image-rotate-to-always-look-at-mouse-cursor](https://stackoverflow.com/questions/40120470/javascript-making-image-rotate-to-always-look-at-mouse-cursor)

😛

---

<div class="post-metadata">

**Author:** ![brilliant](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/brilliant/32/10552_2.png) [@brilliant](https://forum.kirupa.com/u/brilliant)\
**Post date:** [December 14, 2019, 11:44am UTC](https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330/4 "2019-12-14T11:44:09Z")

</div>

Thank You! Exactly what I was looking for. I normally do use Stack Overflow but I guess this works better! 🙂

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [December 14, 2019, 9:40pm UTC](https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330/5 "2019-12-14T21:40:26Z")

</div>

Glad to hear you are unblocked 😀

There are several things with the SO solution that aren’t ideal. I wouldn’t use the slower `getBoundingClientRect` to get the mouse position relative to any offsets on the page. I would use `getMousePosition` from here: [https://www.kirupa.com/canvas/follow\_mouse\_cursor.htm](https://www.kirupa.com/canvas/follow_mouse_cursor.htm)

There are a few areas where I would just use `requestAnimationFrame` instead of _mousemove_, and I will try to provide you with a working solution in a couple of days.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [December 16, 2019, 11:40pm UTC](https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330/6 "2019-12-16T23:40:41Z")

</div>

Here is my take on this that addresses some of the perf issues I outlined in my earlier response:

```
<!DOCTYPE html>
<html>

<head>
  <title>Rotate with Mouse</title>
  <style>
    canvas {
      border: #333 10px solid;
    }
    body {
      padding: 50px;
    }
  </style>
</head>

<body>
  <canvas id="myCanvas" width="550px" height="350px"></canvas>

  <script>
    let canvas = document.querySelector("#myCanvas");
    let context = canvas.getContext("2d");

    let mouseX = 0;
    let mouseY = 0;

    // deal with page offsets
    let canvasPos = getPosition(canvas);

    let x_midpoint = canvas.width / 2;
    let y_midpoint = canvas.height / 2;

    canvas.addEventListener("mousemove", setMousePosition, false);

    function setMousePosition(e) {
      mouseX = e.clientX - canvasPos.x;
      mouseY = e.clientY - canvasPos.y;

      // ensure we update only when a frame update is happening
      requestAnimationFrame(update);
    }

    let rectangleWidth = 50;
    let rectangleHeight = 50;

    function update() {
      context.clearRect(0, 0, canvas.width, canvas.height);

      context.fillStyle = "#F0C808";
      context.fillRect(0, 0, canvas.width,canvas.height);

      let diffX = mouseX - x_midpoint;
      let diffY = mouseY - y_midpoint;

      // shift starting point to middle of canvas
      context.setTransform(1, 0, 0, 1, x_midpoint, y_midpoint);

      // rotate based on the mouse position
      context.rotate(Math.atan2(diffY, diffX));

      // the triangle
      context.beginPath();
      context.moveTo(-15, -15);
      context.lineTo(-15, 15);
      context.lineTo(30, 0);
      context.closePath();
  
      // the fill color
      context.fillStyle = "#315659";
      context.fill();

      // reset transform
      context.setTransform(1, 0, 0, 1, 0, 0);

      // the line
      context.beginPath();
      context.moveTo(mouseX, mouseY);
      context.lineTo(x_midpoint, y_midpoint);

      context.lineWidth = 2;
      context.setLineDash([2, 5]);
      context.strokeStyle = "#315659";
      context.stroke();
    }
    update();

    // Deal with page positioning shenanigans
    // https://www.kirupa.com/html5/get_element_position_using_javascript.htm
    function getPosition(el) {
      let xPosition = 0;
      let yPosition = 0;

      while (el) {
        xPosition += (el.offsetLeft - el.scrollLeft + el.clientLeft);
        yPosition += (el.offsetTop - el.scrollTop + el.clientTop);
        el = el.offsetParent;
      }
      return {
        x: xPosition,
        y: yPosition
      };
    }
  </script>

</body>

</html>

```

You can see a video of this in the following tweet:

> <https://twitter.com/kirupa/status/1206811890405408768>

This is based largely on combining the contents of the [Follow the Mouse Cursor](https://www.kirupa.com/canvas/follow_mouse_cursor.htm), [Drawing a Triangle](https://www.kirupa.com/html5/drawing_triangles_on_the_canvas.htm), [Canvas Transformations](https://www.kirupa.com/canvas/canvas_transformations.htm), and [Get an Element’s Position](https://www.kirupa.com/html5/get_element_position_using_javascript.htm) articles.

😉

---

<div class="post-metadata">

**Author:** ![brilliant](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/brilliant/32/10552_2.png) [@brilliant](https://forum.kirupa.com/u/brilliant)\
**Post date:** [December 17, 2019, 10:41am UTC](https://forum.kirupa.com/t/how-to-make-an-object-face-the-mouse-in-canvas/641330/7 "2019-12-17T10:41:51Z")

</div>

Amazing! Thank You So Much!
