Re: Simulating a Mouseover event in EventHandler
- To: mathgroup at smc.vnet.net
- Subject: [mg91244] Re: Simulating a Mouseover event in EventHandler
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Mon, 11 Aug 2008 06:06:39 -0400 (EDT)
- References: <g7lvoh$fpq$1@smc.vnet.net>
chandler.seth at gmail.com wrote: > I would like to create an interface so that when the mouse moves > within certain graphics primitives within a Graphics object various > global variables are set that, among other things, alter both the > object itself and, potentially, other directives or graphics > primitives within the object. I would have thought that EventHandler > would have an event such as "Mouseover" or "MouseIn" or "MouseOut" > that handled this sort of thing. But, so far as I can see, it does > not. > > Here is the best code I've been able to come up to address this idea. > But it just looks really ugly and wrong. Anyone have ideas? > > (mouseisover = False; {Dynamic[mouseisover], > Graphics[{Dynamic[{If[mouseisover, Red, Green], Disk[{3, 3}, 1/2]}], > Dynamic@ > If[mouseisover, {Black, Line[{{1, 1}, {2, 2}}]}, {Black, > Circle[{1, 2}, 1/2]}], > Dynamic@EventHandler[{If[ > MousePosition["EventHandlerScaled", False] /. {x_, y_} :> > True, (mouseisover = True; Yellow), (mouseisover = False; > Blue)], Disk[{0, 0}, 1]}, {(* no events are defined, > which makes me think that the event handler construct is \ > unnecessary *)}]}, Frame -> True]}) > > P.S. The Mouseover command does not appear to accomplish what I need > because, even with compound expressions as the arguments and Dynamic, > I cannot get it to alter global variables. But maybe I am missing > something. > > > Here is a solution using MouseAnnotation. Note that although the effect of this example could be achieved using MouseOver, the function foo can do anything you want. foo[] := If[MouseAnnotation[] === Null, Graphics[{RGBColor[0, 0, 0], Disk[]}], Graphics[{RGBColor[1, 0, 0], Disk[]}]]; Annotation[Dynamic[foo[]], "An annotation", "Mouse"] From my experiments, it would seem that the function foo is called more frequently than I would have expected - so bear this in mind. David Bailey http://www.dbaileyconsultancy.co.uk