| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API | [Back] [Original] |
Get to know MDN better
API JavaScript API CSS CSS Window.requestAnimationFrame() 1
API JavaScript CSS DOM CSS JavaScript API
APIRachel Nabors CodePen
API CSS CSS
CSS
<div class="wrapper">
<div id="tunnel"></div>
<div id="alice">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">
<path
d="M110.1 2.7h8.9c3.4.4 6.7.8 10.1 1.3 9.8 1.5 17.8 6.4 24.5 13.7.4.5 1.9.6 2.5.3 6.8-4.4 13.9-8.2 21.9-9.9 1.3-.3 3.4-1.2 3.7 1.5.6 4.9 1.4 9.9 1.7 14.8.3 4.4.1 8.7.1 12.2 2.1 1.5 4.6 2.3 5.5 4 4.2 8.4 3.2 17.6 3.1 26.6 0 1.2-.4 3.3.1 3.6 10.3 4.9 20.7 9.6 31.1 14.4 2.5-4.9-2.3-16-15.8-14.4.6-.5 1.4-1.1 2.2-1.1 2.5.1 4.9.4 7.4.7 6 .8 10.9 3.7 14.6 8.4 1.2 1.5 1.6 4.2 1.1 6.1-.7 3.2-3.7 4-7.1 4.1 4.5 3.5 6.5 8.1 6.8 13.3.6 9.4-1.1 18.6-4.8 27.1-3.9 8.8-5.2 17.5-3.3 26.8.6 3.2 1.2 7 .2 9.9-2 6.2-7.8 8.6-13.4 10.9-3 1.2-7.4 1.2-6.3 6.3.8 3.7-.4 4 .2 4.5 5.8 5.8 11.8 11.5 17.6 17.3 1.7 1.7 3 3.8 4.3 5.5-1.1.4-1.8.7-2.4 1 7.5 5.8 14.9 11.6 22.4 17.4 4.3-4.3 8.6-9 13.3-13.2 8.1-7.3 16.7-14 24.5-21.7 3.3-3.3 4.9-8.2 7.4-12.3.3-.4 1.3-.9 1.6-.7 4.6 2.7 6.8 7.2 7.9 12.1 1.3 5.7 1.6 11.6 2.3 17.1 4.2-.2 8.8-.8 13.4-.4 2 .1 4.6 1.8 5.5 3.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3.1-22.4-.2-1.1.2-2.3.3-3.4-22.1 17.6-38.8 38.4-42.9 67.4-4 28-2.8 54.8 13.5 79.1-36.3-13.8-53-48.6-58.3-84.1-3 8-15 16.3-22.4 16.6v-.2c2.1-2.9 11.1-10.6 7-30.2-1.3-10.7-4.1-21.2-5.1-31.9-1-10.9-1-21.9-.5-32.9.3-11.6 3.8-22.7 8.6-33.2 5.7-12.5 13.5-23.8 23-33.6 5.6-5.8 11.9-11 18.2-16.1 8.6-6.8 17.7-12.9 28.2-16.5 5.1-1.9 10.4-3 15.7-4.5zm96.4 221.9c-.4.9-1.2 2-1.1 3 .5 7.6 1.2 15.2 2 22.7.2 2.1 0 4.8 3.3 5.5 3.3.7 6.6 1.8 9.9 2.6.3.1.9-.1 1.1-.4 3.8-4.8 7.5-9.6 10.9-14-8.4-6.1-17.1-12.6-26.1-19.4zm-23.1-42.5v6.3c1.9-2 3.6-3.9 5.3-5.7-1.7-.2-3.5-.4-5.3-.6z" />
</svg>
</div>
</div>
"Play" CSS
#alice {
animation: alice-tumbling infinite 3s linear;
}
@keyframes alice-tumbling {
0% {
color: black;
transform: rotate(0) translate3d(-50%, -50%, 0);
}
30% {
color: #431236;
}
100% {
color: black;
transform: rotate(360deg) translate3d(-50%, -50%, 0);
}
}
#tunnel {
animation: tunnel-fly 1s linear infinite;
}
@keyframes tunnel-fly {
100% {
transform: translate3d(0, -300px, 0);
}
}
#alice {
color: #431236;
width: 25%;
position: absolute;
top: 50%;
left: 50%;
transform-origin: 0 0;
transform: rotate(0) translate3d(-50%, -50%, 0);
backface-visibility: hidden;
will-change: transform, color;
}
path {
fill: currentColor;
}
#tunnel {
background:
url("/shared-assets/images/examples/web-animations/bg-tunnel-border-left.svg")
repeat-y,
url("/shared-assets/images/examples/web-animations/bg-tunnel-border-right.svg")
repeat-y 100% 100%,
url("/shared-assets/images/examples/web-animations/bg-tunnel-specks.png"),
#6c373f;
margin: 0 auto;
height: calc(100% + 300px);
width: 60%;
min-width: 400px;
backface-visibility: hidden;
will-change: transform;
}
.wrapper {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
body {
background: black;
}
html,
body {
height: 100%;
}
3 @keyframes 30% 0.9
API
Keyframe CSS @keyframes
const aliceTumbling = [
{ transform: "rotate(0) translate3d(-50%, -50%, 0)", color: "black" },
{ color: "#431236", offset: 0.3 },
{ transform: "rotate(360deg) translate3d(-50%, -50%, 0)", color: "black" },
];
CSS CSS API 3 Keyframe 50%
CSS 30%50% offset: 0.3
2 2 1 Element.animate() NotSupportedError DOMException
const aliceTiming = {
duration: 3000,
iterations: Infinity,
};
CSS
setTimeout() Window.requestAnimationFrame() API iteration-count iterations :
CSS "infinite" JavaScript Infinity timing-function easing easing animation-timing-function ease CSS API linear
document.getElementById("alice").animate(aliceTumbling, aliceTiming);
document
.getElementById("tunnel")
.animate(
[
{ transform: "translate3d(0, 0, 0)" },
{ transform: "translate3d(0, -300px, 0)" },
],
{
duration: 1000,
iterations: Infinity,
},
);
animate() CSS DOM
document.getElementById("alice").animate(
[
{ transform: "rotate(0) translate3d(-50%, -50%, 0)", color: "black" },
{ color: "#431236", offset: 0.3 },
{ transform: "rotate(360deg) translate3d(-50%, -50%, 0)", color: "black" },
],
{
duration: 3000,
iterations: Infinity,
},
);
1
document.getElementById("alice").animate(
[
{ transform: "rotate(0) translate3d(-50%, -50%, 0)", color: "black" },
{ color: "#431236", offset: 0.3 },
{ transform: "rotate(360deg) translate3d(-50%, -50%, 0)", color: "black" },
],
3000,
);
API CSS API API Follow the White Rabbit
<div class="wrapper">
<div class="page">
<div class="background"></div>
<div id="rabbit"></div>
<div class="foreground"></div>
<p>
</p>
</div>
</div>
#rabbit {
background: url("/shared-assets/images/examples/web-animations/park5_rabbit.png")
0 0 / 100% 100%;
cursor: pointer;
position: absolute;
top: 15%;
left: 60%;
width: 14.64844%;
padding-top: 31.00586%;
}
body {
background: black;
}
.wrapper {
max-width: 133.33vh;
margin: 0 auto;
}
.page {
background: #431236;
height: 0;
overflow: hidden;
padding-top: 75%;
position: relative;
text-indent: 100%;
white-space: nowrap;
}
.foreground {
height: 100%;
background: url("/shared-assets/images/examples/web-animations/bg_park5_2.png")
no-repeat 100% 100% / 100% auto;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
pointer-events: none;
}
.background {
background: url("/shared-assets/images/examples/web-animations/bg_park5_1.png")
no-repeat 0 0 / 100% auto;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
animate()
const whiteRabbit = document.getElementById("rabbit");
const rabbitDownAnimation = whiteRabbit.animate(
[{ transform: "translateY(0%)" }, { transform: "translateY(100%)" }],
{ duration: 3000, fill: "forwards" },
);
Element.animate() Animation.pause()
rabbitDownAnimation.pause();
:
Animation() rabbitDownAnimation play()
whiteRabbit.addEventListener("click", downHeGoes);
whiteRabbit.addEventListener("touchstart", downHeGoes);
function downHeGoes(event) {
whiteRabbit.removeEventListener("click", downHeGoes);
whiteRabbit.removeEventListener("touchstart", downHeGoes);
rabbitDownAnimation.play();
}
downHeGoes
playbackRate playbackRate
Through the Looking-Glass
<div class="wrapper">
<div class="sky"></div>
<div class="earth">
<div id="red-queen-and-alice">
<img
id="red-queen-and-alice-sprite"
src="/shared-assets/images/examples/web-animations/sprite_running-alice-queen_small.png"
srcset="
/shared-assets/images/examples/web-animations/sprite_running-alice-queen.png 2x
"
alt="Alice and the Red Queen running to stay in place." />
</div>
</div>
<div class="scenery" id="foreground1">
<img
id="palm3"
src="/shared-assets/images/examples/web-animations/palm3_small.png"
srcset="/shared-assets/images/examples/web-animations/palm3.png 2x"
alt="" />
</div>
<div class="scenery" id="foreground2">
<img
id="bush"
src="/shared-assets/images/examples/web-animations/bush_small.png"
srcset="/shared-assets/images/examples/web-animations/bush.png 2x"
alt="" />
<img
id="w_rook_upright"
src="/shared-assets/images/examples/web-animations/w_rook_upright_small.png"
srcset="
/shared-assets/images/examples/web-animations/w_rook_upright.png 2x
"
alt="" />
</div>
<div class="scenery" id="background1">
<img
id="r_pawn_upright"
src="/shared-assets/images/examples/web-animations/r_pawn_upright_small.png"
srcset="
/shared-assets/images/examples/web-animations/r_pawn_upright.png 2x
"
alt="" />
<img
id="w_rook"
src="/shared-assets/images/examples/web-animations/w_rook_small.png"
srcset="/shared-assets/images/examples/web-animations/w_rook.png 2x"
alt="" />
<img
id="palm1"
src="/shared-assets/images/examples/web-animations/palm1_small.png"
srcset="/shared-assets/images/examples/web-animations/palm1.png 2x"
alt="" />
</div>
<div class="scenery" id="background2">
<img
id="r_pawn"
src="/shared-assets/images/examples/web-animations/r_pawn_small.png"
srcset="/shared-assets/images/examples/web-animations/r_pawn.png 2x"
alt="" />
<img
id="r_knight"
src="/shared-assets/images/examples/web-animations/r_knight_small.png"
srcset="/shared-assets/images/examples/web-animations/r_knight.png 2x"
alt="" />
<img
id="palm2"
src="/shared-assets/images/examples/web-animations/palm2_small.png"
srcset="/shared-assets/images/examples/web-animations/palm2.png 2x"
alt="" />
</div>
</div>
* {
user-select: none;
}
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.scenery {
width: 100%;
height: 50%;
position: absolute;
bottom: 0;
left: 0;
}
#foreground1,
#foreground2 {
z-index: 1;
}
#foreground2,
#background2 {
transform: translateX(100%);
}
#palm3 {
top: 0;
left: 10%;
}
#w_rook_upright {
top: 30%;
left: 75%;
}
#r_pawn {
top: 10%;
left: 15%;
}
#w_rook {
top: 10%;
left: 80%;
}
#r_pawn_upright {
top: 5%;
left: 30%;
}
#r_knight {
top: 0;
left: 70%;
}
#palm2 {
top: -15%;
left: 90%;
}
#palm1 {
top: -15%;
left: 40%;
}
#bush {
top: 55%;
left: 20%;
}
#red-queen-and-alice {
position: absolute;
top: 0;
left: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
width: 80%;
max-width: 450px;
z-index: 1;
}
#red-queen-and-alice::before {
content: " ";
display: block;
padding-top: 87%;
}
#red-queen-and-alice img {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.sky,
.earth {
position: absolute;
left: 0;
height: 50vh;
width: 100%;
}
.earth {
background: #eb125d
url("/shared-assets/images/examples/web-animations/bg_earth.jpg") repeat-x 0
0 / 100% auto;
bottom: 0;
}
.sky {
background: #246e89
url("/shared-assets/images/examples/web-animations/bg_sky.jpg") repeat-x
100% 100% / auto 100%;
top: 0;
}
html,
body {
width: 100%;
height: 100%;
}
.wrapper {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
const background1 = document.getElementById("background1");
const background2 = document.getElementById("background2");
const foreground1 = document.getElementById("foreground1");
const foreground2 = document.getElementById("foreground2");
const redQueenAliceSprite = document.getElementById(
"red-queen-and-alice-sprite",
);
/* Background animations */
const sceneryFrames = [
{ transform: "translateX(100%)" },
{ transform: "translateX(-100%)" },
];
const sceneryTimingBackground = {
duration: 36000,
iterations: Infinity,
};
const sceneryTimingForeground = {
duration: 12000,
iterations: Infinity,
};
const background1Movement = background1.animate(
sceneryFrames,
sceneryTimingBackground,
);
background1Movement.currentTime =
background1Movement.effect.getComputedTiming().duration / 2;
const background2Movement = background2.animate(
sceneryFrames,
sceneryTimingBackground,
);
const foreground1Movement = foreground1.animate(
sceneryFrames,
sceneryTimingForeground,
);
foreground1Movement.currentTime =
foreground1Movement.effect.getComputedTiming().duration / 2;
const foreground2Movement = foreground2.animate(
sceneryFrames,
sceneryTimingForeground,
);
const spriteFrames = [
{ transform: "translateY(0)" },
{ transform: "translateY(-100%)" },
];
const redQueenAlice = redQueenAliceSprite.animate(spriteFrames, {
easing: "steps(7, end)",
direction: "reverse",
duration: 600,
playbackRate: 1,
iterations: Infinity,
});
playbackRate updatePlaybackRate() playbackRate
setInterval(() => {
// 0.4
if (redQueenAlice.playbackRate > 0.4) {
redQueenAlice.updatePlaybackRate(redQueenAlice.playbackRate * 0.9);
}
adjustBackgroundPlayback();
}, 1000);
playbackRate
function goFaster() {
//
redQueenAlice.updatePlaybackRate(redQueenAlice.playbackRate * 1.1);
adjustBackgroundPlayback();
}
document.addEventListener("click", goFaster);
document.addEventListener("touchstart", goFaster);
playbackRate 2
/*
*/
const sceneries = [
foreground1Movement,
foreground2Movement,
background1Movement,
background2Movement,
];
function adjustBackgroundPlayback() {
// 0.8 1.2
//
// 0.8
if (redQueenAlice.playbackRate < 0.8) {
sceneries.forEach((anim) => {
anim.updatePlaybackRate(-redQueenAlice.playbackRate / 2);
});
} else if (redQueenAlice.playbackRate > 1.2) {
sceneries.forEach((anim) => {
anim.updatePlaybackRate(redQueenAlice.playbackRate / 2);
});
} else {
sceneries.forEach((anim) => {
anim.updatePlaybackRate(0);
});
}
}
adjustBackgroundPlayback();
fill forwards 2 fill
Animation.commitStyles() style
fill forwards backwards both fill DocumentTimeline scroll-timeline AnimationEffect 2 4 JavaScript
replaceState removed persist() persisted active
playbackRate CSS CSS API Document.getAnimations playbackRate
document.getAnimations().forEach((animation) => {
animation.updatePlaybackRate(animation.playbackRate * 0.5);
});
API 1
CSS /
document.getElementById("eat-me-sprite").animate([], {
duration: aliceChange.effect.getComputedTiming().duration / 2,
});
const aliceChange = document
.getElementById("alice")
.animate(
[
{ transform: "translate(-50%, -50%) scale(.5)" },
{ transform: "translate(-50%, -50%) scale(2)" },
],
{
duration: 8000,
easing: "ease-in-out",
fill: "both",
},
);
8 2
aliceChange.pause();
"playhead" Animation.currentTime 4
aliceChange.currentTime = 4000;
currentTime 2 aliceChange Animation.effect
aliceChange.currentTime = aliceChange.effect.getComputedTiming().duration / 2;
effect aliceChange.effect.getComputedTiming() duration
const drinking = document
.getElementById("liquid")
.animate([{ height: "100%" }, { height: "0" }], {
fill: "forwards",
duration: aliceChange.effect.getComputedTiming().duration / 2,
});
drinking.pause();
3 1 1
API currentTime activeDuration
const endGame = () => {
//
const alicePlayhead = aliceChange.currentTime;
const aliceTimeline = aliceChange.effect.getComputedTiming().activeDuration;
//
stopPlayingAlice();
// 3
const aliceHeight = alicePlayhead / aliceTimeline;
if (aliceHeight <= 0.333) {
//
//
} else if (aliceHeight >= 0.666) {
//
//
} else {
//
//
}
};
CSS API
endGame
//
nommingCake.onfinish = endGame;
drinking.onfinish = endGame;
//
aliceChange.onfinish = endGame;
API finished
API
| Web Proxy Viewer | New URL | Original Page |