Randy Matinee
Randy Matinee

Reputation: 71

How to set the location of the cursor. (p5.js JavaScript)

I would like to set the cursors location. I've tried really off the wall solutions such as...

MouseX = (location in X axis);
MouseY = (location in Y axis);

However this doesn't work (obviously), I'm really not sure how to move the cursors position with my script, does anyone have suggestions?

Upvotes: 0

Views: 1438

Answers (3)

Liam Frey
Liam Frey

Reputation: 11

This may not help you now, but it should help other people:

let mouse = {
    x: 0,
    y: 0,
    img: null
};
function setup() {
    createCanvas(windowWidth, windowHeight);
}
function doubleClicked() {
    requestPointerLock();
}
function button(x,y,w,h){
    return mouse.x>x && mouse.y>y && mouse.x<w+x && mouse.y<h+y;
}
function draw() {
    background(0);
    if (button(20,20,100,100)){
        fill(100);
        if (mouseIsPressed){
            fill(255);
            mouse.x=width/2;
            mouse.y=height/2;
        }
    }
    rect(20,20,100,100,10);
    mouse.x += movedX;
    mouse.y += movedY;
    noFill();
    stroke(255);
    strokeWeight(3);
    ellipse(mouse.x, mouse.y, 20,20);
}

sketch: editor.p5js.org

Upvotes: 1

wersimmon
wersimmon

Reputation: 2869

You can't. mouseX and mouseY are system variables that can only be used to retrieve the value of the cursor position. Changing them will not change the cursor position. You could look into the pointerlock API, which is supported in most browsers.

Upvotes: 0

Shawn Janas
Shawn Janas

Reputation: 2743

I'm not sure if you can due to the browsers security constrains. I know in Java you can accomplish such functionality but in JavaScript, I don't think it is possible.

Upvotes: 0

Related Questions