全部动画

22

细胞分裂

等值面融合:1 → 2 → 4 → 2 → 1,首尾相接无缝循环。

  • 仿生
  • 周期 4.6s
  • Canvas,内含一小段脚本
  • 无依赖

自定义

配色
预览背景

代码

<!--
  "Cell Division" loader by onemilliontools.com
  https://loading.onemilliontools.com/loaders/cell-division
  License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
  Keep this notice, or credit "onemilliontools.com" with a link, wherever you use it.
-->
<div id="omt-loader"></div>
<script>
const OMT_SPEC = {
  w: 96, h: 96, label: "Cell Division",
  create(g) {
    const { x, hx, TH, TAU } = g;
    // metaballs: four blobs whose field values add up; where the sum passes a threshold the pixel is "cell".
    // They are painted into a 192 px buffer once per frame, then scaled onto the visible canvas.
    const W = 192, buf = document.createElement("canvas");
    buf.width = buf.height = W;
    const bg = buf.getContext("2d"), img = bg.createImageData(W, W), d = img.data;
    const ease = k => .5 - .5 * Math.cos(Math.PI * k), r2 = 400;
    return function (t) {
      const u = (t % 4600) / 4600; // 1 -> 2 -> 4 -> 2 -> 1, ends exactly where it starts
      let a, b;
      if (u < .25) { a = ease(u / .25); b = 0; } else if (u < .5) { a = 1; b = ease((u - .25) / .25); } else if (u < .75) { a = 1 - ease((u - .5) / .25); b = 1; } else { a = 0; b = 1 - ease((u - .75) / .25); }
      const ox = a * 30, oy = b * 30, B = [[96 - ox, 96 - oy], [96 + ox, 96 - oy], [96 - ox, 96 + oy], [96 + ox, 96 + oy]], [R, G, Bl] = hx(TH.c1);
      d.fill(0);
      for (let y = 24; y < 168; y++) for (let X = 24; X < 168; X++) {
        let f = 0;
        for (let i = 0; i < 4; i++) { const dx = X - B[i][0], dy = y - B[i][1]; f += r2 / (dx * dx + dy * dy + 1); }
        const al = Math.max(0, Math.min(1, (f - 1) / .1));
        if (al > 0) { const p = (y * W + X) * 4; d[p] = R; d[p + 1] = G; d[p + 2] = Bl; d[p + 3] = al * 235; }
      }
      bg.clearRect(0, 0, W, W); bg.putImageData(img, 0, 0);
      bg.fillStyle = TH.c2; bg.globalAlpha = .95;
      const seen = new Set();
      B.forEach(q => { const k = q[0].toFixed(1) + "," + q[1].toFixed(1); if (seen.has(k)) return; seen.add(k); bg.beginPath(); bg.arc(q[0], q[1], 5, 0, TAU); bg.fill(); });
      bg.globalAlpha = 1;
      x.clearRect(0, 0, 96, 96); x.globalAlpha = 1;
      x.drawImage(buf, 0, 0, 96, 96);
    };
  }
};

/* ---- omt runtime (shared by every canvas loader, no need to edit) ---- */
function omtLoader(root, spec, opts) {
  var o = { c1: "#2979FF", c2: "#FF9100", speed: 1, size: 96 };
  if (opts) for (var k in opts) if (opts[k] != null) o[k] = opts[k];
  var TAU = Math.PI * 2;
  var TH = { c1: o.c1, c2: o.c2, v: 0 };
  var cache = {};
  function hx(c) { return [1, 3, 5].map(function (i) { return parseInt(c.slice(i, i + 2), 16); }); }
  function rgb(a, b, k) { return "rgb(" + a.map(function (v, j) { return Math.round(v + (b[j] - v) * k); }).join() + ")"; }
  function mix(c1, c2, k) { return rgb(hx(c1), hx(c2), k); }
  function pal(c1, c2, n) { var a = hx(c1), b = hx(c2), r = []; for (var i = 0; i < n; i++) r.push(rgb(a, b, n > 1 ? i / (n - 1) : 0)); return r; }
  function P(n) { var key = n + ":" + TH.v; return cache[key] || (cache[key] = pal(TH.c1, TH.c2, n)); }
  function fr(v) { return Math.abs(Math.sin(v * 12.9898) * 43758.5453) % 1; }
  function dot(x, px, py, r, col, al) { x.globalAlpha = al; x.fillStyle = col; x.beginPath(); x.arc(px, py, r, 0, TAU); x.fill(); }

  var cv = document.createElement("canvas");
  cv.setAttribute("role", "img");
  cv.setAttribute("aria-label", spec.label || "Loading");
  cv.style.display = "block";
  var x = cv.getContext("2d");
  var dpr = Math.min(3, Math.max(1, window.devicePixelRatio || 1));
  function applySize() {
    var k = o.size / 96, s = dpr * k;
    cv.width = Math.round(spec.w * s);
    cv.height = Math.round(spec.h * s);
    cv.style.width = spec.w * k + "px";
    cv.style.height = spec.h * k + "px";
    x.setTransform(s, 0, 0, s, 0, 0);
  }

  var draw = spec.create({ x: x, W: spec.w, H: spec.h, TAU: TAU, TH: TH, P: P, pal: pal, mix: mix, hx: hx, fr: fr, dot: dot });
  var VT = 0, last = 0, raf = 0, running = false, visible = true;
  var mq = window.matchMedia ? window.matchMedia("(prefers-reduced-motion: reduce)") : { matches: false };

  function frame(t) {
    raf = 0;
    if (!running) return;
    var d = t - last, dt = 0;
    last = t;
    if (d > 0 && d < 200) { dt = d * o.speed; VT += dt; }
    draw(VT, dt);
    raf = requestAnimationFrame(frame);
  }
  function start() { if (running) return; running = true; last = 0; raf = requestAnimationFrame(frame); }
  function stop() { running = false; if (raf) cancelAnimationFrame(raf); raf = 0; }
  function still() { for (var i = 0; i < 120; i++) { VT += 16.7; draw(VT, 16.7); } }
  function sync() {
    if (mq.matches) { stop(); still(); }
    else if (visible && !document.hidden) start();
    else stop();
  }
  var io = window.IntersectionObserver ? new IntersectionObserver(function (e) { visible = e[0].isIntersecting; sync(); }) : null;
  if (io) io.observe(cv);
  document.addEventListener("visibilitychange", sync);
  if (mq.addEventListener) mq.addEventListener("change", sync);

  root.appendChild(cv);
  applySize();
  sync();

  return {
    set: function (n) {
      if (n.c1 || n.c2) { TH.c1 = n.c1 || TH.c1; TH.c2 = n.c2 || TH.c2; TH.v++; }
      if (n.speed != null) o.speed = n.speed;
      if (n.size != null && n.size !== o.size) { o.size = n.size; applySize(); }
      if (mq.matches) draw(VT, 0);
    },
    destroy: function () {
      stop();
      if (io) io.disconnect();
      document.removeEventListener("visibilitychange", sync);
      if (mq.removeEventListener) mq.removeEventListener("change", sync);
      if (cv.parentNode) cv.parentNode.removeChild(cv);
    }
  };
}

omtLoader(document.getElementById("omt-loader"), OMT_SPEC, { c1: "#2979FF", c2: "#FF9100", speed: 1, size: 160 });
</script>
121 行

背后的数学

四个等值面场 r²/d²,场强之和超过 1 的像素属于细胞,因此细胞会拉长、缢缩、融合。余弦缓动,一周期 4.6 秒。

署名

按 CC BY 4.0 免费用于个人与商业项目。请保留代码开头的注释,或用一个可见链接署名作者。

Loader animation by onemilliontools.com: <a href="https://loading.onemilliontools.com/loaders/cell-division">onemilliontools.com</a>

授权