aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/web-animations-js/docs/examples.md
blob: 7957a29c6492cd8cb3911ed52a3d3739b2a3f1d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#Examples of using Web Animations

Property indexed keyframes syntax
---------------------------------
- Each CSS property specifies its keyframe values as a list, different properties may have differently sized lists.
- The `easing` property applies its timing function to all keyframes.

[**Live demo**](http://jsbin.com/qiyeriruru/edit?js,output)
```javascript
element.animate({
  transform: [
    'scaleY(0.5)',
    'scaleX(0.5)',
    'scaleY(0.5)',
  ],
  background: [
    'red',
    'blue',
    'orange',
    'red',
  ],
  easing: 'ease-in-out',
}, {
  duration: 2000,
  iterations: Infinity,
});
```

Keyframe list syntax
--------------------
- Keyframes can be specified as a list of multiple CSS property values.
- Individual keyframes can be given specific offsets and easings.
- Not all properties need to be specified in every keyframe.
- Offsets are implicitly distributed if not specified.

[**Live demo**](http://jsbin.com/yajatoyere/edit?js,output)
```javascript
element.animate([
  {
    background: 'red',
    transform: 'none',
    easing: 'ease-out',
  },
  {
    offset: 0.1,
    transform: 'translateY(100px)',
    easing: 'ease-in-out',
  },
  {
    offset: 0.2,
    transform: 'translate(100px, 100px)',
    easing: 'ease-in-out',
  },
  {
    offset: 0.4,
    transform: 'translateX(100px)',
    easing: 'ease-out',
  },
  {
    background: 'blue',
    transform: 'none',
  },
], {
  duration: 4000,
  iterations: Infinity,
});
```

Timing parameters
-----------------
- Web Animations inherits many of its timing parameters from CSS Animations.
- See the [specification](http://w3c.github.io/web-animations/#animationeffecttimingreadonly) for details on each parameter.

[**Live demo**](http://jsbin.com/dabehipiyo/edit?js,output)
```javascript
element.animate({
  transform: ['none', 'translateX(100px)'],
  background: ['green', 'lime'],
}, {
  // Apply effect during delay.
  fill: 'backwards',

  // Delay starting by 500ms.
  delay: 500,

  // Iterations last for 2000ms.
  duration: 2000,

  // Start at 25% through an iteration.
  iterationStart: 0.25,

  // Run for 2 iterations.
  iterations: 2,

  // Play every second iteration backwards.
  direction: 'alternate',

  // Stop animating 500ms earlier.
  endDelay: -500,

  // The timing function to use with each iteration.
  easing: 'ease-in-out',
});
```

Playback controls
-----------------
- element.animate() returns an Animation object with basic playback controls.
- See the [specification](http://w3c.github.io/web-animations/#the-animation-interface) for details on each method.

[**Live demo**](http://jsbin.com/kutaqoxejo/edit?js,output)
```javascript
var animation = element.animate({
  transform: ['none', 'translateX(200px)'],
  background: ['red', 'orange'],
}, {
  duration: 4000,
  fill: 'both',
});
animation.play();
animation.reverse();
animation.pause();
animation.currentTime = 2000;
animation.playbackRate += 0.25;
animation.playbackRate -= 0.25;
animation.finish();
animation.cancel();
```

Transitioning states with element.animate()
-------------------------------------------
- This is an example of how to animate from one state to another using Web Animations.

[**Live demo**](http://jsbin.com/musufiwule/edit?js,output)
```javascript
var isOpen = false;
var openHeight = '100px';
var closedHeight = '0px';
var duration = 300;

button.addEventListener('click', function() {
  // Prevent clicks while we transition states.
  button.disabled = true;
  button.textContent = '...';

  // Determine where we're animation from/to.
  var fromHeight = isOpen ? openHeight : closedHeight;
  var toHeight = isOpen ? closedHeight : openHeight;

  // Start an animation transitioning from our current state to the final state.
  var animation = element.animate({ height: [fromHeight, toHeight] }, duration);

  // Update the button once the animation finishes.
  animation.onfinish = function() {
    isOpen = !isOpen;
    button.textContent = isOpen ? 'Close' : 'Open';
    button.disabled = false;
  };

  // Put our element in the final state.
  // Inline styles are overridden by active animations.
  // When the above animation finishes it will stop applying and
  // the element's style will fall back onto this inline style value.
  element.style.setProperty('height', toHeight);
});
```

Generating animations
---------------------
- The Javascript API allows for procedurally generating a diverse range of interesting animations.

[**Live demo**](http://jsbin.com/xolacasiyu/edit?js,output)
```html
<!DOCTYPE html>
<script src="https://rawgit.com/web-animations/web-animations-js/master/web-animations.min.js"></script>

<style>
#perspective {
  margin-left: 100px;
  width: 300px;
  height: 300px;
  perspective: 600px;
}

#container {
  width: 300px;
  height: 300px;
  line-height: 0;
  transform-style: preserve-3d;
}

.box {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: black;
}
</style>

<div id="perspective">
  <div id="container"></div>
</div>

<script>
container.animate({
  transform: [
    'rotateX(70deg) rotateZ(0deg)',
    'rotateX(70deg) rotateZ(360deg)',
  ],
}, {
  duration: 20000,
  iterations: Infinity,
});

for (var y = -7; y <= 7; y++) {
  for (var x = -7; x <= 7; x++) {
    var box = createBox();
    box.animate({
      transform: [
        'translateZ(0px)',
        'translateZ(20px)',
      ],
      opacity: [1, 0],
    }, {
      delay: (x*x + y*y) * 20,
      duration: 2000,
      iterations: Infinity,
      direction: 'alternate',
      easing: 'ease-in',
    });
  }
}

function createBox() {
  var box = document.createElement('div');
  box.className = 'box';
  container.appendChild(box);
  return box;
}
</script>
```