"continueRoutine = false" not working properly in PsychoJS

OS: Windows 11
Version: PsychoPy 2024.1.5
Standard Standalone: Yes
Goal: End routine using continueRoutine = false in JS tab of code component

Problem description:
I am translating a .psyexp file to JavaScript, with the help of the builder. Some custom code components break with the auto-translator. Among those is this Python code:

Each frame:
if redSquare.contains(mouseRedSquare):
continueRoutine = False

As you can see I check whether a mouse component “mouseRedSquare” is within the bounding box of a polygon “redSquare”, and when it is I want to end the routine.

I wrote this JS code to do that (it’s a bit of a mess):

Each frame:
mouseRedSquarePos = mouseRedSquare.getPos();
function convertToHeightUnits(mouseRedSquarePos) {
let [mouseX, mouseY] = mouseRedSquarePos;
let heightX = ((mouseX * (psychoJS.window.size[0] / psychoJS.window.size[1]))/2); // Convert x to height units
let heightY = (mouseY/2); // Convert y to height units

return [heightX, heightY];

let [XRedSquare, YRedSquare] = convertToHeightUnits(mouseRedSquarePos);

ratio = psychoJS.window.size[0] / psychoJS.window.size[1]
console.log([XRedSquare, YRedSquare]);
console.log([-0.0625ratio, -0.4125ratio]);

if (-0.00625 * ratio <= XRedSquare && 0.00625 * ratio >= XRedSquare && YRedSquare >= -0.4125 && YRedSquare <= -0.3875) {
continueRoutine = false;

This code works fine the first time I encounter the routine. However, it only works once. When I later in the experiment use the same routine, placing my cursor on the red square no longer ends the routine for some reason.

What I tried:
I have tried to split the final if statement and the continueRoutine = false using some counters and ‘==’ logic. Didn’t work.
I tried continueRoutine = true in the “begin routine” tab. Didn’t work.
I tried to use some other logic to check whether the cursor is in the red square:

(mouseRSPos = mouseRedSquare.getPos();
mouseRSPosHeight = [(mouseRSPos[0]/2)*psychoJS.window.size[0]/psychoJS.window.size[1], mouseRSPos[1]/2];
mouseRSPosPix = [mouseRSPos[0]*psychoJS.window.size[0], mouseRSPos[1]*psychoJS.window.size[1]];

if (redSquare.contains(mouseRSPosPix, ‘pix’)){
continueRoutine = false;

This didn’t work even once, so I disabled it.

I am at wit’s end, any help is very much appreciated!

Thanks in advance :smiley:

The contains method should auto translate. Why are you manually editing the JS code?

Hi wakecarter,
Thanks for your response! That’s a fair question.

I did it manually because the auto translate does not work.

In the experiment settings, I’ve got the screen units set to height, same as the spatial units for the redSquare. I thought maybe the mouse gives its position using different units? Either way I did not manage to get the auto translate version working.

Kind regards,