Reply To: Track Mouse Position

PennController for IBEX Forums Requests Track Mouse Position Reply To: Track Mouse Position

#5072
Jeremy
Keymaster

Hi Valeria,

Yes, getting the mouse’s coordinates upon click is actually easy enough, but you should make sure it’s really what you want. Keep in mind that different participants will have pages of different heights and widths. If you consider checking the mouse’s coordinates upon click, ask yourself whether it wouldn’t make more sense to place multiple clickable elements on your page, e.g. in the form of transparent canvases.

For example, this bit of script uses a MouseTracker element to keep track of the mouse’s coordinates and checks them later:

newVar("mousex"),newVar("mousey")
,
newMouseTracker("mouse")
    .callback( (x,y) => [getVar("mousex").set(x)._runPromises(),getVar("mousey").set(y)._runPromises()] )
    .start()
,
newCanvas( "wholescreen" , "100vw" , "100vh" ).print( "top at 0" , "left at 0" )
,
newSelector().add( getCanvas("wholescreen") ).wait()
,
getVar("mousex").test.is( v=>v<100 )
    .success( newText("The mouse is very much toward the left").print( 100 , getVar("mousey") , getCanvas("wholescreen") ) )
,
getVar("mousey").test.is( v=>v<100 )
    .success( newText("The mouse is very much toward the top").print( getVar("mousex") , 100 , getCanvas("wholescreen") ) )

But this other bit of script does almost the same thing more simply:

newCanvas("left", 100 , "100vh").print("left at 0", "top at 0"),
newCanvas("top", "100vw" , 100).print("left at 0", "top at 0")
,
newSelector()
    .add( getCanvas("left") )
    .callback( newText("The mouse is very much toward the left").print(100,"middle at 50vh") )
,
newSelector()
    .add( getCanvas("top") )
    .callback( newText("The mouse is very much toward the top").print("center at 50vw",100) )

Feel free to tell me more about how your experiment will use the mouse's coordinates

Jeremy