Optimizing drawing script
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 = (), 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 ?