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

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

该文本是一个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>
复制全文 生成海报 网页设计 前端开发 动画效果

推荐文章

使用 `nohup` 命令的概述及案例
2024-11-18 08:18:36 +0800 CST
关于 `nohup` 和 `&` 的使用说明
2024-11-19 08:49:44 +0800 CST
H5端向App端通信(Uniapp 必会)
2025-02-20 10:32:26 +0800 CST
Grid布局的简洁性和高效性
2024-11-18 03:48:02 +0800 CST
CSS 奇技淫巧
2024-11-19 08:34:21 +0800 CST
设置mysql支持emoji表情
2024-11-17 04:59:45 +0800 CST
CSS 特效与资源推荐
2024-11-19 00:43:31 +0800 CST
CSS 媒体查询
2024-11-18 13:42:46 +0800 CST
Vue3 中提供了哪些新的指令
2024-11-19 01:48:20 +0800 CST
JavaScript数组 splice
2024-11-18 20:46:19 +0800 CST
html夫妻约定
2024-11-19 01:24:21 +0800 CST
纯CSS绘制iPhoneX的外观
2024-11-19 06:39:43 +0800 CST
Nginx 性能优化有这篇就够了!
2024-11-19 01:57:41 +0800 CST
HTML + CSS 实现微信钱包界面
2024-11-18 14:59:25 +0800 CST
Golang实现的交互Shell
2024-11-19 04:05:20 +0800 CST
liunx服务器监控workerman进程守护
2024-11-18 13:28:44 +0800 CST
Vue3中的JSX有什么不同?
2024-11-18 16:18:49 +0800 CST
Vue3中如何进行错误处理?
2024-11-18 05:17:47 +0800 CST
windows下mysql使用source导入数据
2024-11-17 05:03:50 +0800 CST
乐观锁和悲观锁,如何区分?
2024-11-19 09:36:53 +0800 CST
全栈工程师的技术栈
2024-11-19 10:13:20 +0800 CST
程序员茄子在线接单