catching mouse events when mouse not in image

28 views
Skip to first unread message

Michael Burns-Kaurin

unread,
Feb 15, 2025, 6:40:17 PMFeb 15
to glowscri...@googlegroups.com
I see how to use scene.bind, but that seems to only catch mouse events when the cursor is on the scene (graphical image).  Is there a way to simply catch mouse events, not on the image?

Michael Burns-Kaurin

Bruce Sherwood

unread,
Feb 16, 2025, 1:57:35 AMFeb 16
to Glowscript Users
Not sure whether the following does what you want:

s = sphere(color=color.cyan)
show = label()

def view():
    show.text=scene.mouse.pos
   
scene.bind('mousedown', view)

scene2 = canvas(background=color.white)
scene2.autoscale = False
sphere(pos=vec(0,0,0), radius=0.1, opacity=0)

def view2():
    show.text = scene2.mouse.pos

scene2.bind('mousedown', view2)

Bruce

Michael Burns-Kaurin

unread,
Feb 16, 2025, 12:19:13 PMFeb 16
to Glowscript Users
Thank you for the suggestion, but that still does not capture mouse events on the slider.  I have not found a way to make the slider part of scene2.  Here is what I have, the goal is to get the value of L (controlled by the slider) to change at mouseup, not when the slider is changing.  That is, L should change only at the end of moving the slider (for reasons related to the larger program involved, which involves making a tone that does poorly if it tries to change continuously with the slider).

s = sphere(color=color.cyan)
show = label()

def view():
    global L
    show.text=L
   
scene.bind('mouseup', view)


scene2 = canvas(background=color.white)
scene2.autoscale = False
sphere(pos=vec(0,0,0), radius=0.1, opacity=0)

def view2():
    global L
    show.text = L

scene2.bind('mouseup', view2)

def L_change(s):
    global L
    L = s.value
    l_caption.text = L

L = 1
scene2.append_to_title("\n\n\n")
Ls = slider(bind=L_change,min=0.2,max=5,value=L,pos=scene2.title_anchor)
l_caption = wtext(text = f"width = {L:.2f} nm\n\n")

Michael Burns-Kaurin

unread,
Feb 16, 2025, 1:42:56 PMFeb 16
to Glowscript Users
Here is a pared down version of what I'm trying to do with the sound.  The play() in the slider binding function produces horrible sound.  The play in the mouseup binding function only works when I click in the scene, my hope is for something that works when I release the mouse button after moving the slider (to get sound at the end of moving the slider).

Web VPython 3.2

'''
audio code from Mark Schaedel
taken from
https://www.glowscript.org/#/user/schaedelm/folder/FunStuff/program/ChaosBall1-sound/edit
'''

audioCtx = new window.AudioContext();
def playFreq(freq, time):
    toneGen = audioCtx.createOscillator();
    toneGen.frequency.value = freq;
    toneGen.type = "sine";
    toneGen.connect(audioCtx.destination);
    volume = audioCtx.createGain();
    toneGen.start();
    toneGen.stop(audioCtx.currentTime + time);

def play():
    global f
    playFreq(f,0.25)
   
scene.bind('mouseup', play)

def f_change(s):
    f = s.value
    f_caption.text = f
    playFreq(f,0.25)

f = 500
Fs = slider(bind=f_change,min=300,max=1000,value=f)
f_caption = wtext(text = f"frequency = {f:.2f} Hz\n\n")

On Sunday, February 16, 2025 at 1:57:35 AM UTC-5 Bruce Sherwood wrote:

Bruce Sherwood

unread,
Feb 16, 2025, 2:24:41 PMFeb 16
to glowscri...@googlegroups.com
I don't understand. You say you want "to get sound at the end of moving the slider". When I touch or move the slider I get a sound, and when I mouse up on the slider it also plays a sound.

Bruce

Bruce Sherwood

unread,
Feb 16, 2025, 2:29:23 PMFeb 16
to Glowscript Users
I guess I should mention that I'm using Chrome on Windows. Dunno whether that makes any difference.

Bruce

Michael Burns-Kaurin

unread,
Feb 16, 2025, 3:30:29 PMFeb 16
to glowscri...@googlegroups.com
I should have been more explicit.  I want the sound only after I finish moving the slider and let up on the mouse, not while the slider is moving.  The sound made when the slider is moving, at least on my system (windows 11, chrome) is not a pure tone.  The sound made when I click in the scene is a pure tone.  

The sound is part of a larger program, the sound frequency will be linearly related to the energy of a particle.

I found one bug, the play() function should be

def play():
    f = Fs.value
    playFreq(f,0.25)

If there is no fix, the program will just need a click in scene to play sound, and sound would not be part of the slider binding.  Or maybe a specific button to play the sound.

On Sun, Feb 16, 2025 at 2:24 PM Bruce Sherwood <bruce.s...@gmail.com> wrote:
I don't understand. You say you want "to get sound at the end of moving the slider". When I touch or move the slider I get a sound, and when I mouse up on the slider it also plays a sound.

Bruce

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/glowscript-users/CA%2BWuaSerWrY6sderDvZ0vJ0yO4Fb7ZZ_JuhyC7Vi_1FmeGEbHA%40mail.gmail.com.

Harlan Gilbert

unread,
Feb 16, 2025, 5:16:12 PMFeb 16
to glowscri...@googlegroups.com
Here's a workaround that checks if the slider is no longer moving. 

audioCtx = new window.AudioContext();
def playFreq(freq, time):
    toneGen = audioCtx.createOscillator();
    toneGen.frequency.value = freq;
    toneGen.type = "sine";
    toneGen.connect(audioCtx.destination);
    volume = audioCtx.createGain();
    toneGen.start();
    toneGen.stop(audioCtx.currentTime + time);

def play():
    global f
    playFreq(f,0.25)
   
scene.bind('mouseup', play)

def f_change(s):
    global newf
    newf = s.value
   
newf = oldf = 500

Fs = slider(bind=f_change,min=300,max=1000,value=f)
f_caption = wtext(text = f"frequency = {f:.2f} Hz\n\n")

while True:                  #Check if the slider has stopped moving
    rate(10)                   #A tenth of a second of stability will suffice; this can be changed
    if newf == oldf:        #If the slider is stable play the frequency settled on
        f_caption.text = newf
        playFreq(newf,0.25)
    oldf = newf



--
Harlan Gilbert, Ph.D.
High School Math, Physics, Computer Science, and Philosophy Teacher
Collegium Chair
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977

Michael Burns-Kaurin

unread,
Feb 16, 2025, 7:25:14 PMFeb 16
to glowscri...@googlegroups.com
Thanks.  I'll play with it and see how it works with the other stuff that goes on in the larger program.

Reply all
Reply to author
Forward
0 new messages