Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

randomly chage rectangle color

Hi'


Try to change color of rectanglem

wrote in inline java script , with chatgpt help:


// choose rectangle color randomly

// blue = "#0000FF", green = "#00FF00"

var colors = ["#0000FF", "#00FF00"];

vars.rect_color = colors[Math.floor(Math.random() * colors.length)];



and in the sketchpad I drew a recyangle and wrote in the color - color=[rect_color] or color="[rect_color]",

but either I get the variable is not recognised or nothing is shown

Comments

  • Hi @Raanan,

    It's difficult to ascertain what the issue is on your task without seeing how/where you implement the code and the sketchpad.

    I see nothgin wrong with your code in Javascrip, so the issue is almost certainly due to where the code is placed in the experiment structure or how the variable is being referenced in the Sketchpad.

    I notice that you're using the antiquated [ ] instead of { } preferred in the latest versions of Open Sesame. Theb [ ] will keep cworking for now but its a good habit to start using { } (among other things because it is more flexible in injecting code than [ ].

    The issue might relate to how exactly you coded the color reference for the rectangle, though at first sight, based on your description, this should not be an issue 8using color=[rect_color] or color={rect_color}.

    It's not clear to me whether you set the fill parameter to 1 (without it, the rectangle's color would refer to its border and not its filling + border).

    If the code and the sketchpad are located at the same level of hierarchy and your javascript code is located under the "run" tab instead of the "prepare" tab, the sketchpad would be drawn before the javascript is executed (hence color would have no value on the first trial and have the value of the previous trial thereafter). To fix that, you should insert yuour javascript code under the "prepare" tab of the inline_javascript object. Alternatively, you could use a feedback object instead of the sketchpad (the feedback object is the ionly one that is set up in real time as the task runs.

    I attach a working example that should hel you spot where you went wrong.

    Hope this helps.

    Fabrice.

    Buy Me A Coffee

Sign In or Register to comment.