Joan León PRO
Frontend Developer @AdevintaSpain · Perf Reviewer at @PerfReviews_ · Creative Coder at @CodingGirona · @GoogleDevExpert in web technology · ❤️ CSS, Animation & #webperf
Joan León
element.animate( keyframes, options )
Línea de
@keyframes
CSS
Propiedades de tiempo
animate-*
CSS
JS
JS
WAAPI
let keyframes = {
transform: ['none', 'scale(2)']
}
WAAPI Creando una animación
CSS
JS
@keyframe scale {
from { transform: none }
to { transform: scale(2) }
}
element.animate( keyframes, properties )
let keyframes = [
{ transform: 'none' },
{ transform: 'scale(2)' }
]
WAAPI Creando una animación
CSS
JS
@keyframe scale {
from { transform: none }
to { transform: scale(2) }
}
element.animate( keyframes, properties )
let properties = {
duration: 3000,
iterations: Infinity,
direction: 'alternate',
delay: 2000
}
WAAPI Creando una animación
CSS
JS
.anomated {
animation-name: scale;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-delay: 2s;
}
element.animate( keyframes, properties )
let properties = {
delay: 0,
direction: 'normal',
duration: 0,
easing: 'linear',
endDelay: 0, // No CSS version
fill: 'none',
iterationStart: 0, // No CSS version
iterations: 1
}
JS
WAAPI Creando una animación
element.animate( keyframes, properties )
let animation = new Animation([effect][, timeline])
animation.pause()
animation.play()
animation.reverse()
animation.finish()
animation.cancel()
Methods
WAAPI Creando una animación
element.animate( keyframes, properties )
animation.currentTime
animation.effect
animation.finished
animation.id
animation.playState
animation.playbackRate
animation.ready
animation.startTime
animation.timeline
Properties
WAAPI Creando una animación
element.animate( keyframes, properties )
animation.oncancel
animation.onfinish
Events
animation.playbackRate = 0.5;
animation.playbackRate = 1;
animation.playbackRate = 2;
animation.playbackRate = 3;
Properties
WAAPI Creando una animación
element.animate( keyframes, properties )
if ( synced ) {
p2.currentTime = p.currentTime - (Math.random() * 700);
} else {
p2.currentTime = p.currentTime;
}
WAAPI Creando una animación
element.animate( keyframes, properties )
WAAPI
let keyframes = [
{ width: '50px'},
{ width: '100px'}
]
let options = {
duration: 2000,
iterations: 2,
delay: 3000
}
let animation = element.animate(keyframes, options)
WAAPI
let keyframes = [
{ width: '50px'},
{ width: '100px'}
]
element.animate( keyframes, properties )
WAAPI
let keyframes = [
{ width: '50px'},
{ width: '75px'},
{ width: '100px'}
]
element.animate( keyframes, properties )
WAAPI
let keyframes = [
{ width: '50px'},
{ width: '75px'},
{ width: '85px'},
{ width: '100px'}
]
element.animate( keyframes, properties )
WAAPI
let keyframes = [
{ width: '50px', offset: 0},
{ width: '75px', offset: 0.75},
{ width: '85px', offset: 0.85},
{ width: '100px', offset: 1}
]
element.animate( keyframes, properties )
WAAPI
WAAPI
let animated = document.querySelector('.toAnimate');
let pulseKeyframes,
activateKeyframes,
haveFunKeyframes;
let pulse = animated.animate(pulseKeyframes, 1000);
let activate = animated.animate(activateKeyframes, 3000);
let haveFunWithIt = animated.animate(haveFunKeyframes, 2500);
CSS
JS
.toAnimate {
animation: pulse 1s, activate 3000ms, have-fun-with-it 2.5s;
}
@keyframes pulse { /* ... */ }
@keyframes activate { /* ... */ }
@keyframes have-fun-with-it { /* ... */ }
WAAPI
var a = document.querySelectorAll('div');
a = Array.prototype.slice.call(a);
a.forEach(function(el, i, ra) {
var to = {
x: Math.random() * (i % 2 === 0 ?-11 : 11),
y: Math.random() * 12
}
el.animate([
{ transform: 'translate(0,0)' },
{ transform: 'translate('+to.x+'rem,'+to.y+'rem)' }
], {
duration: (Math.random() + 1) * 2000,
direction: 'alternate',
fill: 'both',
iterations: Infinity,
easing: 'ease-in-out'
});
el.animate([
{ opacity: Math.random() },
{ opacity: Math.random() }
], {
duration: (Math.random() + 1) * 2000,
direction: 'alternate',
fill: 'both',
iterations: Infinity,
easing: 'ease-in-out'
});
el.animate([
{ background: 'lightcoral' },
{ background: 'lightgreen' }
], {
duration: (Math.random() + 1) * 2000,
direction: 'alternate',
fill: 'both',
iterations: Infinity,
easing: 'ease-in-out'
});
});
WAAPI
let rabbitDownKeyframes = new KeyframeEffect(
whiteRabbit,
[
{ transform: 'translateY(0%)' },
{ transform: 'translateY(100%)' }
],
{ duration: 3000, fill: 'forwards' }
)
let anim = new Animation(rabbitDownKeyframes, document.timeline)
anim.play()
KeyframeEffect(elm,keyframes,options)
element
keyframes
timing
WAAPI
let timings = {
duration: 1000
}
let keyframes = [
{ opacity: 1 },
{ opacity: 0 }
]
let kEffects = [
new KeyframeEffect(elem, keyframes, timings),
new KeyframeEffect(elem2, keyframes, timings)
]
let group = new GroupEffect(kEffects)
document.timeline.play(group)
GroupEffect(kEffects)
Polyfill
WAAPI
var timings = {
duration: 1000
}
var keyframes = [
{ opacity: 1 },
{ opacity: 0 }
]
var kEffects = [
new KeyframeEffect(elem, keyframes, timings),
new KeyframeEffect(elem2, keyframes, timings)
]
let sequence = new SequenceEffect(kEffects)
document.timeline.play(sequence)
GroupEffect(kEffects)
Polyfill
WAAPI vs CSS
CSS
JS
document.timeline
WAAPI vs CSS
CSS
JS
.element.animated {
animation: fadeOut 2s linear 0s 1 forwards;
}
@keyframes fadeOut {
from { opacity: 1; }
to { opacity: 0; }
}
element.addEventListener('click', (e) => {
element.classList.toggle('animated')
})
element.addEventListener('click', (e) => {
element.animate([
{ opacity: 1 },
{ opacity: 0 },
], {
duration: 2000,
easing: 'ease',
iterations: 1,
direction: 'normal',
fill: 'forwards'
})
})
WAAPI
WAAPI + CSS
CSS
WAAPI
WAAPI
Easing
let keyframes = [
{ opacity: 0, easing: 'easing-in'},
{ opacity: 0, easing: 'easing-out'},
{ opacity: 1 }
]
let options = {
duration: 1000,
easing: 'ease-in-out'
}
WAAPI
Offset
let keyframes = [
{ offset: 0, opacity: 0, easing: 'easing-in'},
{ offset: 0.8, opacity: 0, easing: 'easing-out'},
{ offset: 1, opacity: 1 }
]
WAAPI
<!-- Include the polyfill -->
<script src="web-animations.min.js"></script>
<!-- Set up a target to animate -->
<div class="pulse" style="width: 150px;">Hello world!</div>
<!-- Animate! -->
<script>
var elem = document.querySelector('.pulse');
var animation = elem.animate({
opacity: [0.5, 1],
transform: ['scale(0.5)', 'scale(1)'],
}, {
direction: 'alternate',
duration: 500,
iterations: Infinity,
});
</script>
Joan León
By Joan León
Presentación de la charla ¿Quieres animaciones? Claro que sí, WAAPI | Codemotion 2017