Animation and Web Components
Yi-Fan Liao
2016.Mar.21
CSSTransition
How to apply CSSTransition on an element:
Define the CSS properties you'd like to have CSSTransition
Change the value of the CSS properties
Syntax
See the Pen <a href='http://codepen.io/begeeben/pen/BjzWMP/'>BjzWMP</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Shorthand
transition: [transition-property] [transition-duration] [transition-timing-function] [transition-delay];
Multiple Animations
See the Pen <a href='http://codepen.io/begeeben/pen/EPyybd/'>Multiple animated properties example</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
ease
See the Pen <a href='http://codepen.io/begeeben/pen/vLKmxr/'>vLKmxr</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
cubic-bezier
See the Pen <a href='http://codepen.io/begeeben/pen/dGXWVm/'>dGXWVm</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Example
See the Pen <a href='http://codepen.io/begeeben/pen/zrBwRw/'>zrBwRw</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
transitionend
Element.addEventListener('transitionend', function (e) {
// Do something after a transition ends...
});
Example
See the Pen <a href='http://codepen.io/begeeben/pen/bEemgZ/'>bEemgZ</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
transitionend note
transitionend event will not be fired if transition-property is removed before a transition finishes.
Workaround:
var handler = function () {
// Do something after transition finishes...
Element.removeEventListener('transitionend', handler);
window.clearTimeout(timeoutFunction);
};
Element.addEventListener('transitionend', handler);
var timeoutFunction = setTimeout(handler, 2000); // A timestamp that is slightly longer than the transition duration
Practice
Insert a CSSTransition in the transitionend example.
Practice
In the transitionend example above, when the transition ends create a new div with the same css transistion.
CSSAnimation
How to apply CSSAnimation on an element:
Define a keyframe animation
Set the animation-name property with the keyframe animation
CSSAnimation
See the Pen <a href='http://codepen.io/begeeben/pen/GoqYBP/'>GoqYBP</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Shorthand
animation: [duration] [timing-function] [delay] [iteration-count] [direction] [fill-mode] [play-state] [name];
keyframes
See the Pen <a href='http://codepen.io/begeeben/pen/ZQOqwL/'>ZQOqwL</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
animation-direction
animation-direction: normal | reverse | alternate | alternate-reverse;
See the Pen <a href='http://codepen.io/begeeben/pen/PZzyLv/'>PZzyLv</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
animation-fill-mode
animation-fill-mode: none | forwards | backwards | both;
See the Pen <a href='http://codepen.io/begeeben/pen/rxLqgw/'>rxLqgw</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
CSSAnimation animation-play-state
animation-play-state: running | paused;
See the Pen <a href='http://codepen.io/begeeben/pen/jWregq/'>jWregq</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Multiple CSSAnimations
See the Pen <a href='http://codepen.io/begeeben/pen/mVEQdg/'>mVEQdg</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
CSSAnimation Events
Element.addEventListener("animationstart", listener);
Element.addEventListener("animationend", listener);
Element.addEventListener("animationiteration", listener);
Practice
Fork the 1st CSSAnimation example, 1) add a rotation animation and 2) show the animation elapsed time at every iteration on the screen.
Fork the animation-play-state example, revise the pause button to enable resuming the animation.
Practice
Create an fullscreen element and slide it in from left. When the animation ends, zoom out and fade out the element.
Differences, in general
- CSSAnimation:
- Flexible keyframe definition
- Easy looping and playback control
- Need to be predefined and is harder to change animation in the middle
Differences, in general
- CSSTransition:
- Not as flexible as keyframe animation
- No easy playback control
- Easy to interpolate a transition with another
JS Animation
For complex animations with fine-grained control, physics, separate timelines etc.
requestAnimationFrame
See the Pen <a href='http://codepen.io/begeeben/pen/obLOJY/'>obLOJY</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Revise with elapsed time
See the Pen <a href='http://codepen.io/begeeben/pen/adZxXv/'>adZxXv</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Easing
See the Pen <a href='http://codepen.io/begeeben/pen/YwWbyr/'>YwWbyr</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Practice
Fork the previous example, after the bounce animation finishes move the box back to the start position with another kind of easing.
Performance considerations
Limit your animations to:
- translate
- rotate
- scale
- opacity
Look up what triggers layout, paint and composite:
CSS TRIGGERS
All about making your web page smooth:
Jank FreeWeb Animations
The programming interface for CSSTransition, CSSAnimation and SVG.
Controlling animations
See the Pen <a href='http://codepen.io/begeeben/pen/RrGKrv/'>RrGKrv</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Practice
Fork the Web Animations example, add a fast forward button, a slow motion button and a reverse button.
Web Components
Specifications that let us implement custom HTML elements
- Custom Elements
- HTML Imports
- Templates
- Shadow DOM
Custom Elements - custom tag
See the Pen <a href='http://codepen.io/begeeben/pen/EPgyMd/'>EPgyMd</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Custom Elements - type extension
See the Pen <a href='http://codepen.io/begeeben/pen/XXjjJy/'>XXjjJy</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
smart-button
https://github.com/fxos-components/smart-button
See the Pen <a href='http://codepen.io/begeeben/pen/PZGNxV/'>PZGNxV</a> by Yi-Fan Liao (<a href='http://codepen.io/begeeben'>@begeeben</a>) on <a href='http://codepen.io'>CodePen</a>.
Practice
Create a simple custom HTML element, when its attribute 'pass' is changed to true, change its color to green.
Animation and Web Components
Yi-Fan Liao
2016.Mar.21