Repository navigation
Reusable anime instance for different onUpdate() functions #1198
|
I have a bunch of transitions that I want to use a single re-sable 'bell curve' anime instance for. Is it possible to call this.transition and pass in some identifier variable (var1, var2, var3...) that triggers the correct behavior on update? Thanks. |
Replies: 1 comment 1 reply
|
Yes. Anime does not take arguments when you play an instance, but you don't need that. The callback is just a function, so you can have it read a variable that you change before each play. this.mode = null;
this.transition = animate(this.x, {
value: [0, 1, 0],
autoplay: false,
onUpdate: () => {
if (this.x.value > 0.95) {
if (this.mode === 'var1') { /* do this */ }
if (this.mode === 'var2') { /* do this */ }
}
}
});
// later
this.mode = 'var2';
this.transition.restart();If you'd rather keep one handler per behaviour, keep a map of handlers and look one up inside onUpdate: const handlers = { var1: () => {}, var2: () => {}, var3: () => {} };
onUpdate: () => { if (this.x.value > 0.95) handlers[this.mode]?.(); }You can also swap the callback itself. The instance stores it as a plain this.transition.onUpdate = () => { /* behaviour for var2 */ };
this.transition.restart();The first option is the safest, since it relies only on the public options. If you want a fresh instance per use instead, wrap (I looked this up in the code with help from an AI assistant; the links are to the lines I checked.) |
Yes. Anime does not take arguments when you play an instance, but you don't need that. The callback is just a function, so you can have it read a variable that you change before each play.
If you'd rather keep one handler per behaviour, keep a map of handlers and look one up inside onUpdate: