0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【供養】跳ねるロゴ背景を作った

Posted at

概要

姉のお店の予約画面をGoogleFormから移行するサイトを作成していたところ、GoogleForm見たいといわれてしまいショックを受けたため、跳ねるロゴを背景に入れるようにした。
最終的にお気に召さなかったため、ここで供養する。

動き

2025-02-16_20h01_15.gif

gifにしたのでかくかくしているが、滑らかに動くようにしている。

実装

<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Bouncing Logo</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }

    canvas {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 0;
      pointer-events: none;
    }
  </style>
</head>

<body>
  <canvas id="bouncingCanvas"></canvas>

  <script>
    document.addEventListener("DOMContentLoaded", function () {
      const canvas = document.getElementById("bouncingCanvas");
      const ctx = canvas.getContext("2d");

      function handleResize() {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
      }

      handleResize();
      window.addEventListener("resize", handleResize);

      const logoImg = new Image();
      logoImg.src = "sample-logo.svg"; // 適宜修正

      let requestId;
      let imagesLoaded = false;
      logoImg.onload = () => {
        imagesLoaded = true;
        startAnimation();
      };

      const leaderWidth = 200;
      const leaderHeight = 40;
      const leader = {
        x: 0,
        y: 0,
        vx: (Math.random() - 0.5) * 8,
        vy: 5,
        width: leaderWidth,
        height: leaderHeight,
      };

      const gravity = 0.5;
      const frictionX = 0.999;
      const restitution = 0.8;

      function animate() {
        requestId = requestAnimationFrame(animate);

        if (!imagesLoaded) return;

        ctx.clearRect(0, 0, canvas.width, canvas.height);

        leader.vy += gravity;
        leader.x += leader.vx;
        leader.y += leader.vy;
        leader.vx *= frictionX;

        if (leader.x > canvas.width) {
          leader.x = canvas.width;
          leader.vx = -leader.vx * restitution;
        } else if (leader.x < leader.width) {
          leader.x = leader.width;
          leader.vx = -leader.vx * restitution;
        }

        const bottom = leader.y + leader.height;
        if (bottom > canvas.height) {
          leader.y = canvas.height - leader.height;
          leader.vy = -leader.vy * restitution;

          if (Math.abs(leader.vy) < 8) {
            leader.vy = -(10 + Math.random() * 10);
            leader.vx = (Math.random() - 0.5) * 8;
          }
        } else if (leader.y < 0) {
          leader.y = 0;
          leader.vy = -leader.vy * restitution;
        }

        ctx.drawImage(
          logoImg,
          leader.x - leader.width,
          leader.y,
          leader.width,
          leader.height
        );
      }

      function startAnimation() {
        requestId = requestAnimationFrame(animate);
      }
    });
  </script>
</body>

</html>

canvasを利用して書いている。ロゴを一つ描画している程度なので、スマホでも問題なく動作する。1個のロゴの軌跡を追従するようにしてみたときは、少しほっとくと書くつくほど重かったため、1つに変えた。

一定の基準を下回ると大きくはね、ランダムに左右にはねるように作った。

まとめ

でざいんってむずかしい٩( ᐛ )و

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?