罗小黑网站分析

https://blog.hzchu.top/ 2026-10-27 22:40:42 累计浏览 142 次

背景

面试完了没事干,刷到学院推文想着水下学分
要求如下:

要求内容
要求内容

说实话第一眼我还以为他要搞AR,想了下应该是用虚拟文化的元素来装饰网页就够了。
参考里提到了《罗小黑战记2》,之前看过动漫,但是更新太慢了后面就没看了,电影的话可能是因为之前那些事没有大力宣发,看到推文才知道,下载下来看看,确实不错

内容上

电影看完了,想想怎么实现比较好。往年的作品很多都是富媒体的,或者说是用大量的文字、图片、视频来客观的叙述一部作品,内部细分到每个人物等等。

不对,这么玩我剪素材得累死,而且单纯的堆积素材,我觉得没有多大意义。所以我决定以小黑的第一视角来展开叙述,同时限定时间线为电影一二部。

既然是第一视角,那就简单自我介绍加叙述经历,省事

技术上

比赛是叫设计大赛,但我也不是学平面设计的,想专业做UI/UX肯定不行。干脆参考国外的一些设计回国内降维打击了(最明显的反差可能是南孚电池官网了吧)

考虑能不能炫点技,好歹是计院的比赛,有技术含量的好点

点击访问:

设计

事后诸葛亮嘛,直接放我当时答辩ppt吧

简单来说就是全屏滚动+横向滚动+视频播放绑定

经历有删改,不然篇幅太长太累赘,配文就随便写点了,刻意模仿角色的性格来写,很难做到完全贴合,写多错多。而且人家就几岁能说啥长篇大论,有点OOC了

因为中途解决了个问题,改了下方向,导致经历页面的背景不太合适现在这样,不过p半天了懒得改

结尾不知道放什么,就放个符合氛围的MV收尾吧

技术

预览:

  1. 使用Live2d进行渲染人物模型
  2. 使用Matter.js模拟背景中“精灵”的移动
  3. 使用Gapless-5实现背景音乐无缝循环播放
  4. 使用Gsap提供良好的动画反馈及横向滚动效果
  5. 使用Lenis提供平滑滚动

全屏滚动

FullPageScroll.vue
<script setup>
import { onMounted, onBeforeUnmount } from 'vue'
import Lenis from 'lenis'
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'

gsap.registerPlugin(ScrollTrigger)

let lenis = null

onMounted(() => {
  // 初始化 Lenis 平滑滚动
  lenis = new Lenis({
    duration: 1.2,
    easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
    smoothWheel: true,
    smoothTouch: true,
  })

  // 连接 Lenis 和 GSAP ScrollTrigger
  lenis.on('scroll', ScrollTrigger.update)

  gsap.ticker.add((time) => {
    lenis.raf(time * 1000)
  })

  gsap.ticker.lagSmoothing(0)

  // 获取所有 section
  const sections = gsap.utils.toArray('.fp-section')

  // 为每个 section 设置 ScrollTrigger
  sections.forEach((section) => {
    ScrollTrigger.create({
      trigger: section,
      start: 'top top',
      end: 'bottom top',
      snap: {
        snapTo: 1,
        duration: { min: 0.2, max: 0.6 },
        ease: 'power2.inOut'
      },
      // markers: true
    })
  })
})

onBeforeUnmount(() => {
  // 清理 Lenis
  if (lenis) {
    lenis.destroy()
  }

  // 清理所有 ScrollTrigger 实例
  ScrollTrigger.getAll().forEach(st => st.kill())

  gsap.ticker.remove((time) => {
    if (lenis) lenis.raf(time * 1000)
  })
})
</script>

<template>
  <div class="fp-container">
    <slot />
  </div>
</template>

<style scoped>
.fp-container {
  width: 100%;
}

/* 全局样式,确保每个 section 占满视口 */
:deep(.fp-section) {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
</style>

调用时:

HomePage.vue
......
<template>
  <FullPageScroll>
    <section class="fp-section">
      <FirstCard />
    </section>
    <SecondCard :slides="secondCardSlides" section-id="second-card-1" />
    <section class="fp-section">
      <ThirdCard />
    </section>
    <SecondCard :slides="FourthCardSlides" section-id="second-card-2" />
    <FifthCard :slides="FifthCardSlides" />
    <SixthCard :slides="SixthCardSlides" />
  </FullPageScroll>
</template>

对于需要实现全屏吸附的页面,需用<section class="fp-section"></section>包裹

首页

背景

起初背景打算用一个视频解决,后来觉得视频首末帧衔接太突兀就打算实现前端实时渲染

分析

“灵”,或者说“精灵”,表现为球状,能自运动,速度不一,会受阻力影响变为椭球状,不受重力影响,可融合/分裂

原图已失效
原图已失效

投影到平面上时呈发光圆环状,内部半透明,外发光效果

实操

不考虑其他形态,简单实现样式:

ctx.fillStyle = 'rgba(254, 254, 252, 0.6)'
ctx.strokeStyle = 'rgba(255, 255, 255)'
ctx.lineWidth = 1
ctx.shadowColor = '#b8d0bd'
ctx.shadowBlur = 10

使用Matter.js进行2D物理模拟

FirstCard.vue
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import Matter from 'matter-js'

let live2dInitialized = false
let cellAnimation = null
let cells = [] // 存储精灵数据供 Live2D 追踪
let targetCellIndex = null // 当前追踪的精灵索引
let eyeTrackingInterval = null // 眼神追踪定时器
let idleTimer = null // 空闲计时器
let isTracking = false // 是否正在追踪
// 初始化精灵动画
const initCellAnimation = async () => {
  const canvas = document.getElementById('cell-canvas')

  canvas.width = window.innerWidth
  canvas.height = window.innerHeight
  const ctx = canvas.getContext('2d')

  const { Engine, World, Bodies, Body, Vector } = Matter

  // 创建引擎并关闭重力
  const engine = Engine.create()
  engine.gravity.scale = 0
  engine.gravity.x = 0
  engine.gravity.y = 0
  const world = engine.world

  // 默认值
  let numCells = 15
  let speedModifier = 0

  // 请求天气数据
  try {
    const response = await fetch('https://generate-cloud-image.hzchu.top/v1/image?format=json')
    const data = await response.json()
    const weatherCode = data.weather_code || 0
    console.log(`天气代码: ${weatherCode}`)

    // 根据天气代码调整参数
    numCells = Math.max(0, 15 - weatherCode)
    speedModifier = 0.03 * weatherCode
    console.log(`精灵数量: ${numCells}, 速度修正: +${speedModifier.toFixed(3)}`)
  } catch (error) {
    console.warn('天气数据获取失败,使用默认值:', error)
  }

  cells = [] // 使用全局变量
  const radius = 10

  // 初始化精灵
  for (let i = 0; i < numCells; i++) {
    const body = Bodies.circle(
      Math.random() * canvas.width,
      Math.random() * canvas.height,
      radius,
      {
        frictionAir: 0,
        friction: 0,
        restitution: 1,
      }
    )
    const speed = 0.001 + Math.random() * 0.2 + speedModifier
    const angle = Math.random() * Math.PI * 2
    Body.setVelocity(body, {
      x: Math.cos(angle) * speed,
      y: Math.sin(angle) * speed
    })
    World.add(world, body)
    cells.push({ body, speed, angle })
  }

  // 更新运动
  const updateMovement = () => {
    cells.forEach(cellData => {
      const cell = cellData.body

      // 保持恒定速度
      const currentSpeed = Vector.magnitude(cell.velocity)
      if (currentSpeed < cellData.speed * 0.9) {
        const normalizedVel = Vector.normalise(cell.velocity)
        Body.setVelocity(cell, {
          x: normalizedVel.x * cellData.speed,
          y: normalizedVel.y * cellData.speed
        })
      }

      // Wrap-around 边界穿越
      if (cell.position.x < -radius) Body.setPosition(cell, {x: canvas.width + radius, y: cell.position.y})
      if (cell.position.x > canvas.width + radius) Body.setPosition(cell, {x: -radius, y: cell.position.y})
      if (cell.position.y < -radius) Body.setPosition(cell, {x: cell.position.x, y: canvas.height + radius})
      if (cell.position.y > canvas.height + radius) Body.setPosition(cell, {x: cell.position.x, y: -radius})
    })
  }

  // 绘制
  const render = () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height)
    cells.forEach(cellData => {
      const cell = cellData.body
      const vx = cell.velocity.x
      const vy = cell.velocity.y
      const speed = Math.sqrt(vx*vx + vy*vy)
      const stretch = 1 + Math.min(speed / 5, 0.3)
      const angle = Math.atan2(vy, vx)

      ctx.save()
      ctx.translate(cell.position.x, cell.position.y)
      ctx.rotate(angle)

      ctx.beginPath()
      ctx.ellipse(0, 0, radius * stretch, radius / stretch, 0, 0, Math.PI * 2)
      ctx.fillStyle = 'rgba(254, 254, 252, 0.6)'
      ctx.strokeStyle = 'rgba(255, 255, 255)'
      ctx.lineWidth = 1
      ctx.shadowColor = '#b8d0bd'
      ctx.shadowBlur = 10
      ctx.fill()
      ctx.stroke()
      ctx.restore()
    })
  }

  // 主循环
  const update = () => {
    updateMovement()
    Engine.update(engine, 1000 / 60)
    render()
    cellAnimation = requestAnimationFrame(update)
  }

  update()

  // 窗口大小改变时更新 canvas
  const handleResize = () => {
    canvas.width = window.innerWidth
    canvas.height = window.innerHeight
  }
  window.addEventListener('resize', handleResize)

  return () => {
    window.removeEventListener('resize', handleResize)
    if (cellAnimation) {
      cancelAnimationFrame(cellAnimation)
    }
  }
}
</script>

同时补充了个设定:天气恶劣时精灵数量减少,运动速度加快,符合逻辑一点。天气信息获取参考使用小米天气接口获取天气信息

FirstCard.vue
<template>
    <div class="video-background">
      <img
        v-webp
        src="/assets/background.png"
        alt="背景"
        class="background-image"
      />
      <!-- 精灵动画层 -->
      <canvas id="cell-canvas" class="cell-canvas"></canvas>
      <!-- 遮罩层,增强内容可读性 -->
      <div class="video-overlay"></div>
    </div>
</template>

样式

FirstCard.vue
<style scoped>
/* 视频背景容器 */
.video-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  overflow: hidden;
}

/* 背景图片 */
.background-image {
  position: absolute;
  top: 50%;
  left: 50%;
  min-width: 100%;
  min-height: 100%;
  width: auto;
  height: auto;
  transform: translate(-50%, -50%);
  object-fit: cover;
}

/* 精灵动画层 */
.cell-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

/* 视频遮罩层 - 增强文字可读性 */
.video-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.90);
}

/* 深色模式下的遮罩 */
:global(.dark) .video-overlay {
  background: rgba(0, 0, 0, 0.85);
}
</style>

PS:其实写的时候考虑过鸟群算法,后来发现效果不太好就去掉了

Live2D

起初是想加点互动性,在考虑到实际情况后用了Live2D,模型来自盒装现烤奕潞和躺师傅轻食小炒,感谢

这部分还是搞了挺久,首先模型是经过Live2DViewerEX打包后的lpk文件,加密后无法直接使用,解密后模型是Live2D 3.0 版本,网上大多数都是2.0为基础的。找了好久找到LSTM-Kirigaya/Live2dRender,但是不太符合我的实际需求就二次开发了一些功能

具体就不展开讲了,前几天土土推荐了个guansss/pixi-live2d-display,好家伙又白造轮子了
效果可以参考学游渊的博客

LPK解密相关:尽管网上确实能找到相关资料,为了保护版权,这里不展开叙述

有个小彩蛋:长时间无动作时人物会盯着页面里的“精灵”看。计算坐标有点麻烦,我简单实现了下,没过多琢磨

背景音乐

同样考虑到衔接问题,使用了regosen/Gapless-5来实现无缝播放音频,同时使用AU处理音频得到可循环音乐片段

处理音频

具体操作参考:

同分类推荐文章

  1. Embracing Playful Imperfection With The CSS `random()` Function (2026-11-14 03:10:02)
  2. Document Picture-in-Picture API与任意元素画中画实现 (2026-11-13 17:10:02)
  3. 给博客做个站内全文搜索:纯前端离线倒排索引的设计与取舍 (2026-10-03 16:40:31)

查看更多 前端 文章 →

建议继续学习

  1. 经常在各种框架之间切换使用是种什么体验? (累计阅读 3,326)
  2. 门户、创新、创业之道 (累计阅读 2,967)
  3. 如何优化QQ秀-我的收藏 (累计阅读 2,730)
  4. 社区获取新用户的一些尝试 (累计阅读 2,265)
  5. HyperFrames 实战:用 HTML 写一支 41 秒的产品介绍视频 (累计阅读 312)
  6. 文心:文字优先的 Hugo 博客主题 (累计阅读 272)
  7. Astro Markdown Component Utility for Any Framework (累计阅读 226)
  8. 本站 CMS 已开源,欢迎体验 (累计阅读 137)
  9. 在 Vue 或 Svelte 项目上使用 StyleX (累计阅读 138)