Enabling/disabling mousemove odd behavior #2648
jimthouston
started this conversation in
General
Replies: 1 comment
-
Hi @jimthouston, We actually never thought about changing the event list at runtime. Therefore there's no public state = {'down': False, 'x0': 0.0, 'y0': 0.0}
def mouse_handler(e: events.MouseEventArguments):
if e.type == 'mousedown':
state.update(down=True, x0=e.image_x, y0=e.image_y)
ii.props(''':events="['mousedown', 'mouseup', 'mousemove']"''')
if e.type == 'mouseup':
state.update(down=False)
ii.props(''':events="['mousedown', 'mouseup']"''')
if e.type == 'mousemove' and state['down']:
x = min(e.image_x, state['x0'])
y = min(e.image_y, state['y0'])
w = abs(e.image_x - state['x0'])
h = abs(e.image_y - state['y0'])
ii.set_content(f'<rect x="{x}" y="{y}" width="{w}" height="{h}" fill="none" stroke="black" />')
src = 'https://picsum.photos/id/565/640/360'
ii = ui.interactive_image(src, on_mouse=mouse_handler, events=['mousedown', 'mouseup']) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have been playing with nicegui for a couple weeks. At this time of year I think I might do a GUI for an open source tax program. I'm playing with overlaying input fields over a PDF form.
Today I was trying to drawing a rubber band box by clicking and dragging the mouse.
Here is what I tried:
I tried enabling the mousemove events on mousedown and turning them off on mouseup.
There are a few things which surprised me. I thought that I could enable the
mousemove event with:
ii.events = ['mousedown', 'mouseup', 'mousemove']
I found that I needed to use
ii._props['events']
.I also found that changing the events wasn't enough. I'm setting the
context
to draw a red circle. If I comment out this line, I never see a mousemove event.The disable of the mousemove event doesn't take effect immediately so I need to check if I have seen the mouseup event and discard a mousemove event.
I also tried using the generic
ii.on('mousemove', on_move)
and was disappointed to find that the argument to on_move wasn't the same as the events function.I guess I'm guilty of optimizing too soon. Running top, I don't see a difference in CPU load when I'm holding the mouse button and dragging the mouse around. I tried doing something similar last year using justpy and used javascript to draw the box.
Is a cleaner way to do this?
Beta Was this translation helpful? Give feedback.
All reactions