shopar-plugin 0.0.4-alpha.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,7 +32,7 @@ There are two distinct ways of integrating the plugin: via Script tag and via NP
32
32
  Add the following script to your HTML.
33
33
 
34
34
  ```html
35
- <script src="https://cdn.jsdelivr.net/npm/shopar-plugin@{{VERSION}}/dist/shopar-plugin.js"></script>
35
+ <script src="https://cdn.jsdelivr.net/npm/shopar-plugin@0.0.4/dist/shopar-plugin.js"></script>
36
36
  ```
37
37
 
38
38
  It is possible to use a different CDN instead of [jsDelivr](https://www.jsdelivr.com/) (e.g. [cdnjs](https://cdnjs.com/), [unpkg](https://www.unpkg.com/)), or even a relative path if the plugin is distributed as a static asset to your app. Just make sure to set the `baseUrl` parameter accordingly (see [setup options](#shoparpluginsetupoptions) for more details).
@@ -117,7 +117,7 @@ Options used for the plugin setup:
117
117
  - `baseUrl` (optional)
118
118
  - Type: `string`
119
119
  - If provided, defines where the additional ShopAR plugin files are fetched from.
120
- - Default value: `https://cdn.jsdelivr.net/npm/shopar-plugin@{{VERSION}}/dist`
120
+ - Default value: `https://cdn.jsdelivr.net/npm/shopar-plugin@0.0.4/dist`
121
121
 
122
122
  ## License
123
123
 
@@ -1 +1 @@
1
- function e(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{c(o.next(e))}catch(e){r(e)}}function a(e){try{c(o.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const t=[Uint8Array,Uint32Array],n=t.length-2,o=["addEventListener","alert","atob","blur","btoa","cancelAnimationFrame","localStorage","location","locationbar","hash","hasOwnProperty","host","hostname","href","requestAnimationFrame"],i=[2,12,7,1],r=window[o[i[i[n]]]][o[i[i[i.length-1]]]],s=r.charCodeAt(n),a=s^s,c=new t[1]([a]);function l(){const e=(new TextEncoder).encode(r),t=o.map((e=>e.length));for(let n=0;n<e.length;n++)e[n]^=t[n%t.length];return window[o[t[2]]](String.fromCodePoint(...e))}const d=[1e3,2e3,4e3],u=d.length;function h(t,n=0){return e(this,void 0,void 0,(function*(){try{return yield t()}catch(e){if(n>=u)throw e;return yield new Promise((e=>setTimeout(e,d[n]))),h(t,n+1)}}))}function p(t,n){return e(this,void 0,void 0,(function*(){const o=yield h((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:t,sku:n,sid:l()})}`).catch((e=>{throw console.error(e),new Error("API unavailable.")}));if(!e.ok)throw new Error(`API call failed with status ${e.status}.`);return e}))));try{return yield o.json()}catch(e){throw console.error(e),new Error("API returned invalid body.")}}))}const f=["Glasses","Shoes","Watches"];function v(e){return null!=e&&f.includes(e)}let w;const m=()=>w,g=e=>(null==w&&(w=y(e)),w),y=e=>{let t=!1;const n=[];e.then((()=>{t=!0,n.forEach((e=>e()))}));const o=e=>{t?e():n.push(e)},i={organizationId:"Unknown",sku:"Unknown"};let r,s=0;function a(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),i),{vtoTime:(performance.now()-s)/1e3,vtoSessionId:r}),o((()=>{const{trackEventImpl:n}=window.ShopAR__analytics;n(e,t)}))}return{init:function(e,t){i.organizationId=e||"Unknown",i.sku=t||"Unknown",s=performance.now(),o((()=>{const{initializeImpl:e,uuidv4:t}=window.ShopAR__analytics;e(),r=t()}))},initialized:()=>a("initialized"),vtoLaunched:()=>a("vto_launched"),vtoCameraAsked:()=>a("vto_camera_asked"),vtoCameraGranted:()=>a("vto_camera_granted"),vtoCameraDenied:e=>a("vto_camera_denied",{error:e}),vtoInitSuccess:()=>a("vto_init_success"),previewLaunched:()=>a("preview_launched"),previewInteracted:()=>a("preview_interacted")}};const E="0.0.4-alpha.1";let b=`https://cdn.jsdelivr.net/npm/shopar-plugin@${E}/dist`;const C={update:e=>{null!=e&&(b=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>b},A=10;function S(t,n){return e(this,void 0,void 0,(function*(){const{ShopAR__TrueScale:o}=window;yield o.initialize(`${C}/wasm/mediapipe`,t);const i=[];setInterval((()=>e(this,void 0,void 0,(function*(){const{error:e,ipd:t}=yield o.predict(performance.now());if(null==e){!function(e,t){e.length>A&&e.splice(0,1);e.push(t)}(i,t);const e=62/(i.reduce(((e,t)=>t+e),0)/i.length);n.changeParameterVector("GLASSES","","scale",e,e,e,0),n.changeParameterVector("shopar_glasses","","scale",e,e,e,0)}}))),200)}))}function P(e,t,n){const o=null==e?void 0:e.querySelector(`#${t}`);if(null!=o)return o;const i=n();return null==e||e.appendChild(i),i}function R(e,t,n){const o=P(e,t,n),i=o.style;return{element:o,show:()=>{i.display=""},hide:()=>{i.display="none"}}}const $="shopar-deepar-output";function L(){const e=document.createElement("div");e.id=$;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}let k;const I=()=>k,U=e=>(null==k&&(k=x(e)),k),x=t=>{const n=m(),o=function(e){return P(e,$,L)}(t),i=o.style;let r,s;let a;let c=!0;return{domElement:o,init:(t,i)=>e(void 0,void 0,void 0,(function*(){const a=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i),c=function(e){var t;const n={Glasses:"user",Shoes:"environment",Watches:"environment"};return null==e?"user":null!==(t=n[e])&&void 0!==t?t:"user"}(i);return null==s&&(s=e(void 0,void 0,void 0,(function*(){if(null==(null===(s=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===s?void 0:s.getUserMedia))throw new Error("No camera available!");var s;const l=function(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}(c);n.vtoCameraAsked(),r=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:t||"your_license_key_goes_here",previewElement:o,additionalOptions:{hint:a,cameraConfig:{disableDefaultCamera:!0}}});const d=yield function(t){return e(this,void 0,void 0,(function*(){const e=document.createElement("video");return e.setAttribute("playsinline","playsinline"),e.srcObject=yield t,e}))}(l);n.vtoCameraGranted(),yield function(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()}))))}}))}))}(r,d,"user"===c),"Glasses"===i&&S(d,r),n.vtoInitSuccess()}))),s})),parse:t=>e(void 0,void 0,void 0,(function*(){return null==a&&(a=r.switchEffect(t)),a})),isPaused:()=>c,resume:()=>{c=!1,r.setPaused(c),i.display=""},pause:()=>{c=!0,r.setPaused(c),i.display="none"}}};const D="shopar-three-output";function _(){const e=document.createElement("canvas");e.id=D;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",e}let M;const T=()=>M,j=(e,t)=>(null==M&&(M=N(e,t)),M),N=(t,n)=>{const o=m(),i=function(e){return P(e,D,_)}(t),r=i.style,s=window.ShopAR__THREE,a=s.THREE,c=new a.WebGLRenderer({powerPreference:"high-performance",canvas:i,antialias:!0,alpha:!0});c.outputEncoding=a.sRGBEncoding,c.toneMapping=a.ACESFilmicToneMapping,c.setPixelRatio(2*window.devicePixelRatio),c.setClearColor(new a.Color(16777215));const l=new a.PerspectiveCamera(25,1,.5),d=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);l.position.set(d[0],d[1],d[2]);const u=new a.Scene,h=new s.OrbitControls(l,c.domElement);h.target.set(0,.03,0),h.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>o.previewInteracted()),200);h.addEventListener("end",e)}{const e=new a.AmbientLight(new a.Color(16777215),.7),t=new a.RectAreaLight(new a.Color(16772574),10,50,50);t.position.set(-40,60,60);const n=new a.RectAreaLight(new a.Color(16774893),15,50,50);n.position.set(60,60,0),u.add(e,t,n)}const p=()=>{const e=i.clientWidth,t=i.clientHeight;i.width===e&&i.height===t||(c.setSize(e,t,!1),l.aspect=e/t,l.updateProjectionMatrix())};let f;let v=!1;const w=()=>{v||(requestAnimationFrame(w),p(),h.update(),c.render(u,l))};return{domElement:i,updateSize:p,parse:(t,n)=>e(void 0,void 0,void 0,(function*(){return null==f&&(f=e(void 0,void 0,void 0,(function*(){const e=new s.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}(s,e.parse(n));o.mapping=a.EquirectangularReflectionMapping,o.encoding=a.LinearEncoding,u.environment=o;const i=(new s.GLTFLoader).setDRACOLoader((new s.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new s.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(c)).setMeshoptDecoder("function"==typeof s.MeshoptDecoder?s.MeshoptDecoder():s.MeshoptDecoder),r=yield i.parseAsync(t.buffer,"");u.add(r.scene),u.traverse((e=>{if(e instanceof a.Mesh){const t=e.material;t.depthWrite=!0,t instanceof a.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))),f})),isPaused:()=>v,resume:()=>{v=!1,w(),r.display=""},pause:()=>{v=!0,r.display="none"}}};function z(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 G="shopar-error";function K(){const e=document.createElement("div");e.id=G;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=z("4rem",`${C}/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}function O(e,t){return P(e,t.id,(()=>function(e){const{id:t,iconUrl:n,textContent:o,ariaLabel:i}=e,r=document.createElement("button");if(r.id=t,r.type="button",r.className="shopar-btn",null!=n){const e=z("1.75rem",n);r.appendChild(e)}{const e=document.createElement("span");e.textContent=o,r.appendChild(e)}return r.ariaLabel=i,r.style.zIndex="2",r}(t)))}const W="shopar-control";function F(){const e=document.createElement("div");return e.id=W,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e}const H="shopar-loading";function V(){const e=document.createElement("div");e.id=H;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}const q="shopar-main";function B(e){return P(e,q,X)}function X(){const e=document.createElement("div");e.id=q;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}function Y(t){return e(this,void 0,void 0,(function*(){const n=yield h((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(t).catch((e=>{throw console.error(e),new Error("Resource unavailable.")}));if(!e.ok)throw new Error(`Resource download failed with status ${e.status}.`);return e}))));try{return yield(yield n.blob()).arrayBuffer()}catch(e){throw console.error(e),new Error("Resource has invalid body.")}}))}function J(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())}))}function Q(n,o,i){return e(this,void 0,void 0,(function*(){let r="glb".charCodeAt(0),s="3d".charCodeAt(0);r>>=1,r+=s;const l=String.fromCharCode(r+14);s>>=2,r^=s+2;const d=String.fromCharCode(r-3);r^=5;const u=String.fromCharCode(r-7);r^=2;const p=String.fromCharCode(r-7),f=window[`${u}${d+l+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${p}`],v=yield h((()=>e(this,void 0,void 0,(function*(){const e=yield f(`${n}`).catch((()=>{throw new Error("Unknown error.")}));if(!e.ok)throw new Error("Unknown error.");return e})))),w=new t[0](yield(yield v.blob().catch((()=>{throw new Error("Unknown error.")}))).arrayBuffer().catch((()=>{throw new Error("Unknown error.")})));i+=s;const m=w.byteLength;if(c[a]=Math.min(76,o)*((r>>5)-1),152===c[a]&&(c[a]=m),i==a)return w;i=a,i^=a;for(let e=0,t=w.length;e<t;e++)c[a]^=c[i]<<13,c[a]^=c[i]>>17,c[a]^=c[i]<<5,w[e]=w[e]^c[a];return i^=c[0],i^=c[1],i^=c[2],w}))}function Z(e,t){if(null==e)throw new Error(`'${t}' not specified.`)}function ee(e,t){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`)}const te=["AR","3D"];function ne(e){const{apiKey:t,sku:n,targetElement:o,initialState:i,baseUrl:r,defaultUI:s}=e;Z(t,"apiKey"),ee(t,"apiKey"),Z(n,"sku"),ee(n,"sku"),Z(o,"targetElement"),function(e,t){if(!(e instanceof HTMLElement))throw new Error(`'${t}' must be an HTMLElement.`)}(o,"targetElement"),null!=i&&function(e,t,n){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`);if(!n.includes(e))throw new Error(`'${t}' must be ${n.join("' or '")}.`)}(i,"initialState",te),null!=r&&ee(r,"baseUrl"),null!=s&&function(e,t){if("boolean"!=typeof e)throw new Error(`'${t}' must be a boolean.`)}(s,"defaultUI")}function oe(e){const t=getComputedStyle(e);if(!["static","relative"].includes(t.position))throw new Error(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${t.position}'.`)}var ie;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(ie||(ie={}));let re,se=ie.None,ae=0;const ce=[];let le;function de(){if(se===ie.AR){const e=I();null==e||e.isPaused()||e.pause()}else if(se===ie.Preview){const e=T();null==e||e.isPaused()||e.pause()}}function ue(e){if(se=e,se===ie.None){const e=ce.filter((e=>null!=e));re.replaceChildren(...e)}else re.replaceChildren(le)}function he(t){return e(this,void 0,void 0,(function*(){return ne(t),null==t.defaultUI||t.defaultUI?function(t){var n,o;return e(this,void 0,void 0,(function*(){const{apiKey:i,sku:r,targetElement:s,initialState:a}=t;oe(s),C.update(t.baseUrl);const c=p(i,r),l=g(J(`${C}/shopar-analytics.js`));l.init(i,r),s.style.position="relative";const d=B(s);re=P(d,W,F),le=O(null,{id:"shopar-btn-close",textContent:"Close",ariaLabel:"Close ShopAR view",iconUrl:`${C}/img/icons/close.svg`}),le.onclick=()=>{u.hide(),h.hide(),de(),ue(ie.None)};const u=function(e){return R(e,H,V)}(d),h=function(e){return R(e,G,K)}(d),{category:f,arUrl:w,arKey:m,previewUrl:y}=yield c;if(l.initialized(),null!=w&&v(f)){const t=Promise.all([J(`${C}/shopar-deepar.js`),Y(w),..."Glasses"===f?[J(`${C}/shopar-true-scale.js`)]:[]]),n={Glasses:`${C}/img/icons/glasses.svg`,Shoes:`${C}/img/icons/shoe.svg`,Watches:`${C}/img/icons/watch.svg`},o=null!=f&&n[f]||n.Glasses;ce[ie.AR]=O(re,{id:"shopar-btn-vto",textContent:"Try-on",ariaLabel:"Launch ShopAR virtual try-on",iconUrl:o}),ce[ie.AR].onclick=()=>e(this,void 0,void 0,(function*(){const e=++ae,n=ie.AR,o=se===n?ie.None:n;if(de(),ue(o),se===ie.None)return;l.vtoLaunched(),h.hide(),u.show();const[,i]=yield t;if(ae!==e)return;const r=U(d);try{yield r.init(m,f)}catch(e){return l.vtoCameraDenied(e),u.hide(),void h.show()}ae===e&&(yield r.parse(i),ae===e&&(r.resume(),u.hide()))}))}if(null!=y){const t=255,n=Promise.all([J(`${C}/shopar-three.js`),Q(y,(new Date).getTime()+t,Math.random()*t),Y(`${C}/env/studio5.hdr`)]);ce[ie.Preview]=O(re,{id:"shopar-btn-3d",textContent:"3D",ariaLabel:"Launch ShopAR 3D preview",iconUrl:`${C}/img/icons/cube.svg`}),ce[ie.Preview].onclick=()=>e(this,void 0,void 0,(function*(){const e=++ae,t=ie.Preview,o=se===t?ie.None:t;if(de(),ue(o),se===ie.None)return;l.previewLaunched(),h.hide(),u.show();const[,i,r]=yield n;if(ae!==e)return;const s=j(d,f);s.updateSize(),yield s.parse(i,r),ae===e&&(s.resume(),u.hide())}))}"AR"===a?null===(n=ce[ie.AR])||void 0===n||n.click():"3D"===a&&(null===(o=ce[ie.Preview])||void 0===o||o.click())}))}(t):function(t){return e(this,void 0,void 0,(function*(){const{apiKey:n,sku:o,targetElement:i,initialState:r}=t;oe(i),C.update(t.baseUrl);const s=p(n,o),a=g(J(`${C}/shopar-analytics.js`));a.init(n,o),i.style.position="relative";const c=B(i),{category:l,arUrl:d,arKey:u,previewUrl:h}=yield s;a.initialized();let f={enabled:!1};if(null!=d&&v(l)){const e=Promise.all([J(`${C}/shopar-deepar.js`),Y(d),..."Glasses"===l?[J(`${C}/shopar-true-scale.js`)]:[]]);f={enabled:!0,predownload:e,category:l}}const w=()=>e(this,void 0,void 0,(function*(){if(!f.enabled)throw new Error("Model does not have AR enabled.");if(se===ie.AR)throw new Error("AR already launched.");const e=++ae;if(se===ie.Preview){const e=T();null==e||e.isPaused()||e.pause()}se=ie.AR,a.vtoLaunched();const[,t]=yield f.predownload;if(ae!==e)return;const n=U(c);try{yield n.init(u,f.category)}catch(e){throw a.vtoCameraDenied(e),e}ae===e&&(yield n.parse(t),ae===e&&n.resume())}));let m={enabled:!1};if(null!=h){const e=255,t=Promise.all([J(`${C}/shopar-three.js`),Q(h,(new Date).getTime()+e,Math.random()*e),Y(`${C}/env/studio5.hdr`)]);m={enabled:!0,predownload:t}}const y=()=>e(this,void 0,void 0,(function*(){if(!m.enabled)throw new Error("Model does not have AR enabled.");if(se===ie.Preview)throw new Error("3D already launched.");const e=++ae;if(se===ie.AR){const e=I();null==e||e.isPaused()||e.pause()}se=ie.Preview,a.previewLaunched();const[,t,n]=yield m.predownload;if(ae!==e)return;const o=j(c,l);o.updateSize(),yield o.parse(t,n),ae===e&&o.resume()}));return"AR"===r?yield w():"3D"===r&&(yield y()),{launchAR:w,launch3D:y,closeAR:()=>e(this,void 0,void 0,(function*(){if(se!==ie.AR)throw new Error("AR not launched.");const e=I();null==e||e.isPaused()||e.pause(),se=ie.None})),close3D:()=>e(this,void 0,void 0,(function*(){if(se!==ie.Preview)throw new Error("3D not launched.");const e=T();null==e||e.isPaused()||e.pause(),se=ie.None}))}}))}(t)}))}const pe={setup:function(t){return e(this,void 0,void 0,(function*(){return function(t,n){return e(this,void 0,void 0,(function*(){try{return n()}catch(e){throw e instanceof Error?new pe.PluginError(`${t} failed: ${e.message}`):(console.error(e),new pe.PluginError(`${t} failed.`))}}))}("setup",(()=>he(t)))}))},version:E,PluginError:class extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}};export{pe as plugin};
1
+ function e(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{c(o.next(e))}catch(e){r(e)}}function a(e){try{c(o.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const t=[Uint8Array,Uint32Array],n=t.length-2,o=["addEventListener","alert","atob","blur","btoa","cancelAnimationFrame","localStorage","location","locationbar","hash","hasOwnProperty","host","hostname","href","requestAnimationFrame"],i=[2,12,7,1],r=window[o[i[i[n]]]][o[i[i[i.length-1]]]],s=r.charCodeAt(n),a=s^s,c=new t[1]([a]);function l(){const e=(new TextEncoder).encode(r),t=o.map((e=>e.length));for(let n=0;n<e.length;n++)e[n]^=t[n%t.length];return window[o[t[2]]](String.fromCodePoint(...e))}const d=[1e3,2e3,4e3],u=d.length;function h(t,n=0){return e(this,void 0,void 0,(function*(){try{return yield t()}catch(e){if(n>=u)throw e;return yield new Promise((e=>setTimeout(e,d[n]))),h(t,n+1)}}))}function p(t,n){return e(this,void 0,void 0,(function*(){const o=yield h((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:t,sku:n,sid:l()})}`).catch((e=>{throw console.error(e),new Error("API unavailable.")}));if(!e.ok)throw new Error(`API call failed with status ${e.status}.`);return e}))));try{return yield o.json()}catch(e){throw console.error(e),new Error("API returned invalid body.")}}))}const f=["Glasses","Shoes","Watches"];function v(e){return null!=e&&f.includes(e)}let w;const m=()=>w,g=e=>(null==w&&(w=y(e)),w),y=e=>{let t=!1;const n=[];e.then((()=>{t=!0,n.forEach((e=>e()))}));const o=e=>{t?e():n.push(e)},i={organizationId:"Unknown",sku:"Unknown"};let r,s=0;function a(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),i),{vtoTime:(performance.now()-s)/1e3,vtoSessionId:r}),o((()=>{const{trackEventImpl:n}=window.ShopAR__analytics;n(e,t)}))}return{init:function(e,t){i.organizationId=e||"Unknown",i.sku=t||"Unknown",s=performance.now(),o((()=>{const{initializeImpl:e,uuidv4:t}=window.ShopAR__analytics;e(),r=t()}))},initialized:()=>a("initialized"),vtoLaunched:()=>a("vto_launched"),vtoCameraAsked:()=>a("vto_camera_asked"),vtoCameraGranted:()=>a("vto_camera_granted"),vtoCameraDenied:e=>a("vto_camera_denied",{error:e}),vtoInitSuccess:()=>a("vto_init_success"),previewLaunched:()=>a("preview_launched"),previewInteracted:()=>a("preview_interacted")}};const E="0.0.4";let b=`https://cdn.jsdelivr.net/npm/shopar-plugin@${E}/dist`;const C={update:e=>{null!=e&&(b=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>b},A=10;function S(t,n){return e(this,void 0,void 0,(function*(){const{ShopAR__TrueScale:o}=window;yield o.initialize(`${C}/wasm/mediapipe`,t);const i=[];setInterval((()=>e(this,void 0,void 0,(function*(){const{error:e,ipd:t}=yield o.predict(performance.now());if(null==e){!function(e,t){e.length>A&&e.splice(0,1);e.push(t)}(i,t);const e=62/(i.reduce(((e,t)=>t+e),0)/i.length);n.changeParameterVector("GLASSES","","scale",e,e,e,0),n.changeParameterVector("shopar_glasses","","scale",e,e,e,0)}}))),200)}))}function P(e,t,n){const o=null==e?void 0:e.querySelector(`#${t}`);if(null!=o)return o;const i=n();return null==e||e.appendChild(i),i}function R(e,t,n){const o=P(e,t,n),i=o.style;return{element:o,show:()=>{i.display=""},hide:()=>{i.display="none"}}}const $="shopar-deepar-output";function L(){const e=document.createElement("div");e.id=$;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}let k;const I=()=>k,U=e=>(null==k&&(k=x(e)),k),x=t=>{const n=m(),o=function(e){return P(e,$,L)}(t),i=o.style;let r,s;let a;let c=!0;return{domElement:o,init:(t,i)=>e(void 0,void 0,void 0,(function*(){const a=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i),c=function(e){var t;const n={Glasses:"user",Shoes:"environment",Watches:"environment"};return null==e?"user":null!==(t=n[e])&&void 0!==t?t:"user"}(i);return null==s&&(s=e(void 0,void 0,void 0,(function*(){if(null==(null===(s=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===s?void 0:s.getUserMedia))throw new Error("No camera available!");var s;const l=function(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}(c);n.vtoCameraAsked(),r=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:t||"your_license_key_goes_here",previewElement:o,additionalOptions:{hint:a,cameraConfig:{disableDefaultCamera:!0}}});const d=yield function(t){return e(this,void 0,void 0,(function*(){const e=document.createElement("video");return e.setAttribute("playsinline","playsinline"),e.srcObject=yield t,e}))}(l);n.vtoCameraGranted(),yield function(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()}))))}}))}))}(r,d,"user"===c),"Glasses"===i&&S(d,r),n.vtoInitSuccess()}))),s})),parse:t=>e(void 0,void 0,void 0,(function*(){return null==a&&(a=r.switchEffect(t)),a})),isPaused:()=>c,resume:()=>{c=!1,r.setPaused(c),i.display=""},pause:()=>{c=!0,r.setPaused(c),i.display="none"}}};const D="shopar-three-output";function _(){const e=document.createElement("canvas");e.id=D;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",e}let M;const T=()=>M,j=(e,t)=>(null==M&&(M=N(e,t)),M),N=(t,n)=>{const o=m(),i=function(e){return P(e,D,_)}(t),r=i.style,s=window.ShopAR__THREE,a=s.THREE,c=new a.WebGLRenderer({powerPreference:"high-performance",canvas:i,antialias:!0,alpha:!0});c.outputEncoding=a.sRGBEncoding,c.toneMapping=a.ACESFilmicToneMapping,c.setPixelRatio(2*window.devicePixelRatio),c.setClearColor(new a.Color(16777215));const l=new a.PerspectiveCamera(25,1,.5),d=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);l.position.set(d[0],d[1],d[2]);const u=new a.Scene,h=new s.OrbitControls(l,c.domElement);h.target.set(0,.03,0),h.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>o.previewInteracted()),200);h.addEventListener("end",e)}{const e=new a.AmbientLight(new a.Color(16777215),.7),t=new a.RectAreaLight(new a.Color(16772574),10,50,50);t.position.set(-40,60,60);const n=new a.RectAreaLight(new a.Color(16774893),15,50,50);n.position.set(60,60,0),u.add(e,t,n)}const p=()=>{const e=i.clientWidth,t=i.clientHeight;i.width===e&&i.height===t||(c.setSize(e,t,!1),l.aspect=e/t,l.updateProjectionMatrix())};let f;let v=!1;const w=()=>{v||(requestAnimationFrame(w),p(),h.update(),c.render(u,l))};return{domElement:i,updateSize:p,parse:(t,n)=>e(void 0,void 0,void 0,(function*(){return null==f&&(f=e(void 0,void 0,void 0,(function*(){const e=new s.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}(s,e.parse(n));o.mapping=a.EquirectangularReflectionMapping,o.encoding=a.LinearEncoding,u.environment=o;const i=(new s.GLTFLoader).setDRACOLoader((new s.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new s.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(c)).setMeshoptDecoder("function"==typeof s.MeshoptDecoder?s.MeshoptDecoder():s.MeshoptDecoder),r=yield i.parseAsync(t.buffer,"");u.add(r.scene),u.traverse((e=>{if(e instanceof a.Mesh){const t=e.material;t.depthWrite=!0,t instanceof a.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))),f})),isPaused:()=>v,resume:()=>{v=!1,w(),r.display=""},pause:()=>{v=!0,r.display="none"}}};function z(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 G="shopar-error";function K(){const e=document.createElement("div");e.id=G;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=z("4rem",`${C}/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}function O(e,t){return P(e,t.id,(()=>function(e){const{id:t,iconUrl:n,textContent:o,ariaLabel:i}=e,r=document.createElement("button");if(r.id=t,r.type="button",r.className="shopar-btn",null!=n){const e=z("1.75rem",n);r.appendChild(e)}{const e=document.createElement("span");e.textContent=o,r.appendChild(e)}return r.ariaLabel=i,r.style.zIndex="2",r}(t)))}const W="shopar-control";function F(){const e=document.createElement("div");return e.id=W,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e}const H="shopar-loading";function V(){const e=document.createElement("div");e.id=H;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}const q="shopar-main";function B(e){return P(e,q,X)}function X(){const e=document.createElement("div");e.id=q;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}function Y(t){return e(this,void 0,void 0,(function*(){const n=yield h((()=>e(this,void 0,void 0,(function*(){const e=yield fetch(t).catch((e=>{throw console.error(e),new Error("Resource unavailable.")}));if(!e.ok)throw new Error(`Resource download failed with status ${e.status}.`);return e}))));try{return yield(yield n.blob()).arrayBuffer()}catch(e){throw console.error(e),new Error("Resource has invalid body.")}}))}function J(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())}))}function Q(n,o,i){return e(this,void 0,void 0,(function*(){let r="glb".charCodeAt(0),s="3d".charCodeAt(0);r>>=1,r+=s;const l=String.fromCharCode(r+14);s>>=2,r^=s+2;const d=String.fromCharCode(r-3);r^=5;const u=String.fromCharCode(r-7);r^=2;const p=String.fromCharCode(r-7),f=window[`${u}${d+l+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${p}`],v=yield h((()=>e(this,void 0,void 0,(function*(){const e=yield f(`${n}`).catch((()=>{throw new Error("Unknown error.")}));if(!e.ok)throw new Error("Unknown error.");return e})))),w=new t[0](yield(yield v.blob().catch((()=>{throw new Error("Unknown error.")}))).arrayBuffer().catch((()=>{throw new Error("Unknown error.")})));i+=s;const m=w.byteLength;if(c[a]=Math.min(76,o)*((r>>5)-1),152===c[a]&&(c[a]=m),i==a)return w;i=a,i^=a;for(let e=0,t=w.length;e<t;e++)c[a]^=c[i]<<13,c[a]^=c[i]>>17,c[a]^=c[i]<<5,w[e]=w[e]^c[a];return i^=c[0],i^=c[1],i^=c[2],w}))}function Z(e,t){if(null==e)throw new Error(`'${t}' not specified.`)}function ee(e,t){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`)}const te=["AR","3D"];function ne(e){const{apiKey:t,sku:n,targetElement:o,initialState:i,baseUrl:r,defaultUI:s}=e;Z(t,"apiKey"),ee(t,"apiKey"),Z(n,"sku"),ee(n,"sku"),Z(o,"targetElement"),function(e,t){if(!(e instanceof HTMLElement))throw new Error(`'${t}' must be an HTMLElement.`)}(o,"targetElement"),null!=i&&function(e,t,n){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`);if(!n.includes(e))throw new Error(`'${t}' must be ${n.join("' or '")}.`)}(i,"initialState",te),null!=r&&ee(r,"baseUrl"),null!=s&&function(e,t){if("boolean"!=typeof e)throw new Error(`'${t}' must be a boolean.`)}(s,"defaultUI")}function oe(e){const t=getComputedStyle(e);if(!["static","relative"].includes(t.position))throw new Error(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${t.position}'.`)}var ie;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(ie||(ie={}));let re,se=ie.None,ae=0;const ce=[];let le;function de(){if(se===ie.AR){const e=I();null==e||e.isPaused()||e.pause()}else if(se===ie.Preview){const e=T();null==e||e.isPaused()||e.pause()}}function ue(e){if(se=e,se===ie.None){const e=ce.filter((e=>null!=e));re.replaceChildren(...e)}else re.replaceChildren(le)}function he(t){return e(this,void 0,void 0,(function*(){return ne(t),null==t.defaultUI||t.defaultUI?function(t){var n,o;return e(this,void 0,void 0,(function*(){const{apiKey:i,sku:r,targetElement:s,initialState:a}=t;oe(s),C.update(t.baseUrl);const c=p(i,r),l=g(J(`${C}/shopar-analytics.js`));l.init(i,r),s.style.position="relative";const d=B(s);re=P(d,W,F),le=O(null,{id:"shopar-btn-close",textContent:"Close",ariaLabel:"Close ShopAR view",iconUrl:`${C}/img/icons/close.svg`}),le.onclick=()=>{u.hide(),h.hide(),de(),ue(ie.None)};const u=function(e){return R(e,H,V)}(d),h=function(e){return R(e,G,K)}(d),{category:f,arUrl:w,arKey:m,previewUrl:y}=yield c;if(l.initialized(),null!=w&&v(f)){const t=Promise.all([J(`${C}/shopar-deepar.js`),Y(w),..."Glasses"===f?[J(`${C}/shopar-true-scale.js`)]:[]]),n={Glasses:`${C}/img/icons/glasses.svg`,Shoes:`${C}/img/icons/shoe.svg`,Watches:`${C}/img/icons/watch.svg`},o=null!=f&&n[f]||n.Glasses;ce[ie.AR]=O(re,{id:"shopar-btn-vto",textContent:"Try-on",ariaLabel:"Launch ShopAR virtual try-on",iconUrl:o}),ce[ie.AR].onclick=()=>e(this,void 0,void 0,(function*(){const e=++ae,n=ie.AR,o=se===n?ie.None:n;if(de(),ue(o),se===ie.None)return;l.vtoLaunched(),h.hide(),u.show();const[,i]=yield t;if(ae!==e)return;const r=U(d);try{yield r.init(m,f)}catch(e){return l.vtoCameraDenied(e),u.hide(),void h.show()}ae===e&&(yield r.parse(i),ae===e&&(r.resume(),u.hide()))}))}if(null!=y){const t=255,n=Promise.all([J(`${C}/shopar-three.js`),Q(y,(new Date).getTime()+t,Math.random()*t),Y(`${C}/env/studio5.hdr`)]);ce[ie.Preview]=O(re,{id:"shopar-btn-3d",textContent:"3D",ariaLabel:"Launch ShopAR 3D preview",iconUrl:`${C}/img/icons/cube.svg`}),ce[ie.Preview].onclick=()=>e(this,void 0,void 0,(function*(){const e=++ae,t=ie.Preview,o=se===t?ie.None:t;if(de(),ue(o),se===ie.None)return;l.previewLaunched(),h.hide(),u.show();const[,i,r]=yield n;if(ae!==e)return;const s=j(d,f);s.updateSize(),yield s.parse(i,r),ae===e&&(s.resume(),u.hide())}))}"AR"===a?null===(n=ce[ie.AR])||void 0===n||n.click():"3D"===a&&(null===(o=ce[ie.Preview])||void 0===o||o.click())}))}(t):function(t){return e(this,void 0,void 0,(function*(){const{apiKey:n,sku:o,targetElement:i,initialState:r}=t;oe(i),C.update(t.baseUrl);const s=p(n,o),a=g(J(`${C}/shopar-analytics.js`));a.init(n,o),i.style.position="relative";const c=B(i),{category:l,arUrl:d,arKey:u,previewUrl:h}=yield s;a.initialized();let f={enabled:!1};if(null!=d&&v(l)){const e=Promise.all([J(`${C}/shopar-deepar.js`),Y(d),..."Glasses"===l?[J(`${C}/shopar-true-scale.js`)]:[]]);f={enabled:!0,predownload:e,category:l}}const w=()=>e(this,void 0,void 0,(function*(){if(!f.enabled)throw new Error("Model does not have AR enabled.");if(se===ie.AR)throw new Error("AR already launched.");const e=++ae;if(se===ie.Preview){const e=T();null==e||e.isPaused()||e.pause()}se=ie.AR,a.vtoLaunched();const[,t]=yield f.predownload;if(ae!==e)return;const n=U(c);try{yield n.init(u,f.category)}catch(e){throw a.vtoCameraDenied(e),e}ae===e&&(yield n.parse(t),ae===e&&n.resume())}));let m={enabled:!1};if(null!=h){const e=255,t=Promise.all([J(`${C}/shopar-three.js`),Q(h,(new Date).getTime()+e,Math.random()*e),Y(`${C}/env/studio5.hdr`)]);m={enabled:!0,predownload:t}}const y=()=>e(this,void 0,void 0,(function*(){if(!m.enabled)throw new Error("Model does not have AR enabled.");if(se===ie.Preview)throw new Error("3D already launched.");const e=++ae;if(se===ie.AR){const e=I();null==e||e.isPaused()||e.pause()}se=ie.Preview,a.previewLaunched();const[,t,n]=yield m.predownload;if(ae!==e)return;const o=j(c,l);o.updateSize(),yield o.parse(t,n),ae===e&&o.resume()}));return"AR"===r?yield w():"3D"===r&&(yield y()),{launchAR:w,launch3D:y,closeAR:()=>e(this,void 0,void 0,(function*(){if(se!==ie.AR)throw new Error("AR not launched.");const e=I();null==e||e.isPaused()||e.pause(),se=ie.None})),close3D:()=>e(this,void 0,void 0,(function*(){if(se!==ie.Preview)throw new Error("3D not launched.");const e=T();null==e||e.isPaused()||e.pause(),se=ie.None}))}}))}(t)}))}const pe={setup:function(t){return e(this,void 0,void 0,(function*(){return function(t,n){return e(this,void 0,void 0,(function*(){try{return n()}catch(e){throw e instanceof Error?new pe.PluginError(`${t} failed: ${e.message}`):(console.error(e),new pe.PluginError(`${t} failed.`))}}))}("setup",(()=>he(t)))}))},version:E,PluginError:class extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}};export{pe as plugin};
@@ -1 +1 @@
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{c(o.next(e))}catch(e){r(e)}}function a(e){try{c(o.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;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),c=a^a,l=new n[1]([c]);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(e,n=0){return t(this,void 0,void 0,(function*(){try{return yield e()}catch(t){if(n>=h)throw t;return yield new Promise((e=>setTimeout(e,u[n]))),p(e,n+1)}}))}function f(e,n){return t(this,void 0,void 0,(function*(){const o=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:e,sku:n,sid:d()})}`).catch((e=>{throw console.error(e),new Error("API unavailable.")}));if(!t.ok)throw new Error(`API call failed with status ${t.status}.`);return t}))));try{return yield o.json()}catch(e){throw console.error(e),new Error("API returned invalid body.")}}))}const v=["Glasses","Shoes","Watches"];function w(e){return null!=e&&v.includes(e)}let m;const g=()=>m,y=e=>(null==m&&(m=E(e)),m),E=e=>{let t=!1;const n=[];e.then((()=>{t=!0,n.forEach((e=>e()))}));const o=e=>{t?e():n.push(e)},i={organizationId:"Unknown",sku:"Unknown"};let r,s=0;function a(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),i),{vtoTime:(performance.now()-s)/1e3,vtoSessionId:r}),o((()=>{const{trackEventImpl:n}=window.ShopAR__analytics;n(e,t)}))}return{init:function(e,t){i.organizationId=e||"Unknown",i.sku=t||"Unknown",s=performance.now(),o((()=>{const{initializeImpl:e,uuidv4:t}=window.ShopAR__analytics;e(),r=t()}))},initialized:()=>a("initialized"),vtoLaunched:()=>a("vto_launched"),vtoCameraAsked:()=>a("vto_camera_asked"),vtoCameraGranted:()=>a("vto_camera_granted"),vtoCameraDenied:e=>a("vto_camera_denied",{error:e}),vtoInitSuccess:()=>a("vto_init_success"),previewLaunched:()=>a("preview_launched"),previewInteracted:()=>a("preview_interacted")}};const b="0.0.4-alpha.1";let C=`https://cdn.jsdelivr.net/npm/shopar-plugin@${b}/dist`;const A={update:e=>{null!=e&&(C=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>C},S=10;function R(e,n){return t(this,void 0,void 0,(function*(){const{ShopAR__TrueScale:o}=window;yield o.initialize(`${A}/wasm/mediapipe`,e);const i=[];setInterval((()=>t(this,void 0,void 0,(function*(){const{error:e,ipd:t}=yield o.predict(performance.now());if(null==e){!function(e,t){e.length>S&&e.splice(0,1);e.push(t)}(i,t);const e=62/(i.reduce(((e,t)=>t+e),0)/i.length);n.changeParameterVector("GLASSES","","scale",e,e,e,0),n.changeParameterVector("shopar_glasses","","scale",e,e,e,0)}}))),200)}))}function P(e,t,n){const o=null==e?void 0:e.querySelector(`#${t}`);if(null!=o)return o;const i=n();return null==e||e.appendChild(i),i}function $(e,t,n){const o=P(e,t,n),i=o.style;return{element:o,show:()=>{i.display=""},hide:()=>{i.display="none"}}}const L="shopar-deepar-output";function k(){const e=document.createElement("div");e.id=L;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}let x;const I=()=>x,U=e=>(null==x&&(x=D(e)),x),D=e=>{const n=g(),o=function(e){return P(e,L,k)}(e),i=o.style;let r,s;let a;let c=!0;return{domElement:o,init:(e,i)=>t(void 0,void 0,void 0,(function*(){const a=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i),c=function(e){var t;const n={Glasses:"user",Shoes:"environment",Watches:"environment"};return null==e?"user":null!==(t=n[e])&&void 0!==t?t:"user"}(i);return null==s&&(s=t(void 0,void 0,void 0,(function*(){if(null==(null===(s=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===s?void 0:s.getUserMedia))throw new Error("No camera available!");var s;const l=function(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}(c);n.vtoCameraAsked(),r=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:e||"your_license_key_goes_here",previewElement:o,additionalOptions:{hint:a,cameraConfig:{disableDefaultCamera:!0}}});const d=yield function(e){return t(this,void 0,void 0,(function*(){const t=document.createElement("video");return t.setAttribute("playsinline","playsinline"),t.srcObject=yield e,t}))}(l);n.vtoCameraGranted(),yield function(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()}))))}}))}))}(r,d,"user"===c),"Glasses"===i&&R(d,r),n.vtoInitSuccess()}))),s})),parse:e=>t(void 0,void 0,void 0,(function*(){return null==a&&(a=r.switchEffect(e)),a})),isPaused:()=>c,resume:()=>{c=!1,r.setPaused(c),i.display=""},pause:()=>{c=!0,r.setPaused(c),i.display="none"}}};const _="shopar-three-output";function T(){const e=document.createElement("canvas");e.id=_;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",e}let M;const j=()=>M,N=(e,t)=>(null==M&&(M=z(e,t)),M),z=(e,n)=>{const o=g(),i=function(e){return P(e,_,T)}(e),r=i.style,s=window.ShopAR__THREE,a=s.THREE,c=new a.WebGLRenderer({powerPreference:"high-performance",canvas:i,antialias:!0,alpha:!0});c.outputEncoding=a.sRGBEncoding,c.toneMapping=a.ACESFilmicToneMapping,c.setPixelRatio(2*window.devicePixelRatio),c.setClearColor(new a.Color(16777215));const l=new a.PerspectiveCamera(25,1,.5),d=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);l.position.set(d[0],d[1],d[2]);const u=new a.Scene,h=new s.OrbitControls(l,c.domElement);h.target.set(0,.03,0),h.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>o.previewInteracted()),200);h.addEventListener("end",e)}{const e=new a.AmbientLight(new a.Color(16777215),.7),t=new a.RectAreaLight(new a.Color(16772574),10,50,50);t.position.set(-40,60,60);const n=new a.RectAreaLight(new a.Color(16774893),15,50,50);n.position.set(60,60,0),u.add(e,t,n)}const p=()=>{const e=i.clientWidth,t=i.clientHeight;i.width===e&&i.height===t||(c.setSize(e,t,!1),l.aspect=e/t,l.updateProjectionMatrix())};let f;let v=!1;const w=()=>{v||(requestAnimationFrame(w),p(),h.update(),c.render(u,l))};return{domElement:i,updateSize:p,parse:(e,n)=>t(void 0,void 0,void 0,(function*(){return null==f&&(f=t(void 0,void 0,void 0,(function*(){const t=new s.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}(s,t.parse(n));o.mapping=a.EquirectangularReflectionMapping,o.encoding=a.LinearEncoding,u.environment=o;const i=(new s.GLTFLoader).setDRACOLoader((new s.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new s.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(c)).setMeshoptDecoder("function"==typeof s.MeshoptDecoder?s.MeshoptDecoder():s.MeshoptDecoder),r=yield i.parseAsync(e.buffer,"");u.add(r.scene),u.traverse((e=>{if(e instanceof a.Mesh){const t=e.material;t.depthWrite=!0,t instanceof a.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))),f})),isPaused:()=>v,resume:()=>{v=!1,w(),r.display=""},pause:()=>{v=!0,r.display="none"}}};function G(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 K="shopar-error";function O(){const e=document.createElement("div");e.id=K;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=G("4rem",`${A}/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}function W(e,t){return P(e,t.id,(()=>function(e){const{id:t,iconUrl:n,textContent:o,ariaLabel:i}=e,r=document.createElement("button");if(r.id=t,r.type="button",r.className="shopar-btn",null!=n){const e=G("1.75rem",n);r.appendChild(e)}{const e=document.createElement("span");e.textContent=o,r.appendChild(e)}return r.ariaLabel=i,r.style.zIndex="2",r}(t)))}const F="shopar-control";function H(){const e=document.createElement("div");return e.id=F,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e}const V="shopar-loading";function q(){const e=document.createElement("div");e.id=V;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}const B="shopar-main";function X(e){return P(e,B,Y)}function Y(){const e=document.createElement("div");e.id=B;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}function J(e){return t(this,void 0,void 0,(function*(){const n=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(e).catch((e=>{throw console.error(e),new Error("Resource unavailable.")}));if(!t.ok)throw new Error(`Resource download failed with status ${t.status}.`);return t}))));try{return yield(yield n.blob()).arrayBuffer()}catch(e){throw console.error(e),new Error("Resource has invalid body.")}}))}function Q(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())}))}function Z(e,o,i){return t(this,void 0,void 0,(function*(){let r="glb".charCodeAt(0),s="3d".charCodeAt(0);r>>=1,r+=s;const a=String.fromCharCode(r+14);s>>=2,r^=s+2;const d=String.fromCharCode(r-3);r^=5;const u=String.fromCharCode(r-7);r^=2;const h=String.fromCharCode(r-7),f=window[`${u}${d+a+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${h}`],v=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield f(`${e}`).catch((()=>{throw new Error("Unknown error.")}));if(!t.ok)throw new Error("Unknown error.");return t})))),w=new n[0](yield(yield v.blob().catch((()=>{throw new Error("Unknown error.")}))).arrayBuffer().catch((()=>{throw new Error("Unknown error.")})));i+=s;const m=w.byteLength;if(l[c]=Math.min(76,o)*((r>>5)-1),152===l[c]&&(l[c]=m),i==c)return w;i=c,i^=c;for(let e=0,t=w.length;e<t;e++)l[c]^=l[i]<<13,l[c]^=l[i]>>17,l[c]^=l[i]<<5,w[e]=w[e]^l[c];return i^=l[0],i^=l[1],i^=l[2],w}))}function ee(e,t){if(null==e)throw new Error(`'${t}' not specified.`)}function te(e,t){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`)}const ne=["AR","3D"];function oe(e){const{apiKey:t,sku:n,targetElement:o,initialState:i,baseUrl:r,defaultUI:s}=e;ee(t,"apiKey"),te(t,"apiKey"),ee(n,"sku"),te(n,"sku"),ee(o,"targetElement"),function(e,t){if(!(e instanceof HTMLElement))throw new Error(`'${t}' must be an HTMLElement.`)}(o,"targetElement"),null!=i&&function(e,t,n){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`);if(!n.includes(e))throw new Error(`'${t}' must be ${n.join("' or '")}.`)}(i,"initialState",ne),null!=r&&te(r,"baseUrl"),null!=s&&function(e,t){if("boolean"!=typeof e)throw new Error(`'${t}' must be a boolean.`)}(s,"defaultUI")}function ie(e){const t=getComputedStyle(e);if(!["static","relative"].includes(t.position))throw new Error(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${t.position}'.`)}var re;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(re||(re={}));let se,ae=re.None,ce=0;const le=[];let de;function ue(){if(ae===re.AR){const e=I();null==e||e.isPaused()||e.pause()}else if(ae===re.Preview){const e=j();null==e||e.isPaused()||e.pause()}}function he(e){if(ae=e,ae===re.None){const e=le.filter((e=>null!=e));se.replaceChildren(...e)}else se.replaceChildren(de)}function pe(e){return t(this,void 0,void 0,(function*(){return oe(e),null==e.defaultUI||e.defaultUI?function(e){var n,o;return t(this,void 0,void 0,(function*(){const{apiKey:i,sku:r,targetElement:s,initialState:a}=e;ie(s),A.update(e.baseUrl);const c=f(i,r),l=y(Q(`${A}/shopar-analytics.js`));l.init(i,r),s.style.position="relative";const d=X(s);se=P(d,F,H),de=W(null,{id:"shopar-btn-close",textContent:"Close",ariaLabel:"Close ShopAR view",iconUrl:`${A}/img/icons/close.svg`}),de.onclick=()=>{u.hide(),h.hide(),ue(),he(re.None)};const u=function(e){return $(e,V,q)}(d),h=function(e){return $(e,K,O)}(d),{category:p,arUrl:v,arKey:m,previewUrl:g}=yield c;if(l.initialized(),null!=v&&w(p)){const e=Promise.all([Q(`${A}/shopar-deepar.js`),J(v),..."Glasses"===p?[Q(`${A}/shopar-true-scale.js`)]:[]]),n={Glasses:`${A}/img/icons/glasses.svg`,Shoes:`${A}/img/icons/shoe.svg`,Watches:`${A}/img/icons/watch.svg`},o=null!=p&&n[p]||n.Glasses;le[re.AR]=W(se,{id:"shopar-btn-vto",textContent:"Try-on",ariaLabel:"Launch ShopAR virtual try-on",iconUrl:o}),le[re.AR].onclick=()=>t(this,void 0,void 0,(function*(){const t=++ce,n=re.AR,o=ae===n?re.None:n;if(ue(),he(o),ae===re.None)return;l.vtoLaunched(),h.hide(),u.show();const[,i]=yield e;if(ce!==t)return;const r=U(d);try{yield r.init(m,p)}catch(e){return l.vtoCameraDenied(e),u.hide(),void h.show()}ce===t&&(yield r.parse(i),ce===t&&(r.resume(),u.hide()))}))}if(null!=g){const e=255,n=Promise.all([Q(`${A}/shopar-three.js`),Z(g,(new Date).getTime()+e,Math.random()*e),J(`${A}/env/studio5.hdr`)]);le[re.Preview]=W(se,{id:"shopar-btn-3d",textContent:"3D",ariaLabel:"Launch ShopAR 3D preview",iconUrl:`${A}/img/icons/cube.svg`}),le[re.Preview].onclick=()=>t(this,void 0,void 0,(function*(){const e=++ce,t=re.Preview,o=ae===t?re.None:t;if(ue(),he(o),ae===re.None)return;l.previewLaunched(),h.hide(),u.show();const[,i,r]=yield n;if(ce!==e)return;const s=N(d,p);s.updateSize(),yield s.parse(i,r),ce===e&&(s.resume(),u.hide())}))}"AR"===a?null===(n=le[re.AR])||void 0===n||n.click():"3D"===a&&(null===(o=le[re.Preview])||void 0===o||o.click())}))}(e):function(e){return t(this,void 0,void 0,(function*(){const{apiKey:n,sku:o,targetElement:i,initialState:r}=e;ie(i),A.update(e.baseUrl);const s=f(n,o),a=y(Q(`${A}/shopar-analytics.js`));a.init(n,o),i.style.position="relative";const c=X(i),{category:l,arUrl:d,arKey:u,previewUrl:h}=yield s;a.initialized();let p={enabled:!1};if(null!=d&&w(l)){const e=Promise.all([Q(`${A}/shopar-deepar.js`),J(d),..."Glasses"===l?[Q(`${A}/shopar-true-scale.js`)]:[]]);p={enabled:!0,predownload:e,category:l}}const v=()=>t(this,void 0,void 0,(function*(){if(!p.enabled)throw new Error("Model does not have AR enabled.");if(ae===re.AR)throw new Error("AR already launched.");const e=++ce;if(ae===re.Preview){const e=j();null==e||e.isPaused()||e.pause()}ae=re.AR,a.vtoLaunched();const[,t]=yield p.predownload;if(ce!==e)return;const n=U(c);try{yield n.init(u,p.category)}catch(e){throw a.vtoCameraDenied(e),e}ce===e&&(yield n.parse(t),ce===e&&n.resume())}));let m={enabled:!1};if(null!=h){const e=255,t=Promise.all([Q(`${A}/shopar-three.js`),Z(h,(new Date).getTime()+e,Math.random()*e),J(`${A}/env/studio5.hdr`)]);m={enabled:!0,predownload:t}}const g=()=>t(this,void 0,void 0,(function*(){if(!m.enabled)throw new Error("Model does not have AR enabled.");if(ae===re.Preview)throw new Error("3D already launched.");const e=++ce;if(ae===re.AR){const e=I();null==e||e.isPaused()||e.pause()}ae=re.Preview,a.previewLaunched();const[,t,n]=yield m.predownload;if(ce!==e)return;const o=N(c,l);o.updateSize(),yield o.parse(t,n),ce===e&&o.resume()}));return"AR"===r?yield v():"3D"===r&&(yield g()),{launchAR:v,launch3D:g,closeAR:()=>t(this,void 0,void 0,(function*(){if(ae!==re.AR)throw new Error("AR not launched.");const e=I();null==e||e.isPaused()||e.pause(),ae=re.None})),close3D:()=>t(this,void 0,void 0,(function*(){if(ae!==re.Preview)throw new Error("3D not launched.");const e=j();null==e||e.isPaused()||e.pause(),ae=re.None}))}}))}(e)}))}const fe={setup:function(e){return t(this,void 0,void 0,(function*(){return function(e,n){return t(this,void 0,void 0,(function*(){try{return n()}catch(t){throw t instanceof Error?new fe.PluginError(`${e} failed: ${t.message}`):(console.error(t),new fe.PluginError(`${e} failed.`))}}))}("setup",(()=>pe(e)))}))},version:b,PluginError:class extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}};e.plugin=fe}));
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{c(o.next(e))}catch(e){r(e)}}function a(e){try{c(o.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;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),c=a^a,l=new n[1]([c]);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(e,n=0){return t(this,void 0,void 0,(function*(){try{return yield e()}catch(t){if(n>=h)throw t;return yield new Promise((e=>setTimeout(e,u[n]))),p(e,n+1)}}))}function f(e,n){return t(this,void 0,void 0,(function*(){const o=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(`https://dashboard.shopar.ai/plugin?${new URLSearchParams({apiKey:e,sku:n,sid:d()})}`).catch((e=>{throw console.error(e),new Error("API unavailable.")}));if(!t.ok)throw new Error(`API call failed with status ${t.status}.`);return t}))));try{return yield o.json()}catch(e){throw console.error(e),new Error("API returned invalid body.")}}))}const v=["Glasses","Shoes","Watches"];function w(e){return null!=e&&v.includes(e)}let m;const g=()=>m,y=e=>(null==m&&(m=E(e)),m),E=e=>{let t=!1;const n=[];e.then((()=>{t=!0,n.forEach((e=>e()))}));const o=e=>{t?e():n.push(e)},i={organizationId:"Unknown",sku:"Unknown"};let r,s=0;function a(e,t={}){t=Object.assign(Object.assign(Object.assign({},t),i),{vtoTime:(performance.now()-s)/1e3,vtoSessionId:r}),o((()=>{const{trackEventImpl:n}=window.ShopAR__analytics;n(e,t)}))}return{init:function(e,t){i.organizationId=e||"Unknown",i.sku=t||"Unknown",s=performance.now(),o((()=>{const{initializeImpl:e,uuidv4:t}=window.ShopAR__analytics;e(),r=t()}))},initialized:()=>a("initialized"),vtoLaunched:()=>a("vto_launched"),vtoCameraAsked:()=>a("vto_camera_asked"),vtoCameraGranted:()=>a("vto_camera_granted"),vtoCameraDenied:e=>a("vto_camera_denied",{error:e}),vtoInitSuccess:()=>a("vto_init_success"),previewLaunched:()=>a("preview_launched"),previewInteracted:()=>a("preview_interacted")}};const b="0.0.4";let C=`https://cdn.jsdelivr.net/npm/shopar-plugin@${b}/dist`;const A={update:e=>{null!=e&&(C=e.endsWith("/")?e.substring(0,e.length-1):e)},toString:()=>C},S=10;function R(e,n){return t(this,void 0,void 0,(function*(){const{ShopAR__TrueScale:o}=window;yield o.initialize(`${A}/wasm/mediapipe`,e);const i=[];setInterval((()=>t(this,void 0,void 0,(function*(){const{error:e,ipd:t}=yield o.predict(performance.now());if(null==e){!function(e,t){e.length>S&&e.splice(0,1);e.push(t)}(i,t);const e=62/(i.reduce(((e,t)=>t+e),0)/i.length);n.changeParameterVector("GLASSES","","scale",e,e,e,0),n.changeParameterVector("shopar_glasses","","scale",e,e,e,0)}}))),200)}))}function P(e,t,n){const o=null==e?void 0:e.querySelector(`#${t}`);if(null!=o)return o;const i=n();return null==e||e.appendChild(i),i}function $(e,t,n){const o=P(e,t,n),i=o.style;return{element:o,show:()=>{i.display=""},hide:()=>{i.display="none"}}}const L="shopar-deepar-output";function k(){const e=document.createElement("div");e.id=L;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}let x;const I=()=>x,U=e=>(null==x&&(x=D(e)),x),D=e=>{const n=g(),o=function(e){return P(e,L,k)}(e),i=o.style;let r,s;let a;let c=!0;return{domElement:o,init:(e,i)=>t(void 0,void 0,void 0,(function*(){const a=function(e){const t={Glasses:"rigidFaceTrackingInit",Shoes:"footInit",Watches:void 0};return null==e?void 0:t[e]}(i),c=function(e){var t;const n={Glasses:"user",Shoes:"environment",Watches:"environment"};return null==e?"user":null!==(t=n[e])&&void 0!==t?t:"user"}(i);return null==s&&(s=t(void 0,void 0,void 0,(function*(){if(null==(null===(s=null===navigator||void 0===navigator?void 0:navigator.mediaDevices)||void 0===s?void 0:s.getUserMedia))throw new Error("No camera available!");var s;const l=function(e){return navigator.mediaDevices.getUserMedia({video:{facingMode:e,frameRate:{ideal:30},width:{ideal:640},height:{ideal:360}}})}(c);n.vtoCameraAsked(),r=yield window.ShopAR__DeepAR.deepar.initialize({licenseKey:e||"your_license_key_goes_here",previewElement:o,additionalOptions:{hint:a,cameraConfig:{disableDefaultCamera:!0}}});const d=yield function(e){return t(this,void 0,void 0,(function*(){const t=document.createElement("video");return t.setAttribute("playsinline","playsinline"),t.srcObject=yield e,t}))}(l);n.vtoCameraGranted(),yield function(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()}))))}}))}))}(r,d,"user"===c),"Glasses"===i&&R(d,r),n.vtoInitSuccess()}))),s})),parse:e=>t(void 0,void 0,void 0,(function*(){return null==a&&(a=r.switchEffect(e)),a})),isPaused:()=>c,resume:()=>{c=!1,r.setPaused(c),i.display=""},pause:()=>{c=!0,r.setPaused(c),i.display="none"}}};const _="shopar-three-output";function T(){const e=document.createElement("canvas");e.id=_;const t=e.style;return t.position="absolute",t.top="0",t.left="0",t.width="100%",t.height="100%",t.zIndex="1",e}let M;const j=()=>M,N=(e,t)=>(null==M&&(M=z(e,t)),M),z=(e,n)=>{const o=g(),i=function(e){return P(e,_,T)}(e),r=i.style,s=window.ShopAR__THREE,a=s.THREE,c=new a.WebGLRenderer({powerPreference:"high-performance",canvas:i,antialias:!0,alpha:!0});c.outputEncoding=a.sRGBEncoding,c.toneMapping=a.ACESFilmicToneMapping,c.setPixelRatio(2*window.devicePixelRatio),c.setClearColor(new a.Color(16777215));const l=new a.PerspectiveCamera(25,1,.5),d=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);l.position.set(d[0],d[1],d[2]);const u=new a.Scene,h=new s.OrbitControls(l,c.domElement);h.target.set(0,.03,0),h.dampingFactor=.3;{const e=function(e,t){let n;return()=>{n||e(),clearTimeout(n),n=setTimeout((()=>{n=void 0}),t)}}((()=>o.previewInteracted()),200);h.addEventListener("end",e)}{const e=new a.AmbientLight(new a.Color(16777215),.7),t=new a.RectAreaLight(new a.Color(16772574),10,50,50);t.position.set(-40,60,60);const n=new a.RectAreaLight(new a.Color(16774893),15,50,50);n.position.set(60,60,0),u.add(e,t,n)}const p=()=>{const e=i.clientWidth,t=i.clientHeight;i.width===e&&i.height===t||(c.setSize(e,t,!1),l.aspect=e/t,l.updateProjectionMatrix())};let f;let v=!1;const w=()=>{v||(requestAnimationFrame(w),p(),h.update(),c.render(u,l))};return{domElement:i,updateSize:p,parse:(e,n)=>t(void 0,void 0,void 0,(function*(){return null==f&&(f=t(void 0,void 0,void 0,(function*(){const t=new s.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}(s,t.parse(n));o.mapping=a.EquirectangularReflectionMapping,o.encoding=a.LinearEncoding,u.environment=o;const i=(new s.GLTFLoader).setDRACOLoader((new s.DRACOLoader).setDecoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/draco/gltf/`)).setKTX2Loader((new s.KTX2Loader).setTranscoderPath(`https://cdn.jsdelivr.net/npm/three@0.${a.REVISION}.0/examples/jsm/libs/basis/`).detectSupport(c)).setMeshoptDecoder("function"==typeof s.MeshoptDecoder?s.MeshoptDecoder():s.MeshoptDecoder),r=yield i.parseAsync(e.buffer,"");u.add(r.scene),u.traverse((e=>{if(e instanceof a.Mesh){const t=e.material;t.depthWrite=!0,t instanceof a.MeshPhysicalMaterial&&t.transmissionMap&&(t.transmission=1,t.toneMapped=!1,t.fog=!1,t.needsUpdate=!0)}}))}))),f})),isPaused:()=>v,resume:()=>{v=!1,w(),r.display=""},pause:()=>{v=!0,r.display="none"}}};function G(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 K="shopar-error";function O(){const e=document.createElement("div");e.id=K;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=G("4rem",`${A}/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}function W(e,t){return P(e,t.id,(()=>function(e){const{id:t,iconUrl:n,textContent:o,ariaLabel:i}=e,r=document.createElement("button");if(r.id=t,r.type="button",r.className="shopar-btn",null!=n){const e=G("1.75rem",n);r.appendChild(e)}{const e=document.createElement("span");e.textContent=o,r.appendChild(e)}return r.ariaLabel=i,r.style.zIndex="2",r}(t)))}const F="shopar-control";function H(){const e=document.createElement("div");return e.id=F,e.role="group",e.ariaLabel="Preview type chooser",e.className="shopar-btn-container",e}const V="shopar-loading";function q(){const e=document.createElement("div");e.id=V;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}const B="shopar-main";function X(e){return P(e,B,Y)}function Y(){const e=document.createElement("div");e.id=B;const t=e.style;return t.position="absolute",t.top="0",t.bottom="0",t.left="0",t.right="0",e}function J(e){return t(this,void 0,void 0,(function*(){const n=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield fetch(e).catch((e=>{throw console.error(e),new Error("Resource unavailable.")}));if(!t.ok)throw new Error(`Resource download failed with status ${t.status}.`);return t}))));try{return yield(yield n.blob()).arrayBuffer()}catch(e){throw console.error(e),new Error("Resource has invalid body.")}}))}function Q(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())}))}function Z(e,o,i){return t(this,void 0,void 0,(function*(){let r="glb".charCodeAt(0),s="3d".charCodeAt(0);r>>=1,r+=s;const a=String.fromCharCode(r+14);s>>=2,r^=s+2;const d=String.fromCharCode(r-3);r^=5;const u=String.fromCharCode(r-7);r^=2;const h=String.fromCharCode(r-7),f=window[`${u}${d+a+(typeof[])[(+!+[]+ +!+[])*(+!+[]+ +!+[])]}${h}`],v=yield p((()=>t(this,void 0,void 0,(function*(){const t=yield f(`${e}`).catch((()=>{throw new Error("Unknown error.")}));if(!t.ok)throw new Error("Unknown error.");return t})))),w=new n[0](yield(yield v.blob().catch((()=>{throw new Error("Unknown error.")}))).arrayBuffer().catch((()=>{throw new Error("Unknown error.")})));i+=s;const m=w.byteLength;if(l[c]=Math.min(76,o)*((r>>5)-1),152===l[c]&&(l[c]=m),i==c)return w;i=c,i^=c;for(let e=0,t=w.length;e<t;e++)l[c]^=l[i]<<13,l[c]^=l[i]>>17,l[c]^=l[i]<<5,w[e]=w[e]^l[c];return i^=l[0],i^=l[1],i^=l[2],w}))}function ee(e,t){if(null==e)throw new Error(`'${t}' not specified.`)}function te(e,t){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`)}const ne=["AR","3D"];function oe(e){const{apiKey:t,sku:n,targetElement:o,initialState:i,baseUrl:r,defaultUI:s}=e;ee(t,"apiKey"),te(t,"apiKey"),ee(n,"sku"),te(n,"sku"),ee(o,"targetElement"),function(e,t){if(!(e instanceof HTMLElement))throw new Error(`'${t}' must be an HTMLElement.`)}(o,"targetElement"),null!=i&&function(e,t,n){if("string"!=typeof e)throw new Error(`'${t}' must be a string.`);if(!n.includes(e))throw new Error(`'${t}' must be ${n.join("' or '")}.`)}(i,"initialState",ne),null!=r&&te(r,"baseUrl"),null!=s&&function(e,t){if("boolean"!=typeof e)throw new Error(`'${t}' must be a boolean.`)}(s,"defaultUI")}function ie(e){const t=getComputedStyle(e);if(!["static","relative"].includes(t.position))throw new Error(`Invalid targetElement's position. Expected 'static' or 'relative', but found '${t.position}'.`)}var re;!function(e){e[e.None=0]="None",e[e.AR=1]="AR",e[e.Preview=2]="Preview"}(re||(re={}));let se,ae=re.None,ce=0;const le=[];let de;function ue(){if(ae===re.AR){const e=I();null==e||e.isPaused()||e.pause()}else if(ae===re.Preview){const e=j();null==e||e.isPaused()||e.pause()}}function he(e){if(ae=e,ae===re.None){const e=le.filter((e=>null!=e));se.replaceChildren(...e)}else se.replaceChildren(de)}function pe(e){return t(this,void 0,void 0,(function*(){return oe(e),null==e.defaultUI||e.defaultUI?function(e){var n,o;return t(this,void 0,void 0,(function*(){const{apiKey:i,sku:r,targetElement:s,initialState:a}=e;ie(s),A.update(e.baseUrl);const c=f(i,r),l=y(Q(`${A}/shopar-analytics.js`));l.init(i,r),s.style.position="relative";const d=X(s);se=P(d,F,H),de=W(null,{id:"shopar-btn-close",textContent:"Close",ariaLabel:"Close ShopAR view",iconUrl:`${A}/img/icons/close.svg`}),de.onclick=()=>{u.hide(),h.hide(),ue(),he(re.None)};const u=function(e){return $(e,V,q)}(d),h=function(e){return $(e,K,O)}(d),{category:p,arUrl:v,arKey:m,previewUrl:g}=yield c;if(l.initialized(),null!=v&&w(p)){const e=Promise.all([Q(`${A}/shopar-deepar.js`),J(v),..."Glasses"===p?[Q(`${A}/shopar-true-scale.js`)]:[]]),n={Glasses:`${A}/img/icons/glasses.svg`,Shoes:`${A}/img/icons/shoe.svg`,Watches:`${A}/img/icons/watch.svg`},o=null!=p&&n[p]||n.Glasses;le[re.AR]=W(se,{id:"shopar-btn-vto",textContent:"Try-on",ariaLabel:"Launch ShopAR virtual try-on",iconUrl:o}),le[re.AR].onclick=()=>t(this,void 0,void 0,(function*(){const t=++ce,n=re.AR,o=ae===n?re.None:n;if(ue(),he(o),ae===re.None)return;l.vtoLaunched(),h.hide(),u.show();const[,i]=yield e;if(ce!==t)return;const r=U(d);try{yield r.init(m,p)}catch(e){return l.vtoCameraDenied(e),u.hide(),void h.show()}ce===t&&(yield r.parse(i),ce===t&&(r.resume(),u.hide()))}))}if(null!=g){const e=255,n=Promise.all([Q(`${A}/shopar-three.js`),Z(g,(new Date).getTime()+e,Math.random()*e),J(`${A}/env/studio5.hdr`)]);le[re.Preview]=W(se,{id:"shopar-btn-3d",textContent:"3D",ariaLabel:"Launch ShopAR 3D preview",iconUrl:`${A}/img/icons/cube.svg`}),le[re.Preview].onclick=()=>t(this,void 0,void 0,(function*(){const e=++ce,t=re.Preview,o=ae===t?re.None:t;if(ue(),he(o),ae===re.None)return;l.previewLaunched(),h.hide(),u.show();const[,i,r]=yield n;if(ce!==e)return;const s=N(d,p);s.updateSize(),yield s.parse(i,r),ce===e&&(s.resume(),u.hide())}))}"AR"===a?null===(n=le[re.AR])||void 0===n||n.click():"3D"===a&&(null===(o=le[re.Preview])||void 0===o||o.click())}))}(e):function(e){return t(this,void 0,void 0,(function*(){const{apiKey:n,sku:o,targetElement:i,initialState:r}=e;ie(i),A.update(e.baseUrl);const s=f(n,o),a=y(Q(`${A}/shopar-analytics.js`));a.init(n,o),i.style.position="relative";const c=X(i),{category:l,arUrl:d,arKey:u,previewUrl:h}=yield s;a.initialized();let p={enabled:!1};if(null!=d&&w(l)){const e=Promise.all([Q(`${A}/shopar-deepar.js`),J(d),..."Glasses"===l?[Q(`${A}/shopar-true-scale.js`)]:[]]);p={enabled:!0,predownload:e,category:l}}const v=()=>t(this,void 0,void 0,(function*(){if(!p.enabled)throw new Error("Model does not have AR enabled.");if(ae===re.AR)throw new Error("AR already launched.");const e=++ce;if(ae===re.Preview){const e=j();null==e||e.isPaused()||e.pause()}ae=re.AR,a.vtoLaunched();const[,t]=yield p.predownload;if(ce!==e)return;const n=U(c);try{yield n.init(u,p.category)}catch(e){throw a.vtoCameraDenied(e),e}ce===e&&(yield n.parse(t),ce===e&&n.resume())}));let m={enabled:!1};if(null!=h){const e=255,t=Promise.all([Q(`${A}/shopar-three.js`),Z(h,(new Date).getTime()+e,Math.random()*e),J(`${A}/env/studio5.hdr`)]);m={enabled:!0,predownload:t}}const g=()=>t(this,void 0,void 0,(function*(){if(!m.enabled)throw new Error("Model does not have AR enabled.");if(ae===re.Preview)throw new Error("3D already launched.");const e=++ce;if(ae===re.AR){const e=I();null==e||e.isPaused()||e.pause()}ae=re.Preview,a.previewLaunched();const[,t,n]=yield m.predownload;if(ce!==e)return;const o=N(c,l);o.updateSize(),yield o.parse(t,n),ce===e&&o.resume()}));return"AR"===r?yield v():"3D"===r&&(yield g()),{launchAR:v,launch3D:g,closeAR:()=>t(this,void 0,void 0,(function*(){if(ae!==re.AR)throw new Error("AR not launched.");const e=I();null==e||e.isPaused()||e.pause(),ae=re.None})),close3D:()=>t(this,void 0,void 0,(function*(){if(ae!==re.Preview)throw new Error("3D not launched.");const e=j();null==e||e.isPaused()||e.pause(),ae=re.None}))}}))}(e)}))}const fe={setup:function(e){return t(this,void 0,void 0,(function*(){return function(e,n){return t(this,void 0,void 0,(function*(){try{return n()}catch(t){throw t instanceof Error?new fe.PluginError(`${e} failed: ${t.message}`):(console.error(t),new fe.PluginError(`${e} failed.`))}}))}("setup",(()=>pe(e)))}))},version:b,PluginError:class extends Error{constructor(e){super(`ShopAR Plugin: ${e}`)}}};e.plugin=fe}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shopar-plugin",
3
- "version": "0.0.4-alpha.1",
3
+ "version": "0.0.4",
4
4
  "description": "Plugin for the Web that seamlessly integrates into your webpage to create embedded virtual try-on and 3D preview capabilities.",
5
5
  "scripts": {
6
6
  "clean": "rm -rf dist",