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

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

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

推荐文章

Go 接口:从入门到精通
2024-11-18 07:10:00 +0800 CST
go错误处理
2024-11-18 18:17:38 +0800 CST
向满屏的 Import 语句说再见!
2024-11-18 12:20:51 +0800 CST
Elasticsearch 的索引操作
2024-11-19 03:41:41 +0800 CST
Vue3中的v-for指令有什么新特性?
2024-11-18 12:34:09 +0800 CST
php获取当前域名
2024-11-18 00:12:48 +0800 CST
Vue3中如何实现插件?
2024-11-18 04:27:04 +0800 CST
Vue3中的Store模式有哪些改进?
2024-11-18 11:47:53 +0800 CST
Vue 3 是如何实现更好的性能的?
2024-11-19 09:06:25 +0800 CST
小技巧vscode去除空格方法
2024-11-17 05:00:30 +0800 CST
Rust 与 sqlx:数据库迁移实战指南
2024-11-19 02:38:49 +0800 CST
JavaScript数组 splice
2024-11-18 20:46:19 +0800 CST
Python Invoke:强大的自动化任务库
2024-11-18 14:05:40 +0800 CST
2024年公司官方网站建设费用解析
2024-11-18 20:21:19 +0800 CST
Go 并发利器 WaitGroup
2024-11-19 02:51:18 +0800 CST
MySQL数据库的36条军规
2024-11-18 16:46:25 +0800 CST
为什么大厂也无法避免写出Bug?
2024-11-19 10:03:23 +0800 CST
一些实用的前端开发工具网站
2024-11-18 14:30:55 +0800 CST
WebSocket在消息推送中的应用代码
2024-11-18 21:46:05 +0800 CST
php客服服务管理系统
2024-11-19 06:48:35 +0800 CST
你可能不知道的 18 个前端技巧
2025-06-12 13:15:26 +0800 CST
五个有趣且实用的Python实例
2024-11-19 07:32:35 +0800 CST
Vue 中如何处理跨组件通信?
2024-11-17 15:59:54 +0800 CST
Go 协程上下文切换的代价
2024-11-19 09:32:28 +0800 CST
#免密码登录服务器
2024-11-19 04:29:52 +0800 CST
程序员茄子在线接单