How to simulate mouse events

1,712 views
Skip to first unread message

Thug

unread,
Dec 6, 2011, 10:31:21 AM12/6/11
to d3...@googlegroups.com
Hi all

I would like to simulate mouse actions (for example a click) in a way that would be caught by d3.event, and so would allow me to query (for example) d3.event.target.

Have you any suggestions or examples as to how I should best go about it?

Thanks
Thug

Darron

unread,
Dec 7, 2011, 8:54:18 AM12/7/11
to d3-js
With jQuery you could use $(".node").trigger("click"); which is
caught by node.on("click", function() {...});

Thug

unread,
Dec 7, 2011, 10:55:35 AM12/7/11
to d3...@googlegroups.com
Thanks, Darron, I'm trying this out now.

The motivation behind the use of events is threefold:
  • to trigger transitions at will
  • keep my code simple
  • automate certain tests
Though my console logging appears to be picking up everything generated on real mouseclick event handling, programmatically-generated (javascript) MouseEvents are yet to picked up by d3.event. Inexperience, I guess. 

I did in fact find another alternative - wrapping the freestanding program (including all event handling) in a js function, and then passing in the parameters I wanted to change. This works, but at the expense of a full redraw.

Given the motivations above, I'd still be interested in the possibility of direct generation from within d3. Maybe it's a candidate for the wish list, though.

Thug

Ian Johnson

unread,
Dec 7, 2011, 5:37:31 PM12/7/11
to d3...@googlegroups.com
Do you need mouse related data when you simulate these clicks or you just want to trigger some action programmatically?

You could use something like backbone.js for a global event object which you use to listen for and trigger custom events. You also make your own custom events with d3 like if you look at the source for the drag behavior.

--
Ian Johnson

Darron

unread,
Dec 12, 2011, 10:27:05 AM12/12/11
to d3-js
I came up with a solution that works for me, more here
http://groups.google.com/group/d3-js/browse_thread/thread/a12077e03c4fc537

Regards,
Darron


On Dec 8, 12:37 am, Ian Johnson <enja...@gmail.com> wrote:
> Do you need mouse related data when you simulate these clicks or you just
> want to trigger some action programmatically?
>
> You could use something like backbone.js for a global event object which
> you use to listen for and trigger custom events. You also make your own
> custom events with d3 like if you look at the source for the drag behavior.
>
>
>
>
>
>
>
>
>
> On Wed, Dec 7, 2011 at 7:55 AM, Thug <a.douglas.h...@gmail.com> wrote:
> > Thanks, Darron, I'm trying this out now.
>
> > The motivation behind the use of events is threefold:
>

> >    - to trigger transitions at will
> >    - keep my code simple
> >    - automate certain tests


>
> > Though my console logging appears to be picking up everything generated on

> > *real* mouseclick event handling,

Reply all
Reply to author
Forward
0 new messages