Howdy, Stranger!

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

Supported by

canvas question


Hey guys,
I need some ideas on how to implement something that seems easy but yet eludes me. Basically, I want to show a map and when people click on a location to save its pixel coordinates. I've done the click and save part by recycling some code from another example and drawing a fixation point on the mouse location on the canvas (see attachment). The problem is that when I add the png for the map in the while loop that controls the mouse, things slow down a lot. This is of course to be expected as the png gets redrawn on the canvas all the time, but how should I do this instead? Basically, I want to display the map once and redraw the mouse continuously. I tried putting the map outside of the loop but I haven't manged to make the mouse update as it should. Any ideas?
Thanks in advance,
marios

Comments

  • Hi Marios,

    First thought: Why don't you simply let the operating system draw the mouse cursor?

    my_mouse.show_cursor(True)
    

    That way you don't have to redraw the canvas over and over again. But if, for whatever reason, that is not an option, there are a few other things you could try to improve performance:

    • Set the timeout to 0, instead of 20
    • Change the backend to legacy, which is faster when it comes to stimulus rendering

    Cheers!
    Sebastiaan

  • Thanks Sebastiaan! Initially I was thinking of doing mouse tracking so I went down this route. But, if I decide against it then I could just have the os draw the mouse. Good point. I'll also try the other things you suggested and see what happens. :)

  • Hi Sebastiaan,
    I finally found some time to get back to this and still I cant figure it out. I moved the canvas functions out of the loop and let the OS control the mouse. But, for some reason I can't get a mouse cursor that can be freely moved by the user. Any idea what I'm doing wrong again? I attach my inline.
    Thanks,
    marios

  • But, for some reason I can't get a mouse cursor that can be freely moved by the user.

    But what happens then? Is the mouse cursor invisible, or is it visible but for some reason unmovable?

  • As is, the mouse only shows up for a bit in thr center of the screen. Playing around with code (if I remember correctly, by changing the timeout value), Igot it to show up for longer but when I was moving it away from the center it kept pushing back to it.By the way, I dont think I understand what timeout really does!

  • Hi Mario,

    Try this code, it works quite nicely for me.


    ms = mouse() cv = canvas() path = exp.pool[u'yourFile.png'] cv.image(path) cv.show() ms.show_cursor(True) for i in range(6): # I made a loop around the main function to see how it works for multiple mouse clicks while True: cv.show() # showing the canvas in the beginning of the loop improves the quality a lot! button, position, timestamp = ms.get_click(timeout=0) if button is not None: cv.fixdot(x=position[0],y=position[1],color='red') # fixation marks the click var.lloc_clicked = position var.x=position[0],var.y=position[1] break log.write_vars()

    By the way, I dont think I understand what timeout really does!

    Timeout indicates how long the computer waits before continuing with the script. So a timeout of 20 means that for 20ms the experiment is idle while waiting for the mouse click. A timeout of 0 means, that it does not wait at all (But in that case the function is still responsive for pending clicks, so if you click a mouse button, the click will be registered on the next iteration).

    Hope this helps,.

    Eduard

    Buy Me A Coffee

  • Perfect! Thank you so much for your help Eduard! I'll test the code later today and adapt for my purpose.
    Cheers,
    marios

Sign In or Register to comment.