WE.mount('water-resistance', function (S) {
var C = S.C, ctx = S.ctx, bub = [], R = [[30, 'Splashes, rain and hand washing'], [50, 'Brief swims in calm water'], [100, 'Swimming and snorkeling'], [200, 'Scuba diving'], [300, 'Deep diving']];
for (var i = 0; i < 40; i++) bub.push([Math.random(), Math.random(), .5 + Math.random()]);
var depth = S.slider('Depth', 0, 300, 3, 1, function (v) { return v + ' m'; });
var rating = S.select('Watch rating', R.map(function (r) { return [String(r[0]), r[0] + ' m']; }), '50');
var ACT = [[0, 1, 'At the surface', 30], [1, 6, 'Swimming', 50], [6, 12, 'Snorkeling and shallow dives', 100], [12, 45, 'Scuba diving', 200], [45, 301, 'Deep technical diving', 300]];
S.loop(function (dt, t) {
var w = S.W, h = S.H, d = depth.v, bar = 1 + d / 10, top = h * .14, k = d / 300;
var g = ctx.createLinearGradient(0, top, 0, h); g.addColorStop(0, 'rgb(' + Math.round(40 - 30 * k) + ',' + Math.round(130 - 100 * k) + ',' + Math.round(175 - 120 * k) + ')'); g.addColorStop(1, 'rgb(' + Math.round(8 - 6 * k) + ',' + Math.round(40 - 34 * k) + ',' + Math.round(80 - 62 * k) + ')');
S.clear('#0d0f13'); ctx.fillStyle = g; ctx.fillRect(0, top, w, h - top);
ctx.beginPath(); ctx.moveTo(0, top); for (var x = 0; x <= w; x += 8) ctx.lineTo(x, top + Math.sin(x / 30 + t * 2) * 3); ctx.lineTo(w, 0); ctx.lineTo(0, 0); ctx.fillStyle = '#0d0f13'; ctx.fill();
bub.forEach(function (b) { b[1] -= dt * .05 * b[2]; if (b[1] < 0) b[1] = 1; S.ring(b[0] * w + Math.sin(t + b[2] * 9) * 6, top + b[1] * (h - top), 1.5 + b[2] * 1.5, 'rgba(255,255,255,.25)', 1); });
// the watch
var wx = w * .3, wy = top + (h - top) * .5, r = Math.min(w, h) * .13, act = ACT.filter(function (a) { return d >= a[0] && d < a[1]; })[0], ok = +rating.v >= act[3];
S.rr(wx - r * .45, wy - r * 1.9, r * .9, r * 3.8, r * .2, '#2a2f38'); S.dot(wx, wy, r, '#1a1e26'); S.ring(wx, wy, r, ok ? '#b9ac95' : C.red, 5);
for (var i = 0; i < 12; i++) { var a = i * S.TAU / 12; S.ln(wx + r * .74 * Math.sin(a), wy - r * .74 * Math.cos(a), wx + r * .88 * Math.sin(a), wy - r * .88 * Math.cos(a), C.ink, 2.5); }
S.ln(wx, wy, wx + r * .45, wy - r * .25, C.ink, 4); S.ln(wx, wy, wx - r * .2, wy - r * .62, C.ink, 3);
if (!ok) { for (var j = 0; j < 5; j++) S.dot(wx - r * .5 + j * r * .25, wy + r * .35 + Math.sin(t * 3 + j) * 4, 4, 'rgba(111,177,230,.8)'); S.txt('Water gets in', wx, wy + r + 22, C.red, 14, 'center', 700); }
for (var n = 0; n < Math.min(8, Math.round(bar / 4) + 1); n++) { var aa = n * S.TAU / 8 + t * .3; S.arrow(wx + (r + 34) * Math.cos(aa), wy + (r + 34) * Math.sin(aa), wx + (r + 10) * Math.cos(aa), wy + (r + 10) * Math.sin(aa), 'rgba(255,255,255,.55)', 1.5); }
// readout
var rx = w * (S.small() ? .56 : .58); S.txt(d + ' m', rx, top + (h - top) * .22, C.ink, S.small() ? 30 : 44, 'left', 700); S.txt(bar.toFixed(1) + ' bar of pressure', rx, top + (h - top) * .22 + (S.small() ? 28 : 38), '#d8e6f2', S.small() ? 13 : 16);
S.txt(act[2], rx, top + (h - top) * .5, '#d8e6f2', S.small() ? 13 : 16, 'left', 650);
R.forEach(function (q, i) { var yy = top + (h - top) * .62 + i * (S.small() ? 17 : 22); S.txt((q[0] === +rating.v ? '▸ ' : ' ') + q[0] + ' m: ' + q[1], rx, yy, q[0] === +rating.v ? C.acc : 'rgba(255,255,255,.6)', S.small() ? 10.5 : 13, 'left', q[0] === +rating.v ? 700 : 500); });
S.txt('Surface', 12, top - 14, C.mut, 12);
S.say(ok ? 'A watch rated ' + rating.v + ' m is built for this: ' + act[2].toLowerCase() + ' at ' + d + ' m.' : 'A watch rated ' + rating.v + ' m is not built for ' + act[2].toLowerCase() + '. The number comes from a still pressure test in a lab, not a real dive, so moving water asks for a much higher rating.');
});
});