Starfield <mv-starfield>

canvas 星空:带色调的星星在缓慢旋转中向你飞来,远处的星星是清晰的像素,近处的则绽开为闪烁的星芒,不时还有一颗星星伴着衍射尖峰闪耀。

工作原理

canvas 星空:带色调的星星在缓慢旋转中向你飞来,远处的星星是清晰的像素,近处的则绽开为闪烁的星芒,不时还有一颗星星伴着衍射尖峰闪耀。启用 warp 后,悬停即跃入光速:光痕头部偏蓝、尾部偏红,并由指针引导方向。

分类背景
类型Web Component(<mv-starfield>)
状态稳定版
Keywordssafe-rewrite, stars, space, flare, hyperspeed, warp, hero, canvas

When to use

  • A space, sci-fi or launch page needs stars with real depth that fly toward the viewer
  • Hovering the hero should trigger a hyperspace jump that the visitor can steer with the pointer
  • A night-sky section wants a calm, living backdrop with an occasional bright flare, without WebGL

Avoid when

  • Only a small card needs diagonal shooting stars → use Meteors instead
  • A still starry texture is enough and nothing should move → use Backdrop instead

安装

node scripts/add.mjs starfield --out ./src/marvelous

使用 Marvelous UI MCP 服务器的 AI 智能体:install_components({ slugs: ["starfield"], target_dir: "<absolute path>/src/marvelous", framework: "react" })。

复制的文件(含依赖):tokens/tokens.css, core/base.css, core/canvas.js, core/dom.js, core/element.js, core/i18n.js, core/motion.js, components/starfield/starfield.js, components/starfield/starfield.css。

用法

标准标记,可在此基础上通过属性、data-* 和 CSS 变量进行定制:

<div style="display:flex;flex-wrap:wrap;min-width:0;min-height:320px">
  <section style="flex:1.6 1 20rem;min-height:320px;position:relative;display:grid;place-items:center;text-align:center;overflow:hidden;padding:2rem">
    <mv-starfield data-fill warp></mv-starfield>
    <div style="position:relative">
      <p style="margin:0;font:700 clamp(1.6rem,4vw,2.6rem)/1.1 var(--mv-font-sans);letter-spacing:-.03em">Your next launch starts here</p>
      <p style="margin:.6rem auto 1.2rem;max-width:30rem;color:var(--mv-fg-muted)">Hover the sky to jump to light speed. Move the pointer to steer.</p>
      <a class="mv-button" href="#starfield">Book a demo</a>
    </div>
  </section>
  <section style="flex:1 1 14rem;min-height:240px;position:relative;display:grid;place-items:center;text-align:center;overflow:hidden;box-shadow:-1px 0 0 var(--mv-border);padding:1.5rem;color:oklch(0.97 0 0)">
    <mv-starfield data-fill data-sky flares="1.4" density="1.4"></mv-starfield>
    <div style="position:relative">
      <strong style="display:block;font-size:1.1rem">Deep sky</strong>
      <span style="font-size:.85rem;color:oklch(0.97 0 0 / .78)">data-sky, a flare every 1.4 s</span>
    </div>
  </section>
</div>

API

Attributes

Name类型DefaultDescription
countnumberNumber of stars (otherwise proportional to the area, max 1000).
densitynumber1Multiplies the automatic count.
flaresnumber4Average interval between two star flares (s); 0 to disable. The first starfield's shooting attribute is read as an alias.
warpbooleanHovering the area (the parent with data-fill) jumps to light speed; the vanishing point leans toward the pointer.
speednumber1Flight and roll speed (0 freezes the sky).
colorCSS colorvar(--mv-starfield-color)Neutral star color (tokens accepted, follows the theme).
flare-colorCSS colorvar(--mv-starfield-flare)Flare color; shooting-color is read as an alias.
data-skybooleanAdds a deep dusk background with a faint haze band (identical in both themes).
data-fillbooleanFills the positioned parent (absolute, inset 0) and ignores the pointer itself.

Content structure

NameDescription
childrenWithout data-fill, content placed inside the element sits above the stars.

CSS variables

NameDefaultDescription
--mv-starfield-colorfg 62% (light) / near white (dark)Neutral stars, about half of the sky.
--mv-starfield-coolaccent hueCool stars, and the head of the warp streaks.
--mv-starfield-warmamberWarm stars, and the tail of the warp streaks.
--mv-starfield-flareaccent hue, brightFlare halo, spikes and ring.

Accessibility

Decorative: the canvas is aria-hidden and never takes pointer events, and content placed inside stays readable by assistive tech; with data-fill the whole element ignores the pointer. The warp is a hover flourish only and carries no information. The loop stops off screen and in hidden tabs. Reduced motion (OS or [data-motion="reduce"]): one still sky with a flare frozen near its peak, no warp. Forced colors and print hide the canvas and the data-sky background.

本页面由 AI 翻译。报告翻译问题