【CSS】ボールがバウンドするアニメーション

CSS
動点P
動点P

いい跳ね方をしているボールを作りたい!

動点Qちゃん
動点Qちゃん

シンプルなようで難しいのです・・・

 

@keyframes

バウンドするボール

@keyframes bounce {
      0% {
        margin-top: 10px;
        transform: scaleY(0.4);
	transform-origin: top center;
      }
      3.3% {
        margin-top: 10px;
        transform: scaleY(0.4);
        transform-origin: top center;
      }							      
      5.5% {
         margin-top: 10px;
        transform: scaleY(0.4);
	transform-origin: top center;
      }					      
      12.5% {									            
        margin-top: 10px;
        transform: scaleY(0.5);
	transform-origin: top center;
      }
       25% {
        transform: scaleY(1.0);
	transform-origin: center;
      }
      50% {
        transform: scaleY(1.1);
	transform-origin: center;
      }
      75% {
        transform: scaleY(1.0);
	transform-origin: center;
      }						                
      80% {
        margin-top: 200px;
        transform: scaleY(0.6);
	transform-origin: bottom center;
      }							          
       87% {
        margin-top: 200px;
        transform: scaleY(0.5);
	transform-origin: bottom center;
      }
        90% {
        transform: scaleY(0.4);
        margin-top: 200px;
	transform-origin: bottom center;
      }
      100% {
        transform: scaleY(0.4);
        margin-top: 200px;
	transform-origin: bottom center;
      }
    }
 @keyframes push {
      0% {
        width: 130px;
      }
      25% {
        width: 100px;
      }
      50% {
        width: 80px;
      }
      75% {
        width: 100px;
      }
      100% {
        width: 130px;
      }
    }
animation: bounce .7s cubic-bezier(0.45, 0, 0.55, 1)  0s infinite alternate, push .7s cubic-bezier(0.45, 0, 0.55, 1)  0s infinite alternate;

上下に動くbounceと横の幅の増減を動かすpushという2つのanimationをつけています。

やわらかい弾むボールの場合、ボールの形が変わったり、上下で物にぶつかった瞬間にちょっと時間を溜めて動きをデフォルメするとかわいいかなと思いました。

アニメーション実行スピード調整には今回cubic-bezierを使いました。他にもイージングの値があります。

動き
easeデフォルト:開始と完了を滑らかに 
linear速度一定
ease-inゆっくり 始まる
ease-outゆっくり 終わる
ease-in-outゆっくり 始まり ゆっくり 終わる
cubic-bezier(x1,y1,x2,y2)ベジェ曲線 で指定
stepsspriteなどのコマ撮りアニメに使う
イージングの値

どうやらふわっと出てくる表現などではナチュラルになるようにease-outが推奨されているらしいですが、ボールのバウンドとかだとease-in-outかベジェ曲線で作る方がそれっぽいのかな…と思いました。どんな動きがいいのかはそれぞれの動かしたいアニメによって変わってきますね。

cubic-bezier は自分で値を設定するのは難しいので、オンライン上で自分でベジェ曲線を操作することで値を作ってくれるサイトなどで作成できます。

イージング関数集もあります。↓

コメント