尘埃粒子删除

反馈 · 元素 · 已复制 0 次

删一张照片,不淡出也不缩小,而是让它从左往右碎成细粒、原地飘散。

整屏只有一件事:深色底上一张竖版照片卡。环形进度走一遍(Processing→Generating),照片由模糊淡入到清晰后停住等人操作。这时候把鼠标移到照片上,右上角才浮出四个小圆钮,点删除,照片才从左边缘开始逐列化开——分界线是一条竖直的线,右边没轮到的部分始终还是清晰照片。颗粒保留原图每个像素的颜色(雪是白、毛衣是米白),基本停在原地慢慢淡出,只带一点点上飘,所以看着像照片本身碎了,不是盖了一层粒子特效。

消散必须由点击触发,这是重量感来源:进度演完、照片停住、用户主动点删除,三步缺一不可。播完卡片中央浮出「再删一次」可以重来。要无人值守自动演示,把「自动演示」切成是,进度走完会自己消散一遍。

实现用 canvas:把照片采样成几千个像素点,按列计算各自的启动时间,未消散的右侧从离屏画布原样贴回、边界干净;粒子裁在卡片圆角内,不会飞到底色上。时长和粒子疏密都能调,移动端建议把密度调大(粒子更疏)。

调参预览改哪一项,上面预览立刻变

演示页加载中…

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

可调参数

4 项均为默认值
改完看「复制代码 → 使用示例」
01

其它参数

尚未归类的可调项

这份组件怎么用

当前为默认值
  1. 上面调参数,预览立刻变;下方「复制代码」里的 HTML 与样式就是预览里这一版, 两份一起贴进页面即可。
  2. 两份各管一段:HTML/CSS 是结构与文案,样式 CSS 是它用到的规则 (已含 :root 变量与响应式,只挑这个组件用到的部分)。
  3. 素材全部在 uibits.cn 上(代码里是绝对地址),换成自己的图只改 src。

复制代码

「使用示例」就是你上面调出来的这一版;源码与样式各一份,三份配合即可跑起来

<!-- 尘埃粒子删除:整块贴进页面即可,图片换成自己的 -->
<div class="ub-dust">
  <div class="ub-stage" id="stage" data-state="processing">
    <div class="ub-processing" aria-hidden="true">
      <div class="ub-aurora"></div>
      <div class="ub-ring" id="ring"><span id="pct">0%</span></div>
      <div class="ub-proc-label" id="procLabel">Processing…</div>
    </div>

    <img class="ub-photo" id="photo" src="https://uibits.cn/demo/component/assets/dust-particle-deletion/portrait.jpg" alt="雪地里戴墨镜的白色毛衣人像">
    <canvas class="ub-canvas" id="cv"></canvas>

    <div class="ub-tools" aria-label="图片操作">
      <button class="ub-tool" type="button" id="likeBtn" title="喜欢" aria-label="喜欢">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20s-7-4.5-7-9.5A3.9 3.9 0 0 1 12 8a3.9 3.9 0 0 1 7 2.5C19 15.5 12 20 12 20z"/></svg>
      </button>
      <button class="ub-tool" type="button" id="resetBtn" title="复位" aria-label="复位">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M4 11a8 8 0 1 1 2.3 6"/><path d="M4 5v6h6"/></svg>
      </button>
      <button class="ub-tool" type="button" id="downBtn" title="下载" aria-label="下载">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3v12m0 0 4-4m-4 4-4-4"/><path d="M4 17v3h16v-3"/></svg>
      </button>
      <button class="ub-tool ub-tool--del" type="button" id="delBtn" title="删除" aria-label="删除照片">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13"/><path d="M10 11v5M14 11v5"/></svg>
      </button>
    </div>

    <button class="ub-replay" type="button" id="replayBtn">再删一次 ↺</button>
  </div>
</div>
<script>
(function () {
  'use strict';
  var stage = document.getElementById('stage');
  var photo = document.getElementById('photo');
  var cv = document.getElementById('cv');
  var ctx = cv.getContext('2d', { willReadFrequently: true });
  var delBtn = document.getElementById('delBtn');
  var replayBtn = document.getElementById('replayBtn');
  var likeBtn = document.getElementById('likeBtn');
  var resetBtn = document.getElementById('resetBtn');
  var downBtn = document.getElementById('downBtn');
  var ring = document.getElementById('ring');
  var pctEl = document.getElementById('pct');
  var label = document.getElementById('procLabel');

  // autoplay 默认是关的:进度走完照片停住,等用户点删除钮才消散。
  // 列表卡/调参里想自动演示就把 autoplay 打开,两种都要认(种子给的是字符串)。
  var P = { duration: 1.4, density: 3, autoplay: false, heading: '再删一次' };
  var raf = 0, procTimer = 0, autoTimer = 0, particles = [], startedAt = 0;
  var offscreen = null, photoBox = { x: 0, y: 0, w: 0, h: 0 };

  function state(v) { stage.dataset.state = v; }
  function roundRect(c, x, y, w, h, r) {
    c.beginPath();
    c.moveTo(x + r, y); c.lineTo(x + w - r, y); c.quadraticCurveTo(x + w, y, x + w, y + r);
    c.lineTo(x + w, y + h - r); c.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
    c.lineTo(x + r, y + h); c.quadraticCurveTo(x, y + h, x, y + h - r);
    c.lineTo(x, y + r); c.quadraticCurveTo(x, y, x + r, y); c.closePath();
  }

  /* 原片时序(10.1s,逐帧看过):
     0–4s  进度环 0→99%,Processing 走前 2/3、后段切 Generating,同时极光扫过
     4–6s  照片由模糊淡入到清晰,进度罩退场
     6–7s  照片完整停住,鼠标移上去右上角浮出四个圆钮,停在删除钮上
     7–10s 点删除后照片从左往右逐列化开,颗粒保留原色、原地淡出带轻微上飘
     关键:**默认消散必须由点击触发**(autoplay=false);进度只走一遍,不循环。 */
  function boot() {
    clearInterval(procTimer); cancelAnimationFrame(raf); clearTimeout(autoTimer);
    photo.style.visibility = '';
    photo.style.opacity = '';
    state('processing');
    var n = 0;
    ring.style.setProperty('--p', 0);
    pctEl.textContent = '0%';
    label.textContent = 'Processing…';
    procTimer = setInterval(function () {
      // 约 3.4 秒走满(38 拍 × 90ms),和原片 0→99% 的节奏一致
      n += 2 + Math.random() * 1.4;
      if (n > 99) n = 99;
      ring.style.setProperty('--p', n);
      pctEl.textContent = Math.round(n) + '%';
      if (n >= 67) label.textContent = 'Generating…';
      if (n >= 99) {
        clearInterval(procTimer);
        ring.style.setProperty('--p', 100);
        pctEl.textContent = '100%';
        // 罩子淡出的同一拍照片开始显影,两者衔接不留空帧
        setTimeout(function () {
          state('ready');
          // 只有打开「自动演示」才自己接着走一遍消散;默认是关的,停住等用户点删除。
          // 这样列表卡里是静态首屏,点进去才有交互,不会像一段循环视频。
          if (P.autoplay === true || P.autoplay === 'true') {
            autoTimer = setTimeout(dissolve, 650);
          }
        }, 120);
      }
    }, 90);
  }

  function build() {
    var w = stage.clientWidth, h = stage.clientHeight;
    var dpr = Math.min(window.devicePixelRatio || 1, 2);
    cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    ctx.clearRect(0, 0, w, h);

    var natW = photo.naturalWidth || w, natH = photo.naturalHeight || h;
    var scale = Math.max(w / natW, h / natH);
    var drawW = natW * scale, drawH = natH * scale;
    photoBox = { x: (w - drawW) / 2, y: (h - drawH) / 2, w: drawW, h: drawH };
    var off = document.createElement('canvas');
    off.width = w; off.height = h;
    var octx = off.getContext('2d', { willReadFrequently: true });
    octx.drawImage(photo, photoBox.x, photoBox.y, drawW, drawH);
    offscreen = off;
    var data = octx.getImageData(0, 0, w, h).data;

    var step = Math.max(2, Math.min(8, Number(P.density) || 3));
    var list = [];
    for (var y = 0; y < h; y += step) {
      for (var x = 0; x < w; x += step) {
        var i = (y * w + x) * 4;
        if (data[i + 3] < 30) continue;
        // 原片是从左往右逐列化开(8.1s 左边缘先碎、分界线竖直),
        // 颗粒保留原图像素色、基本原地淡出,只带一点点上飘,不是四散飞走。
        var col = x / w;
        list.push({
          x: x, y: y,
          r: data[i], g: data[i + 1], b: data[i + 2],
          delay: col * 0.62,
          vx: (Math.random() - .5) * 5,
          vy: -(4 + Math.random() * 12),
          size: step * (.9 + Math.random() * .4),
          wob: Math.random() * Math.PI * 2
        });
      }
    }
    particles = list;
  }

  function render(now) {
    var t = (now - startedAt) / 1000 / Math.max(.4, Number(P.duration) || 2.2);
    ctx.clearRect(0, 0, cv.width, cv.height);

    /* 底层:把「还没轮到」的右侧照片原样贴回 —— 左边化开、右边仍是清晰照片,
       分界线是一条竖直的直线(和原片一致)。只按 x 推进,y 保持全高。 */
    var sweep = Math.min(1, t / .62);
    var keepX = photoBox.x + photoBox.w * sweep;
    var keepW = photoBox.w * (1 - sweep);
    if (keepW > 0 && offscreen) {
      ctx.save();
      roundRect(ctx, 0, 0, stage.clientWidth, stage.clientHeight, 12);
      ctx.clip();
      ctx.drawImage(offscreen, keepX, photoBox.y, keepW, photoBox.h,
                    keepX, photoBox.y, keepW, photoBox.h);
      ctx.restore();
    }
    // 粒子也裁在圆角里:原片是「卡片在消散」,不是粒子飞到卡片外的黑底上
    ctx.save();
    roundRect(ctx, 0, 0, stage.clientWidth, stage.clientHeight, 12);
    ctx.clip();
    for (var i = 0; i < particles.length; i++) {
      var p = particles[i];
      var local = (t - p.delay) / .55;
      if (local <= 0 || local >= 1) continue;
      var e = local * local;
      // 原片颗粒基本停在原地、缓慢淡出,只轻微上飘;位移一大就成了「飞散爆炸」。
      var px = p.x + p.vx * e + Math.sin(p.wob + local * 4) * 2 * local;
      var py = p.y + p.vy * e;
      ctx.globalAlpha = 1 - local;
      ctx.fillStyle = 'rgb(' + p.r + ',' + p.g + ',' + p.b + ')';
      ctx.fillRect(px, py, p.size, p.size);
    }
    ctx.restore();
    ctx.globalAlpha = 1;
    if (t < 1.2) raf = requestAnimationFrame(render);
    else { cancelAnimationFrame(raf); ctx.clearRect(0, 0, cv.width, cv.height); state('done'); replayBtn.textContent = P.heading + ' ↺'; }
  }

  /* 只有点删除按钮才走到这里 */
  function dissolve() {
    if (stage.dataset.state !== 'ready') return;
    clearTimeout(autoTimer);
    if (!photo.complete || !photo.naturalWidth) { photo.onload = dissolve; return; }
    state('dissolving');
    build();
    startedAt = performance.now();
    raf = requestAnimationFrame(render);
  }

  delBtn.addEventListener('click', dissolve);
  replayBtn.addEventListener('click', boot);
  resetBtn.addEventListener('click', boot);
  likeBtn.addEventListener('click', function () { likeBtn.classList.toggle('ub-tool--on'); });
  downBtn.addEventListener('click', function () {
    var a = document.createElement('a');
    a.href = photo.src; a.download = 'photo.jpg'; a.click();
  });

  function apply(props) {
    if (!props) return;
    if (typeof props.duration === 'number') P.duration = Math.max(.4, Math.min(8, props.duration));
    if (typeof props.density === 'number') P.density = Math.max(2, Math.min(8, Math.round(props.density)));
    // 种子面板给的是字符串 'true'/'false',URL 参数可能是布尔,两种都要认。
    if (typeof props.autoplay === 'boolean') P.autoplay = props.autoplay;
    else if (props.autoplay === 'true') P.autoplay = true;
    else if (props.autoplay === 'false') P.autoplay = false;
    if (typeof props.heading === 'string' && props.heading) {
      P.heading = props.heading;
      replayBtn.textContent = props.heading + ' ↺';
    }
  }
  window.addEventListener('message', function (e) {
    var d = e.data;
    if (!d || d.type !== 'ub:props' || d.key !== 'dustParticleDeletion') return;
    apply(d.props);
    // 参数面板改动要当场可见:从头走一遍,但绝不自动消散。
    boot();
  });
  var m = location.search.match(/[?&]p=([^&]+)/);
  if (m) { try { apply(JSON.parse(decodeURIComponent(m[1]))); } catch (e) {} }

  if (photo.complete) boot(); else photo.onload = boot;
})();
</script>

可调参数

参数类型默认值
消散时长(秒)duration数值 0.8–6 / 步长 0.21.4
粒子疏密(数越大越疏)density数值 2–8 / 步长 13
重播按钮文字headingtext再删一次
自动演示(关=等点击删除)autoplay枚举:false / truefalse

上方的调参预览可以直接拖动;改完往下看「复制代码」,HTML 与样式两份贴进页面就是预览里这一版。

相似组件

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

光标光斑卡片

布局 · 元素

作品轮播 截图

作品轮播

媒体 · 区块

通知堆叠 截图

通知堆叠

反馈 · 组件

Bento 网格 截图

Bento 网格

布局 · 区块