In AVG.js, the tweened animation function is provided as a component.
import { React, Component, components, core } from 'avg-core';
const { Tween } = components;
<Image src='title/bg.png'>
<Tween schemes={schemes} ref={tween => this.tween = tween}>
<Textbutton
text='Start' style={style} anchor={[0.5, 0.5]} x={400} y={310} key={'title1'}
onClick={() => this.context.router.push('/story')}
onTap={() => this.context.router.push('/story')}
/>
<Textbutton text='Load' style={style} anchor={[0.5, 0.5]} x={400} y={390} key={'title2'} />
<Textbutton text='Info' style={style} anchor={[0.5, 0.5]} x={400} y={470} key={'title3'} />
</Tween>
</Image>
Specific steps:
- Wrap the subcomponent to be tweened with the Tween component
- Add a unique key to the subcomponent
- According to key to create the corresponding schemes
Scheme format is Object, which can be imported externally via JSON or can be created using the util class
Tween.builder()
. E.g,
const schemes = Tween.builder()
.scheme('default')
.sequence(1, true)
.moveTo('title1', { y: 100 }, 2000)
.moveTo('title2', { x: 100, y: 180 }, 2000)
.parallel(1, true)
.callback('title1', () => console.log('emit!'))
.moveTo('title1', { x: 600, y: 100 }, 3000)
.moveTo('title2', { x: 600, y: 180 }, 2000, Tween.Easing.Quadratic.InOut, 1, true)
.end()
.moveTo('title2', { x: 600, y: 480 }, 2000, Tween.Easing.Quadratic.In, 2, false)
.end()
.end()
.scheme('another')
.end()
.getSchemes();
The above created two animation schemes, named default
and another
, another
is an empty scheme.
The default
scheme defines a more complex animation program, including multiple sprites and serial/parallel execution,the details of each command [can be found later in the specific explanation](#Command Details)
4. Pass schemes to Tween component as a property
Tween components are only valid for direct subcomponents
If the default
scheme exists, it will play automatically after the component is mounted.
To play other animation schemes, you can call the component method:
- Get the component instance by ref, such as
ref={tween => this.tween = tween}
- In proper position, execute
this.tween.runTween('scheme name')
Complete example:
export default class Title extends Component {
static contextTypes = {
router: React.PropTypes.any,
}
render() {
const schemes = Tween.builder()
.scheme('default')
.sequence(1, true)
.moveTo('title1', { y: 100 }, 2000)
.moveTo('title2', { x: 100, y: 180 }, 2000)
.parallel(1, true)
.callback('title1', () => console.log('emit!'))
.moveTo('title1', { x: 600, y: 100 }, 3000)
.moveTo('title2', { x: 600, y: 180 }, 2000, Tween.Easing.Quadratic.InOut, 1, true)
.end()
.moveTo('title2', { x: 600, y: 480 }, 2000, Tween.Easing.Quadratic.In, 2, false)
.end()
.end()
.scheme('another')
.end()
.getSchemes();
return (
<Image src='title/bg.png'>
<Tween schemes={schemes} ref={tween => this.tween = tween}>
<Textbutton
text='Start' style={style} anchor={[0.5, 0.5]} x={400} y={310} key={'title1'}
onClick={() => this.context.router.push('/story')}
onTap={() => this.context.router.push('/story')}
/>
<Textbutton text='Load' style={style} anchor={[0.5, 0.5]} x={400} y={390} key={'title2'} />
<Textbutton text='Info' style={style} anchor={[0.5, 0.5]} x={400} y={470} key={'title3'} />
</Tween>
</Image>
);
}
}
As you can see from the above example, we set a lot of properties for each <Textbutton>
, which is automatically used as the initial value of the animation. In the playing of animation, you can also change them freely.
It is advisable to specify an initial value for all properties that will be used in the animation scheme to avoid the illegal value of NaN
due to the process of null
.
Run a pre-defined animation scheme (passed in the schemes
property of Tween component).
Passing a new animation scheme and running it, you can specify a name for it by the second argument name
.
Stop a running animation scheme by name.
Parameter | Description |
---|---|
target | The component to perform the action, aka the key of the component |
params | parameters of the action, maybe an Object or other type |
duration | Duration |
easing | Easing function, refers to [Easing Functions] (#Easing Functions) |
repeat | repeat time, default to 0 |
yoyo | repeat animation like yoyo |
Define a new animation scheme.
Create a serial execution queue, each action will be executed in order.
Create a parallel execution queue, each action will be executed at the same time. When the last action is ended, the queue is ended.
End an animation scheme definition or serial/parallel queue.
Get the defined animation schemes object that can be passed as a property to the Tween component.
Move the node to a absolute coordinate or by a relative offset.
At least one of the two parameters x, y
must be filled.
Fade the node's alpha to a absolute value or by a relative value.
Rotate the node to a absolute value or by a relative value in radians.
Scale the node to a absolute value or by a relative value.
Shake effect
.quake(target, { offsetX, offsetY, speed = 10 }, duration = 0, easing = linear, repeat = 0, yoyo = false)
Quake effect The difference between shake and quake is that the offset of the vibration is random and offset of quake is used as maximum value.
Set the properties of the node directly, such as visible
, etc., Except for target
and params
, DO NOT set the following parameters, otherwise it may cause an exception.
When the animation is played to that position, a function callback is triggered and the node object specified by target
is the context.
If it is in a sequence
or parallel
which is repeat > 0
, it will be triggered on each repeat.
callback defination: (progress, lastProgress, target, finish) => { /* do something... */; finish() }
。
If duration
is specified greater than 0, you do not need to executefinish ()
manually, otherwise you must executefinish ()
manually.
Tween.Easing presets a variety of commonly used easing functions, including Linear, Quadratic, Cubic, Quartic, Quintic, Sinusoidal, Exponential, Circular, Elastic, Back and Bounce. At the same time each type is divided into In, Out and InOut three sub-categories (except Linear Linear, which only has a None type). The curve for each function can be found here.
You can use it like Tween.Easing.Linear.None
or Tween.Easing.Cubic.In
.
Or you can pass your own easing function:
function myEasing(t) {
return Math.sin(Math.PI / 2 * t);
}
.moveTo('key', { x: 100 }, 1000, myEasing);