File size: 6,329 Bytes
c04dd6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Art kit: drawing helpers for figures and scenes, plus a per-renderer pose smoother (BB.ART).  Game-agnostic:
// no layout and no colours here.  Pure presentation: reads the Case, never writes it.  Everything is procedural
// and seeded (BB.RNG) or a function of presentation time t, so frames are reproducible.
(function (g) {
  var BB = g.BB, TAU = Math.PI * 2;
  var ART = BB.ART = BB.ART || {};

  // ---------- small maths ----------
  function c01(v) { return v < 0 ? 0 : v > 1 ? 1 : v; }
  ART.c01 = c01;
  ART.mix = function (a, b, k) { return a + (b - a) * k; };
  ART.easeOut = function (k) { k = c01(k); return 1 - (1 - k) * (1 - k) * (1 - k); };
  ART.easeIn = function (k) { k = c01(k); return k * k; };
  ART.easeInOut = function (k) { k = c01(k); return k * k * (3 - 2 * k); };
  // hit flash: tint a fill most of the way to warm white (keeps the form readable instead of a flat white cut-out)
  var litCache = {};
  ART.lit = function (on, col) {
    if (!on) return col;
    if (typeof col !== 'string' || col.charAt(0) !== '#') return '#f2ece2';
    if (litCache[col]) return litCache[col];
    var h = col.length === 4 ? col.replace(/#(.)(.)(.)/, '#$1$1$2$2$3$3') : col, out = '#';
    for (var i = 0; i < 3; i++) { var v = parseInt(h.substr(1 + i * 2, 2), 16); v = Math.round(v + (255 - v) * 0.55); out += (v < 16 ? '0' : '') + v.toString(16); }
    return (litCache[col] = out);
  };
  ART.rgba = function (rgb, a) { return 'rgba(' + rgb + ',' + (+a).toFixed(3) + ')'; };
  ART.hash = function (i) { var s = Math.sin(i * 127.1 + 311.7) * 43758.5453; return s - Math.floor(s); };

  // ---------- pose smoothing ----------
  // The renderer calls begin() once per frame with its own store; smooth() then eases numeric pose fields toward the
  // target of the current FSM state, so a state change blends over a few frames instead of snapping.
  // A new case, a rewind or a long gap resets the store (first frame = exact target pose).
  ART.begin = function (store, c, t) {
    store.fresh = store.c !== c || store.t == null || t < store.t || t - store.t > 0.5;
    if (store.fresh) { store.c = c; store.p = {}; }
    store.dt = store.fresh ? 0 : t - store.t; store.t = t; ART.store = store;
  };
  ART.smooth = function (key, tgt, rate) {
    var st = ART.store, cur = st.p[key], k;
    if (!cur) { cur = st.p[key] = {}; k = 1; } else k = 1 - Math.exp(-st.dt * (rate || 20));
    for (var n in tgt) {
      var v = tgt[n];
      if (typeof v !== 'number' || cur[n] == null || typeof cur[n] !== 'number') cur[n] = v;
      else cur[n] += (v - cur[n]) * k;
    }
    return cur;
  };
  ART.dt = function () { return ART.store ? ART.store.dt : 0; };

  // ---------- path helpers ----------
  ART.bez = function (p0, p1, p2, p3, n) {
    var out = [];
    for (var i = 0; i <= n; i++) {
      var t = i / n, u = 1 - t, a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t;
      out.push([a * p0[0] + b * p1[0] + c * p2[0] + d * p3[0], a * p0[1] + b * p1[1] + c * p2[1] + d * p3[1]]);
    }
    return out;
  };
  // closed ribbon along a polyline, width w0 -> w1 (roots, vines, horns, tails, cloth strips)
  ART.ribbon = function (x, pts, w0, w1) {
    var n = pts.length, L = [], R = [], i;
    for (i = 0; i < n; i++) {
      var a = pts[Math.max(0, i - 1)], b = pts[Math.min(n - 1, i + 1)], dx = b[0] - a[0], dy = b[1] - a[1], d = Math.sqrt(dx * dx + dy * dy) || 1;
      var w = (w0 + (w1 - w0) * i / Math.max(1, n - 1)) / 2;
      L.push([pts[i][0] - dy / d * w, pts[i][1] + dx / d * w]); R.push([pts[i][0] + dy / d * w, pts[i][1] - dx / d * w]);
    }
    x.moveTo(L[0][0], L[0][1]);
    for (i = 1; i < n; i++) x.lineTo(L[i][0], L[i][1]);
    for (i = n - 1; i >= 0; i--) x.lineTo(R[i][0], R[i][1]);
    x.closePath();
  };
  ART.poly = function (x, pts) { x.moveTo(pts[0][0], pts[0][1]); for (var i = 1; i < pts.length; i++) x.lineTo(pts[i][0], pts[i][1]); x.closePath(); };
  // smooth closed shape through points (quadratic midpoints)
  ART.blobPath = function (x, pts) {
    var n = pts.length, m = function (i) { var p = pts[i % n], q = pts[(i + 1) % n]; return [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2]; };
    var s = m(n - 1); x.moveTo(s[0], s[1]);
    for (var i = 0; i < n; i++) { var e = m(i); x.quadraticCurveTo(pts[i][0], pts[i][1], e[0], e[1]); }
    x.closePath();
  };
  // fill the current path, then an ink outline
  ART.ink = function (x, fill, lw, ink) {
    x.fillStyle = fill; x.fill();
    if (lw) { x.lineJoin = 'round'; x.lineCap = 'round'; x.lineWidth = lw; x.strokeStyle = ink || '#05070a'; x.stroke(); }
  };
  ART.glow = function (x, cx, cy, r, rgb, a) {            // soft additive light
    if (!(r > 0) || !(a > 0)) return;
    var gr = x.createRadialGradient(cx, cy, 0, cx, cy, r);
    gr.addColorStop(0, ART.rgba(rgb, a)); gr.addColorStop(0.4, ART.rgba(rgb, a * 0.45)); gr.addColorStop(1, ART.rgba(rgb, 0));
    var o = x.globalCompositeOperation; x.globalCompositeOperation = 'lighter';
    x.fillStyle = gr; x.fillRect(cx - r, cy - r, 2 * r, 2 * r);
    x.globalCompositeOperation = o;
  };
  ART.shade = function (x, cx, cy, rx, ry, a) {          // contact shadow on the floor
    if (!(rx > 0) || !(a > 0)) return;
    var gr = x.createRadialGradient(cx, cy, 0, cx, cy, rx);
    gr.addColorStop(0, 'rgba(0,0,0,' + a.toFixed(3) + ')'); gr.addColorStop(1, 'rgba(0,0,0,0)');
    x.save(); x.translate(cx, cy); x.scale(1, ry / rx); x.translate(-cx, -cy);
    x.fillStyle = gr; x.beginPath(); x.arc(cx, cy, rx, 0, TAU); x.fill(); x.restore();
  };
  // tattered hem: lineTo points from (x0,y) to (x1,y), alternating long / short tongues, waving with ph
  ART.hem = function (x, x0, x1, y, n, depth, wave, ph, seed) {
    for (var i = 0; i <= n; i++) {
      var k = i / n, h = ART.hash(i + (seed || 0)), d = (i % 2 ? depth : depth * 0.25) * (0.7 + 0.6 * h);
      x.lineTo(x0 + (x1 - x0) * k + Math.sin(ph + i * 1.7) * wave, y + d + Math.cos(ph * 0.8 + i) * wave * 0.4);
    }
  };
  // four-point star sparkle
  ART.spark = function (x, cx, cy, s, col) {
    x.fillStyle = col; x.beginPath();
    x.moveTo(cx - s, cy); x.quadraticCurveTo(cx, cy, cx, cy - s); x.quadraticCurveTo(cx, cy, cx + s, cy);
    x.quadraticCurveTo(cx, cy, cx, cy + s); x.quadraticCurveTo(cx, cy, cx - s, cy); x.fill();
  };
})(typeof window !== 'undefined' ? window : globalThis);