色涡

布局区块HTML/CSS已复制 0 次

两千六百颗粒子绕着一颗看不见的中心旋转,拖出彩色轨迹。色相从中心向外按可调的跨度铺开——内暖外冷,不会变成一团黄绿。中心有一枚强光圆点,大小和颜色都可调;鼠标附近粒子会局部加速、扰动流场。粒子密度、旋转速度、色相起点、色相跨度、拖尾长度、鼠标扰动、中心强光与白点大小都可调。给的是去掉调参台的独立版:一整页 HTML/CSS,参数集中在脚本开头的 CONFIG。

组件预览改右侧参数,这里和下方代码同步更新

组件演示加载中

演示在 iframe 里独立运行(1280×720 虚拟视口,等比缩放 40%)。滚动类交互要在框内滚动才看得到。

复制代码

代码跟着右侧参数走:你调的每个值都已经写进代码

<div class="scene">
  <div class="nebula"></div>
  <canvas id="cv"></canvas>
  <div class="vignette"></div>
  <div class="grain"></div>
  <div class="content" id="content">
    <div class="eyebrow">Background · No.27</div>
    <h1 id="heading">色涡</h1>
    <p class="lede" id="lede">两千六百颗粒子,绕着一颗看不见的中心。</p>
    <div class="actions">
      <a class="btn btn-solid" href="#start">开始使用</a>
      <a class="btn btn-ghost" href="#more">看实现</a>
    </div>
  </div>
</div>
<div class="credit" id="credit"><a href="https://uibits.cn" target="_blank" rel="noopener"><span class="mk">UI Bits</span><span class="hair"></span><span>uibits.cn</span><span>↗</span></a><span>组件来自 UI Bits 组件库</span></div>

<script>
/* 参数集中在这里,改数字 / 颜色即生效 */
var CONFIG={
  count:70,
  speed:50,
  hue:24,
  band:130,
  trail:80,
  warp:60,
  mouse:true,
  core:55,
  dot:24,
  coreColor:'#FFB46B',
  content:true,
  credit:true,
  heading:'色涡',
  lede:'两千六百颗粒子,绕着一颗看不见的中心。'
};
var S=Object.assign({},CONFIG);
var el=function(id){return document.getElementById(id)};
var reduce=matchMedia('(prefers-reduced-motion:reduce)').matches;
var paused=false;

var cv=el('cv'),ctx=cv.getContext('2d');
var W=0,H=0,cx=0,cy=0,R=0,P=[],PAL=[],mx=-9999,my=-9999;

function palette(){
  PAL=[];
  var n=36;
  for(var i=0;i<n;i++){
    var h=(S.hue-(i/(n-1))*S.band+720)%360;
    var l=70+ i*0.2;
    PAL.push('hsl('+h.toFixed(0)+',94%,'+l.toFixed(0)+'%)');
  }
}

function build(){
  W=cv.width=Math.max(320,innerWidth);
  H=cv.height=Math.max(320,innerHeight);
  cx=W*.5; cy=H*.6; R=Math.min(W,H)*.68;
  var n=Math.round(Math.min(2600,W*H/860)*S.count/70);
  P=[];
  for(var i=0;i<n;i++){
    var r0=Math.pow(Math.random(),.78);
    P.push({
      r:r0*R,
      a:Math.random()*6.2832+r0*4.6,
      w:(30+Math.random()*14)/Math.pow(r0*R+36,.58),
      sz:r0<.35?1.4+Math.random()*1.8:.9+Math.random()*1.4,
      ph:Math.random()*6.2832,
      pa:Math.random()*6.2832+r0*4.6,
      aw:1,star:Math.random()<.045
    });
  }
  palette();
}

function draw(now){
  if(!paused){
    var t=now*.001;
    var spd=S.speed/50;

    /* 拖尾:不清屏,用低透明度底色盖一层,旧位置自然衰减成光轨 */
    var fade=0.5-(S.trail/100)*0.46;
    ctx.globalCompositeOperation='source-over';
    ctx.fillStyle='rgba(5,3,10,'+fade.toFixed(3)+')';
    ctx.fillRect(0,0,W,H);

    ctx.globalCompositeOperation='lighter';
    for(var i=0;i<P.length;i++){
      var p=P[i];
      p.a+=p.w*spd*0.016*p.aw;
      p.aw+=(1-p.aw)*0.045;
      p.r-=0.17*spd;
      if(p.r<16){p.r=R*(.9+Math.random()*.35);p.a=Math.random()*6.2832}

      /* 椭圆的漩涡:横向略扁,读起来更有“星系”的透视 */
      var rr=p.r*(1+.05*Math.sin(t*.5+p.ph));
      var x=cx+Math.cos(p.a)*rr;
      var y=cy+Math.sin(p.a)*rr*.66;

      /* 鼠标附近加速,像被搅了一下 */
      if(S.warp>0&&S.mouse){
        var dx=x-mx, dy=y-my, d2=dx*dx+dy*dy;
        if(d2<30000)p.aw+= (1-d2/30000)*S.warp/100*2.4;
      }

      var idx=Math.min(PAL.length-1,Math.floor(p.r/R*PAL.length));
      ctx.fillStyle=p.star?'rgba(255,252,245,.95)':PAL[idx];
      ctx.globalAlpha=(.62+.38*(1-p.r/R))*(p.r<32?.45:1);
      ctx.fillRect(x,y,p.sz,p.sz);
    }
    ctx.globalAlpha=1;

    /* 中心:一颗被光晕包住的奇点 */
    if(S.core>0){
      /* 强光颜色由 coreColor 决定,core 仍控制光晕强度 */
      var hc=String(S.coreColor||'#FFB46B').replace('#','');
      if(hc.length===3)hc=hc[0]+hc[0]+hc[1]+hc[1]+hc[2]+hc[2];
      var hn=parseInt(hc,16);
      if(isNaN(hn))hn=0xFFB46B;
      var hr=(hn>>16)&255,hg=(hn>>8)&255,hb=hn&255;
      var br=Math.min(255,hr+48),bg=Math.min(255,hg+48),bb=Math.min(255,hb+48);
      var k=S.core/100;
      var halo=ctx.createRadialGradient(cx,cy,0,cx,cy,104);
      halo.addColorStop(0,'rgba('+br+','+bg+','+bb+','+(.42*k).toFixed(2)+')');
      halo.addColorStop(.3,'rgba('+hr+','+hg+','+hb+','+(.16*k).toFixed(2)+')');
      halo.addColorStop(1,'rgba('+hr+','+hg+','+hb+',0)');
      ctx.fillStyle=halo;
      ctx.beginPath();ctx.arc(cx,cy,104,0,6.2832);ctx.fill();

      /* 纯白奇点:半径由 dot 控制,dot=0 时完全消失 */
      if(S.dot>0){
        var d=+S.dot;
        var core=ctx.createRadialGradient(cx,cy,0,cx,cy,d);
        core.addColorStop(0,'rgba(255,255,255,1)');
        core.addColorStop(.38,'rgba('+br+','+bg+','+bb+',.92)');
        core.addColorStop(.72,'rgba('+hr+','+hg+','+hb+',.34)');
        core.addColorStop(1,'rgba('+hr+','+hg+','+hb+',0)');
        ctx.fillStyle=core;
        ctx.beginPath();ctx.arc(cx,cy,d,0,6.2832);ctx.fill();
      }
    }
    ctx.globalCompositeOperation='source-over';
  }
  requestAnimationFrame(draw);
}

function apply(){
  var r=document.documentElement.style;
  r.setProperty('--hue',S.hue);
  el('content').style.display=S.content?'flex':'none';
  el('credit').style.display=S.credit?'flex':'none';
  el('heading').textContent=S.heading;
  el('lede').textContent=S.lede;
  palette();build();
}

window.addEventListener('resize',build);
window.addEventListener('mousemove',function(e){mx=e.clientX;my=e.clientY});
window.addEventListener('mouseleave',function(){mx=my=-9999});

apply();
if(reduce)paused=true;
requestAnimationFrame(draw);
</script>

速览

类别
布局
层级
区块
框架
HTML/CSS
复制
0 次

安装

复制「HTML/CSS」与「样式 CSS」两份代码即可,无第三方依赖;可调项集中在脚本开头的 CONFIG。

能力

  • 支持深色
  • 带动效
  • 响应式

自定义参数

10 项默认

其它参数

尚未归类的可调项
70
50
24
130
80
60
55
24

使用提示

  1. 先在上方调参数看效果 —— 下方「复制代码」 会跟着你调的值一起变,复制走的就是眼前这一版。
  2. 「HTML/CSS」是结构与文案,「样式 CSS」是它用到的规则(含变量与响应式, 只挑了这个组件用到的部分)。两份一起贴。
  3. 代码里的素材是本站绝对地址,换成自己的图只需改 src。

相似组件

按标签重合度推荐,不是运营手配的榜单。

墨蚀 截图

墨蚀

布局 · 区块

网格地平线 截图

网格地平线

布局 · 区块

极光帷幔 截图

极光帷幔

布局 · 区块

流光绸缎 截图

流光绸缎

布局 · 区块