Tracking Mouse Move with Velo Code

This example implements mousemove event handler using a custom element.
The need was raised few times by designers who wanted to create animations based on mouse move and position.
Here is the live demo and a template to view the code.
2 things to keep in mind when using custom element:

  1. The solution can only run on premium sites

  2. Code may only run the published site, i.e. in some case it won’t work on preview mode

I’m focusing only on the mousemove code ( highlighted code ) and not on the animation code.

Homepage code

import {setArrowDirection,getArrowPosition} from 'public/arrow-rotate.js';
$w.onReady(function () 
    $w('#customElement1').on('coor', (event) => {
        $w('#coorText').text="Mouse location: "+event.detail.x+','+event.detail.y;
        //Arrow animation
        setArrowDirection(event.detail.x,event.detail.y);
    });
    //Get The arrow element position data from the Custom Element
    $w('#customElement1').on('element-data', (event) => {
        getArrowPosition(event.detail);
        console.log('arrow position retrieved from customelement');
    });
    })

mouse-move.js (source of the custom element)

const functionsStr=
    `document.body.addEventListener("mousemove", myFunction);
    function myFunction(e) {
            var x = e.clientX;
            var y = e.clientY;
            var coor = "Coordinates: (" + x + "," + y + ")";
            document.getElementsByTagName("mouse-move")[0].dispatchEvent(new CustomEvent('coor',{detail:{x:x,y:y}})); 
        }`;

class MouseMove extends HTMLElement {
    
        constructor() {
            super();
        }

        connectedCallback() {
            var funcScript=document.createElement('script');
            funcScript.innerHTML=functionsStr;
            this.appendChild(funcScript);
        }
        static get observedAttributes() {
            return ['element-id'];
        }
        attributeChangedCallback(name, oldValue, newValue) {
            const elemenPosition=document.getElementById(newValue).getBoundingClientRect();
            this.dispatchEvent(new CustomEvent('element-data',{detail: elemenPosition}));
        }
   
}
customElements.define('mouse-move', MouseMove);

If you check the animation code then you can see a code solution to find the position of the arrow element.

Have fun!

16 Likes

Nice one!

Very useful, thank you very much!

           Hello. 

 Please clarify the point: 1. - why is the condition of premium version www, when I can't find anything similar in the comparison of premium plans? What is the condition (restriction) and what premium plans are needed? 

 Indeed with your example it says that the custom element only works with premium plans, I copied everything from your example (hopefully well, I'm not entirely sure) to my site and it really doesn't work. I plan to then just use the mouse cursor position detection feature, I have called for this feature to be available on WIX years ago, I want to hide everything on my site except the background video when the visitor won't move the mouse and have no article open. The buttons to view articles only appear when the mouse is moved ... 

 Thank you in advance for the answer.

The custom component will work on any premium site with their own domain and the Wix ads removed. The reason for that is mostly technical.
I suggest you try a basic custom web component example and once it works for you, jump back to the mouse tracking solution. A good starting point Velo: About Custom Elements | Help Center | Wix.com

This is great! Thanks so much!
Do you know how to make the element move with the mouse? Not necessarily changing the mouse curser icon but I guess that could work?
Any help would me much appreciated!

Moving elements can be done with the animation API wix-animations-frontend - Velo API Reference - Wix.com

Thank you so much for this excellent post!

I was able to create this effect in Editor X thanks to you @moran-frumer
https://www.iloveeditorx.com/mouse-position

@moran-frumer what about the other way around, like dispatchEvent on $w.onReady() and document.AddListener on the custom element, can we do that?

I didn’t check this option.

but is it possible to do document.dispatchEvent from $w.onReady()? @moran-frumer

when I wrote the instruction" import { setArrowDirection , getArrowPosition } from ‘public/arrow-rotate.js’ ;" it showed me a error “Cannot find module ‘public/arrow-rotate.js’ in ‘public/pages/wpii7.js’”

how can i solve it? please help

Make sure you have the file public/arrow-rotate.js on you project. You can copy it from this template

Woow incredible resource THANK YOU!!! :fire:

Does this only work for EditorX, or will is also work in the Classic ?

This demo was built on the classic editor