Search code examples
css-animationskeyframeclip-pathanime.js

Transpose Clippath CSS Keyframe animations into animejs animation


I have this Keyframe animation that does work very well:

body{
  background: red;
}
.myAnimation{
   width: 100px;
   height: 200px;
   background: blue;
   clip-path: inset(100px 50px);
   animation: myClipAnim 3s infinite;
   transition: animation 2s;
}

@keyframes myClipAnim {
    100% { clip-path: inset(0) }
}
<body>
  <div class="myAnimation"></div>
</body>

Now I want to reproduce the exact same animation with animejs, so I tried the following:

anime({
  targets: '.myAnimation',
  direction: 'reverse',
  keyframes: [
      {clipPath: 'inset(0)' }
  ],
  duration: 3000
});
body{
  background: red;
}
.myAnimation{
   width: 100px;
   height: 200px;
   background: blue;
   clip-path: inset(100px 50px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.0/anime.min.js"></script>
<body>
  <div class="myAnimation"></div>
</body>

But it doesn't work, I don't understand why. Where am I wrong ? Please help!


Solution

  • In animejs you have to specify at least two keyFrames (start and end frame), otherwise it will be static.

    So, after adding the second keyFrame {clipPath: 'inset(100px 50px)'}, I added the loop and easing attributes to match your CSS based example.

    anime({
      targets: '.myAnimation',
      direction: 'reverse',
      easing: 'easeInOutSine', // choose from https://animejs.com/documentation/#pennerFunctions
      loop: true, // let the animation repeat itself 
      keyframes: [
          {clipPath: 'inset(0)' }, // start frame
          {clipPath: 'inset(100px 50px)'}, // end frame
      ],
      duration: 3000
    });
    body{
      background: red;
    }
    .myAnimation{
       width: 100px;
       height: 200px;
       background: blue;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.0/anime.min.js"></script>
    <body>
      <div class="myAnimation"></div>
    </body>