Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Is it me or the computer?

KittenKatja

Well-Known Coder
I got
JavaScript:
addEventListener(
    "mousemove",
    () => {
        let x = event.screenX / 100 * window.innerWidth;
        let y = event.screenY / 100 * window.innerHeight;
        let a = document.documentElement.style;
        a.setProperty("--mouseX", x + "%");
        a.setProperty("--mouseY", y + "%");
    }
)
but the values are anything but inside the 0 to 100% range.
I have also switched around the equation a lot.
 
But it's an arrow function, and I'm not inserting anything from outside the function to be used inside the function.
It's like declaring a const without a value.

Also, it works anyway, as the problem is the equation, not the arrow function.
 
Is it actually changing anything? There is a defined in Javascript pages default "event" that you may be using in that function instead. If you want 0 to 100% then divide by inner height and width and multiply by 100. Like this:
JavaScript:
addEventListener(
    "mousemove",
    function (event) {
        //0 to 100 x and y
        let x = event.screenX / window.innerWidth * 100;
        let y = event.screenY / window.innerHeight * 100;
        let a = document.documentElement.style;
        a.setProperty("--mouseX", x + "%");
        a.setProperty("--mouseY", y + "%");
        //X E
    }
    //X E
)
//X E
You may also use console.log(x+", "+y) or something like it to see what x and y are. Anyway, this seems like correct equation for 0 to 100% to me. X E.
 
The Lowest Y value is 9.12906610703043%, and the highest is 109.02413431269676%. (0% and 99.9074074074074% in full-screen)
But the X value stays within 0% to 99.94791666666667%. (Even with DevTools open)
How can I limit it to only the website?

I'm also using clamp(x).toFixed(4) + "%" in the last step.
Code:
function clamp(num, min, max) {return Math.min(Math.max(num, min), max);}
It results in NaN%, and putting Number() in doesn't solve it.
 
I got
JavaScript:
addEventListener(
    "mousemove",
    () => {
        let x = event.screenX / 100 * window.innerWidth;
        let y = event.screenY / 100 * window.innerHeight;
        let a = document.documentElement.style;
        a.setProperty("--mouseX", x + "%");
        a.setProperty("--mouseY", y + "%");
    }
)
but the values are anything but inside the 0 to 100% range.
I have also switched around the equation a lot.
My question, to @JosiahMaybe's point... where are you getting the variable "event" from? I don't see it defined anywhere in your code snippet... plus, whenever you are doing an event listener, you should be passing the "event", or "e" as you will see in some cases because laziness, variable into your callback function.
 
The Lowest Y value is 9.12906610703043%, and the highest is 109.02413431269676%. (0% and 99.9074074074074% in full-screen)
But the X value stays within 0% to 99.94791666666667%. (Even with DevTools open)
How can I limit it to only the website?

I'm also using clamp(x).toFixed(4) + "%" in the last step.
Code:
function clamp(num, min, max) {return Math.min(Math.max(num, min), max);}
It results in NaN%, and putting Number() in doesn't solve it.
It seems like you need to input min and max to clamp. You could also try using document.body.addEventListener and event.client(X or Y) or event.page(X or Y). Result of clamp with undefined arguments should be NaN seemingly. X E.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom