# Adapt polygon size/position to text size/position

**URL:** <https://discourse.psychopy.org/t/adapt-polygon-size-position-to-text-size-position/13221>\
**Category:** Builder\
**Created:** [May 11, 2020, 11:27am UTC](https://discourse.psychopy.org/t/adapt-polygon-size-position-to-text-size-position/13221 "2020-05-11T11:27:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcrible](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lcrible/32/6233_2.png) [@lcrible](https://discourse.psychopy.org/u/lcrible)\
**Post date:** [May 11, 2020, 11:27am UTC](https://discourse.psychopy.org/t/adapt-polygon-size-position-to-text-size-position/13221/1 "2020-05-11T11:27:23Z")

</div>

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:

```auto
#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"?)](https://discourse.psychopy.org/t/randomization-not-working-online-problem-with-map/13164)). 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!

---

<div class="post-metadata">

**Author:** ![TParsons](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tparsons/32/8301_2.png) [@TParsons](https://discourse.psychopy.org/u/TParsons)\
**Post date:** [May 12, 2020, 10:38am UTC](https://discourse.psychopy.org/t/adapt-polygon-size-position-to-text-size-position/13221/2 "2020-05-12T10:38:01Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![lcrible](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lcrible/32/6233_2.png) [@lcrible](https://discourse.psychopy.org/u/lcrible)\
**Post date:** [May 12, 2020, 1:04pm UTC](https://discourse.psychopy.org/t/adapt-polygon-size-position-to-text-size-position/13221/3 "2020-05-12T13:04:44Z")

</div>

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:

```auto
#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!

---

<div class="post-metadata">

**Author:** ![lcrible](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lcrible/32/6233_2.png) [@lcrible](https://discourse.psychopy.org/u/lcrible)\
**Post date:** [May 13, 2020, 9:09am UTC](https://discourse.psychopy.org/t/adapt-polygon-size-position-to-text-size-position/13221/4 "2020-05-13T09:09:14Z")

</div>

I’ve found the solutions to all my problems now, the details are in this post: [Randomization not working online (problem with "map"?)](https://discourse.psychopy.org/t/randomization-not-working-online-problem-with-map/13164)  
Thanks again!
