js动态几何背景

1、Html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
 
<body>
    <div id="container" style="width: 100%; height: 600px">
        <canvas id="canvas"></canvas>
    </div>
</body>
<script>
    // 创建画布和上下文
    const canvas = document.getElementById("canvas");
    const ctx = canvas.getContext("2d");

    // 设置画布尺寸
    canvas.width = document.getElementById("container").clientWidth; // window.innerWidth;
    canvas.height = document.getElementById("container").clientHeight; // window.innerHeight;

    // 创建渐变背景色
    const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
    // gradient.addColorStop(0, "#6495ED");
    // gradient.addColorStop(1, "#FFD700");

    // 定义几何图形
    const shapes = [
    {
        x: 450,
        y: 200,
        dx: 1,
        dy: -1,
        radius: 50,
        color: "rgba(255, 127, 80, 0.5)",
        type: "circle",
    }, // 圆形
    {
        x: 300,
        y: 300,
        dx: -2,
        dy: 0.5,
        width: 100,
        height: 100,
        color: "rgba(197, 83, 251, 0.5)",
        type: "rectangle",
    }, // 矩形
    {
        x: 500,
        y: 400,
        dx: 0.5,
        dy: 1.5,
        side: 80,
        color: "rgba(123, 201, 154, 0.5)",
        type: "square",
    }, // 正方形
    {
        x: 200,
        y: 200,
        dx: -1,
        dy: 1,
        radius: 30,
        color: "rgba(228, 246, 147, 0.5)",
        type: "circle",
    }, // 圆形
    {
        x: 700,
        y: 200,
        dx: 2,
        dy: 0.5,
        width: 60,
        height: 100,
        color: "rgba(239, 187, 231, 0.5)",
        type: "rectangle",
    }, // 矩形
    {
        x: 600,
        y: 300,
        dx: -1.5,
        dy: 1,
        side: 70,
        color: "rgba(162, 235, 154, 0.5)",
        type: "square",
    }, // 正方形
    {
        x: 800,
        y: 100,
        dx: -1,
        dy: 0.5,
        side: 40,
        color: "rgba(123, 214, 254, 0.5)",
        type: "triangle",
    }, // 三角形
    ];

    function animate() {
    // 清除画布
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 绘制背景色
    ctx.fillStyle = gradient;
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // 绘制图形
    shapes.forEach((shape) => {
        ctx.fillStyle = shape.color;

        if (shape.type === "circle") {
        ctx.beginPath();
        ctx.arc(shape.x, shape.y, shape.radius, 0, Math.PI * 2);
        ctx.fill();
        ctx.closePath();
        } else if (shape.type === "rectangle") {
        ctx.fillRect(shape.x, shape.y, shape.width, shape.height);
        } else if (shape.type === "square") {
        ctx.beginPath();
        ctx.rect(shape.x, shape.y, shape.side, shape.side);
        ctx.fill();
        ctx.closePath();
        } else if (shape.type === "triangle") {
        ctx.beginPath();
        ctx.moveTo(shape.x, shape.y);
        ctx.lineTo(shape.x + 40, shape.y + 40);
        ctx.lineTo(shape.x - 40, shape.y + 40);
        ctx.fill();
        ctx.closePath();
        }

        // 更新图形位置
        shape.x += shape.dx;
        shape.y += shape.dy;

        // 碰撞检测和反弹
        if (shape.type === "circle") {
        if (
            shape.x + shape.radius > canvas.width ||
            shape.x - shape.radius < 0
        ) {
            shape.dx *= -1;
        }
        if (
            shape.y + shape.radius > canvas.height ||
            shape.y - shape.radius < 0
        ) {
            shape.dy *= -1;
        }
        } else if (shape.type === "rectangle") {
        if (shape.x + shape.width > canvas.width || shape.x < 0) {
            shape.dx *= -1;
        }
        if (shape.y + shape.height > canvas.height || shape.y < 0) {
            shape.dy *= -1;
        }
        } else if (shape.type === "square") {
        if (shape.x + shape.side > canvas.width || shape.x < 0) {
            shape.dx *= -1;
        }
        if (shape.y + shape.side > canvas.height || shape.y < 0) {
            shape.dy *= -1;
        }
        } else if (shape.type === "triangle") {
        if (shape.x + shape.side > canvas.width || shape.x < 0) {
            shape.dx *= -1;
        }
        if (shape.y + shape.side > canvas.height || shape.y < 0) {
            shape.dy *= -1;
        }
        }
    });

    // 递归调用动画
    requestAnimationFrame(animate);
    }

    animate();
</script>
</html>

2、Vue 

<template>
  <div id="container">
    <canvas id="canvas"></canvas>
  </div>
</template>
<script>
export default {
  mounted() {
    this.dynamicBackground();
  },
  methods: {
    dynamicBackground() {
      // 创建画布和上下文
      const canvas = document.getElementById("canvas");
      const ctx = canvas.getContext("2d");

      // 设置画布尺寸
      canvas.width = document.getElementById("container").clientWidth; // window.innerWidth;
      canvas.height = document.getElementById("container").clientHeight; // window.innerHeight;

      // 创建渐变背景色
      const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
      // gradient.addColorStop(0, "#6495ED");
      // gradient.addColorStop(1, "#FFD700");

      // 定义几何图形
      const shapes = [
        {
          x: 450,
          y: 200,
          dx: 1,
          dy: -1,
          radius: 50,
          color: "rgba(255, 127, 80, 0.5)",
          type: "circle",
        }, // 圆形
        {
          x: 300,
          y: 300,
          dx: -2,
          dy: 0.5,
          width: 100,
          height: 100,
          color: "rgba(197, 83, 251, 0.5)",
          type: "rectangle",
        }, // 矩形
        {
          x: 500,
          y: 400,
          dx: 0.5,
          dy: 1.5,
          side: 80,
          color: "rgba(123, 201, 154, 0.5)",
          type: "square",
        }, // 正方形
        {
          x: 200,
          y: 200,
          dx: -1,
          dy: 1,
          radius: 30,
          color: "rgba(228, 246, 147, 0.5)",
          type: "circle",
        }, // 圆形
        {
          x: 700,
          y: 200,
          dx: 2,
          dy: 0.5,
          width: 60,
          height: 100,
          color: "rgba(239, 187, 231, 0.5)",
          type: "rectangle",
        }, // 矩形
        {
          x: 600,
          y: 300,
          dx: -1.5,
          dy: 1,
          side: 70,
          color: "rgba(162, 235, 154, 0.5)",
          type: "square",
        }, // 正方形
        {
          x: 800,
          y: 100,
          dx: -1,
          dy: 0.5,
          side: 40,
          color: "rgba(123, 214, 254, 0.5)",
          type: "triangle",
        }, // 三角形
      ];

      function animate() {
        // 清除画布
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        // 绘制背景色
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        // 绘制图形
        shapes.forEach((shape) => {
          ctx.fillStyle = shape.color;

          if (shape.type === "circle") {
            ctx.beginPath();
            ctx.arc(shape.x, shape.y, shape.radius, 0, Math.PI * 2);
            ctx.fill();
            ctx.closePath();
          } else if (shape.type === "rectangle") {
            ctx.fillRect(shape.x, shape.y, shape.width, shape.height);
          } else if (shape.type === "square") {
            ctx.beginPath();
            ctx.rect(shape.x, shape.y, shape.side, shape.side);
            ctx.fill();
            ctx.closePath();
          } else if (shape.type === "triangle") {
            ctx.beginPath();
            ctx.moveTo(shape.x, shape.y);
            ctx.lineTo(shape.x + 40, shape.y + 40);
            ctx.lineTo(shape.x - 40, shape.y + 40);
            ctx.fill();
            ctx.closePath();
          }

          // 更新图形位置
          shape.x += shape.dx;
          shape.y += shape.dy;

          // 碰撞检测和反弹
          if (shape.type === "circle") {
            if (
              shape.x + shape.radius > canvas.width ||
              shape.x - shape.radius < 0
            ) {
              shape.dx *= -1;
            }
            if (
              shape.y + shape.radius > canvas.height ||
              shape.y - shape.radius < 0
            ) {
              shape.dy *= -1;
            }
          } else if (shape.type === "rectangle") {
            if (shape.x + shape.width > canvas.width || shape.x < 0) {
              shape.dx *= -1;
            }
            if (shape.y + shape.height > canvas.height || shape.y < 0) {
              shape.dy *= -1;
            }
          } else if (shape.type === "square") {
            if (shape.x + shape.side > canvas.width || shape.x < 0) {
              shape.dx *= -1;
            }
            if (shape.y + shape.side > canvas.height || shape.y < 0) {
              shape.dy *= -1;
            }
          } else if (shape.type === "triangle") {
            if (shape.x + shape.side > canvas.width || shape.x < 0) {
              shape.dx *= -1;
            }
            if (shape.y + shape.side > canvas.height || shape.y < 0) {
              shape.dy *= -1;
            }
          }
        });

        // 递归调用动画
        requestAnimationFrame(animate);
      }

      animate();
    },
  },
};
</script>
<style>
  #container {
    width: 100%;
    height: 600px;
  }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QmagicianRX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值