-
-
Notifications
You must be signed in to change notification settings - Fork 893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Declaratively Stepped Animations #96
Comments
Hi, Megh! Thanks for the issue! You can use this.rollie
.pause()
.then({
y: {to : -200},
x: {to : -200},
duration: 1000
})
.play(); There is demo. this.rollie
.pause()
.tune({
y: {600 : 0},
x: {600: 0},
duration: 1000
})
.play(); There is demo. Hope it helps, please let me know if you have any questions. Cheers! |
I appreciate the help! The Here's my code for reference: componentDidMount () {
const that = this
this.shape = new mojs.Shape({
x: { 500 : 0 },
y: 0,
duration: 5000,
onComplete() {
that.setState({ bounceRollie: true })
}
}).replay()
}
componentDidUpdate (prevProps, prevState) {
if(this.state.bounceRollie) {
this.shape
.pause()
.tune({
x: { 0 : 0 },
y: { 0 : -100 },
duration: 1000,
})
.play()
}
} As you can see, the state of the React component is updated, triggering the second transition. |
It seems that using |
@MeghV happy to help!
You can also get the current properties from non-public // get current `x`
const {x} = shape._props;
shape
.pause()
// tune animation from current `x`
.tune({ x: { [parseInt(x, 10)]: 0 }, duration: 3000 })
.replay(); There is demo. Of course, you need to apply the
shape
.pause()
// tune animation from current `x`
.tune({ x: { current: 0 }, duration: 3000 })
.replay(); Please let me know. Cheers! |
Great, this works perfectly. Thanks so much for the prompt help! |
Also, in looking for a solution to this problem, I found another way to accomplish (state-driven) stepped animations, though it's neither as clean nor as efficient as the method mentioned above. However, it may be helpful to others as a reference to others aiming to use React to build declarative animations. It involves separating the object's animation into two (or n) separate timelines, the second / nth timeline playing on completion of the prior one. Note: This will create two copies of the same SVG element on the page, so it's definitely not as efficient as the method mentioned above. Here's an example - I used it to roll in a circle SVG and then bounce it repeatedly until my sites assets were loaded: componentDidMount () {
const that = this
// Step 1: Roll-In Animation
this.rollInAnimation = new mojs.Shape({
el: '#rollie-svg',
shape: 'circle',
x: { -500 : 0 },
y: 0,
duration: 3000,
isShowEnd: true,
isShowStart: true,
onComplete (isForward, isYoyo) {
// on completion, set state to begin
// the next animation
that.setState({ startBounce: true })
}
})
// Step 2: Bounce Animation
// NOTE:
// tween start values in this animation
// should equal the tween end values of the
// previous animation to give an impression that
// it's the same element
this.bounceAnimation = new mojs.Shape({
el: '#rollie-svg',
shape: 'circle',
x: 0,
y: { 0 : -100 , curve: /* bounce curve */ },
duration: 600,
isShowEnd: true,
isShowStart: true,
opacity: 0,
repeat: 3,
onComplete (isForward, isYoyo) {
// you can use this to trigger
// the next animation after this bounce
if(this.state.loadedState) {
console.log("do something")
} else {
// if the desired state isn't ready,
// replay the bounce
that.bounceTimeline.replay()
}
},
onFirstUpdate () {
// as soon as the animation is started for
// the first time or for repeats, the
// opacity should go to 1 and the previous
// shape should be hidden
this.el.style['opacity'] = 1
that.rollie._hide()
}
})
this.rollInTimeline = new mojs.Timeline;
this.rollInTimeline.add ( this.rollInAnimation )
this.bounceTimeline = new mojs.Timeline;
this.bounceTimeline.add ( this.bounceAnimation )
this.rollInTimeline.play()
}
componentDidUpdate (prevProps, prevState) {
// once the first (or nth) animation is completed,
// we can start the next one
if(this.state.startBounce) {
this.bounceTimeline.play()
}
} |
Megh, welcome and thanks for the another approach! |
Hello,
I'm having trouble figuring out how to do state-based step animations using mojs in React/Redux. What I would like to accomplish is state-driven animations. For example, the first animation should play automatically, but I would like the second animation to play once the state has changed. Here's what I have so far:
The following is in my
componentDidMount
function:Now, I would like to play a second animation once the component updates with state "rollieEntered" set to true. This will be called in
componentDidUpdate
. What I am trying to do is:However, it seems that "play" has already been called, disallowing the then call to take place. What is your recommended approach on achieving stepped animations like above?
Thanks!
The text was updated successfully, but these errors were encountered: