juliangarnier / anime

JavaScript animation engine
https://animejs.com
MIT License
50.4k stars 3.68k forks source link

letter flip and swap #812

Open oskude opened 2 years ago

oskude commented 2 years ago

i'm trying to do this effect with animejs:

  1. flip letters of a text line 0deg -> 90deg
  2. overlap flip letters from next text line 90deq -> 0deg
  3. pause
  4. repeat

i got this far, where the letters from "foo" -> "baar" flip and swap like i want, but for "baar" -> "foo" (end/start of loop) i could not figure out how to overlap them.

<!doctype html>
<html>
<head>
<script src="anime.min.js"></script>
<style>
ol {
    list-style: none;
    display: inline-grid;
    margin: 0;
    padding: 0;
}
li {
    display: inline-block;
    grid-column-start: 1;
    grid-row-start: 1;
}
span {
    display: inline-block;
}
</style>
</head>
<body>
<h1>hello <ol><li>foo</li><li>baar</li></ol> world</h1>
</body>
<script>
let lines = document.querySelectorAll("li");
let lineChars = [];
let duration = 500;
let delay = 200;
let pause = 1000;
let timeline = anime.timeline({
    loop: true,
});
lines.forEach((line, i)=>{
    line.innerHTML = line.textContent.replace(/\S/g, "<span>$&</span>");
    let chars = line.querySelectorAll("span");
    lineChars[i] = chars.length;
    let offset = delay * (lineChars[i-1] - 1);
    timeline.add({
        targets: chars,
        easing: 'linear',
        rotateY: [90, 0],
        perspective: 10,
        duration: duration,
        delay: anime.stagger(delay),
        endDelay: pause,
    }, '-=' + (offset > 0 ? offset : 0));
    timeline.add({
        targets: chars,
        easing: 'linear',
        rotateY: [0, 90],
        perspective: 10,
        duration: duration,
        delay: anime.stagger(delay),
    });
});
</script>
</html>

any ideas?