shopar-plugin
Advanced tools
Comparing version 0.0.5-alpha.5 to 0.0.5-alpha.6
@@ -1,1 +0,1 @@ | ||
function e(e,n,t,o){return new(t||(t=Promise))((function(i,r){function s(e){try{a(o.next(e))}catch(e){r(e)}}function l(e){try{a(o.throw(e))}catch(e){r(e)}}function a(e){var n;e.done?i(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(s,l)}a((o=o.apply(e,n||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class n extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}const t=[Uint8Array,Uint32Array],o=t.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]]]],l=s.charCodeAt(o),a=l^l,c=new t[1]([a]);function d(){const e=(new TextEncoder).encode(s),n=i.map((e=>e.length));for(let t=0;t<e.length;t++)e[t]^=n[t%n.length];return window[i[n[2]]](String.fromCodePoint(...e))}const u=[1e3,2e3,4e3],h=u.length;function p(n,t=0){return e(this,void 0,void 0,(function*(){try{return yield n()}catch(e){if(t>=h)throw e;return yield new Promise((e=>setTimeout(e,u[t]))),p(n,t+1)}}))}const f=["Glasses","Shoes","Watches"];const v="0.0.5-alpha.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 g(n){return e(this,void 0,void 0,(function*(){if("function"!=typeof importScripts){const e=document.createElement("script");return e.setAttribute("src",n),e.setAttribute("crossorigin","anonymous"),new Promise((n=>{e.addEventListener("load",(()=>n()),!1),e.addEventListener("error",(()=>n()),!1),document.body.appendChild(e)}))}importScripts(n.toString())}))}let y,b,E=!1;const C=[],S={organizationId:"Unknown",sku:"Unknown"};let A;function P(e,n={}){n=Object.assign(Object.assign(Object.assign({},n),S),{vtoTime:(performance.now()-A)/1e3,vtoSessionId:b}),R((()=>{window.ShopAR__analytics.trackEventImpl(e,n)}))}function R(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(n){return e(this,void 0,void 0,(function*(){const e=document.createElement("video");return e.setAttribute("playsinline","playsinline"),e.srcObject=yield n,e}))}let I=!1;function x(e,n,t){const o=null==e?void 0:e.querySelector(`#${n}`);if(null!=o)return o;const i=t();return null==e||e.appendChild(i),i}function L(e,n,t){const o=x(e,n,t),i=o.style;return{element:o,show:()=>{i.display=""},hide:()=>{i.display="none"}}}const D="shopar-deepar-output";function _(e){return x(e,D,U)}function U(){const e=document.createElement("div");e.id=D;const n=e.style;return n.position="absolute",n.top="0",n.left="0",n.width="100%",n.height="100%",n.zIndex="1",n.display="none",e}function M(t){return e(this,void 0,void 0,(function*(){const o=yield p((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(t).catch((e=>{throw console.error(e),new n("Resource unavailable.")}));if(!e.ok)throw new n(`Resource download failed with status ${e.status}.`);return e}))));try{return yield(yield o.blob()).arrayBuffer()}catch(e){throw console.error(e),new n("Resource has invalid body.")}}))}let T,j,N,G,O;const z={peek:()=>O,get:(e,n,t)=>{if(null==T&&(T=g(`${m}/shopar-deepar.js`)),null==j&&"Glasses"===t&&(j=g(`${m}/shopar-true-scale.js`)),n!==N&&function(e){N=e,G=M(e)}(n),null==O){const n=_(e);O=W(n)}return O}},W=t=>{let o,i,r,s;let l;let a=!0;return{domElement:t,init:(l,a)=>e(void 0,void 0,void 0,(function*(){return r=a,yield T,null==s&&(s=e(void 0,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=F(a),c=$(s);P("vto_camera_asked");const d=function(e){const n={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:n[e]}(a);o=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:l||"your_license_key_goes_here",previewElement:t,additionalOptions:{hint:d,cameraConfig:{disableDefaultCamera:!0}}}),i=yield k(c),P("vto_camera_granted"),yield K(o,i,"user"===s),"Glasses"===a&&(yield j,function(n,t){e(this,void 0,void 0,(function*(){I=!1;const{ShopAR__TrueScale:o}=window;yield o.initialize(`${m}/wasm/mediapipe`,n);const i=[],r=setInterval((()=>e(this,void 0,void 0,(function*(){if(I)return void clearInterval(r);const{error:e,faceWidth:n}=yield o.predict(performance.now());if(I)return void clearInterval(r);if(null!=e)return void console.error(`TrueScale predict error: ${e}`);if(i.length<10)return void i.push(n);clearInterval(r);const s=137/(function(e){let n=0;const t=e.length;for(let o=0;o<t;o++)n+=e[o];return n/t}(i)+5);t.changeParameterVector("GLASSES","","scale",s,s,s,0),t.changeParameterVector("shopar_glasses","","scale",s,s,s,0)}))),200)}))}(i,o)),P("vto_init_success")}))),s})),setDomElement:e=>{t=e,null==o||o.shutdown(),o=null,s=null},download:()=>e(void 0,void 0,void 0,(function*(){return G})),parse:n=>e(void 0,void 0,void 0,(function*(){return null==l&&(l=o.switchEffect(n)),l})),clear:()=>{l=null,null==o||o.clearEffect(),I=!0},isPaused:()=>a,resume:()=>e(void 0,void 0,void 0,(function*(){if(null!=o){if(a=!1,null==i){const e=F(r),n=$(e);i=yield k(n),yield K(o,i,"user"===e)}o.setPaused(a),t.style.display=""}})),pause:()=>{null!=o&&(a=!0,o.setPaused(a),null!=i&&null!=i.srcObject&&i.srcObject instanceof MediaStream&&(i.srcObject.getTracks().forEach((e=>e.stop())),i=null),o.stopCamera(),t.style.display="none")}}};function F(e){var n;return null==e?"user":null!==(n={Glasses:"user",Shoes:"environment",Watches:"environment"}[e])&&void 0!==n?n:"user"}function K(n,t,o){return e(this,void 0,void 0,(function*(){yield new Promise((i=>{t.onloadedmetadata=()=>{t.play().then((()=>e(this,void 0,void 0,(function*(){n.setVideoElement(t,o),i()}))))}}))}))}const H="shopar-three-output";function q(e){return x(e,H,V)}function V(){const e=document.createElement("canvas");e.id=H;const n=e.style;return n.position="absolute",n.top="0",n.left="0",n.width="100%",n.height="100%",n.zIndex="1",e}let B,X,Y,J,Q,Z;const ee={peek:()=>Z,get:(o,i,r,s)=>{if(null==B&&(B=g(`${m}/shopar-three.js`)),i!==X&&function(o){const i=255;X=o,Y=function(o,i,r){return e(this,void 0,void 0,(function*(){let s="glb".charCodeAt(0),l="3d".charCodeAt(0);s>>=1,s+=l;const d=String.fromCharCode(s+14);l>>=2,s^=l+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 n("Unknown error.")}));if(!e.ok)throw new n("Unknown error.");return e})))),m=new t[0](yield(yield w.blob().catch((()=>{throw new n("Unknown error.")}))).arrayBuffer().catch((()=>{throw new n("Unknown error.")})));r+=l;const g=m.byteLength;if(c[a]=Math.min(76,i)*((s>>5)-1),152===c[a]&&(c[a]=g),r==a)return m;r=a,r^=a;for(let e=0,n=m.length;e<n;e++)c[a]^=c[r]<<13,c[a]^=c[r]>>17,c[a]^=c[r]<<5,m[e]=m[e]^c[a];return r^=c[0],r^=c[1],r^=c[2],m}))}(o,(new Date).getTime()+i,Math.random()*i)}(i),r!==J&&function(e){J=e,Q=M(e)}(r),null==Z){const e=q(o);Z=ne(e,s)}return Z}},ne=(n,t)=>{let o,i,r,s,l,a,c=!1;let d;let u=!1;const h=()=>{u||(requestAnimationFrame(h),p(),a.update(),r.render(s,l))},p=()=>{const e=n.clientWidth,t=n.clientHeight;n.width===e&&n.height===t||(r.setSize(e,t,!1),l.aspect=e/t,l.updateProjectionMatrix())};return{domElement:n,init:()=>e(void 0,void 0,void 0,(function*(){if(yield B,!c){o=window.ShopAR__THREE,i=o.THREE,r=new i.WebGLRenderer({powerPreference:"high-performance",canvas:n,antialias:!0,alpha:!0}),r.outputEncoding=i.sRGBEncoding,r.toneMapping=i.ACESFilmicToneMapping,r.setPixelRatio(window.devicePixelRatio||1),r.setClearColor(new i.Color(16777215)),s=new i.Scene,l=new i.PerspectiveCamera(25,1,.5);const e=function(e){var n;const t={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?t.Glasses:null!==(n=t[e])&&void 0!==n?n:t.Glasses}(t);l.position.set(e[0],e[1],e[2]),a=new o.OrbitControls(l,r.domElement),a.target.set(0,.03,0),a.dampingFactor=.3;{const e=function(e,n){let t;return()=>{t||e(),clearTimeout(t),t=setTimeout((()=>{t=void 0}),n)}}((()=>{P("preview_interacted")}),200);a.addEventListener("end",e)}c=!0}p()})),setDomElement:t=>e(void 0,void 0,void 0,(function*(){n=t,c=!1})),downloadModel:()=>e(void 0,void 0,void 0,(function*(){return Y})),downloadEnv:()=>e(void 0,void 0,void 0,(function*(){return Q})),parse:(n,t)=>e(void 0,void 0,void 0,(function*(){return null==d&&(d=e(void 0,void 0,void 0,(function*(){const e=new o.RGBELoader,l=function(e,n){const t=e.THREE,o=new t.DataTexture(n.data,n.width,n.height,void 0,n.type,void 0,t.ClampToEdgeWrapping,t.ClampToEdgeWrapping,t.LinearFilter,t.LinearFilter,1,t.LinearEncoding);return o.flipY=!0,o.generateMipmaps=!1,o.needsUpdate=!0,o}(o,e.parse(t));l.mapping=i.EquirectangularReflectionMapping,l.encoding=i.LinearEncoding,s.environment=l;const a=(new o.GLTFLoader).setDRACOLoader((new o.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${i.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new o.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${i.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(r)).setMeshoptDecoder("function"==typeof o.MeshoptDecoder?o.MeshoptDecoder():o.MeshoptDecoder),c=yield a.parseAsync(n.buffer,"");s.add(c.scene),s.traverse((e=>{if(e instanceof i.Mesh){const n=e.material;n.depthWrite=!0,n instanceof i.MeshPhysicalMaterial&&n.transmissionMap&&(n.transmission=1,n.toneMapped=!1,n.fog=!1,n.needsUpdate=!0)}}))}))),d})),clear:()=>{d=null,null==s||s.clear()},isPaused:()=>u,resume:()=>{u=!1,h(),n.style.display=""},pause:()=>{u=!0,n.style.display="none"}}};function te(e,n){const t=document.createElementNS("http://www.w3.org/2000/svg","svg");t.setAttribute("width",e),t.setAttribute("height",e);{const o=document.createElementNS("http://www.w3.org/2000/svg","image");o.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",n),o.setAttribute("width",e),o.setAttribute("height",e),t.appendChild(o)}return t}const oe="shopar-error";function ie(){const e=document.createElement("div");e.id=oe;const n=e.style;n.position="absolute",n.width="100%",n.height="100%",n.zIndex="1",n.display="none";const t=document.createElement("div"),o=t.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(t);const i=te("4rem",`${m}/img/icons/close.svg`);t.appendChild(i);const r=document.createElement("div");r.className="shopar-error-title",r.textContent="Camera Error",t.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.",t.appendChild(s),e}function re(e,n){return x(e,n.id,(()=>function(e){const{id:n,iconUrl:t,textContent:o,ariaLabel:i}=e,r=document.createElement("button");if(r.id=n,r.type="button",r.className="shopar-btn",null!=t){const e=te("1.75rem",t);r.appendChild(e)}{const e=document.createElement("span");e.textContent=o,r.appendChild(e)}return r.ariaLabel=i,r.style.zIndex="2",r}(n)))}const se="shopar-control";function le(){const e=document.createElement("div");return e.id=se,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e}const ae="shopar-loading";function ce(){const e=document.createElement("div");e.id=ae;const n=e.style;n.position="absolute",n.width="100%",n.height="100%",n.zIndex="1",n.display="none";const t=document.createElement("div"),o=t.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.justifyContent="center",o.alignItems="center",e.appendChild(t);const i=document.createElement("div");return i.className="shopar-spinner",t.appendChild(i),e}const de="shopar-main";function ue(){const e=document.createElement("div");e.id=de;const n=e.style;return n.position="absolute",n.top="0",n.bottom="0",n.left="0",n.right="0",e}function he(e,t){if(null==e)throw new n(`'${t}' not specified.`)}function pe(e,t){if("string"!=typeof e)throw new n(`'${t}' must be a string.`)}const fe=["AR","3D"];function ve(e){const{apiKey:t,sku:o,targetElement:i,initialState:r,baseUrl:s,defaultUI:l}=e;he(t,"apiKey"),pe(t,"apiKey"),he(o,"sku"),pe(o,"sku"),he(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",fe),null!=s&&pe(s,"baseUrl"),null!=l&&function(e,t){if("boolean"!=typeof e)throw new n(`'${t}' must be a boolean.`)}(l,"defaultUI")}var we;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(we||(we={}));let me,ge,ye=we.None,be=0,Ee=0;const Ce=[];let Se,Ae,Pe,Re=null;function $e(t){return e(this,void 0,void 0,(function*(){ve(t);const{apiKey:o,sku:i,targetElement:r,initialState:s}=t;!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),m.update(t.baseUrl);const l=function(t,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:t,sku:o,sid:d()})}`).catch((e=>{throw console.error(e),new n("API unavailable.")}));if(!e.ok)throw new n(`API call failed with status ${e.status}.`);return e}))));try{return yield i.json()}catch(e){throw console.error(e),new n("API returned invalid body.")}}))}(o,i);null==y&&(y=g(`${m}/shopar-analytics.js`),y.then((()=>{E=!0,b=window.ShopAR__analytics.uuidv4(),C.forEach((e=>e()))}))),function(e,n){S.organizationId=e,S.sku=n,A=performance.now(),R((()=>{window.ShopAR__analytics.initializeImpl()}))}(o,i),null!=Re&&(function(e,n){const t=e.querySelector(`#${n}`);null!=t&&t.remove()}(Re,de),ye=we.None,function(){const e=z.peek();null!=e&&(e.clear(),e.isPaused()||e.pause());const n=ee.peek();null!=n&&(n.clear(),n.isPaused()||n.pause())}()),Re=r;const a=++Ee,{category:c,arUrl:u,arKey:h,previewUrl:v,previewEnvUrl:w}=yield l;if(Ee!==a)throw new n("Setup cancelled. Please ensure that setup is only called once.");P("initialized"),r.style.position="relative";const $=function(e){return x(e,de,ue)}(r),k=null==t.defaultUI||t.defaultUI;k&&(ge=function(e){return x(e,se,le)}($),Se=re(null,{id:"shopar-btn-close",textContent:"Close",ariaLabel:"Close ShopAR view",iconUrl:`${m}/img/icons/close.svg`}),Ae=function(e){return L(e,ae,ce)}($),Pe=function(e){return L(e,oe,ie)}($)),function(e){const n=z.peek();null!=n&&n.setDomElement(_(e));const t=ee.peek();null!=t&&t.setDomElement(q(e))}($);const I=null!=u&&function(e){return null!=e&&f.includes(e)}(c)?z.get($,u,c):null,D=null!=v?ee.get($,v,null!=w?w:`${m}/env/default.hdr`,c):null,U=()=>e(this,void 0,void 0,(function*(){if(null==I)throw new n("Model does not have AR enabled.");if(ye===we.AR)throw new n("AR already launched.");const e=++be;ye===we.Preview&&(null==D||D.isPaused()||D.pause()),ye=we.AR,k&&ge.replaceChildren(Se),P("vto_launched"),k&&(Pe.hide(),Ae.show());const t=yield I.download();if(be===e&&Ee===a){try{yield I.init(h,c)}catch(e){throw function(e){P("vto_camera_denied",{error:e})}(e),k&&(Ae.hide(),Pe.show()),e}be===e&&Ee===a&&(yield I.parse(t),be===e&&Ee===a&&(yield I.resume(),be===e&&Ee===a&&k&&Ae.hide()))}})),M=()=>e(this,void 0,void 0,(function*(){if(null==D)throw new n("Model does not have 3D enabled.");if(ye===we.Preview)throw new n("3D already launched.");const e=++be;ye===we.AR&&(null==I||I.isPaused()||I.pause()),ye=we.Preview,k&&ge.replaceChildren(Se),P("preview_launched"),k&&(Pe.hide(),Ae.show());const t=yield D.downloadModel();if(be!==e||Ee!==a)return;const o=yield D.downloadEnv();be===e&&Ee===a&&(yield D.init(),be===e&&Ee===a&&(yield D.parse(t,o),be===e&&Ee===a&&(D.resume(),k&&Ae.hide())))})),T=()=>e(this,void 0,void 0,(function*(){if(ye===we.None)throw new n("Neither AR or 3D launched.");if(++be,ye===we.AR?null==I||I.isPaused()||I.pause():ye===we.Preview&&(null==D||D.isPaused()||D.pause()),ye=we.None,k){Ae.hide(),Pe.hide();const e=Ce.filter((e=>null!=e));ge.replaceChildren(...e)}}));if(k){if(null!=I){const e={Glasses:`${m}/img/icons/glasses.svg`,Shoes:`${m}/img/icons/shoe.svg`,Watches:`${m}/img/icons/watch.svg`},n=null!=c&&e[c]||e.Glasses;Ce[we.AR]=re(ge,{id:"shopar-btn-vto",textContent:"Try-on",ariaLabel:"Launch ShopAR virtual try-on",iconUrl:n}),Ce[we.AR].onclick=U}null!=D&&(Ce[we.Preview]=re(ge,{id:"shopar-btn-3d",textContent:"3D",ariaLabel:"Launch ShopAR 3D preview",iconUrl:`${m}/img/icons/cube.svg`}),Ce[we.Preview].onclick=M),Se.onclick=T}if("AR"===s){if(yield U(),Ee!==a)throw new n("Setup cancelled. Please ensure that setup is only called once.")}else if("3D"===s&&(yield M(),Ee!==a))throw new n("Setup cancelled. Please ensure that setup is only called once.");return me={launchAR:U,launch3D:M,closeAR:()=>e(this,void 0,void 0,(function*(){if(ye!==we.AR)throw new n("AR not launched.");if(++be,null==I||I.isPaused()||I.pause(),ye=we.None,k){Ae.hide(),Pe.hide();const e=Ce.filter((e=>null!=e));ge.replaceChildren(...e)}})),close3D:()=>e(this,void 0,void 0,(function*(){if(ye!==we.Preview)throw new n("3D not launched.");if(++be,null==D||D.isPaused()||D.pause(),ye=we.None,k){Ae.hide(),Pe.hide();const e=Ce.filter((e=>null!=e));ge.replaceChildren(...e)}})),close:T},me}))}const ke={setup:function(t){return e(this,void 0,void 0,(function*(){return function(t,o){return e(this,void 0,void 0,(function*(){try{return o()}catch(e){throw e instanceof Error?new n(`${t} failed: ${e.message}`):(console.error(e),new n(`${t} failed.`))}}))}("setup",(()=>$e(t)))}))},version:v};export{ke 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-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}; |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).ShopAR={})}(this,(function(e){"use strict";function n(e,n,t,o){return new(t||(t=Promise))((function(i,r){function s(e){try{a(o.next(e))}catch(e){r(e)}}function l(e){try{a(o.throw(e))}catch(e){r(e)}}function a(e){var n;e.done?i(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(s,l)}a((o=o.apply(e,n||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t 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],l=window[r[s[s[i]]]][r[s[s[s.length-1]]]],a=l.charCodeAt(i),c=a^a,d=new o[1]([c]);function u(){const e=(new TextEncoder).encode(l),n=r.map((e=>e.length));for(let t=0;t<e.length;t++)e[t]^=n[t%n.length];return window[r[n[2]]](String.fromCodePoint(...e))}const h=[1e3,2e3,4e3],p=h.length;function f(e,t=0){return n(this,void 0,void 0,(function*(){try{return yield e()}catch(n){if(t>=p)throw n;return yield new Promise((e=>setTimeout(e,h[t]))),f(e,t+1)}}))}const v=["Glasses","Shoes","Watches"];const w="0.0.5-alpha.5";let m=`https://cdn.jsdelivr.net/npm/shopar-plugin@${w}/dist`;const g={update:e=>{null!=e&&(m=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>m};function y(e){return n(this,void 0,void 0,(function*(){if("function"!=typeof importScripts){const n=document.createElement("script");return n.setAttribute("src",e),n.setAttribute("crossorigin","anonymous"),new Promise((e=>{n.addEventListener("load",(()=>e()),!1),n.addEventListener("error",(()=>e()),!1),document.body.appendChild(n)}))}importScripts(e.toString())}))}let b,E,C=!1;const S=[],A={organizationId:"Unknown",sku:"Unknown"};let R;function P(e,n={}){n=Object.assign(Object.assign(Object.assign({},n),A),{vtoTime:(performance.now()-R)/1e3,vtoSessionId:E}),$((()=>{window.ShopAR__analytics.trackEventImpl(e,n)}))}function $(e){C?e():S.push(e)}function k(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}function x(e){return n(this,void 0,void 0,(function*(){const n=document.createElement("video");return n.setAttribute("playsinline","playsinline"),n.srcObject=yield e,n}))}let I=!1;function L(e,n,t){const o=null==e?void 0:e.querySelector(`#${n}`);if(null!=o)return o;const i=t();return null==e||e.appendChild(i),i}function D(e,n,t){const o=L(e,n,t),i=o.style;return{element:o,show:()=>{i.display=""},hide:()=>{i.display="none"}}}const _="shopar-deepar-output";function U(e){return L(e,_,M)}function M(){const e=document.createElement("div");e.id=_;const n=e.style;return n.position="absolute",n.top="0",n.left="0",n.width="100%",n.height="100%",n.zIndex="1",n.display="none",e}function T(e){return n(this,void 0,void 0,(function*(){const o=yield f((()=>n(this,void 0,void 0,(function*(){const n=yield fetch(e).catch((e=>{throw console.error(e),new t("Resource unavailable.")}));if(!n.ok)throw new t(`Resource download failed with status ${n.status}.`);return n}))));try{return yield(yield o.blob()).arrayBuffer()}catch(e){throw console.error(e),new t("Resource has invalid body.")}}))}let j,N,G,O,z;const W={peek:()=>z,get:(e,n,t)=>{if(null==j&&(j=y(`${g}/shopar-deepar.js`)),null==N&&"Glasses"===t&&(N=y(`${g}/shopar-true-scale.js`)),n!==G&&function(e){G=e,O=T(e)}(n),null==z){const n=U(e);z=F(n)}return z}},F=e=>{let o,i,r,s;let l;let a=!0;return{domElement:e,init:(l,a)=>n(void 0,void 0,void 0,(function*(){return r=a,yield j,null==s&&(s=n(void 0,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=K(a),c=k(s);P("vto_camera_asked");const d=function(e){const n={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:n[e]}(a);o=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:l||"your_license_key_goes_here",previewElement:e,additionalOptions:{hint:d,cameraConfig:{disableDefaultCamera:!0}}}),i=yield x(c),P("vto_camera_granted"),yield H(o,i,"user"===s),"Glasses"===a&&(yield N,function(e,t){n(this,void 0,void 0,(function*(){I=!1;const{ShopAR__TrueScale:o}=window;yield o.initialize(`${g}/wasm/mediapipe`,e);const i=[],r=setInterval((()=>n(this,void 0,void 0,(function*(){if(I)return void clearInterval(r);const{error:e,faceWidth:n}=yield o.predict(performance.now());if(I)return void clearInterval(r);if(null!=e)return void console.error(`TrueScale predict error: ${e}`);if(i.length<10)return void i.push(n);clearInterval(r);const s=137/(function(e){let n=0;const t=e.length;for(let o=0;o<t;o++)n+=e[o];return n/t}(i)+5);t.changeParameterVector("GLASSES","","scale",s,s,s,0),t.changeParameterVector("shopar_glasses","","scale",s,s,s,0)}))),200)}))}(i,o)),P("vto_init_success")}))),s})),setDomElement:n=>{e=n,null==o||o.shutdown(),o=null,s=null},download:()=>n(void 0,void 0,void 0,(function*(){return O})),parse:e=>n(void 0,void 0,void 0,(function*(){return null==l&&(l=o.switchEffect(e)),l})),clear:()=>{l=null,null==o||o.clearEffect(),I=!0},isPaused:()=>a,resume:()=>n(void 0,void 0,void 0,(function*(){if(null!=o){if(a=!1,null==i){const e=K(r),n=k(e);i=yield x(n),yield H(o,i,"user"===e)}o.setPaused(a),e.style.display=""}})),pause:()=>{null!=o&&(a=!0,o.setPaused(a),null!=i&&null!=i.srcObject&&i.srcObject instanceof MediaStream&&(i.srcObject.getTracks().forEach((e=>e.stop())),i=null),o.stopCamera(),e.style.display="none")}}};function K(e){var n;return null==e?"user":null!==(n={Glasses:"user",Shoes:"environment",Watches:"environment"}[e])&&void 0!==n?n:"user"}function H(e,t,o){return n(this,void 0,void 0,(function*(){yield new Promise((i=>{t.onloadedmetadata=()=>{t.play().then((()=>n(this,void 0,void 0,(function*(){e.setVideoElement(t,o),i()}))))}}))}))}const q="shopar-three-output";function V(e){return L(e,q,B)}function B(){const e=document.createElement("canvas");e.id=q;const n=e.style;return n.position="absolute",n.top="0",n.left="0",n.width="100%",n.height="100%",n.zIndex="1",e}let X,Y,J,Q,Z,ee;const ne={peek:()=>ee,get:(e,i,r,s)=>{if(null==X&&(X=y(`${g}/shopar-three.js`)),i!==Y&&function(e){const i=255;Y=e,J=function(e,i,r){return n(this,void 0,void 0,(function*(){let s="glb".charCodeAt(0),l="3d".charCodeAt(0);s>>=1,s+=l;const a=String.fromCharCode(s+14);l>>=2,s^=l+2;const u=String.fromCharCode(s-3);s^=5;const h=String.fromCharCode(s-7);s^=2;const p=String.fromCharCode(s-7),v=window[`${h}${u+a+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${p}`],w=yield f((()=>n(this,void 0,void 0,(function*(){const n=yield v(`${e}`).catch((()=>{throw new t("Unknown error.")}));if(!n.ok)throw new t("Unknown error.");return n})))),m=new o[0](yield(yield w.blob().catch((()=>{throw new t("Unknown error.")}))).arrayBuffer().catch((()=>{throw new t("Unknown error.")})));r+=l;const g=m.byteLength;if(d[c]=Math.min(76,i)*((s>>5)-1),152===d[c]&&(d[c]=g),r==c)return m;r=c,r^=c;for(let e=0,n=m.length;e<n;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)}(i),r!==Q&&function(e){Q=e,Z=T(e)}(r),null==ee){const n=V(e);ee=te(n,s)}return ee}},te=(e,t)=>{let o,i,r,s,l,a,c=!1;let d;let u=!1;const h=()=>{u||(requestAnimationFrame(h),p(),a.update(),r.render(s,l))},p=()=>{const n=e.clientWidth,t=e.clientHeight;e.width===n&&e.height===t||(r.setSize(n,t,!1),l.aspect=n/t,l.updateProjectionMatrix())};return{domElement:e,init:()=>n(void 0,void 0,void 0,(function*(){if(yield X,!c){o=window.ShopAR__THREE,i=o.THREE,r=new i.WebGLRenderer({powerPreference:"high-performance",canvas:e,antialias:!0,alpha:!0}),r.outputEncoding=i.sRGBEncoding,r.toneMapping=i.ACESFilmicToneMapping,r.setPixelRatio(window.devicePixelRatio||1),r.setClearColor(new i.Color(16777215)),s=new i.Scene,l=new i.PerspectiveCamera(25,1,.5);const n=function(e){var n;const t={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?t.Glasses:null!==(n=t[e])&&void 0!==n?n:t.Glasses}(t);l.position.set(n[0],n[1],n[2]),a=new o.OrbitControls(l,r.domElement),a.target.set(0,.03,0),a.dampingFactor=.3;{const e=function(e,n){let t;return()=>{t||e(),clearTimeout(t),t=setTimeout((()=>{t=void 0}),n)}}((()=>{P("preview_interacted")}),200);a.addEventListener("end",e)}c=!0}p()})),setDomElement:t=>n(void 0,void 0,void 0,(function*(){e=t,c=!1})),downloadModel:()=>n(void 0,void 0,void 0,(function*(){return J})),downloadEnv:()=>n(void 0,void 0,void 0,(function*(){return Z})),parse:(e,t)=>n(void 0,void 0,void 0,(function*(){return null==d&&(d=n(void 0,void 0,void 0,(function*(){const n=new o.RGBELoader,l=function(e,n){const t=e.THREE,o=new t.DataTexture(n.data,n.width,n.height,void 0,n.type,void 0,t.ClampToEdgeWrapping,t.ClampToEdgeWrapping,t.LinearFilter,t.LinearFilter,1,t.LinearEncoding);return o.flipY=!0,o.generateMipmaps=!1,o.needsUpdate=!0,o}(o,n.parse(t));l.mapping=i.EquirectangularReflectionMapping,l.encoding=i.LinearEncoding,s.environment=l;const a=(new o.GLTFLoader).setDRACOLoader((new o.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${i.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new o.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${i.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(r)).setMeshoptDecoder("function"==typeof o.MeshoptDecoder?o.MeshoptDecoder():o.MeshoptDecoder),c=yield a.parseAsync(e.buffer,"");s.add(c.scene),s.traverse((e=>{if(e instanceof i.Mesh){const n=e.material;n.depthWrite=!0,n instanceof i.MeshPhysicalMaterial&&n.transmissionMap&&(n.transmission=1,n.toneMapped=!1,n.fog=!1,n.needsUpdate=!0)}}))}))),d})),clear:()=>{d=null,null==s||s.clear()},isPaused:()=>u,resume:()=>{u=!1,h(),e.style.display=""},pause:()=>{u=!0,e.style.display="none"}}};function oe(e,n){const t=document.createElementNS("http://www.w3.org/2000/svg","svg");t.setAttribute("width",e),t.setAttribute("height",e);{const o=document.createElementNS("http://www.w3.org/2000/svg","image");o.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",n),o.setAttribute("width",e),o.setAttribute("height",e),t.appendChild(o)}return t}const ie="shopar-error";function re(){const e=document.createElement("div");e.id=ie;const n=e.style;n.position="absolute",n.width="100%",n.height="100%",n.zIndex="1",n.display="none";const t=document.createElement("div"),o=t.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(t);const i=oe("4rem",`${g}/img/icons/close.svg`);t.appendChild(i);const r=document.createElement("div");r.className="shopar-error-title",r.textContent="Camera Error",t.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.",t.appendChild(s),e}function se(e,n){return L(e,n.id,(()=>function(e){const{id:n,iconUrl:t,textContent:o,ariaLabel:i}=e,r=document.createElement("button");if(r.id=n,r.type="button",r.className="shopar-btn",null!=t){const e=oe("1.75rem",t);r.appendChild(e)}{const e=document.createElement("span");e.textContent=o,r.appendChild(e)}return r.ariaLabel=i,r.style.zIndex="2",r}(n)))}const le="shopar-control";function ae(){const e=document.createElement("div");return e.id=le,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e}const ce="shopar-loading";function de(){const e=document.createElement("div");e.id=ce;const n=e.style;n.position="absolute",n.width="100%",n.height="100%",n.zIndex="1",n.display="none";const t=document.createElement("div"),o=t.style;o.position="absolute",o.width="100%",o.height="100%",o.backgroundColor="#ffffff",o.display="flex",o.justifyContent="center",o.alignItems="center",e.appendChild(t);const i=document.createElement("div");return i.className="shopar-spinner",t.appendChild(i),e}const ue="shopar-main";function he(){const e=document.createElement("div");e.id=ue;const n=e.style;return n.position="absolute",n.top="0",n.bottom="0",n.left="0",n.right="0",e}function pe(e,n){if(null==e)throw new t(`'${n}' not specified.`)}function fe(e,n){if("string"!=typeof e)throw new t(`'${n}' must be a string.`)}const ve=["AR","3D"];function we(e){const{apiKey:n,sku:o,targetElement:i,initialState:r,baseUrl:s,defaultUI:l}=e;pe(n,"apiKey"),fe(n,"apiKey"),pe(o,"sku"),fe(o,"sku"),pe(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",ve),null!=s&&fe(s,"baseUrl"),null!=l&&function(e,n){if("boolean"!=typeof e)throw new t(`'${n}' must be a boolean.`)}(l,"defaultUI")}var me;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(me||(me={}));let ge,ye,be=me.None,Ee=0,Ce=0;const Se=[];let Ae,Re,Pe,$e=null;function ke(e){return n(this,void 0,void 0,(function*(){we(e);const{apiKey:o,sku:i,targetElement:r,initialState:s}=e;!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),g.update(e.baseUrl);const l=function(e,o){return n(this,void 0,void 0,(function*(){const i=yield f((()=>n(this,void 0,void 0,(function*(){const n=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:e,sku:o,sid:u()})}`).catch((e=>{throw console.error(e),new t("API unavailable.")}));if(!n.ok)throw new t(`API call failed with status ${n.status}.`);return n}))));try{return yield i.json()}catch(e){throw console.error(e),new t("API returned invalid body.")}}))}(o,i);null==b&&(b=y(`${g}/shopar-analytics.js`),b.then((()=>{C=!0,E=window.ShopAR__analytics.uuidv4(),S.forEach((e=>e()))}))),function(e,n){A.organizationId=e,A.sku=n,R=performance.now(),$((()=>{window.ShopAR__analytics.initializeImpl()}))}(o,i),null!=$e&&(function(e,n){const t=e.querySelector(`#${n}`);null!=t&&t.remove()}($e,ue),be=me.None,function(){const e=W.peek();null!=e&&(e.clear(),e.isPaused()||e.pause());const n=ne.peek();null!=n&&(n.clear(),n.isPaused()||n.pause())}()),$e=r;const a=++Ce,{category:c,arUrl:d,arKey:h,previewUrl:p,previewEnvUrl:w}=yield l;if(Ce!==a)throw new t("Setup cancelled. Please ensure that setup is only called once.");P("initialized"),r.style.position="relative";const m=function(e){return L(e,ue,he)}(r),k=null==e.defaultUI||e.defaultUI;k&&(ye=function(e){return L(e,le,ae)}(m),Ae=se(null,{id:"shopar-btn-close",textContent:"Close",ariaLabel:"Close ShopAR view",iconUrl:`${g}/img/icons/close.svg`}),Re=function(e){return D(e,ce,de)}(m),Pe=function(e){return D(e,ie,re)}(m)),function(e){const n=W.peek();null!=n&&n.setDomElement(U(e));const t=ne.peek();null!=t&&t.setDomElement(V(e))}(m);const x=null!=d&&function(e){return null!=e&&v.includes(e)}(c)?W.get(m,d,c):null,I=null!=p?ne.get(m,p,null!=w?w:`${g}/env/default.hdr`,c):null,_=()=>n(this,void 0,void 0,(function*(){if(null==x)throw new t("Model does not have AR enabled.");if(be===me.AR)throw new t("AR already launched.");const e=++Ee;be===me.Preview&&(null==I||I.isPaused()||I.pause()),be=me.AR,k&&ye.replaceChildren(Ae),P("vto_launched"),k&&(Pe.hide(),Re.show());const n=yield x.download();if(Ee===e&&Ce===a){try{yield x.init(h,c)}catch(e){throw function(e){P("vto_camera_denied",{error:e})}(e),k&&(Re.hide(),Pe.show()),e}Ee===e&&Ce===a&&(yield x.parse(n),Ee===e&&Ce===a&&(yield x.resume(),Ee===e&&Ce===a&&k&&Re.hide()))}})),M=()=>n(this,void 0,void 0,(function*(){if(null==I)throw new t("Model does not have 3D enabled.");if(be===me.Preview)throw new t("3D already launched.");const e=++Ee;be===me.AR&&(null==x||x.isPaused()||x.pause()),be=me.Preview,k&&ye.replaceChildren(Ae),P("preview_launched"),k&&(Pe.hide(),Re.show());const n=yield I.downloadModel();if(Ee!==e||Ce!==a)return;const o=yield I.downloadEnv();Ee===e&&Ce===a&&(yield I.init(),Ee===e&&Ce===a&&(yield I.parse(n,o),Ee===e&&Ce===a&&(I.resume(),k&&Re.hide())))})),T=()=>n(this,void 0,void 0,(function*(){if(be===me.None)throw new t("Neither AR or 3D launched.");if(++Ee,be===me.AR?null==x||x.isPaused()||x.pause():be===me.Preview&&(null==I||I.isPaused()||I.pause()),be=me.None,k){Re.hide(),Pe.hide();const e=Se.filter((e=>null!=e));ye.replaceChildren(...e)}}));if(k){if(null!=x){const e={Glasses:`${g}/img/icons/glasses.svg`,Shoes:`${g}/img/icons/shoe.svg`,Watches:`${g}/img/icons/watch.svg`},n=null!=c&&e[c]||e.Glasses;Se[me.AR]=se(ye,{id:"shopar-btn-vto",textContent:"Try-on",ariaLabel:"Launch ShopAR virtual try-on",iconUrl:n}),Se[me.AR].onclick=_}null!=I&&(Se[me.Preview]=se(ye,{id:"shopar-btn-3d",textContent:"3D",ariaLabel:"Launch ShopAR 3D preview",iconUrl:`${g}/img/icons/cube.svg`}),Se[me.Preview].onclick=M),Ae.onclick=T}if("AR"===s){if(yield _(),Ce!==a)throw new t("Setup cancelled. Please ensure that setup is only called once.")}else if("3D"===s&&(yield M(),Ce!==a))throw new t("Setup cancelled. Please ensure that setup is only called once.");return ge={launchAR:_,launch3D:M,closeAR:()=>n(this,void 0,void 0,(function*(){if(be!==me.AR)throw new t("AR not launched.");if(++Ee,null==x||x.isPaused()||x.pause(),be=me.None,k){Re.hide(),Pe.hide();const e=Se.filter((e=>null!=e));ye.replaceChildren(...e)}})),close3D:()=>n(this,void 0,void 0,(function*(){if(be!==me.Preview)throw new t("3D not launched.");if(++Ee,null==I||I.isPaused()||I.pause(),be=me.None,k){Re.hide(),Pe.hide();const e=Se.filter((e=>null!=e));ye.replaceChildren(...e)}})),close:T},ge}))}const xe={setup:function(e){return n(this,void 0,void 0,(function*(){return function(e,o){return n(this,void 0,void 0,(function*(){try{return o()}catch(n){throw n instanceof Error?new t(`${e} failed: ${n.message}`):(console.error(n),new t(`${e} failed.`))}}))}("setup",(()=>ke(e)))}))},version:w};e.plugin=xe})); | ||
!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})); |
{ | ||
"name": "shopar-plugin", | ||
"version": "0.0.5-alpha.5", | ||
"version": "0.0.5-alpha.6", | ||
"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": { |
@@ -89,6 +89,11 @@ # ShopAR Plugin | ||
my3DButton.onclick = shopAR.launch3D; | ||
myCloseARButton.onclick = shopAR.closeAR; | ||
myClose3DButton.onclick = shopAR.close3D; | ||
// or just: | ||
myCloseButton.onclick = shopAR.close; | ||
``` | ||
If you wish to change the SKU or target element at runtime, simply call `setup()` with different parameters. | ||
## API | ||
@@ -95,0 +100,0 @@ |
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
130
20776516
7305