• 简体中文
  • ConfettiLayer

    庆祝粒子,不依附任何角色。

    import { useRef } from 'react';
    import { ConfettiLayer, CharacterMark, type ConfettiHandle } from '@aora-bot/react';
    
    export default function Demo() {
      const confetti = useRef<ConfettiHandle>(null);
    
      return (
        <div className="site-demo site-demo--center">
          <div style={{ position: 'relative', width: 140, height: 140 }}>
            <CharacterMark shape="blob" color="green" state="celebrate" sizePx={140} />
            <ConfettiLayer ref={confetti} />
          </div>
          <div className="site-controls">
            <button className="site-btn" onClick={() => confetti.current?.burst(18)}>
              burst(18)
            </button>
            <button className="site-btn" onClick={() => confetti.current?.burst(60)}>
              burst(60)
            </button>
            <button className="site-btn" onClick={() => confetti.current?.clear()}>
              clear()
            </button>
          </div>
        </div>
      );
    }

    共享引擎的 ticker

    这一层使用角色同一套粒子系统,并订阅同一个共享的 requestAnimationFrame,所以给页面加上庆祝不需要额外的动画循环。

    物理

    碎片从身体半径的环上发射,带向上偏差,然后在重力下下落并逐帧阻尼。寿命在 0.45 到 0.85 秒之间。大约六分之一是星星,其余是点或沿速度拉长的条。

    不透明度不是线性的——寿命的前十分之一快速淡入,然后按幂曲线淡出,读起来像碎片远去而不是溶解。

    减少动态效果

    读者偏好减少动态效果时,burst() 是空操作。从中心向外辐射的粒子是前庭刺激,所以被抑制而不是仅仅变慢。

    API

    属性类型默认值说明
    classNamestring——
    radiusnumber114Radius the confetti launches from, in viewBox units rather than pixels — the layer scales with its box, so this is a proportion of the character it sits over and does not change when the box does. Deliberately not `radiusPx`.
    refRef<ConfettiHandle>——
    styleCSSProperties——

    接下来