psychopy.org | Reference | Downloads | Github

Adapt polygon size/position to text size/position

Hello everyone,
For a mouse-tracking experiment to be run online I need to create clickable transparent polygons on top of my two text stimuli. I’d like the parameters of the polygon to dynamically adapt to those of the texts.
Position of text is defined by the following code:

#Begin experiment
target_positions = [0.4, -0.4] * 5
shuffle(target_positions)
#Begin routine
target_x = target_positions.pop()
competitor_x = target_x * -1
thisExp.addData('target_x', target_x)
thisExp.addData('competitor_x', competitor_x)

Size of text is defined automatically through number of characters I guess.

There’s a target_text component with position: [target_x, 0] and a compet_text component with position: [competitor_x, 0].

So far I’ve tried setting the size of the position of the polygons (polygon_target and polygon_compet) like this: [target_x[trials.thisN][0], 0] but I don’t really know what I’m doing and so it doesn’t work.
For now I’ve only set constant sizes like (0.5, 0.1) because I’ve tried a few different things like (target_text$width, target_text$height) but that doesn’t work either - again, I don’t know how to code.

Finally, this will all need to be transferred to Pavlovia. I’ve tried unsuccessfully to convert the randomization to JS but that’s in another one of my posts Randomization not working online (problem with "map"?)). It would just be perfect if any solution for the polygons would come with its JS translation.

Your help is much appreciated, as always. Thank you!

Hi @Icrible,

So you already have a functioning method of randomising the text position? If so, what you need to do is access a “property” of your text components - that seems to be what you’re trying to do with target_text$width, but I’m not sure if this is the right syntax.

If you preface something with a dollar sign, what you’re saying to PsychoPy is “this isn’t a value, it’s a variable name”. So if you write target_text, it will try to set the position to the word target_text, which makes no sense and so will fail. But if you set it to $target_text, it will look for the variable or object target_text.

target_text will be an object of class TextStim, with various properties which define things about the text. These properties are outlined here:
https://www.psychopy.org/api/visual/textstim.html

If you want the polygon to be the exact size and position of the text, you would need to set the polygon’s width, height, x and y to these properties. So something like ($target_text.width, $target_text.height) should work! Position is stored together, so for that, you would just need $target_text.pos and it would give you x and y together.

When I’m back at my desk I’ll check how this auto-translates into JS and let you know if you need to make any changes manually or whether the auto-translator can figure it out!

Thanks for your reply!

I’ve now tried setting the size as such [target_text.width, target_text.height] and [compet_text.width, compet_text.height] but that only gives me a screen-wide rectangle, as if there was no end to the width of the TextStim.
(What you suggested ($target_text.width, $target_text.height) returned a syntax error).

I guess I can set a constant width based on the longest word in my materials. Clicks will be considered valid even if they are slightly on the side of the word, but I can work with that. The most important thing was that the position would change with the text, and that seems to work locally if I set the position of the polygon to [target_x, 0], as I’ve done so far, given that target_0 is defined in the code component.

So you already have a functioning method of randomising the text position?

I have one that only works locally, not online (cf. the code in the original post, with “shuffle” etc.). So it would be really great if you could help me translate it! For now I have in JS:

#Begin experiment:
target_positions = [0.4, -0.4].map((val) => val * 5);
util.shuffle(target_positions);
#Begin routine:
target_x = target_positions.pop();
competitor_x = (target_x * (- 1));
psychoJS.experiment.addData("target_x", target_x);
psychoJS.experiment.addData("competitor_x", competitor_x);

I assume there’s a problem with the Begin routine part (the multiplication maybe, or “pop”).
Any ideas? Thanks a lot!

I’ve found the solutions to all my problems now, the details are in this post: Randomization not working online (problem with "map"?)
Thanks again!