Howdy, Stranger!

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

Supported by

Optimizing drawing script

PadPad
edited July 2016 in OpenSesame

Hello,

I'm working on a script within opensesame where the user will draw something on the screen. Here is what my code looks like :

from openexp.canvas import canvas
from openexp.mouse import mouse

my_mouse = mouse(exp, buttonlist = ([1]), visible = True)
my_canvas = canvas(exp)

my_canvas.show()

start = clock.time()
last_show = start

my_mouse.set_pos(pos=(0, 0))

# Draw for 10s
while  clock.time()- start <= 10000: 

    button, position, timestamp = my_mouse.get_click(timeout=100)
    x,y = position if button is not None else (None,None)

    if button is not None:
        (x0, y0) = (None,None)

        while my_mouse.get_pressed()==(1,0,0) and clock.time()- start <= 10000:
            (x, y), timestamp = my_mouse.get_pos()

            if (x0,y0) ==(None,None):
                # Draw a simple dot
                my_canvas.circle(x, y, 10, fill=True, color='black')
            elif (x0,y0) != (None, None) and (x0,y0)!=(x,y):
                # Draw a line between (x0,y0) and (x,y)
                my_canvas.circle(x0, y0, 10, fill=True, color='black')
                my_canvas.line(x0,y0,x,y, penwidth=23)
                my_canvas.circle(x, y, 10, fill=True, color='black')

            (x0,y0) = (x,y)

            #my_canvas.show()
            # optimization : show canvas every 20ms     
            if(clock.time() - last_show>=20):
                my_canvas.show()
                last_show = clock.time()

        my_canvas.show()

Works well, but not fluid enough :

I did some testing, this script can capture a mouse position every 25ms in average. I managed to lower this rate to 17ms using the #optimizization trick (show canvas only every 20ms and not after each line). Do you have any idea to make this code even better ?

If we can't have a better rate than 1 position every 17ms, maybe i should start to think about "curve fitting" : instead of drawing lines between 3 points i could draw a curve.

Any ideas ?

Pad

Comments

  • edited 12:04PM

    Hi Pad,

    I think this largely depend on which backend you are using. Some are more good at this than others. I think the legacy backend will be the fastest for what you are trying to do (and you are currently probably using the default setting: expyriment). Have you tried selecting this?

    Either way, you will only be able to achieve a sample rate as short as your screen refresh rate. If you for instance have a screen at 60 Hz, your screen will be refreshed each 16 ms. The mouse will not be sampled within this period (because my_canvas.show() will wait for a screen refresh to finish and thereby 'block' the execution of the script). So I don't think you will have a higher precision than 17 ms indeed. You could indeed check out some curve fitting solutions if you need smoother lines.

    Buy Me A Coffee

  • PadPad
    edited 12:04PM

    I don't really have a choice, the script will run on a tablet, i have to use the android backend.

    I guessed that this 17ms was linked to the refresh rate but i wasn't sure. I'm surprised that we can't have a better way to get the mouse position with a higher precision : 17ms is pretty high !

    I'm currently working on curve fitting solutions, hope it will give me nice results.

  • edited July 2016

    Hi Pad,

    Nope, you can only use that backend then. You may want to look into multi-threading:
    A solution would be create a separate thread in which the position is continuously polled. This way, if the drawing operation blocks the main thread to wait for the screen refresh, it doesn't block the polling of input.

    Programming threaded applications can be quite daunting, so beware. I also cannot really guarantee it will work, because under the hood pygame is used to draw both the frames to the screen and collect the responses. It may be that pygame pauses completely while waiting for the screen refresh, and thus also stops collecting input data for that time. In this case, the multithreaded solution won't work and will still show a delay of around 17ms.

    Buy Me A Coffee

  • I see you've moved on from this, but I'll answer for anyone else looking in to this.

    The problem here is your hardware. 17 msec the standard refresh time for usb mice, and I'm surprised that android touchscreens even sample that quickly. I've learned this the hard way while designing mouse tracking experiments: if you sample the mouse position more often that this, it will apear that it hasn't moved in between samples.

    Also:

    • See here to check the actual refresh rate on your device using adb
    • The android backend uses pygame, so it's constraints are your constraints. I don't think pygame blocks execution between frames, so that won't be what's slowing you down.
Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games