代码 一个基于canvas的自由贪吃蛇动画效果

2024-11-18 11:45:29 +0800 CST views 682

该文本是一个HTML文档,展示了一个基于canvas的自由贪吃蛇动画效果。通过JavaScript处理鼠标事件,实现动态的光标轨迹。使用GSAP库来增强动画效果,代码中包含了对canvas的设置、鼠标位置的更新以及动画的循环更新。整体设计旨在提供一个互动且视觉上令人满意的用户体验。
images

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

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>自由贪吃蛇</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js"></script>
  <style>
    body,html{padding:0;margin:0;overscroll-behavior:none;overflow:hidden}.links{position:fixed;bottom:10px;right:10px;font-size:18px;font-family:sans-serif;background-color:white;padding:10px}a{text-decoration:none;color:black;margin-left:1em}a:hover{text-decoration:underline}a img.icon{display:inline-block;height:1em;margin:0 0 -0.1em 0.3em}
  </style>
</head>

<body>
  <canvas></canvas>
  <div class="links">
    <a href="https://dev.to/uuuuuulala/coding-an-interactive-and-damn-satisfying-cursor-7-simple-steps-2kb-of-code-1c8b"
      target="_blank">
  </div>
</body>
<script>
  const canvas = document.querySelector("canvas");
  const ctx = canvas.getContext('2d');
  let mouseMoved = false;

  const pointer = {
    x: .5 * window.innerWidth,
    y: .5 * window.innerHeight,
  }
  const params = {
    pointsNumber: 40,
    widthFactor: .3,
    mouseThreshold: .6,
    spring: .4,
    friction: .5
  };

  const trail = new Array(params.pointsNumber);
  for (let i = 0; i < params.pointsNumber; i++) {
    trail[i] = {
      x: pointer.x,
      y: pointer.y,
      dx: 0,
      dy: 0,
    }
  }

  window.addEventListener("click", e => {
    updateMousePosition(e.pageX, e.pageY);
  });
  window.addEventListener("mousemove", e => {
    mouseMoved = true;
    updateMousePosition(e.pageX, e.pageY);
  });
  window.addEventListener("touchmove", e => {
    mouseMoved = true;
    updateMousePosition(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
  });

  function updateMousePosition(eX, eY) {
    pointer.x = eX;
    pointer.y = eY;
  }

  setupCanvas();
  update(0);
  window.addEventListener("resize", setupCanvas);


  function update(t) {

    if (!mouseMoved) {
      pointer.x = (.5 + .3 * Math.cos(.002 * t) * (Math.sin(.005 * t))) * window.innerWidth;
      pointer.y = (.5 + .2 * (Math.cos(.005 * t)) + .1 * Math.cos(.01 * t)) * window.innerHeight;
    }

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    trail.forEach((p, pIdx) => {
      const prev = pIdx === 0 ? pointer : trail[pIdx - 1];
      const spring = pIdx === 0 ? .4 * params.spring : params.spring;
      p.dx += (prev.x - p.x) * spring;
      p.dy += (prev.y - p.y) * spring;
      p.dx *= params.friction;
      p.dy *= params.friction;
      p.x += p.dx;
      p.y += p.dy;
    });

    ctx.lineCap = "round";
    ctx.beginPath();
    ctx.moveTo(trail[0].x, trail[0].y);

    for (let i = 1; i < trail.length - 1; i++) {
      const xc = .5 * (trail[i].x + trail[i + 1].x);
      const yc = .5 * (trail[i].y + trail[i + 1].y);
      ctx.quadraticCurveTo(trail[i].x, trail[i].y, xc, yc);
      ctx.lineWidth = params.widthFactor * (params.pointsNumber - i);
      ctx.stroke();
    }
    ctx.lineTo(trail[trail.length - 1].x, trail[trail.length - 1].y);
    ctx.stroke();

    window.requestAnimationFrame(update);
  }

  function setupCanvas() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
  }
</script>
</html>
复制全文 生成海报 网页设计 前端开发 动画效果

推荐文章

#免密码登录服务器
2024-11-19 04:29:52 +0800 CST
黑客帝国代码雨效果
2024-11-19 01:49:31 +0800 CST
JavaScript 实现访问本地文件夹
2024-11-18 23:12:47 +0800 CST
Mysql允许外网访问详细流程
2024-11-17 05:03:26 +0800 CST
禁止调试前端页面代码
2024-11-19 02:17:33 +0800 CST
JavaScript设计模式:桥接模式
2024-11-18 19:03:40 +0800 CST
JavaScript设计模式:发布订阅模式
2024-11-18 01:52:39 +0800 CST
维护网站维护费一年多少钱?
2024-11-19 08:05:52 +0800 CST
Python中何时应该使用异常处理
2024-11-19 01:16:28 +0800 CST
五个有趣且实用的Python实例
2024-11-19 07:32:35 +0800 CST
JavaScript 的模板字符串
2024-11-18 22:44:09 +0800 CST
PHP解决XSS攻击
2024-11-19 02:17:37 +0800 CST
在 Nginx 中保存并记录 POST 数据
2024-11-19 06:54:06 +0800 CST
使用Vue 3和Axios进行API数据交互
2024-11-18 22:31:21 +0800 CST
vue打包后如何进行调试错误
2024-11-17 18:20:37 +0800 CST
JavaScript中设置器和获取器
2024-11-17 19:54:27 +0800 CST
Shell 里给变量赋值为多行文本
2024-11-18 20:25:45 +0800 CST
内网穿透技术详解与工具对比
2025-04-01 22:12:02 +0800 CST
Elasticsearch 监控和警报
2024-11-19 10:02:29 +0800 CST
HTML和CSS创建的弹性菜单
2024-11-19 10:09:04 +0800 CST
html一个全屏背景视频
2024-11-18 00:48:20 +0800 CST
JavaScript 异步编程入门
2024-11-19 07:07:43 +0800 CST
程序员茄子在线接单