Svg.js: Extend the EventTarget Class with methods for event handling

Created on 18 Mar 2017  路  4Comments  路  Source: svgdotjs/svg.js

Just to be consistent I would like to replace the loaded callback on SVG.Image with a loaded event.

Hacktoberfest good first issue proposal

Most helpful comment

This landed in 3.0 with 47fda3cf67cdc8ab20d3b1ba9d65a810adddf5ee in the following way:

EventTarget is a class that all classes can extend to get event handling.
It comes with the following interface:

  • on() bind the event
  • off() unbind event
  • dispatch() dispatch an event on the object and return the event
  • fire() same as dispatch but returns the object

When you need to use a special event target (e.g. a node), you need to implement the following methods:

  • getEventTarget() returns the target
  • getEventHolder() returns the object on which the events/listeners are saved

Additionally the target needs to implement:

  • addEventListener()
  • removeEventListener() and
  • dispatchEvent()

The default is the EventTarget itself which implements all of these

All 4 comments

I agree with that. In the fx module we use events, too but kept the during()/after() method for not breaking the api. Maybe we could remove this methods, too?

Yes, we should remove them everywhere.

The problem with events is, that they are awfully slow and that they only can be dispatched on nodes.
In the new fx module we dont have a node to dispatch. And creating a detached node only for dispatching events on it is highly memory intensive and leads to lots of garbage collections.

So my proposal is the following:

  • extend the EventTarget class with the following methods:

    • addEventListener

    • removeEventListener

    • getEventDispatcher

    • dispatchEvent

This class should be completely able to remove and add events on its own.
However in case of SVG.Shapes we HAVE a node which we want to use. In that case getEventDispatcher returns a node and not this so that the events are bound and dispatched on the node instead of the object.

Note: Implementation has to be done on the 3.0 branch
For all questions head over to our gitter chat: https://gitter.im/svgdotjs/svg.js

This landed in 3.0 with 47fda3cf67cdc8ab20d3b1ba9d65a810adddf5ee in the following way:

EventTarget is a class that all classes can extend to get event handling.
It comes with the following interface:

  • on() bind the event
  • off() unbind event
  • dispatch() dispatch an event on the object and return the event
  • fire() same as dispatch but returns the object

When you need to use a special event target (e.g. a node), you need to implement the following methods:

  • getEventTarget() returns the target
  • getEventHolder() returns the object on which the events/listeners are saved

Additionally the target needs to implement:

  • addEventListener()
  • removeEventListener() and
  • dispatchEvent()

The default is the EventTarget itself which implements all of these

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mtbradle-ge picture mtbradle-ge  路  6Comments

edemaine picture edemaine  路  9Comments

mixn picture mixn  路  9Comments

Fuzzyma picture Fuzzyma  路  3Comments

momocow picture momocow  路  3Comments