UNPKG

live2d-widgets

Version:
7 lines (6 loc) 18 kB
/*! * Live2D Widget * https://github.com/stevenjoezhang/live2d-widget */ function e(e){return Array.isArray(e)?e[Math.floor(Math.random()*e.length)]:e}function t(e,t){const s=Math.floor(Math.random()*(e-1));return s>=t?s+1:s}function s(e,t){return new Promise(((t,s)=>{let o;o=document.createElement("script"),o.src=e,o&&(o.onload=()=>t(e),o.onerror=()=>s(e),document.head.appendChild(o))}))}let o=null;function i(t,s,i,n=!0){let l=parseInt(sessionStorage.getItem("waifu-message-priority"),10);if(isNaN(l)&&(l=0),!t||n&&l>i||!n&&l>=i)return;o&&(clearTimeout(o),o=null),t=e(t),sessionStorage.setItem("waifu-message-priority",String(i));const a=document.getElementById("waifu-tips");a.innerHTML=t,a.classList.add("waifu-tips-active"),o=setTimeout((()=>{sessionStorage.removeItem("waifu-message-priority"),a.classList.remove("waifu-tips-active")}),s)}function n(e,...t){return e.replace(/\$(\d+)/g,((e,s)=>{var o;const i=parseInt(s,10)-1;return null!==(o=t[i])&&void 0!==o?o:""}))}class l{constructor(e="info"){this.level=e}setLevel(e){e&&(this.level=e)}shouldLog(e){return l.levelOrder[e]<=l.levelOrder[this.level]}error(e,...t){this.shouldLog("error")&&console.error("[Live2D Widget][ERROR]",e,...t)}warn(e,...t){this.shouldLog("warn")&&console.warn("[Live2D Widget][WARN]",e,...t)}info(e,...t){this.shouldLog("info")&&console.log("[Live2D Widget][INFO]",e,...t)}trace(e,...t){this.shouldLog("trace")&&console.log("[Live2D Widget][TRACE]",e,...t)}}l.levelOrder={error:0,warn:1,info:2,trace:3};const a=new l;class c{constructor(e,t=[]){var s;this.modelList=null;let{apiPath:o,cdnPath:i}=e;const{cubism2Path:n,cubism5Path:l}=e;let c=!1;if("string"==typeof i)i.endsWith("/")||(i+="/"),c=!0;else if("string"==typeof o)o.endsWith("/")||(o+="/"),i=o,c=!0,a.warn("apiPath option is deprecated. Please use cdnPath instead.");else if(!t.length)throw"Invalid initWidget argument!";let d=parseInt(localStorage.getItem("modelId"),10),r=parseInt(localStorage.getItem("modelTexturesId"),10);(isNaN(d)||isNaN(r))&&(r=0),isNaN(d)&&(d=null!==(s=e.modelId)&&void 0!==s?s:0),this.useCDN=c,this.cdnPath=i||"",this.cubism2Path=n||"",this.cubism5Path=l||"",this._modelId=d,this._modelTexturesId=r,this.currentModelVersion=0,this.loading=!1,this.modelJSONCache={},this.models=t}static async initCheck(e,t=[]){const s=new c(e,t);if(s.useCDN){const e=await fetch(`${s.cdnPath}model_list.json`);s.modelList=await e.json(),s.modelId>=s.modelList.models.length&&(s.modelId=0);const t=s.modelList.models[s.modelId];if(Array.isArray(t))s.modelTexturesId>=t.length&&(s.modelTexturesId=0);else{const e=`${s.cdnPath}model/${t}/index.json`,o=await s.fetchWithCache(e);if(2===s.checkModelVersion(o)){const e=await s.loadTextureCache(t);s.modelTexturesId>=e.length&&(s.modelTexturesId=0)}}}else s.modelId>=s.models.length&&(s.modelId=0),s.modelTexturesId>=s.models[s.modelId].paths.length&&(s.modelTexturesId=0);return s}set modelId(e){this._modelId=e,localStorage.setItem("modelId",e.toString())}get modelId(){return this._modelId}set modelTexturesId(e){this._modelTexturesId=e,localStorage.setItem("modelTexturesId",e.toString())}get modelTexturesId(){return this._modelTexturesId}resetCanvas(){document.getElementById("waifu-canvas").innerHTML='<canvas id="live2d" width="800" height="800"></canvas>'}async fetchWithCache(e){let t;if(e in this.modelJSONCache)t=this.modelJSONCache[e];else{try{const s=await fetch(e);t=await s.json()}catch(e){t=null}this.modelJSONCache[e]=t}return t}checkModelVersion(e){return 3===e.Version||e.FileReferences?3:2}async loadLive2D(e,t){if(this.loading)a.warn("Still loading. Abort.");else{this.loading=!0;try{const o=this.checkModelVersion(t);if(2===o){if(!this.cubism2model){if(!this.cubism2Path)return void a.error("No cubism2Path set, cannot load Cubism 2 Core.");await s(this.cubism2Path);const{default:e}=await import("./chunk/index.js");this.cubism2model=new e}3===this.currentModelVersion&&(this.cubism5model.release(),this.resetCanvas()),3!==this.currentModelVersion&&this.cubism2model.gl?await this.cubism2model.changeModelWithJSON(e,t):await this.cubism2model.init("live2d",e,t)}else{if(!this.cubism5Path)return void a.error("No cubism5Path set, cannot load Cubism 5 Core.");await s(this.cubism5Path);const{AppDelegate:t}=await import("./chunk/index2.js");this.cubism5model=new t,2===this.currentModelVersion&&(this.cubism2model.destroy(),this.resetCanvas()),2!==this.currentModelVersion&&this.cubism5model.subdelegates.at(0)?this.cubism5model.changeModel(e):(this.cubism5model.initialize(),this.cubism5model.changeModel(e),this.cubism5model.run())}a.info(`Model ${e} (Cubism version ${o}) loaded`),this.currentModelVersion=o}catch(e){console.error("loadLive2D failed",e)}this.loading=!1}}async loadTextureCache(e){return await this.fetchWithCache(`${this.cdnPath}model/${e}/textures.cache`)||[]}async loadModel(e){let t,s;if(this.useCDN){let e=this.modelList.models[this.modelId];Array.isArray(e)&&(e=e[this.modelTexturesId]),t=`${this.cdnPath}model/${e}/index.json`,s=await this.fetchWithCache(t);if(2===this.checkModelVersion(s)){const t=await this.loadTextureCache(e);if(t.length>0){let e=t[this.modelTexturesId];"string"==typeof e&&(e=[e]),s.textures=e}}}else t=this.models[this.modelId].paths[this.modelTexturesId],s=await this.fetchWithCache(t);await this.loadLive2D(t,s),i(e,4e3,10)}async loadRandTexture(e="",s=""){const{modelId:o}=this;let n=!1;if(this.useCDN){const e=this.modelList.models[o];if(Array.isArray(e))this.modelTexturesId=t(e.length,this.modelTexturesId);else{const s=`${this.cdnPath}model/${e}/index.json`,o=await this.fetchWithCache(s);if(2===this.checkModelVersion(o)){const s=await this.loadTextureCache(e);s.length<=1?n=!0:this.modelTexturesId=t(s.length,this.modelTexturesId)}else n=!0}}else 1===this.models[o].paths.length?n=!0:this.modelTexturesId=t(this.models[o].paths.length,this.modelTexturesId);n?i(s,4e3,10):await this.loadModel(e)}async loadNextModel(){this.modelTexturesId=0,this.useCDN?(this.modelId=(this.modelId+1)%this.modelList.models.length,await this.loadModel(this.modelList.messages[this.modelId])):(this.modelId=(this.modelId+1)%this.models.length,await this.loadModel(this.models[this.modelId].message))}}class d{constructor(e,t,s){this.config=t,this.tools={hitokoto:{icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M512 240c0 114.9-114.6 208-256 208c-37.1 0-72.3-6.4-104.1-17.9c-11.9 8.7-31.3 20.6-54.3 30.6C73.6 471.1 44.7 480 16 480c-6.5 0-12.3-3.9-14.8-9.9c-2.5-6-1.1-12.8 3.4-17.4c0 0 0 0 0 0s0 0 0 0s0 0 0 0c0 0 0 0 0 0l.3-.3c.3-.3 .7-.7 1.3-1.4c1.1-1.2 2.8-3.1 4.9-5.7c4.1-5 9.6-12.4 15.2-21.6c10-16.6 19.5-38.4 21.4-62.9C17.7 326.8 0 285.1 0 240C0 125.1 114.6 32 256 32s256 93.1 256 208z"/></svg>',callback:async()=>{const e=await fetch("https://v1.hitokoto.cn"),t=await e.json(),o=n(s.message.hitokoto,t.from,t.creator);i(t.hitokoto,6e3,9),setTimeout((()=>{i(o,4e3,9)}),6e3)}},asteroids:{icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M498.1 5.6c10.1 7 15.4 19.1 13.5 31.2l-64 416c-1.5 9.7-7.4 18.2-16 23s-18.9 5.4-28 1.6L284 427.7l-68.5 74.1c-8.9 9.7-22.9 12.9-35.2 8.1S160 493.2 160 480l0-83.6c0-4 1.5-7.8 4.2-10.8L331.8 202.8c5.8-6.3 5.6-16-.4-22s-15.7-6.4-22-.7L106 360.8 17.7 316.6C7.1 311.3 .3 300.7 0 288.9s5.9-22.8 16.1-28.7l448-256c10.7-6.1 23.9-5.5 34 1.4z"/></svg>',callback:()=>{if(window.Asteroids)window.ASTEROIDSPLAYERS||(window.ASTEROIDSPLAYERS=[]),window.ASTEROIDSPLAYERS.push(new window.Asteroids);else{const e=document.createElement("script");e.src="https://fastly.jsdelivr.net/gh/stevenjoezhang/asteroids/asteroids.js",document.head.appendChild(e)}}},"switch-model":{icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M320 64A64 64 0 1 0 192 64a64 64 0 1 0 128 0zm-96 96c-35.3 0-64 28.7-64 64l0 48c0 17.7 14.3 32 32 32l1.8 0 11.1 99.5c1.8 16.2 15.5 28.5 31.8 28.5l38.7 0c16.3 0 30-12.3 31.8-28.5L318.2 304l1.8 0c17.7 0 32-14.3 32-32l0-48c0-35.3-28.7-64-64-64l-64 0zM132.3 394.2c13-2.4 21.7-14.9 19.3-27.9s-14.9-21.7-27.9-19.3c-32.4 5.9-60.9 14.2-82 24.8c-10.5 5.3-20.3 11.7-27.8 19.6C6.4 399.5 0 410.5 0 424c0 21.4 15.5 36.1 29.1 45c14.7 9.6 34.3 17.3 56.4 23.4C130.2 504.7 190.4 512 256 512s125.8-7.3 170.4-19.6c22.1-6.1 41.8-13.8 56.4-23.4c13.7-8.9 29.1-23.6 29.1-45c0-13.5-6.4-24.5-14-32.6c-7.5-7.9-17.3-14.3-27.8-19.6c-21-10.6-49.5-18.9-82-24.8c-13-2.4-25.5 6.3-27.9 19.3s6.3 25.5 19.3 27.9c30.2 5.5 53.7 12.8 69 20.5c3.2 1.6 5.8 3.1 7.9 4.5c3.6 2.4 3.6 7.2 0 9.6c-8.8 5.7-23.1 11.8-43 17.3C374.3 457 318.5 464 256 464s-118.3-7-157.7-17.9c-19.9-5.5-34.2-11.6-43-17.3c-3.6-2.4-3.6-7.2 0-9.6c2.1-1.4 4.8-2.9 7.9-4.5c15.3-7.7 38.8-14.9 69-20.5z"/></svg>',callback:()=>e.loadNextModel()},"switch-texture":{icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M211.8 0c7.8 0 14.3 5.7 16.7 13.2C240.8 51.9 277.1 80 320 80s79.2-28.1 91.5-66.8C413.9 5.7 420.4 0 428.2 0l12.6 0c22.5 0 44.2 7.9 61.5 22.3L628.5 127.4c6.6 5.5 10.7 13.5 11.4 22.1s-2.1 17.1-7.8 23.6l-56 64c-11.4 13.1-31.2 14.6-44.6 3.5L480 197.7 480 448c0 35.3-28.7 64-64 64l-192 0c-35.3 0-64-28.7-64-64l0-250.3-51.5 42.9c-13.3 11.1-33.1 9.6-44.6-3.5l-56-64c-5.7-6.5-8.5-15-7.8-23.6s4.8-16.6 11.4-22.1L137.7 22.3C155 7.9 176.7 0 199.2 0l12.6 0z"/></svg>',callback:()=>{let t="",o="";s&&(t=s.message.changeSuccess,o=s.message.changeFail),e.loadRandTexture(t,o)}},photo:{icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M220.6 121.2L271.1 96 448 96l0 96-114.8 0c-21.9-15.1-48.5-24-77.2-24s-55.2 8.9-77.2 24L64 192l0-64 128 0c9.9 0 19.7-2.3 28.6-6.8zM0 128L0 416c0 35.3 28.7 64 64 64l384 0c35.3 0 64-28.7 64-64l0-320c0-35.3-28.7-64-64-64L271.1 32c-9.9 0-19.7 2.3-28.6 6.8L192 64l-32 0 0-16c0-8.8-7.2-16-16-16L80 32c-8.8 0-16 7.2-16 16l0 16C28.7 64 0 92.7 0 128zM168 304a88 88 0 1 1 176 0 88 88 0 1 1 -176 0z"/></svg>',callback:()=>{i(s.message.photo,6e3,9);const e=document.getElementById("live2d");if(!e)return;const t=e.toDataURL(),o=document.createElement("a");o.style.display="none",o.href=t,o.download="live2d-photo.png",document.body.appendChild(o),o.click(),document.body.removeChild(o)}},info:{icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM216 336l24 0 0-64-24 0c-13.3 0-24-10.7-24-24s10.7-24 24-24l48 0c13.3 0 24 10.7 24 24l0 88 8 0c13.3 0 24 10.7 24 24s-10.7 24-24 24l-80 0c-13.3 0-24-10.7-24-24s10.7-24 24-24zm40-208a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>',callback:()=>{open("https://github.com/stevenjoezhang/live2d-widget")}},quit:{icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M342.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L192 210.7 86.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L146.7 256 41.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L192 301.3 297.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L237.3 256 342.6 150.6z"/></svg>',callback:()=>{localStorage.setItem("waifu-display",Date.now().toString());i(s.message.goodbye,2e3,11);const e=document.getElementById("waifu");e&&(e.classList.remove("waifu-active"),setTimeout((()=>{e.classList.add("waifu-hidden");const t=document.getElementById("waifu-toggle");null==t||t.classList.add("waifu-toggle-active")}),3e3))}}}}registerTools(){var e;Array.isArray(this.config.tools)||(this.config.tools=Object.keys(this.tools));for(const t of this.config.tools)if(this.tools[t]){const{icon:s,callback:o}=this.tools[t],i=document.createElement("span");i.id=`waifu-tool-${t}`,i.innerHTML=s,null===(e=document.getElementById("waifu-tool"))||void 0===e||e.insertAdjacentElement("beforeend",i),i.addEventListener("click",o)}}}async function r(t){var s;localStorage.removeItem("waifu-display"),sessionStorage.removeItem("waifu-message-priority"),document.body.insertAdjacentHTML("beforeend",'<div id="waifu">\n <div id="waifu-tips"></div>\n <div id="waifu-canvas">\n <canvas id="live2d" width="800" height="800"></canvas>\n </div>\n <div id="waifu-tool"></div>\n </div>');let o,l=[];if(t.waifuPath){const s=await fetch(t.waifuPath);o=await s.json(),l=o.models,function(t){let s,o=!1;const n=t.message.default;let l;t.seasons.forEach((({date:t,text:s})=>{const o=new Date,i=t.split("-")[0],l=t.split("-")[1]||i;Number(i.split("/")[0])<=o.getMonth()+1&&o.getMonth()+1<=Number(l.split("/")[0])&&Number(i.split("/")[1])<=o.getDate()&&o.getDate()<=Number(l.split("/")[1])&&(s=(s=e(s)).replace("{year}",String(o.getFullYear())),n.push(s))})),window.addEventListener("mousemove",(()=>o=!0)),window.addEventListener("keydown",(()=>o=!0)),setInterval((()=>{o?(o=!1,clearInterval(s),s=null):s||(s=setInterval((()=>{i(n,6e3,9)}),2e4))}),1e3),window.addEventListener("mouseover",(s=>{var o;for(let{selector:n,text:a}of t.mouseover)if(null===(o=s.target)||void 0===o?void 0:o.closest(n)){if(l===n)return;return l=n,a=e(a),a=a.replace("{text}",s.target.innerText),void i(a,4e3,8)}})),window.addEventListener("click",(s=>{var o;for(let{selector:n,text:l}of t.click)if(null===(o=s.target)||void 0===o?void 0:o.closest(n))return l=e(l),l=l.replace("{text}",s.target.innerText),void i(l,4e3,8)})),window.addEventListener("live2d:hoverbody",(()=>{i(e(t.message.hoverBody),4e3,8,!1)})),window.addEventListener("live2d:tapbody",(()=>{i(e(t.message.tapBody),4e3,9)}));const a=()=>{};console.log("%c",a),a.toString=()=>{i(t.message.console,6e3,9)},window.addEventListener("copy",(()=>{i(t.message.copy,6e3,9)})),window.addEventListener("visibilitychange",(()=>{document.hidden||i(t.message.visibilitychange,6e3,9)}))}(o),i(function(e,t,s){if("/"===location.pathname)for(const{hour:t,text:s}of e){const e=new Date,o=t.split("-")[0],i=t.split("-")[1]||o;if(Number(o)<=e.getHours()&&e.getHours()<=Number(i))return s}if(!t)return"";const o=n(t,document.title);if(""===document.referrer||!s)return o;const i=new URL(document.referrer);return location.hostname===i.hostname?o:`${n(s,i.hostname)}<br>${o}`}(o.time,o.message.welcome,o.message.referrer),7e3,11)}const a=await c.initCheck(t,l);await a.loadModel(""),new d(a,t,o).registerTools(),t.drag&&function(){const e=document.getElementById("waifu");if(!e)return;let t=window.innerWidth,s=window.innerHeight;const o=e.offsetWidth,i=e.offsetHeight;e.addEventListener("mousedown",(n=>{if(2===n.button)return;const l=document.getElementById("live2d");if(n.target!==l)return;n.preventDefault();const a=n.offsetX,c=n.offsetY;document.onmousemove=n=>{const l=n.clientX,d=n.clientY;let r=l-a,m=d-c;m<0?m=0:m>=s-i&&(m=s-i),r<0?r=0:r>=t-o&&(r=t-o),e.style.top=m+"px",e.style.left=r+"px"},document.onmouseup=()=>{document.onmousemove=null}})),window.onresize=()=>{t=window.innerWidth,s=window.innerHeight}}(),null===(s=document.getElementById("waifu"))||void 0===s||s.classList.add("waifu-active")}window.initWidget=function(e){if("string"==typeof e)return void a.error("Your config for Live2D initWidget is outdated. Please refer to https://github.com/stevenjoezhang/live2d-widget/blob/master/dist/autoload.js");a.setLevel(e.logLevel),document.body.insertAdjacentHTML("beforeend",'<div id="waifu-toggle">\n <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">\x3c!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --\x3e<path d="M96 64a64 64 0 1 1 128 0A64 64 0 1 1 96 64zm48 320l0 96c0 17.7-14.3 32-32 32s-32-14.3-32-32l0-192.2L59.1 321c-9.4 15-29.2 19.4-44.1 10S-4.5 301.9 4.9 287l39.9-63.3C69.7 184 113.2 160 160 160s90.3 24 115.2 63.6L315.1 287c9.4 15 4.9 34.7-10 44.1s-34.7 4.9-44.1-10L240 287.8 240 480c0 17.7-14.3 32-32 32s-32-14.3-32-32l0-96-32 0z"/></svg>\n </div>');const t=document.getElementById("waifu-toggle");null==t||t.addEventListener("click",(()=>{var s;null==t||t.classList.remove("waifu-toggle-active"),(null==t?void 0:t.getAttribute("first-time"))?(r(e),null==t||t.removeAttribute("first-time")):(localStorage.removeItem("waifu-display"),null===(s=document.getElementById("waifu"))||void 0===s||s.classList.remove("waifu-hidden"),setTimeout((()=>{var e;null===(e=document.getElementById("waifu"))||void 0===e||e.classList.add("waifu-active")}),0))})),localStorage.getItem("waifu-display")&&Date.now()-Number(localStorage.getItem("waifu-display"))<=864e5?(null==t||t.setAttribute("first-time","true"),setTimeout((()=>{null==t||t.classList.add("waifu-toggle-active")}),0)):r(e)};export{a as l}; //# sourceMappingURL=waifu-tips.js.map