Follow the Mouse Cursor

This is a companion discussion topic for the original entry at :slight_smile:


Sorry for coming this late but I’m currently working on this kinda stuff. And I know that this tutorial only works for white background as the whole canvas is removed whenever the mouse cursor moves, but in my case, there is an img tag in background, as a result it would remove the img as well. Hence, do you have any idea to fix this? Any comments will be appreciated.


Are you using a canvas to have the element follow the mouse? If not, there may be an easier way using the DOM APIs :slight_smile:


I’m not sure if I get your question but what I have done is exactly what you wrote in the tutorial up there :slight_smile: The only difference is that somehow the way using mouseX = e.clientX - canvasPos.x; doesnt help me get the exact position of the mouse, then I had to use mouseX = e.pageX - $(this).offset().left; instead.


Do you have a link to your example?

What I meant by my canvas question is this: for most cases, you don’t need to use the canvas to draw a shape and move it around. If you are not going to be relying on the canvas for additional functionality, you may be better off using a simple div element and moving that around the screen instead :stuck_out_tongue:

I go into a bit more detail on the differences here:

#6 Here is the link to my code currently. So yeah I’m using canvas to get the element follow the mouse. Could you pls guide me how to get it by DOM APIs?

Script help

Sure! Do you need it constrained to a particular area? Or will it be throughout the entire page similar to replacing your current cursor with a custom one? :slight_smile:

For the entire page, here is an example:

The full code is:

<!DOCTYPE html>

    <title>Follow Mouse!</title>

        body {
            min-width: 100%;
            min-height: 100%;
        #moveItem {
            width: 75px;
            height: 75px;
            background-color: #FF3333;
            position: absolute;
            border-radius: 50%;

    <div id="moveItem"></div>
        var item = document.querySelector("#moveItem");
        var itemRect = item.getBoundingClientRect();

        document.addEventListener("mousemove", followMouse, false);

        function followMouse(e) {
            var xPos = e.pageX - itemRect.width / 2;
            var yPos = e.pageY - itemRect.height / 2;

            console.log(xPos + " " + yPos);

   = "translate3d(" + xPos + "px, " + yPos + "px, 0)";



Thank you so very much for helping me out! Yeah it should be just in a particular area, like in my case it’s an image. I’m taking a look at ur code and trying to figure it out :slight_smile:


Let me know if you get stuck on that, and I can try to create a sample. You’ll find that this DOM approach is much easier to handle when working with backgrounds made up of images, gradients, and other non-simple things :stuck_out_tongue:


ok :slight_smile: I have a couple of questions. Why would you use the listener? and what constrained it to be in a specific area? I have googled out and here is what I got


Sorry, it’s this one


ok I seem to nearly figure it out. But still getting stuck in positioning the follower , in my case it’s a square, so that its top left corner could fit perfectly to the mouse cursor , been trying to set xPos = e.pageX but not succeeded. And let’s say I have a container div (imagine it as the image tag in my case) that wraps up the follower div.


From looking at your fiddle, it seems like the mouse cursor is set to the top-left corner of the square :stuck_out_tongue:


yea it’s supposed to be so, but in fact they are too far away from each other…


I have your code exactly perfect but i get this error:
Uncaught TypeError: Cannot read property ‘addEventListener’ of undefined


Strange. It means the element you are calling addEventListener on is undefined. Can you check if your querySelector call is going for the correct element? If you can post your code, that will also help figure out what is going on :slight_smile:


I love this!!! So useful. I am trying to modify the code so that it does actually leave a cool finger painting effect on the full HTML page. I have tried everything within my limited knowledge but nothing is working.
Please help. Thanks


Are you using a canvas element to leave the finger painting effect?


I have tried using canvas, and that seems to work for the finger painting effect-The effect am trying to use is so that the whole HTML can have the whole finger painting effect.


You can overlay the canvas on top of your HTML elements. With a transparent background, it will look as if you are finger-painting over your HTML content :slight_smile:

The reason why I prefer the canvas approach is performance. You can simulate this effect on HTML using DOM elements where for every few pixels of movement, you place an HTML element at the location of your finger or mouse cursor. For a lot of elements, that can start to get really memory intensive. (I outline the differences between the DOM and Canvas here)