Howdy, Stranger!

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

Supported by

How can I program an RDK using inline JavaScript?

Hi,


I have a task which using random dot kinematograms (moving dots) in OpenSesame which contains inline python script to create and move the dots. I can’t run this online because of the Python script. The only information I can find says you can’t use inline JavaScript to create canvas objects. Where would I begin when trying to make this experiment run online?


Thanks

Comments

  • Hi @jtdeakin ,

    You can implement this with some creativity 😉 Generating random-dot stimuli in JavaScript won't be possible, but you can generate them offline with Python and then save them as images. Next, you store these images in the file pool, and play them back as an animation by putting a sketchpad inside a loop. (There is a practical limit of about 1000 files in the file pool though. Beyond that browsers start to complain.)

    Saving a frame from a psychopy DotStim , which I assume you're using, isn't trivial, but you can do it by accessing its _verticesBase property like so:

    from PIL import Image, ImageDraw
    import os
    import itertools
    
    SIZE = 200
    DOTSIZE = 3
    
    dots.draw()  # assumption: dots is a psychopy.visual.DotStim
    im = Image.new('1', (SIZE, SIZE))
    dr = ImageDraw.Draw(im)
    for x, y in dots._verticesBase:
        for dx, dy in itertools.product(range(DOTSIZE), range(DOTSIZE)):
            xdx = int(x) + dx + SIZE // 2
            ydy = int(y) + dy + SIZE // 2
            if xdx >= SIZE or ydy >= SIZE:
                continue
            dr.point((xdx, ydy), fill='white')
    im.save('my_dot_stim.png')
    

    Hope this helps!

    Sebastiaan

  • edited July 2020

    Thanks Sebastiaan, that is really helpful!

Sign In or Register to comment.