CSS animation で遊び倒す - SVG Clip Path -

CSS animation day 53 となりました。

本日は、SVG-Clip Path を用いて、前回の[作品](https://qiita.com/duka/items/e65354f103ebc9280959)をアレンジします。

#1. 完成版

<p class="codepen" data-height="561" data-theme-id="0" data-default-tab="html,result" data-user="hiroyaiizuka" data-slug-hash="BbxapQ" style="height: 561px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid black; margin: 1em 0; padding: 1em;" data-pen-title="Sakura Fall">
  <span>See the Pen <a href="https://codepen.io/hiroyaiizuka/pen/BbxapQ/">
  Sakura Fall</a> by hiroya iizuka (<a href="https://codepen.io/hiroyaiizuka">@hiroyaiizuka</a>)
  on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

#2. 参考文献
[CSS Tricks](https://css-tricks.com/using-css-clip-path-create-interactive-effects/)
[BRISK](https://b-risk.jp/blog/2018/07/svg-clippath/)

#3. 分解してみる

❶. 

Figma で桜を作ります。

![スクリーンショット 2019-03-16 8.11.44.png](https://qiita-image-store.s3.amazonaws.com/0/232715/2a7e7bc4-88fe-a8da-2421-ae882a7d87d9.png)


上のUnion ボタンを押してグループ化します。
その後、右クリック　→　Copy as SVG で、SVG コードをコピーします。

できたら、マークアップしましょう。

```index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="css/styles.css" />
  </head>
  <body>
    <div class="container">
      <svg
        width="189"
        height="180"
        viewBox="0 0 189 180"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
      >
        <path
          fill-rule="evenodd"
          clip-rule="evenodd"
          d="M92.4998 74.1024C106.031 74.1024 117 56.1961 117 34.1024C117 -9.92887 112.528 -2.62418 105.772 8.41097C101.948 14.661 97.3909 22.1024 92.4998 22.1024C87.6394 22.1024 83.1097 15.2703 79.3001 9.52425C72.5042 -0.729652 67.9998 -7.52262 67.9998 34.1024C67.9998 56.1961 78.969 74.1024 92.4998 74.1024ZM76.0472 85.2C80.2288 72.3328 66.5862 56.3641 45.576 49.5399C3.69804 35.9305 9.26396 42.4422 17.6717 52.2782C22.4314 57.8446 28.1018 64.4774 26.5906 69.1297C25.0887 73.7547 17.1897 75.95 10.5472 77.7977C-1.30245 81.0946 -9.15499 83.2782 30.4339 96.1414C51.4441 102.966 71.866 98.0711 76.0472 85.2ZM157.184 94.1414C136.174 100.97 115.752 96.0711 111.571 83.2C107.389 70.3328 121.032 54.3641 142.042 47.5399C181.632 34.6766 176.562 41.0594 168.912 50.6922C164.624 56.0907 159.526 62.5086 161.028 67.1297C162.539 71.7821 171.027 73.8172 178.15 75.5243C190.732 78.5399 199.061 80.536 157.184 94.1414ZM81.3772 102.403C70.4305 94.45 51.0301 102.489 38.0452 120.364C12.1634 155.985 20.075 152.704 32.0267 147.747C38.7928 144.942 46.8543 141.598 50.8113 144.473C54.7434 147.329 54.3919 155.52 54.096 162.407C53.5686 174.696 53.219 182.841 77.6868 149.165C90.6717 131.294 92.324 110.356 81.3772 102.403ZM114.077 154.048C101.092 136.177 99.4397 115.239 110.387 107.286C121.333 99.3328 140.734 107.376 153.719 125.247C178.186 158.923 170.549 156.075 159.025 151.774C152.565 149.364 144.885 146.501 140.952 149.356C136.995 152.231 137.684 160.931 138.262 168.235C139.283 181.134 139.959 189.673 114.077 154.048ZM96.4998 103.001C103.956 103.001 110 97.1805 110 90.0008C110 82.8211 103.956 77.0008 96.4998 77.0008C89.0437 77.0008 82.9998 82.8211 82.9998 90.0008C82.9998 97.1805 89.0437 103.001 96.4998 103.001Z"
          fill="#C4C4C4"
        />
      </svg>
    </div>
  </body>
</html>
```


```styles.scss
body {
  margin: 0;
  padding: 0;
  background: linear-gradient(to right, #200122, #6f0000);
}

.container {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
```

![スクリーンショット 2019-03-16 8.53.59.png](https://qiita-image-store.s3.amazonaws.com/0/232715/5937c7d4-af96-b11a-3941-7e80092391fd.png)

Figma で作ったSVG画像を、ブラウザで表示できました。
<br/>
❷.
では、ClipPath を作成していきますが、やり方としては、3stepに分かれます。

まず、切り抜きたいSVGを非表示にし、次にClipPath要素を作り、最後に切り抜きたい画像へ適応して完成します。
では、やっていきましょう。
<br/>
1: SVG 自体を非表示にする。 

```
<svg width: 0, height: 0, style=”position: absolute; top: 0; left: 0;” >
　　　　　　　　・・・
</svg>
```

2: ClipPath 要素を作る。

```
<svg width: 0, height: 0, style=”position: absolute; top: 0; left: 0;” >
   <clipPath>
      <path d = "~">
   </clipPath>
</svg>
```

実際にコードを見てみましょう。

```index.html
     <svg
        width="0"
        height="0"
        style="position: absolute; top: 0; left: 0;"
        xmlns="http://www.w3.org/2000/svg"
      >
        <clipPath id="sakura">
          <path
            fill-rule="evenodd"
            clip-rule="evenodd"
            d="M92.4998 74.1024C106.031 74.1024 117 56.1961 117 34.1024C117 -9.92887 112.528 -2.62418 105.772 8.41097C101.948 14.661 97.3909 22.1024 92.4998 22.1024C87.6394 22.1024 83.1097 15.2703 79.3001 9.52425C72.5042 -0.729652 67.9998 -7.52262 67.9998 34.1024C67.9998 56.1961 78.969 74.1024 92.4998 74.1024ZM76.0472 85.2C80.2288 72.3328 66.5862 56.3641 45.576 49.5399C3.69804 35.9305 9.26396 42.4422 17.6717 52.2782C22.4314 57.8446 28.1018 64.4774 26.5906 69.1297C25.0887 73.7547 17.1897 75.95 10.5472 77.7977C-1.30245 81.0946 -9.15499 83.2782 30.4339 96.1414C51.4441 102.966 71.866 98.0711 76.0472 85.2ZM157.184 94.1414C136.174 100.97 115.752 96.0711 111.571 83.2C107.389 70.3328 121.032 54.3641 142.042 47.5399C181.632 34.6766 176.562 41.0594 168.912 50.6922C164.624 56.0907 159.526 62.5086 161.028 67.1297C162.539 71.7821 171.027 73.8172 178.15 75.5243C190.732 78.5399 199.061 80.536 157.184 94.1414ZM81.3772 102.403C70.4305 94.45 51.0301 102.489 38.0452 120.364C12.1634 155.985 20.075 152.704 32.0267 147.747C38.7928 144.942 46.8543 141.598 50.8113 144.473C54.7434 147.329 54.3919 155.52 54.096 162.407C53.5686 174.696 53.219 182.841 77.6868 149.165C90.6717 131.294 92.324 110.356 81.3772 102.403ZM114.077 154.048C101.092 136.177 99.4397 115.239 110.387 107.286C121.333 99.3328 140.734 107.376 153.719 125.247C178.186 158.923 170.549 156.075 159.025 151.774C152.565 149.364 144.885 146.501 140.952 149.356C136.995 152.231 137.684 160.931 138.262 168.235C139.283 181.134 139.959 189.673 114.077 154.048ZM96.4998 103.001C103.956 103.001 110 97.1805 110 90.0008C110 82.8211 103.956 77.0008 96.4998 77.0008C89.0437 77.0008 82.9998 82.8211 82.9998 90.0008C82.9998 97.1805 89.0437 103.001 96.4998 103.001Z"
            fill="#C4C4C4"
          />
        </clipPath>
      </svg>
```
これで、桜型の鋳型が完成します。
<br/>
❸.
最後のステップとして、❷で作った鋳型を、切り抜きたい画像に適応します。

```index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="css/styles.css" />
  </head>
  <body>
    <div class="container">
      <svg
        width="0"
        height="0"
        style="position: absolute; top: 0; left: 0;"
        xmlns="http://www.w3.org/2000/svg"
      >
        <clipPath id="sakura">
          <path
            d="M92.4998 74.1024C106.031 74.1024 117 56.1961 117 34.1024C117 -9.92887 112.528 -2.62418 105.772 8.41097C101.948 14.661 97.3909 22.1024 92.4998 22.1024C87.6394 22.1024 83.1097 15.2703 79.3001 9.52425C72.5042 -0.729652 67.9998 -7.52262 67.9998 34.1024C67.9998 56.1961 78.969 74.1024 92.4998 74.1024ZM76.0472 85.2C80.2288 72.3328 66.5862 56.3641 45.576 49.5399C3.69804 35.9305 9.26396 42.4422 17.6717 52.2782C22.4314 57.8446 28.1018 64.4774 26.5906 69.1297C25.0887 73.7547 17.1897 75.95 10.5472 77.7977C-1.30245 81.0946 -9.15499 83.2782 30.4339 96.1414C51.4441 102.966 71.866 98.0711 76.0472 85.2ZM157.184 94.1414C136.174 100.97 115.752 96.0711 111.571 83.2C107.389 70.3328 121.032 54.3641 142.042 47.5399C181.632 34.6766 176.562 41.0594 168.912 50.6922C164.624 56.0907 159.526 62.5086 161.028 67.1297C162.539 71.7821 171.027 73.8172 178.15 75.5243C190.732 78.5399 199.061 80.536 157.184 94.1414ZM81.3772 102.403C70.4305 94.45 51.0301 102.489 38.0452 120.364C12.1634 155.985 20.075 152.704 32.0267 147.747C38.7928 144.942 46.8543 141.598 50.8113 144.473C54.7434 147.329 54.3919 155.52 54.096 162.407C53.5686 174.696 53.219 182.841 77.6868 149.165C90.6717 131.294 92.324 110.356 81.3772 102.403ZM114.077 154.048C101.092 136.177 99.4397 115.239 110.387 107.286C121.333 99.3328 140.734 107.376 153.719 125.247C178.186 158.923 170.549 156.075 159.025 151.774C152.565 149.364 144.885 146.501 140.952 149.356C136.995 152.231 137.684 160.931 138.262 168.235C139.283 181.134 139.959 189.673 114.077 154.048ZM96.4998 103.001C103.956 103.001 110 97.1805 110 90.0008C110 82.8211 103.956 77.0008 96.4998 77.0008C89.0437 77.0008 82.9998 82.8211 82.9998 90.0008C82.9998 97.1805 89.0437 103.001 96.4998 103.001Z"
          />
        </clipPath>
      </svg>

      <svg width="300" height="300" viewBox="0 0 300 300">
        <image
          xlink:href="https://dl.dropbox.com/s/pzaxobsyspbfrjw/wa.png?dl=0"
          width="100%"
          height="100%"
          preserveAspectRatio="xMidYMid slice"
          clip-path="url(#sakura)"
        />
      </svg>
    </div>
  </body>
</html>
```

![スクリーンショット 2019-03-16 10.49.10.png](https://qiita-image-store.s3.amazonaws.com/0/232715/012e6670-a2f2-189c-ee44-7872c9a781aa.png)


clip-path="url(#sakura)"

で、適応していることがわかりますね。
これで、完成です。
<br/>
❹.
では、このclip-path へアニメーションをつけましょう。

```index.html
  <clipPath id="sakura">
          <path class = "clip"
            d="・・・
  </clipPath>

  <svg width="1000" height="1000" viewBox="-500 0 1000 1000">
        <image
          xlink:href="https://dl.dropbox.com/s/pzaxobsyspbfrjw/wa.png?dl=0"
          width="200%"
          height="200%"
          preserveAspectRatio="xMidYMid slice"
          clip-path="url(#sakura)"
        />
  </svg>
```

```styles.scss

.clip {
  animation: move 10s linear infinite;
}

@keyframes move {
  0% {
    transform: translate(0px, -400px);
  }
  20% {
    transform: translate(-30px, -200px) scale(1);
  }
  40% {
    transform: translate(0px, 0px) scale(1);
  }
  60% {
    transform: translate(-5px, 200px) scale(1);
  }
  80% {
    transform: translate(15px, 400px) scale(1);
  }

  100% {
    transform: translate(0px, 700px) scale(1);
  }
}
```

<p class="codepen" data-height="561" data-theme-id="0" data-default-tab="html,result" data-user="hiroyaiizuka" data-slug-hash="BbxapQ" style="height: 561px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid black; margin: 1em 0; padding: 1em;" data-pen-title="Sakura Fall">
  <span>See the Pen <a href="https://codepen.io/hiroyaiizuka/pen/BbxapQ/">
  Sakura Fall</a> by hiroya iizuka (<a href="https://codepen.io/hiroyaiizuka">@hiroyaiizuka</a>)
  on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

SVG アニメーションも簡単に設定できましたね。
それでは、また明日〜