Skip to content

Commit a680190

Browse files
adding text examples for automatic removal of filling animations
1 parent 9e4b3c9 commit a680190

File tree

2 files changed

+75
-75
lines changed

2 files changed

+75
-75
lines changed

web-animations-api/replace-indefinite-animations-2.html

+50-25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
body {
1313
margin: 0;
1414
height: inherit;
15+
display: flex;
16+
justify-content: center;
17+
align-items: center;
1518
}
1619

1720
div {
@@ -32,31 +35,53 @@
3235
<script>
3336
const divElem = document.querySelector('div');
3437

35-
document.body.addEventListener('mousemove', evt => {
36-
let anim = divElem.animate(
37-
{ transform: `translate(${ evt.clientX}px, ${evt.clientY}px)` },
38-
{ duration: 500, fill: 'forwards' }
39-
);
40-
41-
// commitStyles() writes the end state of the animation to the animated
42-
//element inside a style attribute
43-
anim.commitStyles();
44-
45-
// If you explicity want the animations to be retained, then you can invoke persist()
46-
// But don't do this unless you really need to — having a huge list of animations
47-
// persisted can cause a memory leak
48-
//anim.persist()
49-
50-
// onremove allows you to run an event handler that fires when the animation
51-
// was removed (i.e. put into an active replace state)
52-
anim.onremove = function() {
53-
console.log('Animation removed');
54-
}
55-
56-
// replaceState allows you to query an element to see what its replace state is
57-
// It will be active by default, or persisted if persist() was invoked
58-
console.log(anim.replaceState);
59-
});
38+
// Keyframes
39+
let growShrink = [
40+
{ transform: 'scale(1)' },
41+
{ transform: 'scale(1.5)' }
42+
];
43+
44+
let rotate360 = [
45+
{ transform: 'rotate(0deg)' },
46+
{ transform: 'rotate(360deg)' }
47+
];
48+
49+
let moveLeftRight = [
50+
{ transform: 'translateX(0)' },
51+
{ transform: 'translateX(300px)' },
52+
{ transform: 'translateX(0)' }
53+
];
54+
55+
// timing properties
56+
let slowInfinite = {
57+
duration: 5000,
58+
iterations: Infinity,
59+
fill: 'forwards'
60+
}
61+
62+
let slowTwice = {
63+
duration: 3000,
64+
iterations: 2,
65+
fill: 'forwards'
66+
}
67+
68+
let animation1 = divElem.animate( growShrink, slowTwice );
69+
// commitStyles()
70+
animation1.commitStyles();
71+
72+
73+
let animation2 = divElem.animate( rotate360, slowInfinite );
74+
animation2.addEventListener('remove', () => {
75+
console.log('Animation 2 removed');
76+
})
77+
// persist()
78+
animation2.persist();
79+
80+
let animation3 = divElem.animate( moveLeftRight, slowInfinite );
81+
82+
console.log(animation1.replaceState);
83+
console.log(animation2.replaceState);
84+
console.log(animation3.replaceState);
6085

6186
</script>
6287
</body>

web-animations-api/replace-indefinite-animations.html

+25-50
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
body {
1313
margin: 0;
1414
height: inherit;
15-
display: flex;
16-
justify-content: center;
17-
align-items: center;
1815
}
1916

2017
div {
@@ -35,53 +32,31 @@
3532
<script>
3633
const divElem = document.querySelector('div');
3734

38-
// Keyframes
39-
let growShrink = [
40-
{ transform: 'scale(1)' },
41-
{ transform: 'scale(1.5)' }
42-
];
43-
44-
let rotate360 = [
45-
{ transform: 'rotate(0deg)' },
46-
{ transform: 'rotate(360deg)' }
47-
];
48-
49-
let moveLeftRight = [
50-
{ transform: 'translateX(0)' },
51-
{ transform: 'translateX(300px)' },
52-
{ transform: 'translateX(0)' }
53-
];
54-
55-
// timing properties
56-
let slowInfinite = {
57-
duration: 5000,
58-
iterations: Infinity,
59-
fill: 'forwards'
60-
}
61-
62-
let slowTwice = {
63-
duration: 3000,
64-
iterations: 2,
65-
fill: 'forwards'
66-
}
67-
68-
let animation1 = divElem.animate( growShrink, slowTwice );
69-
// commitStyles()
70-
animation1.commitStyles();
71-
72-
73-
let animation2 = divElem.animate( rotate360, slowInfinite );
74-
animation2.addEventListener('remove', () => {
75-
console.log('Animation 2 removed');
76-
})
77-
// persist()
78-
animation2.persist();
79-
80-
let animation3 = divElem.animate( moveLeftRight, slowInfinite );
81-
82-
console.log(animation1.replaceState);
83-
console.log(animation2.replaceState);
84-
console.log(animation3.replaceState);
35+
document.body.addEventListener('mousemove', evt => {
36+
let anim = divElem.animate(
37+
{ transform: `translate(${ evt.clientX}px, ${evt.clientY}px)` },
38+
{ duration: 500, fill: 'forwards' }
39+
);
40+
41+
// commitStyles() writes the end state of the animation to the animated
42+
//element inside a style attribute
43+
anim.commitStyles();
44+
45+
// If you explicity want the animations to be retained, then you can invoke persist()
46+
// But don't do this unless you really need to — having a huge list of animations
47+
// persisted can cause a memory leak
48+
//anim.persist()
49+
50+
// onremove allows you to run an event handler that fires when the animation
51+
// was removed (i.e. put into an active replace state)
52+
anim.onremove = function() {
53+
console.log('Animation removed');
54+
}
55+
56+
// replaceState allows you to query an element to see what its replace state is
57+
// It will be active by default, or persisted if persist() was invoked
58+
console.log(anim.replaceState);
59+
});
8560

8661
</script>
8762
</body>

0 commit comments

Comments
 (0)