/* Welly, the WeLearn Helper mascot. A small robot that shows what the helper is doing. * Steady states: idle, listening, thinking, speaking, sleeping, offline. * Short reactions: happy, confused, dizzy, greet (waves), waking (stretches). * Usage: const w = Mascot.create(el, { size: 120 }); w.set("thinking"); w.level(0.4); w.react("happy"); * w.wake() plays the wake-up stretch and a wave, then settles in idle. * Shared by the Chrome side panel and the phone page. No dependencies. */ "use strict"; (function (root) { const CSS = ` .welly{display:inline-block;position:relative;line-height:0;user-select:none;-webkit-user-select:none;cursor:pointer;touch-action:manipulation} .welly svg{width:100%;height:100%;overflow:visible} .welly .w-float{animation:w-bob 3.2s ease-in-out infinite;transform-origin:60px 60px} .welly .w-shadow{animation:w-shadow 3.2s ease-in-out infinite;transform-origin:60px 113px} .welly .w-head{transform-origin:60px 70px;transition:transform .35s cubic-bezier(.3,1.5,.6,1)} .welly .w-eyes{transition:transform .18s ease-out} .welly .w-eye{transform-box:fill-box;transform-origin:center;animation:w-blink 5.5s infinite;transition:transform .12s} .welly .w-mouth{transform-box:fill-box;transform-origin:center;transition:transform .07s linear} .welly .w-arm{transform-box:fill-box;transition:transform .35s cubic-bezier(.3,1.5,.6,1)} .welly .w-arm-l{transform-origin:100% 0} .welly .w-arm-r{transform-origin:0 0} .welly .w-bulb{transition:fill .3s} .welly .w-glow{opacity:0;transition:opacity .3s} .welly .w-think,.welly .w-huh,.welly .w-arcs,.welly .w-happy,.welly .w-x,.welly .w-spiral,.welly .w-spark,.welly .w-shut,.welly .w-zzz{opacity:0;transition:opacity .3s} .welly .w-think circle{animation:w-dot 1.2s ease-in-out infinite} .welly .w-think circle:nth-child(2){animation-delay:.2s}.welly .w-think circle:nth-child(3){animation-delay:.4s} .welly[data-state=thinking] .w-think{opacity:1} .welly[data-state=thinking] .w-eyes{transform:translate(4px,-4px)} .welly[data-state=thinking] .w-eye{animation:none;transform:scaleY(.7)} .welly[data-state=thinking] .w-head{animation:w-ponder 2.4s ease-in-out infinite} .welly[data-state=thinking] .w-arm-r{transform:rotate(-125deg)} .welly[data-state=thinking] .w-bulb{animation:w-pulse .9s ease-in-out infinite} .welly[data-state=listening] .w-arcs{opacity:1} .welly[data-state=listening] .w-arcs path{animation:w-arc 1.4s ease-out infinite} .welly[data-state=listening] .w-arcs path:nth-child(2),.welly[data-state=listening] .w-arcs path:nth-child(4){animation-delay:.35s} .welly[data-state=listening] .w-eye{transform:scale(1.15);animation:none} .welly[data-state=listening] .w-glow{opacity:.55} .welly[data-state=listening] .w-head{transform:rotate(-5deg)} .welly[data-state=speaking] .w-glow{opacity:.45} .welly[data-state=speaking] .w-float{animation:w-talkbob .5s ease-in-out infinite} .welly[data-state=speaking] .w-arm-l{animation:w-gesture-l 1.6s ease-in-out infinite} .welly[data-state=speaking] .w-arm-r{animation:w-gesture-r 1.9s ease-in-out infinite} .welly[data-state=happy] .w-eye{opacity:0;animation:none} .welly[data-state=happy] .w-happy{opacity:1} .welly[data-state=happy] .w-spark{opacity:1;animation:w-spark .9s ease-out} .welly[data-state=happy] .w-float{animation:w-hop .6s ease-out 2} .welly[data-state=happy] .w-arm-l{transform:rotate(45deg)} .welly[data-state=happy] .w-arm-r{transform:rotate(-45deg)} .welly[data-state=greet] .w-eye{opacity:0;animation:none} .welly[data-state=greet] .w-happy{opacity:1} .welly[data-state=greet] .w-arm-r{animation:w-wave .45s ease-in-out 5 alternate} .welly[data-state=greet] .w-head{transform:rotate(6deg)} .welly[data-state=confused] .w-head{transform:rotate(-9deg)} .welly[data-state=confused] .w-huh{opacity:1;animation:w-hover 1.6s ease-in-out infinite} .welly[data-state=confused] .w-eye-r{transform:scaleY(.55)} .welly[data-state=confused] .w-arm-r{transform:rotate(-140deg)} .welly[data-state=sleeping] .w-eye,.welly[data-state=sleeping] .w-mouth{opacity:0;animation:none} .welly[data-state=sleeping] .w-shut{opacity:1} .welly[data-state=sleeping] .w-zzz{opacity:1} .welly[data-state=sleeping] .w-zzz text{animation:w-z 3s ease-in infinite;transform-box:fill-box;transform-origin:center} .welly[data-state=sleeping] .w-zzz text:nth-child(2){animation-delay:1s}.welly[data-state=sleeping] .w-zzz text:nth-child(3){animation-delay:2s} .welly[data-state=sleeping] .w-float{animation:w-breathe 4.5s ease-in-out infinite} .welly[data-state=sleeping] .w-head{transform:rotate(8deg) translateY(4px)} .welly[data-state=sleeping] .w-bulb{fill:#b9a6c9} .welly[data-state=sleeping] .w-visor{opacity:.85} .welly[data-state=waking] .w-float{animation:w-stretch 1.1s cubic-bezier(.3,1.4,.5,1)} .welly[data-state=waking] .w-arm-l{animation:w-armup-l 1.1s ease-in-out} .welly[data-state=waking] .w-arm-r{animation:w-armup-r 1.1s ease-in-out} .welly[data-state=waking] .w-eye{animation:w-open 1.1s ease-out} .welly[data-state=offline] .w-eye{opacity:0;animation:none} .welly[data-state=offline] .w-x{opacity:1} .welly[data-state=offline] .w-float{animation:none;transform:translateY(4px)} .welly[data-state=offline] .w-bulb{fill:#8a90a6} .welly[data-state=dizzy] .w-eye{opacity:0;animation:none} .welly[data-state=dizzy] .w-spiral{opacity:1} .welly[data-state=dizzy] .w-spiral g{animation:w-spin 1s linear infinite;transform-box:fill-box;transform-origin:center} .welly[data-state=dizzy] .w-head{animation:w-wobble .5s ease-in-out infinite} .welly.w-poke .w-float{animation:w-squish .45s cubic-bezier(.3,1.6,.5,1)} .welly .w-arcs path{transform-box:fill-box;transform-origin:center} @keyframes w-bob{0%,100%{transform:translateY(0)}50%{transform:translateY(-4px)}} @keyframes w-talkbob{0%,100%{transform:translateY(0) rotate(0)}30%{transform:translateY(-3px) rotate(-1.5deg)}70%{transform:translateY(-1px) rotate(1.5deg)}} @keyframes w-breathe{0%,100%{transform:translateY(3px) scale(1,1)}50%{transform:translateY(5px) scale(1.02,.98)}} @keyframes w-shadow{0%,100%{transform:scaleX(1);opacity:.22}50%{transform:scaleX(.82);opacity:.14}} @keyframes w-blink{0%,93%,100%{transform:scaleY(1)}95%{transform:scaleY(.1)}} @keyframes w-open{0%{transform:scaleY(.1)}40%{transform:scaleY(.1)}55%{transform:scaleY(1)}70%{transform:scaleY(.2)}85%,100%{transform:scaleY(1)}} @keyframes w-dot{0%,100%{transform:translateY(0);opacity:.35}50%{transform:translateY(-3px);opacity:1}} @keyframes w-ponder{0%,100%{transform:rotate(-4deg)}50%{transform:rotate(4deg)}} @keyframes w-pulse{0%,100%{r:5}50%{r:6.5}} @keyframes w-arc{0%{opacity:0;transform:scale(.8)}40%{opacity:1}100%{opacity:0;transform:scale(1.15)}} @keyframes w-hop{0%,100%{transform:translateY(0)}40%{transform:translateY(-9px)}} @keyframes w-stretch{0%{transform:translateY(4px) scale(1,.94)}45%{transform:translateY(-8px) scale(.96,1.08)}100%{transform:translateY(0) scale(1,1)}} @keyframes w-armup-l{0%,100%{transform:rotate(0)}45%{transform:rotate(60deg)}} @keyframes w-armup-r{0%,100%{transform:rotate(0)}45%{transform:rotate(-60deg)}} @keyframes w-wave{0%{transform:rotate(-58deg)}100%{transform:rotate(-12deg)}} @keyframes w-gesture-l{0%,100%{transform:rotate(0)}50%{transform:rotate(28deg)}} @keyframes w-gesture-r{0%,100%{transform:rotate(0)}40%{transform:rotate(-38deg)}} @keyframes w-spark{0%{transform:scale(.3);opacity:0}50%{opacity:1}100%{transform:scale(1.2);opacity:0}} @keyframes w-hover{0%,100%{transform:translateY(0)}50%{transform:translateY(-3px)}} @keyframes w-squish{0%{transform:scale(1,1)}30%{transform:scale(1.12,.86)}60%{transform:scale(.94,1.08)}100%{transform:scale(1,1)}} @keyframes w-spin{to{transform:rotate(360deg)}} @keyframes w-wobble{0%,100%{transform:rotate(-6deg)}50%{transform:rotate(6deg)}} @keyframes w-z{0%{opacity:0;transform:translate(0,4px) scale(.6)}25%{opacity:1}100%{opacity:0;transform:translate(8px,-16px) scale(1.15)}} @media (prefers-reduced-motion: reduce){.welly *{animation:none!important;transition:none!important}} `; const SVG = ` `; let cssDone = false; function injectCss() { if (cssDone) return; const s = document.createElement("style"); s.textContent = CSS; document.head.appendChild(s); cssDone = true; } function create(host, opts = {}) { injectCss(); const el = document.createElement("div"); el.className = "welly"; el.style.width = el.style.height = (opts.size || 96) + "px"; el.innerHTML = SVG; el.dataset.state = opts.state || "idle"; el.setAttribute("role", "img"); el.setAttribute("aria-label", opts.label || "Welly, the WeLearn Helper"); host.appendChild(el); const eyes = el.querySelector(".w-eyes"); const eyeEls = el.querySelectorAll(".w-eye"); const mouth = el.querySelector(".w-mouth"); const bulb = el.querySelector(".w-bulb"); const glow = el.querySelector(".w-glow"); const head = el.querySelector(".w-head"); let base = el.dataset.state, timer = null, pokes = 0, pokeTimer = null; function set(state) { base = state; clearTimeout(timer); el.dataset.state = state; if (state !== "speaking") { mouth.style.transform = ""; head.style.transform = ""; eyeEls.forEach((e) => (e.style.transform = "")); glow.style.opacity = ""; } if (state !== "listening") bulb.setAttribute("r", 5); } // A short reaction, then back to the steady state. function react(state, ms = 1400) { clearTimeout(timer); el.dataset.state = state; timer = setTimeout(() => { el.dataset.state = base; }, ms); } // Wake-up: stretch, open eyes, then wave hello. function wake(then) { clearTimeout(timer); base = "idle"; el.dataset.state = "waking"; timer = setTimeout(() => { el.dataset.state = "greet"; timer = setTimeout(() => { el.dataset.state = base; then && then(); }, 2300); }, 1100); } // 0..1: while speaking the mouth opens, the head nods and the antenna glows with the voice; while listening the antenna grows. function level(v) { v = Math.max(0, Math.min(1, v || 0)); if (el.dataset.state === "speaking") { mouth.style.transform = `scale(${1 + v * 0.6},${1 + v * 4})`; head.style.transform = `translateY(${(-v * 2.5).toFixed(2)}px) rotate(${((v - 0.3) * 3).toFixed(2)}deg)`; eyeEls.forEach((e) => (e.style.transform = `scaleY(${(1 - v * 0.25).toFixed(2)})`)); glow.style.opacity = (0.25 + v * 0.7).toFixed(2); } if (el.dataset.state === "listening") bulb.setAttribute("r", (5 + v * 4).toFixed(1)); } // Eyes follow the pointer (only in calm states). if (opts.track !== false) { window.addEventListener("pointermove", (e) => { if (!["idle", "listening", "speaking"].includes(el.dataset.state)) { eyes.style.transform = ""; return; } const r = el.getBoundingClientRect(); const dx = e.clientX - (r.left + r.width / 2), dy = e.clientY - (r.top + r.height / 2); const d = Math.hypot(dx, dy) || 1, k = Math.min(1, d / 300); eyes.style.transform = `translate(${(dx / d * 4 * k).toFixed(1)}px,${(dy / d * 3 * k).toFixed(1)}px)`; }, { passive: true }); } // Poke it: a squish and a little reaction. Too many pokes make it dizzy. Poking a sleeping Welly wakes it. el.addEventListener("click", () => { el.classList.remove("w-poke"); void el.offsetWidth; el.classList.add("w-poke"); if (base === "sleeping") { wake(); opts.onWake && opts.onWake(); return; } pokes++; clearTimeout(pokeTimer); pokeTimer = setTimeout(() => { pokes = 0; }, 2500); if (["thinking", "listening", "speaking"].includes(base)) return; if (pokes >= 5) { pokes = 0; react("dizzy", 1800); } else react(pokes === 1 ? "greet" : "happy", pokes === 1 ? 1600 : 900); opts.onPoke && opts.onPoke(pokes); }); return { el, set, react, level, wake, get state() { return base; } }; } root.Mascot = { create }; })(typeof self !== "undefined" ? self : this);