shopar-plugin
Advanced tools
Comparing version 0.0.5-alpha.6 to 0.0.5
@@ -1,1 +0,1 @@ | ||
function e(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{l(o.next(e))}catch(e){r(e)}}function a(e){try{l(o.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}l((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}const n=[Uint8Array,Uint32Array],o=n.length-2,i=["addEventListener","alert","atob","blur","btoa","cancelAnimationFrame","localStorage","location","locationbar","hash","hasOwnProperty","host","hostname","href","requestAnimationFrame"],r=[2,12,7,1],s=window[i[r[r[o]]]][i[r[r[r.length-1]]]],a=s.charCodeAt(o),l=a^a,c=new n[1]([l]);function d(){const e=(new TextEncoder).encode(s),t=i.map((e=>e.length));for(let n=0;n<e.length;n++)e[n]^=t[n%t.length];return window[i[t[2]]](String.fromCodePoint(...e))}const u=[1e3,2e3,4e3],h=u.length;function p(t,n=0){return e(this,void 0,void 0,(function*(){try{return yield t()}catch(e){if(n>=h)throw e;return yield new Promise((e=>setTimeout(e,u[n]))),p(t,n+1)}}))}const f=["Glasses","Shoes","Watches"];const v="0.0.5-alpha.6";let w=`https://cdn.jsdelivr.net/npm/shopar-plugin@${v}/dist`;const m={update:e=>{null!=e&&(w=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>w};function y(t){return e(this,void 0,void 0,(function*(){if("function"!=typeof importScripts){const e=document.createElement("script");return e.setAttribute("src",t),e.setAttribute("crossorigin","anonymous"),new Promise((t=>{e.addEventListener("load",(()=>t()),!1),e.addEventListener("error",(()=>t()),!1),document.body.appendChild(e)}))}importScripts(t.toString())}))}let g,b,E=!1;const C=[],A={organizationId:"Unknown",sku:"Unknown"};let S;function R(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),A),{vtoTime:(performance.now()-S)/1e3,vtoSessionId:b}),P((()=>{window.ShopAR__analytics.trackEventImpl(e,t)}))}function P(e){E?e():C.push(e)}function $(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}function k(t){return e(this,void 0,void 0,(function*(){const e=document.createElement("video");return e.setAttribute("playsinline","playsinline"),e.srcObject=yield t,e}))}const I=200,x=10;let _,L,D,M,T=!1;function N(n){return e(this,void 0,void 0,(function*(){const o=yield p((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(n).catch((e=>{throw console.error(e),new t("Resource unavailable.")}));if(!e.ok)throw new t(`Resource download failed with status ${e.status}.`);return e}))));try{return yield(yield o.blob()).arrayBuffer()}catch(e){throw console.error(e),new t("Resource has invalid body.")}}))}let U,j,G,O,z,W,F,K,H,V,B,q=!0;function X(e){e!==M&&(M=e,D=N(e),W=null,null==U||U.clearEffect(),T=!0)}function Y(n,o,i){return e(this,void 0,void 0,(function*(){return G=i,O=n,null==z&&(z=(()=>e(this,void 0,void 0,(function*(){if(null==(null===(r=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===r?void 0:r.getUserMedia))throw new t("No camera available!");var r;const s=Z(i),a=$(s);R("vto_camera_asked");const l=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i);U=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:o||"your_license_key_goes_here",previewElement:n,additionalOptions:{hint:l,cameraConfig:{disableDefaultCamera:!0}}}),j=yield k(a),R("vto_camera_granted"),yield ee(U,j,"user"===s),"Glasses"===i&&(yield L,function(t,n){e(this,void 0,void 0,(function*(){T=!1;const{ShopAR__TrueScale:o}=window;yield o.initialize(`${m}/wasm/mediapipe`,t);const i=[],r=setInterval((()=>e(this,void 0,void 0,(function*(){if(T)return void clearInterval(r);const{error:e,faceWidth:t}=yield o.predict(performance.now());if(T)return void clearInterval(r);if(null!=e)return void console.error(`TrueScale predict error: ${e}`);if(i.length<x)return void i.push(t);clearInterval(r);const s=137/(function(e){let t=0;const n=e.length;for(let o=0;o<n;o++)t+=e[o];return t/n}(i)+5);n.changeParameterVector("GLASSES","","scale",s,s,s,0),n.changeParameterVector("shopar_glasses","","scale",s,s,s,0)}))),I)}))}(j,U)),R("vto_init_success")})))()),z}))}function J(t){return e(this,void 0,void 0,(function*(){return null==W&&(W=U.switchEffect(t)),W}))}function Q(){null!=U&&(q=!0,U.setPaused(q),null!=j&&null!=j.srcObject&&j.srcObject instanceof MediaStream&&(j.srcObject.getTracks().forEach((e=>e.stop())),j=null),U.stopCamera(),O.style.display="none")}function Z(e){var t;return null==e?"user":null!==(t={Glasses:"user",Shoes:"environment",Watches:"environment"}[e])&&void 0!==t?t:"user"}function ee(t,n,o){return e(this,void 0,void 0,(function*(){yield new Promise((i=>{n.onloadedmetadata=()=>{n.play().then((()=>e(this,void 0,void 0,(function*(){t.setVideoElement(n,o),i()}))))}}))}))}let te,ne,oe,ie,re,se,ae,le,ce=!1,de=!1;function ue(o){if(o!==H){const i=255;H=o,K=function(o,i,r){return e(this,void 0,void 0,(function*(){let s="glb".charCodeAt(0),a="3d".charCodeAt(0);s>>=1,s+=a;const d=String.fromCharCode(s+14);a>>=2,s^=a+2;const u=String.fromCharCode(s-3);s^=5;const h=String.fromCharCode(s-7);s^=2;const f=String.fromCharCode(s-7),v=window[`${h}${u+d+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${f}`],w=yield p((()=>e(this,void 0,void 0,(function*(){const e=yield v(`${o}`).catch((()=>{throw new t("Unknown error.")}));if(!e.ok)throw new t("Unknown error.");return e})))),m=new n[0](yield(yield w.blob().catch((()=>{throw new t("Unknown error.")}))).arrayBuffer().catch((()=>{throw new t("Unknown error.")})));r+=a;const y=m.byteLength;if(c[l]=Math.min(76,i)*((s>>5)-1),152===c[l]&&(c[l]=y),r==l)return m;r=l,r^=l;for(let e=0,t=m.length;e<t;e++)c[l]^=c[r]<<13,c[l]^=c[r]>>17,c[l]^=c[r]<<5,m[e]=m[e]^c[l];return r^=c[0],r^=c[1],r^=c[2],m}))}(o,(new Date).getTime()+i,Math.random()*i),le=null,null==re||re.clear()}}function he(t,n){return e(this,void 0,void 0,(function*(){if(te=t,!de){ne=window.ShopAR__THREE,oe=ne.THREE,ie=new oe.WebGLRenderer({powerPreference:"high-performance",canvas:t,antialias:!0,alpha:!0}),ie.outputEncoding=oe.sRGBEncoding,ie.toneMapping=oe.ACESFilmicToneMapping,ie.setPixelRatio(window.devicePixelRatio||1),ie.setClearColor(new oe.Color(16777215)),re=new oe.Scene,se=new oe.PerspectiveCamera(25,1,.5);const e=function(e){var t;const n={Glasses:[67.5,15,90],Shoes:[-72.8,72.8,54.6],Watches:[0,-10,40],Handbags:[40,0,0],Rings:[0,0,10]};return null==e?n.Glasses:null!==(t=n[e])&&void 0!==t?t:n.Glasses}(n);se.position.set(e[0],e[1],e[2]),ae=new ne.OrbitControls(se,ie.domElement),ae.target.set(0,.03,0),ae.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>{R("preview_interacted")}),200);ae.addEventListener("end",e)}de=!0}me()}))}function pe(t,n){return e(this,void 0,void 0,(function*(){return null==le&&(le=(()=>e(this,void 0,void 0,(function*(){const e=new ne.RGBELoader,o=function(e,t){const n=e.THREE,o=new n.DataTexture(t.data,t.width,t.height,void 0,t.type,void 0,n.ClampToEdgeWrapping,n.ClampToEdgeWrapping,n.LinearFilter,n.LinearFilter,1,n.LinearEncoding);return o.flipY=!0,o.generateMipmaps=!1,o.needsUpdate=!0,o}(ne,e.parse(n));o.mapping=oe.EquirectangularReflectionMapping,o.encoding=oe.LinearEncoding,re.environment=o;const i=(new ne.GLTFLoader).setDRACOLoader((new ne.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${oe.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new ne.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${oe.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(ie)).setMeshoptDecoder("function"==typeof ne.MeshoptDecoder?ne.MeshoptDecoder():ne.MeshoptDecoder);return yield i.parseAsync(t.buffer,"")})))()),le}))}function fe(t){return e(this,void 0,void 0,(function*(){re.add(t.scene),re.traverse((e=>{if(e instanceof oe.Mesh){const t=e.material;t.depthWrite=!0,t instanceof oe.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))}function ve(){ce=!0,te.style.display="none"}function we(){ce||(requestAnimationFrame(we),me(),ae.update(),ie.render(re,se))}function me(){const e=te.clientWidth,t=te.clientHeight;te.width===e&&te.height===t||(ie.setSize(e,t,!1),se.aspect=e/t,se.updateProjectionMatrix())}function ye(e,t){const n=document.createElementNS("http://www.w3.org/2000/svg","svg");n.setAttribute("width",e),n.setAttribute("height",e);{const o=document.createElementNS("http://www.w3.org/2000/svg","image");o.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",t),o.setAttribute("width",e),o.setAttribute("height",e),n.appendChild(o)}return n}const ge="shopar-error";function be(e,t,n,o){const i=document.createElement("button");i.id=e,i.type="button",i.className="shopar-btn";{const e=ye("1.75rem",o);i.appendChild(e)}{const e=document.createElement("span");e.textContent=t,i.appendChild(e)}i.ariaLabel=n;const r=i.style;return r.zIndex="2",r.display="none",i}const Ee="shopar-control";const Ce="shopar-deepar-output";const Ae="shopar-loading";const Se="shopar-main";const Re="shopar-three-output";var Pe;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(Pe||(Pe={}));let $e,ke,Ie,xe,_e,Le,De,Me,Te,Ne=Pe.None;function Ue(e){Ne=e}let je=!1,Ge=null;function Oe(){je||($e=function(){const e=document.createElement("div");e.id=Se;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}(),ke=function(){const e=document.createElement("div");e.id=Ce;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),$e.appendChild(ke),Ie=function(){const e=document.createElement("canvas");e.id=Re;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),$e.appendChild(Ie),xe=function(){const e=document.createElement("div");return e.id=Ee,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e.style.display="none",e}(),$e.appendChild(xe),_e=be("shopar-btn-vto","Try-on","Launch ShopAR virtual try-on","data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22/%3E"),xe.appendChild(_e),Le=be("shopar-btn-3d","3D","Launch ShopAR 3D preview",`${m}/img/icons/cube.svg`),xe.appendChild(Le),De=be("shopar-btn-close","Close","Close ShopAR view",`${m}/img/icons/close.svg`),xe.append(De),Me=function(){const e=document.createElement("div");e.id=Ae;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=document.createElement("div");return i.className="shopar-spinner",n.appendChild(i),e}(),$e.appendChild(Me),Te=function(){const e=document.createElement("div");e.id=ge;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.flexDirection="column",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=ye("4rem",`${m}/img/icons/close.svg`);n.appendChild(i);const r=document.createElement("div");r.className="shopar-error-title",r.textContent="Camera Error",n.appendChild(r);const s=document.createElement("div");return s.className="shopar-error-body",s.textContent="Please refresh the page and allow the use of camera.",n.appendChild(s),e}(),$e.appendChild(Te),je=!0)}function ze(e,t){e.style.display=t?"":"none"}function We(e){""===e.style.display&&ze(e,!1)}function Fe(e,n){if(null==e)throw new t(`'${n}' not specified.`)}function Ke(e,n){if("string"!=typeof e)throw new t(`'${n}' must be a string.`)}const He=["AR","3D"];function Ve(e){const{apiKey:n,sku:o,targetElement:i,initialState:r,baseUrl:s,defaultUI:a}=e;Fe(n,"apiKey"),Ke(n,"apiKey"),Fe(o,"sku"),Ke(o,"sku"),Fe(i,"targetElement"),function(e,n){if(!(e instanceof HTMLElement))throw new t(`'${n}' must be an HTMLElement.`)}(i,"targetElement"),null!=r&&function(e,n,o){if("string"!=typeof e)throw new t(`'${n}' must be a string.`);if(!o.includes(e))throw new t(`'${n}' must be ${o.join("' or '")}.`)}(r,"initialState",He),null!=s&&Ke(s,"baseUrl"),null!=a&&function(e,n){if("boolean"!=typeof e)throw new t(`'${n}' must be a boolean.`)}(a,"defaultUI")}let Be=0,qe=0;function Xe(n){return e(this,void 0,void 0,(function*(){Ve(n);const{apiKey:o,sku:i,targetElement:r,initialState:s}=n;!function(e){const n=getComputedStyle(e);if(!["static","relative"].includes(n.position))throw new t(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${n.position}'.`)}(r),m.update(n.baseUrl);const a=function(n,o){return e(this,void 0,void 0,(function*(){const i=yield p((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:n,sku:o,sid:d()})}`).catch((e=>{throw console.error(e),new t("API unavailable.")}));if(!e.ok)throw new t(`API call failed with status ${e.status}.`);return e}))));try{return yield i.json()}catch(e){throw console.error(e),new t("API returned invalid body.")}}))}(o,i);null==g&&(g=y(`${m}/shopar-analytics.js`),g.then((()=>{E=!0,b=window.ShopAR__analytics.uuidv4(),C.forEach((e=>e()))}))),function(e,t){A.organizationId=e,A.sku=t,S=performance.now(),P((()=>{window.ShopAR__analytics.initializeImpl()}))}(o,i),Oe();const l=++qe,{category:c,arUrl:u,arKey:h,previewUrl:v,previewEnvUrl:w}=yield a;Ye(l),R("initialized");const I=null!=u&&function(e){return null!=e&&f.includes(e)}(c);I&&(null==_&&(_=y(`${m}/shopar-deepar.js`)),X(u),"Glasses"===c&&null==L&&(L=y(`${m}/shopar-true-scale.js`)));const x=null!=v;var M;x&&(null==F&&(F=y(`${m}/shopar-three.js`)),ue(v),(M=null!=w?w:`${m}/env/default.hdr`)!==B&&(B=M,V=N(M))),function(e){e.style.position="relative",null==Ge?e.appendChild($e):e!==Ge&&(Ge.removeChild($e),e.appendChild($e)),Ge=e}(r);const T=null==n.defaultUI||n.defaultUI;if(function(e,t,n){ze(xe,e),ze(_e,e&&t&&Ne===Pe.None),ze(Le,e&&n&&Ne===Pe.None),ze(De,e&&Ne!==Pe.None),e||We(Me),e||We(Te)}(T,I,x),Ne===Pe.AR){if(I&&!q){const e=yield D;Ye(l),yield J(e),Ye(l)}}else if(Ne===Pe.Preview&&x&&!ce){const e=yield K;Ye(l);const t=yield V;Ye(l);const n=yield pe(e,t);Ye(l),fe(n)}const z=()=>e(this,void 0,void 0,(function*(){if(!I)throw new t("Model does not have AR enabled.");if(Ne===Pe.AR)throw new t("AR already launched.");const n=++Be;Ne===Pe.Preview&&x&&!ce&&ve(),Ue(Pe.AR),T&&(ze(_e,!1),ze(Le,!1),ze(De,!0)),R("vto_launched"),T&&(ze(Te,!1),ze(Me,!0));const o=yield D;if(Be===n&&qe===l&&(yield _,Be===n&&qe===l)){try{yield Y(ke,h,c)}catch(e){throw function(e){R("vto_camera_denied",{error:e})}(e),T&&(ze(Me,!1),ze(Te,!0)),e}Be===n&&qe===l&&(yield J(o),Be===n&&qe===l&&(yield function(){return e(this,void 0,void 0,(function*(){if(null!=U){if(q=!1,null==j){const e=Z(G),t=$(e);j=yield k(t),yield ee(U,j,"user"===e)}U.setPaused(q),O.style.display=""}}))}(),Be===n&&qe===l&&T&&ze(Me,!1)))}})),W=()=>e(this,void 0,void 0,(function*(){if(!x)throw new t("Model does not have 3D enabled.");if(Ne===Pe.Preview)throw new t("3D already launched.");const e=++Be;Ne===Pe.AR&&I&&!q&&Q(),Ue(Pe.Preview),T&&(ze(_e,!1),ze(Le,!1),ze(De,!0)),R("preview_launched"),T&&(ze(Te,!1),ze(Me,!0));const n=yield K;if(Be!==e||qe!==l)return;const o=yield V;if(Be!==e||qe!==l)return;if(yield F,Be!==e||qe!==l)return;if(yield he(Ie,c),Be!==e||qe!==l)return;const i=yield pe(n,o);Be===e&&qe===l&&(fe(i),ce=!1,we(),te.style.display="",T&&ze(Me,!1))})),H=()=>e(this,void 0,void 0,(function*(){if(Ne===Pe.None)throw new t("Neither AR or 3D launched.");++Be,Ne===Pe.AR?I&&!q&&Q():Ne===Pe.Preview&&x&&!ce&&ve(),Ue(Pe.None),T&&(ze(Me,!1),ze(Te,!1),ze(De,!1),I&&ze(_e,!0),x&&ze(Le,!0))}));return T&&(I&&(!function(e){const t={Glasses:"glasses.svg",Shoes:"shoe.svg",Watches:"watch.svg"},n=`${m}/img/icons/${null!=e&&t[e]||t.Glasses}`;_e.firstChild.firstChild.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",n)}(c),_e.onclick=z),x&&(Le.onclick=W),De.onclick=H),"AR"===s?(yield z(),Ye(l)):"3D"===s&&(yield W(),Ye(l)),{launchAR:z,launch3D:W,closeAR:()=>e(this,void 0,void 0,(function*(){if(Ne!==Pe.AR)throw new t("AR not launched.");++Be,I&&!q&&Q(),Ue(Pe.None),T&&(ze(Me,!1),ze(Te,!1),ze(De,!1),I&&ze(_e,!0),x&&ze(Le,!0))})),close3D:()=>e(this,void 0,void 0,(function*(){if(Ne!==Pe.Preview)throw new t("3D not launched.");++Be,x&&!ce&&ve(),Ue(Pe.None),T&&(ze(Me,!1),ze(Te,!1),ze(De,!1),I&&ze(_e,!0),x&&ze(Le,!0))})),close:H}}))}function Ye(e){if(qe!==e)throw new t("Setup cancelled. Please ensure that setup is only called once.")}const Je={setup:function(n){return e(this,void 0,void 0,(function*(){return function(n,o){return e(this,void 0,void 0,(function*(){try{return o()}catch(e){throw e instanceof Error?new t(`${n} failed: ${e.message}`):(console.error(e),new t(`${n} failed.`))}}))}("setup",(()=>Xe(n)))}))},version:v};export{Je as plugin}; | ||
function e(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{l(o.next(e))}catch(e){r(e)}}function a(e){try{l(o.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}l((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}const n=[Uint8Array,Uint32Array],o=n.length-2,i=["addEventListener","alert","atob","blur","btoa","cancelAnimationFrame","localStorage","location","locationbar","hash","hasOwnProperty","host","hostname","href","requestAnimationFrame"],r=[2,12,7,1],s=window[i[r[r[o]]]][i[r[r[r.length-1]]]],a=s.charCodeAt(o),l=a^a,c=new n[1]([l]);function d(){const e=(new TextEncoder).encode(s),t=i.map((e=>e.length));for(let n=0;n<e.length;n++)e[n]^=t[n%t.length];return window[i[t[2]]](String.fromCodePoint(...e))}const u=[1e3,2e3,4e3],h=u.length;function p(t,n=0){return e(this,void 0,void 0,(function*(){try{return yield t()}catch(e){if(n>=h)throw e;return yield new Promise((e=>setTimeout(e,u[n]))),p(t,n+1)}}))}const f=["Glasses","Shoes","Watches"];const v="0.0.5";let w=`https://cdn.jsdelivr.net/npm/shopar-plugin@${v}/dist`;const m={update:e=>{null!=e&&(w=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>w};function y(t){return e(this,void 0,void 0,(function*(){if("function"!=typeof importScripts){const e=document.createElement("script");return e.setAttribute("src",t),e.setAttribute("crossorigin","anonymous"),new Promise((t=>{e.addEventListener("load",(()=>t()),!1),e.addEventListener("error",(()=>t()),!1),document.body.appendChild(e)}))}importScripts(t.toString())}))}let g,b,E=!1;const C=[],A={organizationId:"Unknown",sku:"Unknown"};let S;function R(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),A),{vtoTime:(performance.now()-S)/1e3,vtoSessionId:b}),P((()=>{window.ShopAR__analytics.trackEventImpl(e,t)}))}function P(e){E?e():C.push(e)}function $(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}function k(t){return e(this,void 0,void 0,(function*(){const e=document.createElement("video");return e.setAttribute("playsinline","playsinline"),e.srcObject=yield t,e}))}const I=200,x=10;let _,L,D,M,T=!1;function N(n){return e(this,void 0,void 0,(function*(){const o=yield p((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(n).catch((e=>{throw console.error(e),new t("Resource unavailable.")}));if(!e.ok)throw new t(`Resource download failed with status ${e.status}.`);return e}))));try{return yield(yield o.blob()).arrayBuffer()}catch(e){throw console.error(e),new t("Resource has invalid body.")}}))}let U,j,G,O,z,W,F,K,H,V,B,q=!0;function X(e){e!==M&&(M=e,D=N(e),W=null,null==U||U.clearEffect(),T=!0)}function Y(n,o,i){return e(this,void 0,void 0,(function*(){return G=i,O=n,null==z&&(z=(()=>e(this,void 0,void 0,(function*(){if(null==(null===(r=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===r?void 0:r.getUserMedia))throw new t("No camera available!");var r;const s=Z(i),a=$(s);R("vto_camera_asked");const l=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i);U=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:o||"your_license_key_goes_here",previewElement:n,additionalOptions:{hint:l,cameraConfig:{disableDefaultCamera:!0}}}),j=yield k(a),R("vto_camera_granted"),yield ee(U,j,"user"===s),"Glasses"===i&&(yield L,function(t,n){e(this,void 0,void 0,(function*(){T=!1;const{ShopAR__TrueScale:o}=window;yield o.initialize(`${m}/wasm/mediapipe`,t);const i=[],r=setInterval((()=>e(this,void 0,void 0,(function*(){if(T)return void clearInterval(r);const{error:e,faceWidth:t}=yield o.predict(performance.now());if(T)return void clearInterval(r);if(null!=e)return void console.error(`TrueScale predict error: ${e}`);if(i.length<x)return void i.push(t);clearInterval(r);const s=137/(function(e){let t=0;const n=e.length;for(let o=0;o<n;o++)t+=e[o];return t/n}(i)+5);n.changeParameterVector("GLASSES","","scale",s,s,s,0),n.changeParameterVector("shopar_glasses","","scale",s,s,s,0)}))),I)}))}(j,U)),R("vto_init_success")})))()),z}))}function J(t){return e(this,void 0,void 0,(function*(){return null==W&&(W=U.switchEffect(t)),W}))}function Q(){null!=U&&(q=!0,U.setPaused(q),null!=j&&null!=j.srcObject&&j.srcObject instanceof MediaStream&&(j.srcObject.getTracks().forEach((e=>e.stop())),j=null),U.stopCamera(),O.style.display="none")}function Z(e){var t;return null==e?"user":null!==(t={Glasses:"user",Shoes:"environment",Watches:"environment"}[e])&&void 0!==t?t:"user"}function ee(t,n,o){return e(this,void 0,void 0,(function*(){yield new Promise((i=>{n.onloadedmetadata=()=>{n.play().then((()=>e(this,void 0,void 0,(function*(){t.setVideoElement(n,o),i()}))))}}))}))}let te,ne,oe,ie,re,se,ae,le,ce=!1,de=!1;function ue(o){if(o!==H){const i=255;H=o,K=function(o,i,r){return e(this,void 0,void 0,(function*(){let s="glb".charCodeAt(0),a="3d".charCodeAt(0);s>>=1,s+=a;const d=String.fromCharCode(s+14);a>>=2,s^=a+2;const u=String.fromCharCode(s-3);s^=5;const h=String.fromCharCode(s-7);s^=2;const f=String.fromCharCode(s-7),v=window[`${h}${u+d+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${f}`],w=yield p((()=>e(this,void 0,void 0,(function*(){const e=yield v(`${o}`).catch((()=>{throw new t("Unknown error.")}));if(!e.ok)throw new t("Unknown error.");return e})))),m=new n[0](yield(yield w.blob().catch((()=>{throw new t("Unknown error.")}))).arrayBuffer().catch((()=>{throw new t("Unknown error.")})));r+=a;const y=m.byteLength;if(c[l]=Math.min(76,i)*((s>>5)-1),152===c[l]&&(c[l]=y),r==l)return m;r=l,r^=l;for(let e=0,t=m.length;e<t;e++)c[l]^=c[r]<<13,c[l]^=c[r]>>17,c[l]^=c[r]<<5,m[e]=m[e]^c[l];return r^=c[0],r^=c[1],r^=c[2],m}))}(o,(new Date).getTime()+i,Math.random()*i),le=null,null==re||re.clear()}}function he(t,n){return e(this,void 0,void 0,(function*(){if(te=t,!de){ne=window.ShopAR__THREE,oe=ne.THREE,ie=new oe.WebGLRenderer({powerPreference:"high-performance",canvas:t,antialias:!0,alpha:!0}),ie.outputEncoding=oe.sRGBEncoding,ie.toneMapping=oe.ACESFilmicToneMapping,ie.setPixelRatio(window.devicePixelRatio||1),ie.setClearColor(new oe.Color(16777215)),re=new oe.Scene,se=new oe.PerspectiveCamera(25,1,.5);const e=function(e){var t;const n={Glasses:[67.5,15,90],Shoes:[-72.8,72.8,54.6],Watches:[0,-10,40],Handbags:[40,0,0],Rings:[0,0,10]};return null==e?n.Glasses:null!==(t=n[e])&&void 0!==t?t:n.Glasses}(n);se.position.set(e[0],e[1],e[2]),ae=new ne.OrbitControls(se,ie.domElement),ae.target.set(0,.03,0),ae.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>{R("preview_interacted")}),200);ae.addEventListener("end",e)}de=!0}me()}))}function pe(t,n){return e(this,void 0,void 0,(function*(){return null==le&&(le=(()=>e(this,void 0,void 0,(function*(){const e=new ne.RGBELoader,o=function(e,t){const n=e.THREE,o=new n.DataTexture(t.data,t.width,t.height,void 0,t.type,void 0,n.ClampToEdgeWrapping,n.ClampToEdgeWrapping,n.LinearFilter,n.LinearFilter,1,n.LinearEncoding);return o.flipY=!0,o.generateMipmaps=!1,o.needsUpdate=!0,o}(ne,e.parse(n));o.mapping=oe.EquirectangularReflectionMapping,o.encoding=oe.LinearEncoding,re.environment=o;const i=(new ne.GLTFLoader).setDRACOLoader((new ne.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${oe.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new ne.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${oe.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(ie)).setMeshoptDecoder("function"==typeof ne.MeshoptDecoder?ne.MeshoptDecoder():ne.MeshoptDecoder);return yield i.parseAsync(t.buffer,"")})))()),le}))}function fe(t){return e(this,void 0,void 0,(function*(){re.add(t.scene),re.traverse((e=>{if(e instanceof oe.Mesh){const t=e.material;t.depthWrite=!0,t instanceof oe.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))}function ve(){ce=!0,te.style.display="none"}function we(){ce||(requestAnimationFrame(we),me(),ae.update(),ie.render(re,se))}function me(){const e=te.clientWidth,t=te.clientHeight;te.width===e&&te.height===t||(ie.setSize(e,t,!1),se.aspect=e/t,se.updateProjectionMatrix())}function ye(e,t){const n=document.createElementNS("http://www.w3.org/2000/svg","svg");n.setAttribute("width",e),n.setAttribute("height",e);{const o=document.createElementNS("http://www.w3.org/2000/svg","image");o.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",t),o.setAttribute("width",e),o.setAttribute("height",e),n.appendChild(o)}return n}const ge="shopar-error";function be(e,t,n,o){const i=document.createElement("button");i.id=e,i.type="button",i.className="shopar-btn";{const e=ye("1.75rem",o);i.appendChild(e)}{const e=document.createElement("span");e.textContent=t,i.appendChild(e)}i.ariaLabel=n;const r=i.style;return r.zIndex="2",r.display="none",i}const Ee="shopar-control";const Ce="shopar-deepar-output";const Ae="shopar-loading";const Se="shopar-main";const Re="shopar-three-output";var Pe;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(Pe||(Pe={}));let $e,ke,Ie,xe,_e,Le,De,Me,Te,Ne=Pe.None;function Ue(e){Ne=e}let je=!1,Ge=null;function Oe(){je||($e=function(){const e=document.createElement("div");e.id=Se;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}(),ke=function(){const e=document.createElement("div");e.id=Ce;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),$e.appendChild(ke),Ie=function(){const e=document.createElement("canvas");e.id=Re;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),$e.appendChild(Ie),xe=function(){const e=document.createElement("div");return e.id=Ee,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e.style.display="none",e}(),$e.appendChild(xe),_e=be("shopar-btn-vto","Try-on","Launch ShopAR virtual try-on","data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22/%3E"),xe.appendChild(_e),Le=be("shopar-btn-3d","3D","Launch ShopAR 3D preview",`${m}/img/icons/cube.svg`),xe.appendChild(Le),De=be("shopar-btn-close","Close","Close ShopAR view",`${m}/img/icons/close.svg`),xe.append(De),Me=function(){const e=document.createElement("div");e.id=Ae;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=document.createElement("div");return i.className="shopar-spinner",n.appendChild(i),e}(),$e.appendChild(Me),Te=function(){const e=document.createElement("div");e.id=ge;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.flexDirection="column",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=ye("4rem",`${m}/img/icons/close.svg`);n.appendChild(i);const r=document.createElement("div");r.className="shopar-error-title",r.textContent="Camera Error",n.appendChild(r);const s=document.createElement("div");return s.className="shopar-error-body",s.textContent="Please refresh the page and allow the use of camera.",n.appendChild(s),e}(),$e.appendChild(Te),je=!0)}function ze(e,t){e.style.display=t?"":"none"}function We(e){""===e.style.display&&ze(e,!1)}function Fe(e,n){if(null==e)throw new t(`'${n}' not specified.`)}function Ke(e,n){if("string"!=typeof e)throw new t(`'${n}' must be a string.`)}const He=["AR","3D"];function Ve(e){const{apiKey:n,sku:o,targetElement:i,initialState:r,baseUrl:s,defaultUI:a}=e;Fe(n,"apiKey"),Ke(n,"apiKey"),Fe(o,"sku"),Ke(o,"sku"),Fe(i,"targetElement"),function(e,n){if(!(e instanceof HTMLElement))throw new t(`'${n}' must be an HTMLElement.`)}(i,"targetElement"),null!=r&&function(e,n,o){if("string"!=typeof e)throw new t(`'${n}' must be a string.`);if(!o.includes(e))throw new t(`'${n}' must be ${o.join("' or '")}.`)}(r,"initialState",He),null!=s&&Ke(s,"baseUrl"),null!=a&&function(e,n){if("boolean"!=typeof e)throw new t(`'${n}' must be a boolean.`)}(a,"defaultUI")}let Be=0,qe=0;function Xe(n){return e(this,void 0,void 0,(function*(){Ve(n);const{apiKey:o,sku:i,targetElement:r,initialState:s}=n;!function(e){const n=getComputedStyle(e);if(!["static","relative"].includes(n.position))throw new t(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${n.position}'.`)}(r),m.update(n.baseUrl);const a=function(n,o){return e(this,void 0,void 0,(function*(){const i=yield p((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:n,sku:o,sid:d()})}`).catch((e=>{throw console.error(e),new t("API unavailable.")}));if(!e.ok)throw new t(`API call failed with status ${e.status}.`);return e}))));try{return yield i.json()}catch(e){throw console.error(e),new t("API returned invalid body.")}}))}(o,i);null==g&&(g=y(`${m}/shopar-analytics.js`),g.then((()=>{E=!0,b=window.ShopAR__analytics.uuidv4(),C.forEach((e=>e()))}))),function(e,t){A.organizationId=e,A.sku=t,S=performance.now(),P((()=>{window.ShopAR__analytics.initializeImpl()}))}(o,i),Oe();const l=++qe,{category:c,arUrl:u,arKey:h,previewUrl:v,previewEnvUrl:w}=yield a;Ye(l),R("initialized");const I=null!=u&&function(e){return null!=e&&f.includes(e)}(c);I&&(null==_&&(_=y(`${m}/shopar-deepar.js`)),X(u),"Glasses"===c&&null==L&&(L=y(`${m}/shopar-true-scale.js`)));const x=null!=v;var M;x&&(null==F&&(F=y(`${m}/shopar-three.js`)),ue(v),(M=null!=w?w:`${m}/env/default.hdr`)!==B&&(B=M,V=N(M))),function(e){e.style.position="relative",null==Ge?e.appendChild($e):e!==Ge&&(Ge.removeChild($e),e.appendChild($e)),Ge=e}(r);const T=null==n.defaultUI||n.defaultUI;if(function(e,t,n){ze(xe,e),ze(_e,e&&t&&Ne===Pe.None),ze(Le,e&&n&&Ne===Pe.None),ze(De,e&&Ne!==Pe.None),e||We(Me),e||We(Te)}(T,I,x),Ne===Pe.AR){if(I&&!q){const e=yield D;Ye(l),yield J(e),Ye(l)}}else if(Ne===Pe.Preview&&x&&!ce){const e=yield K;Ye(l);const t=yield V;Ye(l);const n=yield pe(e,t);Ye(l),fe(n)}const z=()=>e(this,void 0,void 0,(function*(){if(!I)throw new t("Model does not have AR enabled.");if(Ne===Pe.AR)throw new t("AR already launched.");const n=++Be;Ne===Pe.Preview&&x&&!ce&&ve(),Ue(Pe.AR),T&&(ze(_e,!1),ze(Le,!1),ze(De,!0)),R("vto_launched"),T&&(ze(Te,!1),ze(Me,!0));const o=yield D;if(Be===n&&qe===l&&(yield _,Be===n&&qe===l)){try{yield Y(ke,h,c)}catch(e){throw function(e){R("vto_camera_denied",{error:e})}(e),T&&(ze(Me,!1),ze(Te,!0)),e}Be===n&&qe===l&&(yield J(o),Be===n&&qe===l&&(yield function(){return e(this,void 0,void 0,(function*(){if(null!=U){if(q=!1,null==j){const e=Z(G),t=$(e);j=yield k(t),yield ee(U,j,"user"===e)}U.setPaused(q),O.style.display=""}}))}(),Be===n&&qe===l&&T&&ze(Me,!1)))}})),W=()=>e(this,void 0,void 0,(function*(){if(!x)throw new t("Model does not have 3D enabled.");if(Ne===Pe.Preview)throw new t("3D already launched.");const e=++Be;Ne===Pe.AR&&I&&!q&&Q(),Ue(Pe.Preview),T&&(ze(_e,!1),ze(Le,!1),ze(De,!0)),R("preview_launched"),T&&(ze(Te,!1),ze(Me,!0));const n=yield K;if(Be!==e||qe!==l)return;const o=yield V;if(Be!==e||qe!==l)return;if(yield F,Be!==e||qe!==l)return;if(yield he(Ie,c),Be!==e||qe!==l)return;const i=yield pe(n,o);Be===e&&qe===l&&(fe(i),ce=!1,we(),te.style.display="",T&&ze(Me,!1))})),H=()=>e(this,void 0,void 0,(function*(){if(Ne===Pe.None)throw new t("Neither AR or 3D launched.");++Be,Ne===Pe.AR?I&&!q&&Q():Ne===Pe.Preview&&x&&!ce&&ve(),Ue(Pe.None),T&&(ze(Me,!1),ze(Te,!1),ze(De,!1),I&&ze(_e,!0),x&&ze(Le,!0))}));return T&&(I&&(!function(e){const t={Glasses:"glasses.svg",Shoes:"shoe.svg",Watches:"watch.svg"},n=`${m}/img/icons/${null!=e&&t[e]||t.Glasses}`;_e.firstChild.firstChild.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",n)}(c),_e.onclick=z),x&&(Le.onclick=W),De.onclick=H),"AR"===s?(yield z(),Ye(l)):"3D"===s&&(yield W(),Ye(l)),{launchAR:z,launch3D:W,closeAR:()=>e(this,void 0,void 0,(function*(){if(Ne!==Pe.AR)throw new t("AR not launched.");++Be,I&&!q&&Q(),Ue(Pe.None),T&&(ze(Me,!1),ze(Te,!1),ze(De,!1),I&&ze(_e,!0),x&&ze(Le,!0))})),close3D:()=>e(this,void 0,void 0,(function*(){if(Ne!==Pe.Preview)throw new t("3D not launched.");++Be,x&&!ce&&ve(),Ue(Pe.None),T&&(ze(Me,!1),ze(Te,!1),ze(De,!1),I&&ze(_e,!0),x&&ze(Le,!0))})),close:H}}))}function Ye(e){if(qe!==e)throw new t("Setup cancelled. Please ensure that setup is only called once.")}const Je={setup:function(n){return e(this,void 0,void 0,(function*(){return function(n,o){return e(this,void 0,void 0,(function*(){try{return o()}catch(e){throw e instanceof Error?new t(`${n} failed: ${e.message}`):(console.error(e),new t(`${n} failed.`))}}))}("setup",(()=>Xe(n)))}))},version:v};export{Je as plugin}; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ShopAR={})}(this,(function(e){"use strict";function t(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{l(o.next(e))}catch(e){r(e)}}function a(e){try{l(o.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}l((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class n extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}const o=[Uint8Array,Uint32Array],i=o.length-2,r=["addEventListener","alert","atob","blur","btoa","cancelAnimationFrame","localStorage","location","locationbar","hash","hasOwnProperty","host","hostname","href","requestAnimationFrame"],s=[2,12,7,1],a=window[r[s[s[i]]]][r[s[s[s.length-1]]]],l=a.charCodeAt(i),c=l^l,d=new o[1]([c]);function u(){const e=(new TextEncoder).encode(a),t=r.map((e=>e.length));for(let n=0;n<e.length;n++)e[n]^=t[n%t.length];return window[r[t[2]]](String.fromCodePoint(...e))}const h=[1e3,2e3,4e3],f=h.length;function p(e,n=0){return t(this,void 0,void 0,(function*(){try{return yield e()}catch(t){if(n>=f)throw t;return yield new Promise((e=>setTimeout(e,h[n]))),p(e,n+1)}}))}const v=["Glasses","Shoes","Watches"];const w="0.0.5-alpha.6";let m=`https://cdn.jsdelivr.net/npm/shopar-plugin@${w}/dist`;const y={update:e=>{null!=e&&(m=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>m};function g(e){return t(this,void 0,void 0,(function*(){if("function"!=typeof importScripts){const t=document.createElement("script");return t.setAttribute("src",e),t.setAttribute("crossorigin","anonymous"),new Promise((e=>{t.addEventListener("load",(()=>e()),!1),t.addEventListener("error",(()=>e()),!1),document.body.appendChild(t)}))}importScripts(e.toString())}))}let b,E,C=!1;const A=[],S={organizationId:"Unknown",sku:"Unknown"};let R;function P(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),S),{vtoTime:(performance.now()-R)/1e3,vtoSessionId:E}),$((()=>{window.ShopAR__analytics.trackEventImpl(e,t)}))}function $(e){C?e():A.push(e)}function x(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}function k(e){return t(this,void 0,void 0,(function*(){const t=document.createElement("video");return t.setAttribute("playsinline","playsinline"),t.srcObject=yield e,t}))}const I=200,_=10;let T,L,D,M,N=!1;function U(e){return t(this,void 0,void 0,(function*(){const o=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(e).catch((e=>{throw console.error(e),new n("Resource unavailable.")}));if(!t.ok)throw new n(`Resource download failed with status ${t.status}.`);return t}))));try{return yield(yield o.blob()).arrayBuffer()}catch(e){throw console.error(e),new n("Resource has invalid body.")}}))}let j,G,O,z,W,F,K,H,V,B,q,X=!0;function Y(e){e!==M&&(M=e,D=U(e),F=null,null==j||j.clearEffect(),N=!0)}function J(e,o,i){return t(this,void 0,void 0,(function*(){return O=i,z=e,null==W&&(W=(()=>t(this,void 0,void 0,(function*(){if(null==(null===(r=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===r?void 0:r.getUserMedia))throw new n("No camera available!");var r;const s=ee(i),a=x(s);P("vto_camera_asked");const l=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i);j=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:o||"your_license_key_goes_here",previewElement:e,additionalOptions:{hint:l,cameraConfig:{disableDefaultCamera:!0}}}),G=yield k(a),P("vto_camera_granted"),yield te(j,G,"user"===s),"Glasses"===i&&(yield L,function(e,n){t(this,void 0,void 0,(function*(){N=!1;const{ShopAR__TrueScale:o}=window;yield o.initialize(`${y}/wasm/mediapipe`,e);const i=[],r=setInterval((()=>t(this,void 0,void 0,(function*(){if(N)return void clearInterval(r);const{error:e,faceWidth:t}=yield o.predict(performance.now());if(N)return void clearInterval(r);if(null!=e)return void console.error(`TrueScale predict error: ${e}`);if(i.length<_)return void i.push(t);clearInterval(r);const s=137/(function(e){let t=0;const n=e.length;for(let o=0;o<n;o++)t+=e[o];return t/n}(i)+5);n.changeParameterVector("GLASSES","","scale",s,s,s,0),n.changeParameterVector("shopar_glasses","","scale",s,s,s,0)}))),I)}))}(G,j)),P("vto_init_success")})))()),W}))}function Q(e){return t(this,void 0,void 0,(function*(){return null==F&&(F=j.switchEffect(e)),F}))}function Z(){null!=j&&(X=!0,j.setPaused(X),null!=G&&null!=G.srcObject&&G.srcObject instanceof MediaStream&&(G.srcObject.getTracks().forEach((e=>e.stop())),G=null),j.stopCamera(),z.style.display="none")}function ee(e){var t;return null==e?"user":null!==(t={Glasses:"user",Shoes:"environment",Watches:"environment"}[e])&&void 0!==t?t:"user"}function te(e,n,o){return t(this,void 0,void 0,(function*(){yield new Promise((i=>{n.onloadedmetadata=()=>{n.play().then((()=>t(this,void 0,void 0,(function*(){e.setVideoElement(n,o),i()}))))}}))}))}let ne,oe,ie,re,se,ae,le,ce,de=!1,ue=!1;function he(e){if(e!==V){const i=255;V=e,H=function(e,i,r){return t(this,void 0,void 0,(function*(){let s="glb".charCodeAt(0),a="3d".charCodeAt(0);s>>=1,s+=a;const l=String.fromCharCode(s+14);a>>=2,s^=a+2;const u=String.fromCharCode(s-3);s^=5;const h=String.fromCharCode(s-7);s^=2;const f=String.fromCharCode(s-7),v=window[`${h}${u+l+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${f}`],w=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield v(`${e}`).catch((()=>{throw new n("Unknown error.")}));if(!t.ok)throw new n("Unknown error.");return t})))),m=new o[0](yield(yield w.blob().catch((()=>{throw new n("Unknown error.")}))).arrayBuffer().catch((()=>{throw new n("Unknown error.")})));r+=a;const y=m.byteLength;if(d[c]=Math.min(76,i)*((s>>5)-1),152===d[c]&&(d[c]=y),r==c)return m;r=c,r^=c;for(let e=0,t=m.length;e<t;e++)d[c]^=d[r]<<13,d[c]^=d[r]>>17,d[c]^=d[r]<<5,m[e]=m[e]^d[c];return r^=d[0],r^=d[1],r^=d[2],m}))}(e,(new Date).getTime()+i,Math.random()*i),ce=null,null==se||se.clear()}}function fe(e,n){return t(this,void 0,void 0,(function*(){if(ne=e,!ue){oe=window.ShopAR__THREE,ie=oe.THREE,re=new ie.WebGLRenderer({powerPreference:"high-performance",canvas:e,antialias:!0,alpha:!0}),re.outputEncoding=ie.sRGBEncoding,re.toneMapping=ie.ACESFilmicToneMapping,re.setPixelRatio(window.devicePixelRatio||1),re.setClearColor(new ie.Color(16777215)),se=new ie.Scene,ae=new ie.PerspectiveCamera(25,1,.5);const t=function(e){var t;const n={Glasses:[67.5,15,90],Shoes:[-72.8,72.8,54.6],Watches:[0,-10,40],Handbags:[40,0,0],Rings:[0,0,10]};return null==e?n.Glasses:null!==(t=n[e])&&void 0!==t?t:n.Glasses}(n);ae.position.set(t[0],t[1],t[2]),le=new oe.OrbitControls(ae,re.domElement),le.target.set(0,.03,0),le.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>{P("preview_interacted")}),200);le.addEventListener("end",e)}ue=!0}ye()}))}function pe(e,n){return t(this,void 0,void 0,(function*(){return null==ce&&(ce=(()=>t(this,void 0,void 0,(function*(){const t=new oe.RGBELoader,o=function(e,t){const n=e.THREE,o=new n.DataTexture(t.data,t.width,t.height,void 0,t.type,void 0,n.ClampToEdgeWrapping,n.ClampToEdgeWrapping,n.LinearFilter,n.LinearFilter,1,n.LinearEncoding);return o.flipY=!0,o.generateMipmaps=!1,o.needsUpdate=!0,o}(oe,t.parse(n));o.mapping=ie.EquirectangularReflectionMapping,o.encoding=ie.LinearEncoding,se.environment=o;const i=(new oe.GLTFLoader).setDRACOLoader((new oe.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${ie.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new oe.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${ie.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(re)).setMeshoptDecoder("function"==typeof oe.MeshoptDecoder?oe.MeshoptDecoder():oe.MeshoptDecoder);return yield i.parseAsync(e.buffer,"")})))()),ce}))}function ve(e){return t(this,void 0,void 0,(function*(){se.add(e.scene),se.traverse((e=>{if(e instanceof ie.Mesh){const t=e.material;t.depthWrite=!0,t instanceof ie.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))}function we(){de=!0,ne.style.display="none"}function me(){de||(requestAnimationFrame(me),ye(),le.update(),re.render(se,ae))}function ye(){const e=ne.clientWidth,t=ne.clientHeight;ne.width===e&&ne.height===t||(re.setSize(e,t,!1),ae.aspect=e/t,ae.updateProjectionMatrix())}function ge(e,t){const n=document.createElementNS("http://www.w3.org/2000/svg","svg");n.setAttribute("width",e),n.setAttribute("height",e);{const o=document.createElementNS("http://www.w3.org/2000/svg","image");o.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",t),o.setAttribute("width",e),o.setAttribute("height",e),n.appendChild(o)}return n}const be="shopar-error";function Ee(e,t,n,o){const i=document.createElement("button");i.id=e,i.type="button",i.className="shopar-btn";{const e=ge("1.75rem",o);i.appendChild(e)}{const e=document.createElement("span");e.textContent=t,i.appendChild(e)}i.ariaLabel=n;const r=i.style;return r.zIndex="2",r.display="none",i}const Ce="shopar-control";const Ae="shopar-deepar-output";const Se="shopar-loading";const Re="shopar-main";const Pe="shopar-three-output";var $e;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}($e||($e={}));let xe,ke,Ie,_e,Te,Le,De,Me,Ne,Ue=$e.None;function je(e){Ue=e}let Ge=!1,Oe=null;function ze(){Ge||(xe=function(){const e=document.createElement("div");e.id=Re;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}(),ke=function(){const e=document.createElement("div");e.id=Ae;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),xe.appendChild(ke),Ie=function(){const e=document.createElement("canvas");e.id=Pe;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),xe.appendChild(Ie),_e=function(){const e=document.createElement("div");return e.id=Ce,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e.style.display="none",e}(),xe.appendChild(_e),Te=Ee("shopar-btn-vto","Try-on","Launch ShopAR virtual try-on","data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22/%3E"),_e.appendChild(Te),Le=Ee("shopar-btn-3d","3D","Launch ShopAR 3D preview",`${y}/img/icons/cube.svg`),_e.appendChild(Le),De=Ee("shopar-btn-close","Close","Close ShopAR view",`${y}/img/icons/close.svg`),_e.append(De),Me=function(){const e=document.createElement("div");e.id=Se;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=document.createElement("div");return i.className="shopar-spinner",n.appendChild(i),e}(),xe.appendChild(Me),Ne=function(){const e=document.createElement("div");e.id=be;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.flexDirection="column",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=ge("4rem",`${y}/img/icons/close.svg`);n.appendChild(i);const r=document.createElement("div");r.className="shopar-error-title",r.textContent="Camera Error",n.appendChild(r);const s=document.createElement("div");return s.className="shopar-error-body",s.textContent="Please refresh the page and allow the use of camera.",n.appendChild(s),e}(),xe.appendChild(Ne),Ge=!0)}function We(e,t){e.style.display=t?"":"none"}function Fe(e){""===e.style.display&&We(e,!1)}function Ke(e,t){if(null==e)throw new n(`'${t}' not specified.`)}function He(e,t){if("string"!=typeof e)throw new n(`'${t}' must be a string.`)}const Ve=["AR","3D"];function Be(e){const{apiKey:t,sku:o,targetElement:i,initialState:r,baseUrl:s,defaultUI:a}=e;Ke(t,"apiKey"),He(t,"apiKey"),Ke(o,"sku"),He(o,"sku"),Ke(i,"targetElement"),function(e,t){if(!(e instanceof HTMLElement))throw new n(`'${t}' must be an HTMLElement.`)}(i,"targetElement"),null!=r&&function(e,t,o){if("string"!=typeof e)throw new n(`'${t}' must be a string.`);if(!o.includes(e))throw new n(`'${t}' must be ${o.join("' or '")}.`)}(r,"initialState",Ve),null!=s&&He(s,"baseUrl"),null!=a&&function(e,t){if("boolean"!=typeof e)throw new n(`'${t}' must be a boolean.`)}(a,"defaultUI")}let qe=0,Xe=0;function Ye(e){return t(this,void 0,void 0,(function*(){Be(e);const{apiKey:o,sku:i,targetElement:r,initialState:s}=e;!function(e){const t=getComputedStyle(e);if(!["static","relative"].includes(t.position))throw new n(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${t.position}'.`)}(r),y.update(e.baseUrl);const a=function(e,o){return t(this,void 0,void 0,(function*(){const i=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:e,sku:o,sid:u()})}`).catch((e=>{throw console.error(e),new n("API unavailable.")}));if(!t.ok)throw new n(`API call failed with status ${t.status}.`);return t}))));try{return yield i.json()}catch(e){throw console.error(e),new n("API returned invalid body.")}}))}(o,i);null==b&&(b=g(`${y}/shopar-analytics.js`),b.then((()=>{C=!0,E=window.ShopAR__analytics.uuidv4(),A.forEach((e=>e()))}))),function(e,t){S.organizationId=e,S.sku=t,R=performance.now(),$((()=>{window.ShopAR__analytics.initializeImpl()}))}(o,i),ze();const l=++Xe,{category:c,arUrl:d,arKey:h,previewUrl:f,previewEnvUrl:w}=yield a;Je(l),P("initialized");const m=null!=d&&function(e){return null!=e&&v.includes(e)}(c);m&&(null==T&&(T=g(`${y}/shopar-deepar.js`)),Y(d),"Glasses"===c&&null==L&&(L=g(`${y}/shopar-true-scale.js`)));const I=null!=f;var _;I&&(null==K&&(K=g(`${y}/shopar-three.js`)),he(f),(_=null!=w?w:`${y}/env/default.hdr`)!==q&&(q=_,B=U(_))),function(e){e.style.position="relative",null==Oe?e.appendChild(xe):e!==Oe&&(Oe.removeChild(xe),e.appendChild(xe)),Oe=e}(r);const M=null==e.defaultUI||e.defaultUI;if(function(e,t,n){We(_e,e),We(Te,e&&t&&Ue===$e.None),We(Le,e&&n&&Ue===$e.None),We(De,e&&Ue!==$e.None),e||Fe(Me),e||Fe(Ne)}(M,m,I),Ue===$e.AR){if(m&&!X){const e=yield D;Je(l),yield Q(e),Je(l)}}else if(Ue===$e.Preview&&I&&!de){const e=yield H;Je(l);const t=yield B;Je(l);const n=yield pe(e,t);Je(l),ve(n)}const N=()=>t(this,void 0,void 0,(function*(){if(!m)throw new n("Model does not have AR enabled.");if(Ue===$e.AR)throw new n("AR already launched.");const e=++qe;Ue===$e.Preview&&I&&!de&&we(),je($e.AR),M&&(We(Te,!1),We(Le,!1),We(De,!0)),P("vto_launched"),M&&(We(Ne,!1),We(Me,!0));const o=yield D;if(qe===e&&Xe===l&&(yield T,qe===e&&Xe===l)){try{yield J(ke,h,c)}catch(e){throw function(e){P("vto_camera_denied",{error:e})}(e),M&&(We(Me,!1),We(Ne,!0)),e}qe===e&&Xe===l&&(yield Q(o),qe===e&&Xe===l&&(yield function(){return t(this,void 0,void 0,(function*(){if(null!=j){if(X=!1,null==G){const e=ee(O),t=x(e);G=yield k(t),yield te(j,G,"user"===e)}j.setPaused(X),z.style.display=""}}))}(),qe===e&&Xe===l&&M&&We(Me,!1)))}})),W=()=>t(this,void 0,void 0,(function*(){if(!I)throw new n("Model does not have 3D enabled.");if(Ue===$e.Preview)throw new n("3D already launched.");const e=++qe;Ue===$e.AR&&m&&!X&&Z(),je($e.Preview),M&&(We(Te,!1),We(Le,!1),We(De,!0)),P("preview_launched"),M&&(We(Ne,!1),We(Me,!0));const t=yield H;if(qe!==e||Xe!==l)return;const o=yield B;if(qe!==e||Xe!==l)return;if(yield K,qe!==e||Xe!==l)return;if(yield fe(Ie,c),qe!==e||Xe!==l)return;const i=yield pe(t,o);qe===e&&Xe===l&&(ve(i),de=!1,me(),ne.style.display="",M&&We(Me,!1))})),F=()=>t(this,void 0,void 0,(function*(){if(Ue===$e.None)throw new n("Neither AR or 3D launched.");++qe,Ue===$e.AR?m&&!X&&Z():Ue===$e.Preview&&I&&!de&&we(),je($e.None),M&&(We(Me,!1),We(Ne,!1),We(De,!1),m&&We(Te,!0),I&&We(Le,!0))}));return M&&(m&&(!function(e){const t={Glasses:"glasses.svg",Shoes:"shoe.svg",Watches:"watch.svg"},n=`${y}/img/icons/${null!=e&&t[e]||t.Glasses}`;Te.firstChild.firstChild.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",n)}(c),Te.onclick=N),I&&(Le.onclick=W),De.onclick=F),"AR"===s?(yield N(),Je(l)):"3D"===s&&(yield W(),Je(l)),{launchAR:N,launch3D:W,closeAR:()=>t(this,void 0,void 0,(function*(){if(Ue!==$e.AR)throw new n("AR not launched.");++qe,m&&!X&&Z(),je($e.None),M&&(We(Me,!1),We(Ne,!1),We(De,!1),m&&We(Te,!0),I&&We(Le,!0))})),close3D:()=>t(this,void 0,void 0,(function*(){if(Ue!==$e.Preview)throw new n("3D not launched.");++qe,I&&!de&&we(),je($e.None),M&&(We(Me,!1),We(Ne,!1),We(De,!1),m&&We(Te,!0),I&&We(Le,!0))})),close:F}}))}function Je(e){if(Xe!==e)throw new n("Setup cancelled. Please ensure that setup is only called once.")}const Qe={setup:function(e){return t(this,void 0,void 0,(function*(){return function(e,o){return t(this,void 0,void 0,(function*(){try{return o()}catch(t){throw t instanceof Error?new n(`${e} failed: ${t.message}`):(console.error(t),new n(`${e} failed.`))}}))}("setup",(()=>Ye(e)))}))},version:w};e.plugin=Qe})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ShopAR={})}(this,(function(e){"use strict";function t(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{l(o.next(e))}catch(e){r(e)}}function a(e){try{l(o.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}l((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class n extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}const o=[Uint8Array,Uint32Array],i=o.length-2,r=["addEventListener","alert","atob","blur","btoa","cancelAnimationFrame","localStorage","location","locationbar","hash","hasOwnProperty","host","hostname","href","requestAnimationFrame"],s=[2,12,7,1],a=window[r[s[s[i]]]][r[s[s[s.length-1]]]],l=a.charCodeAt(i),c=l^l,d=new o[1]([c]);function u(){const e=(new TextEncoder).encode(a),t=r.map((e=>e.length));for(let n=0;n<e.length;n++)e[n]^=t[n%t.length];return window[r[t[2]]](String.fromCodePoint(...e))}const h=[1e3,2e3,4e3],f=h.length;function p(e,n=0){return t(this,void 0,void 0,(function*(){try{return yield e()}catch(t){if(n>=f)throw t;return yield new Promise((e=>setTimeout(e,h[n]))),p(e,n+1)}}))}const v=["Glasses","Shoes","Watches"];const w="0.0.5";let m=`https://cdn.jsdelivr.net/npm/shopar-plugin@${w}/dist`;const y={update:e=>{null!=e&&(m=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>m};function g(e){return t(this,void 0,void 0,(function*(){if("function"!=typeof importScripts){const t=document.createElement("script");return t.setAttribute("src",e),t.setAttribute("crossorigin","anonymous"),new Promise((e=>{t.addEventListener("load",(()=>e()),!1),t.addEventListener("error",(()=>e()),!1),document.body.appendChild(t)}))}importScripts(e.toString())}))}let b,E,C=!1;const A=[],S={organizationId:"Unknown",sku:"Unknown"};let R;function P(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),S),{vtoTime:(performance.now()-R)/1e3,vtoSessionId:E}),$((()=>{window.ShopAR__analytics.trackEventImpl(e,t)}))}function $(e){C?e():A.push(e)}function x(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}function k(e){return t(this,void 0,void 0,(function*(){const t=document.createElement("video");return t.setAttribute("playsinline","playsinline"),t.srcObject=yield e,t}))}const I=200,_=10;let T,L,D,M,N=!1;function U(e){return t(this,void 0,void 0,(function*(){const o=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(e).catch((e=>{throw console.error(e),new n("Resource unavailable.")}));if(!t.ok)throw new n(`Resource download failed with status ${t.status}.`);return t}))));try{return yield(yield o.blob()).arrayBuffer()}catch(e){throw console.error(e),new n("Resource has invalid body.")}}))}let j,G,O,z,W,F,K,H,V,B,q,X=!0;function Y(e){e!==M&&(M=e,D=U(e),F=null,null==j||j.clearEffect(),N=!0)}function J(e,o,i){return t(this,void 0,void 0,(function*(){return O=i,z=e,null==W&&(W=(()=>t(this,void 0,void 0,(function*(){if(null==(null===(r=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===r?void 0:r.getUserMedia))throw new n("No camera available!");var r;const s=ee(i),a=x(s);P("vto_camera_asked");const l=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i);j=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:o||"your_license_key_goes_here",previewElement:e,additionalOptions:{hint:l,cameraConfig:{disableDefaultCamera:!0}}}),G=yield k(a),P("vto_camera_granted"),yield te(j,G,"user"===s),"Glasses"===i&&(yield L,function(e,n){t(this,void 0,void 0,(function*(){N=!1;const{ShopAR__TrueScale:o}=window;yield o.initialize(`${y}/wasm/mediapipe`,e);const i=[],r=setInterval((()=>t(this,void 0,void 0,(function*(){if(N)return void clearInterval(r);const{error:e,faceWidth:t}=yield o.predict(performance.now());if(N)return void clearInterval(r);if(null!=e)return void console.error(`TrueScale predict error: ${e}`);if(i.length<_)return void i.push(t);clearInterval(r);const s=137/(function(e){let t=0;const n=e.length;for(let o=0;o<n;o++)t+=e[o];return t/n}(i)+5);n.changeParameterVector("GLASSES","","scale",s,s,s,0),n.changeParameterVector("shopar_glasses","","scale",s,s,s,0)}))),I)}))}(G,j)),P("vto_init_success")})))()),W}))}function Q(e){return t(this,void 0,void 0,(function*(){return null==F&&(F=j.switchEffect(e)),F}))}function Z(){null!=j&&(X=!0,j.setPaused(X),null!=G&&null!=G.srcObject&&G.srcObject instanceof MediaStream&&(G.srcObject.getTracks().forEach((e=>e.stop())),G=null),j.stopCamera(),z.style.display="none")}function ee(e){var t;return null==e?"user":null!==(t={Glasses:"user",Shoes:"environment",Watches:"environment"}[e])&&void 0!==t?t:"user"}function te(e,n,o){return t(this,void 0,void 0,(function*(){yield new Promise((i=>{n.onloadedmetadata=()=>{n.play().then((()=>t(this,void 0,void 0,(function*(){e.setVideoElement(n,o),i()}))))}}))}))}let ne,oe,ie,re,se,ae,le,ce,de=!1,ue=!1;function he(e){if(e!==V){const i=255;V=e,H=function(e,i,r){return t(this,void 0,void 0,(function*(){let s="glb".charCodeAt(0),a="3d".charCodeAt(0);s>>=1,s+=a;const l=String.fromCharCode(s+14);a>>=2,s^=a+2;const u=String.fromCharCode(s-3);s^=5;const h=String.fromCharCode(s-7);s^=2;const f=String.fromCharCode(s-7),v=window[`${h}${u+l+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${f}`],w=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield v(`${e}`).catch((()=>{throw new n("Unknown error.")}));if(!t.ok)throw new n("Unknown error.");return t})))),m=new o[0](yield(yield w.blob().catch((()=>{throw new n("Unknown error.")}))).arrayBuffer().catch((()=>{throw new n("Unknown error.")})));r+=a;const y=m.byteLength;if(d[c]=Math.min(76,i)*((s>>5)-1),152===d[c]&&(d[c]=y),r==c)return m;r=c,r^=c;for(let e=0,t=m.length;e<t;e++)d[c]^=d[r]<<13,d[c]^=d[r]>>17,d[c]^=d[r]<<5,m[e]=m[e]^d[c];return r^=d[0],r^=d[1],r^=d[2],m}))}(e,(new Date).getTime()+i,Math.random()*i),ce=null,null==se||se.clear()}}function fe(e,n){return t(this,void 0,void 0,(function*(){if(ne=e,!ue){oe=window.ShopAR__THREE,ie=oe.THREE,re=new ie.WebGLRenderer({powerPreference:"high-performance",canvas:e,antialias:!0,alpha:!0}),re.outputEncoding=ie.sRGBEncoding,re.toneMapping=ie.ACESFilmicToneMapping,re.setPixelRatio(window.devicePixelRatio||1),re.setClearColor(new ie.Color(16777215)),se=new ie.Scene,ae=new ie.PerspectiveCamera(25,1,.5);const t=function(e){var t;const n={Glasses:[67.5,15,90],Shoes:[-72.8,72.8,54.6],Watches:[0,-10,40],Handbags:[40,0,0],Rings:[0,0,10]};return null==e?n.Glasses:null!==(t=n[e])&&void 0!==t?t:n.Glasses}(n);ae.position.set(t[0],t[1],t[2]),le=new oe.OrbitControls(ae,re.domElement),le.target.set(0,.03,0),le.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>{P("preview_interacted")}),200);le.addEventListener("end",e)}ue=!0}ye()}))}function pe(e,n){return t(this,void 0,void 0,(function*(){return null==ce&&(ce=(()=>t(this,void 0,void 0,(function*(){const t=new oe.RGBELoader,o=function(e,t){const n=e.THREE,o=new n.DataTexture(t.data,t.width,t.height,void 0,t.type,void 0,n.ClampToEdgeWrapping,n.ClampToEdgeWrapping,n.LinearFilter,n.LinearFilter,1,n.LinearEncoding);return o.flipY=!0,o.generateMipmaps=!1,o.needsUpdate=!0,o}(oe,t.parse(n));o.mapping=ie.EquirectangularReflectionMapping,o.encoding=ie.LinearEncoding,se.environment=o;const i=(new oe.GLTFLoader).setDRACOLoader((new oe.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${ie.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new oe.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${ie.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(re)).setMeshoptDecoder("function"==typeof oe.MeshoptDecoder?oe.MeshoptDecoder():oe.MeshoptDecoder);return yield i.parseAsync(e.buffer,"")})))()),ce}))}function ve(e){return t(this,void 0,void 0,(function*(){se.add(e.scene),se.traverse((e=>{if(e instanceof ie.Mesh){const t=e.material;t.depthWrite=!0,t instanceof ie.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))}function we(){de=!0,ne.style.display="none"}function me(){de||(requestAnimationFrame(me),ye(),le.update(),re.render(se,ae))}function ye(){const e=ne.clientWidth,t=ne.clientHeight;ne.width===e&&ne.height===t||(re.setSize(e,t,!1),ae.aspect=e/t,ae.updateProjectionMatrix())}function ge(e,t){const n=document.createElementNS("http://www.w3.org/2000/svg","svg");n.setAttribute("width",e),n.setAttribute("height",e);{const o=document.createElementNS("http://www.w3.org/2000/svg","image");o.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",t),o.setAttribute("width",e),o.setAttribute("height",e),n.appendChild(o)}return n}const be="shopar-error";function Ee(e,t,n,o){const i=document.createElement("button");i.id=e,i.type="button",i.className="shopar-btn";{const e=ge("1.75rem",o);i.appendChild(e)}{const e=document.createElement("span");e.textContent=t,i.appendChild(e)}i.ariaLabel=n;const r=i.style;return r.zIndex="2",r.display="none",i}const Ce="shopar-control";const Ae="shopar-deepar-output";const Se="shopar-loading";const Re="shopar-main";const Pe="shopar-three-output";var $e;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}($e||($e={}));let xe,ke,Ie,_e,Te,Le,De,Me,Ne,Ue=$e.None;function je(e){Ue=e}let Ge=!1,Oe=null;function ze(){Ge||(xe=function(){const e=document.createElement("div");e.id=Re;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}(),ke=function(){const e=document.createElement("div");e.id=Ae;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),xe.appendChild(ke),Ie=function(){const e=document.createElement("canvas");e.id=Pe;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",t.display="none",e}(),xe.appendChild(Ie),_e=function(){const e=document.createElement("div");return e.id=Ce,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e.style.display="none",e}(),xe.appendChild(_e),Te=Ee("shopar-btn-vto","Try-on","Launch ShopAR virtual try-on","data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22/%3E"),_e.appendChild(Te),Le=Ee("shopar-btn-3d","3D","Launch ShopAR 3D preview",`${y}/img/icons/cube.svg`),_e.appendChild(Le),De=Ee("shopar-btn-close","Close","Close ShopAR view",`${y}/img/icons/close.svg`),_e.append(De),Me=function(){const e=document.createElement("div");e.id=Se;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=document.createElement("div");return i.className="shopar-spinner",n.appendChild(i),e}(),xe.appendChild(Me),Ne=function(){const e=document.createElement("div");e.id=be;const t=e.style;t.position="absolute",t.width="100%",t.height="100%",t.zIndex="1",t.display="none";const n=document.createElement("div"),o=n.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.flexDirection="column",o.justifyContent="center",o.alignItems="center",e.appendChild(n);const i=ge("4rem",`${y}/img/icons/close.svg`);n.appendChild(i);const r=document.createElement("div");r.className="shopar-error-title",r.textContent="Camera Error",n.appendChild(r);const s=document.createElement("div");return s.className="shopar-error-body",s.textContent="Please refresh the page and allow the use of camera.",n.appendChild(s),e}(),xe.appendChild(Ne),Ge=!0)}function We(e,t){e.style.display=t?"":"none"}function Fe(e){""===e.style.display&&We(e,!1)}function Ke(e,t){if(null==e)throw new n(`'${t}' not specified.`)}function He(e,t){if("string"!=typeof e)throw new n(`'${t}' must be a string.`)}const Ve=["AR","3D"];function Be(e){const{apiKey:t,sku:o,targetElement:i,initialState:r,baseUrl:s,defaultUI:a}=e;Ke(t,"apiKey"),He(t,"apiKey"),Ke(o,"sku"),He(o,"sku"),Ke(i,"targetElement"),function(e,t){if(!(e instanceof HTMLElement))throw new n(`'${t}' must be an HTMLElement.`)}(i,"targetElement"),null!=r&&function(e,t,o){if("string"!=typeof e)throw new n(`'${t}' must be a string.`);if(!o.includes(e))throw new n(`'${t}' must be ${o.join("' or '")}.`)}(r,"initialState",Ve),null!=s&&He(s,"baseUrl"),null!=a&&function(e,t){if("boolean"!=typeof e)throw new n(`'${t}' must be a boolean.`)}(a,"defaultUI")}let qe=0,Xe=0;function Ye(e){return t(this,void 0,void 0,(function*(){Be(e);const{apiKey:o,sku:i,targetElement:r,initialState:s}=e;!function(e){const t=getComputedStyle(e);if(!["static","relative"].includes(t.position))throw new n(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${t.position}'.`)}(r),y.update(e.baseUrl);const a=function(e,o){return t(this,void 0,void 0,(function*(){const i=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:e,sku:o,sid:u()})}`).catch((e=>{throw console.error(e),new n("API unavailable.")}));if(!t.ok)throw new n(`API call failed with status ${t.status}.`);return t}))));try{return yield i.json()}catch(e){throw console.error(e),new n("API returned invalid body.")}}))}(o,i);null==b&&(b=g(`${y}/shopar-analytics.js`),b.then((()=>{C=!0,E=window.ShopAR__analytics.uuidv4(),A.forEach((e=>e()))}))),function(e,t){S.organizationId=e,S.sku=t,R=performance.now(),$((()=>{window.ShopAR__analytics.initializeImpl()}))}(o,i),ze();const l=++Xe,{category:c,arUrl:d,arKey:h,previewUrl:f,previewEnvUrl:w}=yield a;Je(l),P("initialized");const m=null!=d&&function(e){return null!=e&&v.includes(e)}(c);m&&(null==T&&(T=g(`${y}/shopar-deepar.js`)),Y(d),"Glasses"===c&&null==L&&(L=g(`${y}/shopar-true-scale.js`)));const I=null!=f;var _;I&&(null==K&&(K=g(`${y}/shopar-three.js`)),he(f),(_=null!=w?w:`${y}/env/default.hdr`)!==q&&(q=_,B=U(_))),function(e){e.style.position="relative",null==Oe?e.appendChild(xe):e!==Oe&&(Oe.removeChild(xe),e.appendChild(xe)),Oe=e}(r);const M=null==e.defaultUI||e.defaultUI;if(function(e,t,n){We(_e,e),We(Te,e&&t&&Ue===$e.None),We(Le,e&&n&&Ue===$e.None),We(De,e&&Ue!==$e.None),e||Fe(Me),e||Fe(Ne)}(M,m,I),Ue===$e.AR){if(m&&!X){const e=yield D;Je(l),yield Q(e),Je(l)}}else if(Ue===$e.Preview&&I&&!de){const e=yield H;Je(l);const t=yield B;Je(l);const n=yield pe(e,t);Je(l),ve(n)}const N=()=>t(this,void 0,void 0,(function*(){if(!m)throw new n("Model does not have AR enabled.");if(Ue===$e.AR)throw new n("AR already launched.");const e=++qe;Ue===$e.Preview&&I&&!de&&we(),je($e.AR),M&&(We(Te,!1),We(Le,!1),We(De,!0)),P("vto_launched"),M&&(We(Ne,!1),We(Me,!0));const o=yield D;if(qe===e&&Xe===l&&(yield T,qe===e&&Xe===l)){try{yield J(ke,h,c)}catch(e){throw function(e){P("vto_camera_denied",{error:e})}(e),M&&(We(Me,!1),We(Ne,!0)),e}qe===e&&Xe===l&&(yield Q(o),qe===e&&Xe===l&&(yield function(){return t(this,void 0,void 0,(function*(){if(null!=j){if(X=!1,null==G){const e=ee(O),t=x(e);G=yield k(t),yield te(j,G,"user"===e)}j.setPaused(X),z.style.display=""}}))}(),qe===e&&Xe===l&&M&&We(Me,!1)))}})),W=()=>t(this,void 0,void 0,(function*(){if(!I)throw new n("Model does not have 3D enabled.");if(Ue===$e.Preview)throw new n("3D already launched.");const e=++qe;Ue===$e.AR&&m&&!X&&Z(),je($e.Preview),M&&(We(Te,!1),We(Le,!1),We(De,!0)),P("preview_launched"),M&&(We(Ne,!1),We(Me,!0));const t=yield H;if(qe!==e||Xe!==l)return;const o=yield B;if(qe!==e||Xe!==l)return;if(yield K,qe!==e||Xe!==l)return;if(yield fe(Ie,c),qe!==e||Xe!==l)return;const i=yield pe(t,o);qe===e&&Xe===l&&(ve(i),de=!1,me(),ne.style.display="",M&&We(Me,!1))})),F=()=>t(this,void 0,void 0,(function*(){if(Ue===$e.None)throw new n("Neither AR or 3D launched.");++qe,Ue===$e.AR?m&&!X&&Z():Ue===$e.Preview&&I&&!de&&we(),je($e.None),M&&(We(Me,!1),We(Ne,!1),We(De,!1),m&&We(Te,!0),I&&We(Le,!0))}));return M&&(m&&(!function(e){const t={Glasses:"glasses.svg",Shoes:"shoe.svg",Watches:"watch.svg"},n=`${y}/img/icons/${null!=e&&t[e]||t.Glasses}`;Te.firstChild.firstChild.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",n)}(c),Te.onclick=N),I&&(Le.onclick=W),De.onclick=F),"AR"===s?(yield N(),Je(l)):"3D"===s&&(yield W(),Je(l)),{launchAR:N,launch3D:W,closeAR:()=>t(this,void 0,void 0,(function*(){if(Ue!==$e.AR)throw new n("AR not launched.");++qe,m&&!X&&Z(),je($e.None),M&&(We(Me,!1),We(Ne,!1),We(De,!1),m&&We(Te,!0),I&&We(Le,!0))})),close3D:()=>t(this,void 0,void 0,(function*(){if(Ue!==$e.Preview)throw new n("3D not launched.");++qe,I&&!de&&we(),je($e.None),M&&(We(Me,!1),We(Ne,!1),We(De,!1),m&&We(Te,!0),I&&We(Le,!0))})),close:F}}))}function Je(e){if(Xe!==e)throw new n("Setup cancelled. Please ensure that setup is only called once.")}const Qe={setup:function(e){return t(this,void 0,void 0,(function*(){return function(e,o){return t(this,void 0,void 0,(function*(){try{return o()}catch(t){throw t instanceof Error?new n(`${e} failed: ${t.message}`):(console.error(t),new n(`${e} failed.`))}}))}("setup",(()=>Ye(e)))}))},version:w};e.plugin=Qe})); |
{ | ||
"name": "shopar-plugin", | ||
"version": "0.0.5-alpha.6", | ||
"version": "0.0.5", | ||
"description": "Plugin for the Web that seamlessly integrates into your webpage to create embedded virtual try-on and 3D preview capabilities.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -35,3 +35,3 @@ # ShopAR Plugin | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/shopar-plugin@{{VERSION}}/dist/shopar-plugin.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/shopar-plugin@0.0.5/dist/shopar-plugin.js"></script> | ||
``` | ||
@@ -126,3 +126,3 @@ | ||
- If provided, defines where the additional ShopAR plugin files are fetched from. | ||
- Default value: `https://cdn.jsdelivr.net/npm/shopar-plugin@{{VERSION}}/dist` | ||
- Default value: `https://cdn.jsdelivr.net/npm/shopar-plugin@0.0.5/dist` | ||
@@ -129,0 +129,0 @@ ## License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20776480