setPos of text component: Python to JS

Hi all, I’m in the process of converting my Python code components (made in Builder) to JS such that my experiment will work online. I have pseudorandomization code component in my Begin Experiment tab at the start of the experiment so that the position of the text component can be controlled later on in my trials. In the position field of the text component (set every repeat), I have [vPosX,0] for one text component and [sPosX,0] for the other text component

Python code

if random()>0.5:


var sPosX, vPosX;
var y = Math.random();
if (y< 0.5) {
} else {

When I run the pilot online I immediately get the error: TypeError: Cannot read property ‘setPos’ of undefined

I’ve tried the following JS code as well, replacing (setPos) with (===) which doesn’t work either. My experiment runs until it gets to the trials, where I get the error ReferenceError: sPosX is not defined
for example:

sPosX === ([0.4]);
vPosX === ([-0.4]);

Is setPos not recognized in psychoJS? What is the correct command to set the position of a text component?

I’ve also tried referring to the names of the text components (stim 1 and stim2), but also got the error TypeError: Cannot read property ‘setPos’ of undefined immediately upon starting the experiment. In this case though, in the position element inside the text component, I had just put “$”.



The syntax in your latest comment seems all right, but apparently stim1 and stim2 aren’t defined yet when you try to change them. Try changing their positions in a code component that’s part of the routine that contains these stimuli and put your code in the “begin routine” tab.

Thanks Thomas, I tried that.
I currently am working with the following in the Begin routine tab:

var y = Math.random();
if (y< 0.5) {
} else {

However, I don’t know what to put in the Position field of my two text components, since there isn’t a variable (my code is supposed to directly set the new Position). Leaving it blank and setting it to repeat every trial does not work TypeError: setPos() missing 1 required positional argument: 'newPos’

Maybe just enter some valid value in the position field?

Hi I think this is part of a more general problem PLEASE CORRECT ME IF I AM WRONG. But if you set the thing to ‘set every repeat’ then it forces the declaration of the objects prior to the code that is defined in Begin Routine. So you cant define the objects properties in the Begin Routine Code.
I am trying to find work arounds as I type.

@philip.quinlan can’t you use the PsychoJS API to change properties in the “Begin Routine” tab? I’d expect any changes you make this way would still be reflected in a component when it’s presented

Thanks for your reply. My work around is to set the properties in the End Routine code of the immediately prior Routine.
I have also spent the w/e fighting with the Javascript to get an online version of my experiment to work.
I discovered many infelicities. For instance there was a failure to generate global variables that I had to insert manually. Also I discovered a feature of the way continueRoutine is defined and used. My partial understanding is that it kind of wants it be a global variable but in the Javascript functions that are generated these define a local variable with the same name. Now I am no expert but this seems to give rises to clashes such that the local definition overrides the global value so that the function will ignore values that you may have coded in Python.

The JS simply ignored my conditional test on running a feedback routine. In desperation I simply commented out

let continueRoutine = true;
at the start of the offending JS function

It really doesnt appear to conform to what it says on the tin!

1 Like