All loaders

01

Comet Orbit

A comet swings round its sun on a stretched ellipse, trailing two tails.

  • Cosmos
  • Cycle 2.6s
  • Canvas, uses a small inline script
  • No dependencies

Customize

Palette
Preview background

Code

<!--
  "Comet Orbit" loader by onemilliontools.com
  https://loading.onemilliontools.com/loaders/comet-orbit
  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: 128, h: 96, label: "Comet orbit",
  create(g) {
    const { x, dot, P, TH, TAU, W, H } = g;
    const A = 26, E = .8, B = A * Math.sqrt(1 - E * E), C = A * E, CX = 64, CY = 48, T = 2600, Pp = [];
    let acc = 0;
    // Kepler's equation  M = E - e sin E  solved by fixed-point iteration
    const pos = tt => {
      const M = TAU * (((tt % T) + T) % T) / T;
      let En = M;
      for (let i = 0; i < 8; i++) En = M + E * Math.sin(En);
      return [CX + A * Math.cos(En), CY + B * Math.sin(En)];
    };
    return function (t, dt) {
      const dn = Math.min(3, dt / 16.67), c = pos(t), c0 = pos(t - 16.67);
      const vx = c[0] - c0[0], vy = c[1] - c0[1], sx = CX + C, sy = CY;
      const dx = c[0] - sx, dy = c[1] - sy, dist = Math.hypot(dx, dy), ux = dx / dist, uy = dy / dist;
      const inten = Math.min(1, 14 / dist);
      acc += dn * (2 + 3 * inten);
      while (acc >= 1) {
        acc--;
        // dust tail: inherits the comet's velocity, so it curves
        Pp.push({ x: c[0], y: c[1], vx: vx * .4 + ux * (.3 + Math.random() * .35) + (Math.random() - .5) * .12, vy: vy * .4 + uy * (.3 + Math.random() * .35) + (Math.random() - .5) * .12, age: 0, life: 34, k: 0, i: inten });
        // ion tail: always points straight away from the sun
        Pp.push({ x: c[0], y: c[1], vx: ux * 1.15, vy: uy * 1.15, age: 0, life: 22, k: 1, i: inten });
      }
      const drag = Math.pow(.955, dn);
      for (let n = Pp.length - 1; n >= 0; n--) {
        const q = Pp[n];
        q.age += dn;
        if (q.age >= q.life) { Pp.splice(n, 1); continue; }
        if (q.k) { const rx = q.x - sx, ry = q.y - sy, rl = Math.hypot(rx, ry) || 1; q.vx = rx / rl * 1.15; q.vy = ry / rl * 1.15; }
        else { q.vx *= drag; q.vy *= drag; }
        q.x += q.vx * dn; q.y += q.vy * dn;
      }
      x.clearRect(0, 0, W, H);
      const ion = P(3)[1];
      Pp.forEach(q => {
        const f = 1 - q.age / q.life;
        // fade out near the canvas edge so the tail is never cut off
        const e = Math.max(0, Math.min(1, Math.min(q.x - 3, W - 3 - q.x, q.y - 3, H - 3 - q.y) / 10));
        if (e <= 0) return;
        q.k ? dot(x, q.x, q.y, .4 + .9 * f, ion, .6 * f * e * (.4 + .6 * q.i))
            : dot(x, q.x, q.y, .5 + 1.5 * f, TH.c1, .5 * f * e * (.4 + .6 * q.i));
      });
      dot(x, sx, sy, 9, TH.c2, .25); dot(x, sx, sy, 5, TH.c2, 1);
      dot(x, c[0], c[1], 5.5, TH.c1, .28); dot(x, c[0], c[1], 2.4, TH.c1, 1); dot(x, c[0], c[1], 1, TH.c2, 1);
      x.globalAlpha = 1;
    };
  }
};

/* ---- 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>
140 lines

The math

Kepler's equation M = E − e·sin E with e = 0.82, so it races past the sun and drifts at the far end. The dust tail curves; the ion tail always points straight away from the sun.

Attribution

Free for personal and commercial use under CC BY 4.0. Keep the comment at the top of the snippet, or credit the author with a visible link.

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

License