svgdotjs / svg.js

The lightweight library for manipulating and animating SVG
https://svgjs.dev
Other
11.15k stars 1.08k forks source link

it would be nice to support some life cycle events and methods,like 'created,added,removed,destroyed',etc. #1072

Closed minzojian closed 4 years ago

minzojian commented 4 years ago

life cycle events and methods are very important so that we can manage the elements more flexible for example, i want add each shape a 'mousedown' event to 'select' it then i can listen the 'added' event to the shape elements, then add 'mousedown' event to handle 'select' or i can just override a state method like 'afterAdd' and do the same process inside meanwhile,i can remove listener in 'removed' event or 'beforeRemove' methods what do you think?

Fuzzyma commented 4 years ago

I guess they can come in handy but ultimately it would be just a different way of doing things. I dont like to add events because they can be really slow. Just dispatching events without any listener actually listening is already quite slow. We already had problems with that before. Thats why I prefer not to add them. However, if you really need them you can surely just overwrite the implementation with your own easily with extend(SVG.Dom, {add: ..., remove: ...}

minzojian commented 4 years ago

yes, it is easy to extend and add some custom methods, but the timing to call those methods is hard to handle. if i want observe the original dom added/removed event, at least, i need to call addEventListener methods in initiation method(constructor method), however, there are no initiation or constructor methods to extend

Fuzzyma commented 4 years ago

no actually when you overwrite the add or remove method with your own, you are doing exactly what we would do when we would implement those events