lunchboxjs 2.1.17 → 2.2.0
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/dist/lunchboxjs.cjs +2 -2
- package/dist/lunchboxjs.js +457 -452
- package/dist/lunchboxjs.umd.cjs +2 -2
- package/package.json +1 -1
- package/src/html-anchor.ts +9 -6
- package/src/three-lunchbox.ts +16 -5
package/dist/lunchboxjs.cjs
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
* @license
|
|
25
25
|
* Copyright 2017 Google LLC
|
|
26
26
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
27
|
-
*/function lt(i){return y({...i,state:!0,attribute:!1})}var dt=Object.defineProperty,Se=(i,e,t,s)=>{for(var r=void 0,n=i.length-1,o;n>=0;n--)(o=i[n])&&(r=o(e,t,r)||r);return r&&dt(e,t,r),r};const Z="raycast",ye=[Z,"args","data"],Ee=i=>i.split("-");class U extends C{constructor(e){super(),this.classSource=e,this.instance=null,this.dispose=[],this.mutationObserver=null,this.tryAddOnce=!1,this.connected=!1}observeAttributes(){this.mutationObserver=new MutationObserver(e=>{e.forEach(t=>{var r;if(!t.attributeName||(r=Object.getPrototypeOf(this).constructor.observedAttributes)!=null&&r.includes(t.attributeName))return;const s=this.attributes.getNamedItem(t.attributeName);s&&this.updateProperty(s)})}),this.mutationObserver.observe(this,{attributes:!0})}parsedArgs(){const e=this.args??this.getAttribute("args")??[];return typeof e=="string"?JSON.parse(e):e}createUnderlyingThreeObject(){this.instance=new this.classSource(...this.parsedArgs().map(e=>G(e,this)))}refreshAttributes(){this.getAttributeNames().forEach(e=>{const t=this.attributes.getNamedItem(e);t&&this.updateProperty(t)}),Array.from(this.attributes).forEach(this.updateProperty.bind(this))}async onUnderlyingThreeObjectReady(){var s,r,n,o;const e=this.instance;e.uuid&&this.setAttribute(we,e.uuid);let t=fe(this);for(;this!=null&&this.connected&&!this.tryAddOnce&&!t;)await new Promise(requestAnimationFrame),t=fe(this);if(t&&t.instance||(s=t==null?void 0:t.three)!=null&&s.scene){const a=this.instance,h=this.instance,c=t.instance,d=t.instance??((r=t.three)==null?void 0:r.scene),l=this.instance;if((n=a.type)!=null&&n.toLowerCase().includes("geometry")&&c.geometry)c.geometry=a;else if((o=h.type)!=null&&o.toLowerCase().includes("material")&&c.material)c.material=h;else if(d.add)try{d.add(l),this.dispatchEvent(new CustomEvent("instanceadded",{detail:{instance:this.instance,parent:d}}))}catch{throw new Error(`Error adding ${this.tagName} to ${d}`)}const m=this.getAttribute("attach");m&&this.executeAttach(m,this.instance)}}connectedCallback(){super.connectedCallback(),this.observeAttributes.call(this),this.createUnderlyingThreeObject.call(this),this.refreshAttributes.call(this),this.dispatchEvent(new CustomEvent("instancecreated",{detail:{instance:this.instance}})),this.connected=!0,this.onUnderlyingThreeObjectReady.call(this)}updateProperty(e){const{name:t,value:s}=e;let r=t;Object.keys(this.instance??{}).forEach(a=>{a.toLowerCase()===r&&(r=a)});const n=Ee(r);if(ye.includes(r)||ye.includes(n[0]))return;let o=G(s,this);try{o=JSON.parse(s===""?"true":s)}catch{}this.instance&&K(this.instance,n,o)}executeAttach(e,t){const s=this.parentElement;s.instance&&K(s.instance,Ee(e),t)}disconnectedCallback(){super.disconnectedCallback();const e=[this.instance];this.disposeThreeObjects.call(this,e),this.connected=!1}disposeThreeObjects(e){e.forEach(t=>{var n,o;if(!t)return;const s=t,r=t;(n=s.dispose)==null||n.call(s),(o=r.removeFromParent)==null||o.call(r)})}render(){return se`<slot></slot>`}createRenderRoot(){return this}}Se([y({type:Boolean,attribute:"try-add-once"})],U.prototype,"tryAddOnce");Se([lt()],U.prototype,"connected");const Te=i=>{const e=typeof i=="string"?p[i]:i;if(!it(e))return null;const t=i.toString().toLowerCase().endsWith("loader");class s extends U{constructor(){super(e)}}class r extends U{constructor(){super(...arguments),this.loader=null}createUnderlyingThreeObject(){this.loader=new e(...this.parsedArgs().map(o=>G(o,this)))}async onUnderlyingThreeObjectReady(){const o=this.getAttribute("src");if(!o)throw new Error("Loader requires a source.");if(!this.loader)throw new Error(`Missing loader ${this.tagName}`);this.loader.load(o,a=>{this.instance=a;const h=this.getAttribute("attach");h&&this.executeAttach(h,a),this.refreshAttributes.call(this),this.dispatchEvent(new CustomEvent("loaded",{detail:{instance:a}}))},void 0,a=>{throw new Error(`error loading: ${o}`+a)})}disconnectedCallback(){super.disconnectedCallback(),this.disposeThreeObjects.call(this,[this.loader])}}return t?r:s};var ut=Object.defineProperty,A=(i,e,t,s)=>{for(var r=void 0,n=i.length-1,o;n>=0;n--)(o=i[n])&&(r=o(e,t,r)||r);return r&&ut(e,t,r),r};const pt="orthographic",$e=1/0,re=class re extends C{constructor(){super(),this.scratchV2=new p.Vector2,this.
|
|
27
|
+
*/function lt(i){return y({...i,state:!0,attribute:!1})}var dt=Object.defineProperty,Se=(i,e,t,s)=>{for(var r=void 0,n=i.length-1,o;n>=0;n--)(o=i[n])&&(r=o(e,t,r)||r);return r&&dt(e,t,r),r};const Z="raycast",ye=[Z,"args","data"],Ee=i=>i.split("-");class U extends C{constructor(e){super(),this.classSource=e,this.instance=null,this.dispose=[],this.mutationObserver=null,this.tryAddOnce=!1,this.connected=!1}observeAttributes(){this.mutationObserver=new MutationObserver(e=>{e.forEach(t=>{var r;if(!t.attributeName||(r=Object.getPrototypeOf(this).constructor.observedAttributes)!=null&&r.includes(t.attributeName))return;const s=this.attributes.getNamedItem(t.attributeName);s&&this.updateProperty(s)})}),this.mutationObserver.observe(this,{attributes:!0})}parsedArgs(){const e=this.args??this.getAttribute("args")??[];return typeof e=="string"?JSON.parse(e):e}createUnderlyingThreeObject(){this.instance=new this.classSource(...this.parsedArgs().map(e=>G(e,this)))}refreshAttributes(){this.getAttributeNames().forEach(e=>{const t=this.attributes.getNamedItem(e);t&&this.updateProperty(t)}),Array.from(this.attributes).forEach(this.updateProperty.bind(this))}async onUnderlyingThreeObjectReady(){var s,r,n,o;const e=this.instance;e.uuid&&this.setAttribute(we,e.uuid);let t=fe(this);for(;this!=null&&this.connected&&!this.tryAddOnce&&!t;)await new Promise(requestAnimationFrame),t=fe(this);if(t&&t.instance||(s=t==null?void 0:t.three)!=null&&s.scene){const a=this.instance,h=this.instance,c=t.instance,d=t.instance??((r=t.three)==null?void 0:r.scene),l=this.instance;if((n=a.type)!=null&&n.toLowerCase().includes("geometry")&&c.geometry)c.geometry=a;else if((o=h.type)!=null&&o.toLowerCase().includes("material")&&c.material)c.material=h;else if(d.add)try{d.add(l),this.dispatchEvent(new CustomEvent("instanceadded",{detail:{instance:this.instance,parent:d}}))}catch{throw new Error(`Error adding ${this.tagName} to ${d}`)}const m=this.getAttribute("attach");m&&this.executeAttach(m,this.instance)}}connectedCallback(){super.connectedCallback(),this.observeAttributes.call(this),this.createUnderlyingThreeObject.call(this),this.refreshAttributes.call(this),this.dispatchEvent(new CustomEvent("instancecreated",{detail:{instance:this.instance}})),this.connected=!0,this.onUnderlyingThreeObjectReady.call(this)}updateProperty(e){const{name:t,value:s}=e;let r=t;Object.keys(this.instance??{}).forEach(a=>{a.toLowerCase()===r&&(r=a)});const n=Ee(r);if(ye.includes(r)||ye.includes(n[0]))return;let o=G(s,this);try{o=JSON.parse(s===""?"true":s)}catch{}this.instance&&K(this.instance,n,o)}executeAttach(e,t){const s=this.parentElement;s.instance&&K(s.instance,Ee(e),t)}disconnectedCallback(){super.disconnectedCallback();const e=[this.instance];this.disposeThreeObjects.call(this,e),this.connected=!1}disposeThreeObjects(e){e.forEach(t=>{var n,o;if(!t)return;const s=t,r=t;(n=s.dispose)==null||n.call(s),(o=r.removeFromParent)==null||o.call(r)})}render(){return se`<slot></slot>`}createRenderRoot(){return this}}Se([y({type:Boolean,attribute:"try-add-once"})],U.prototype,"tryAddOnce");Se([lt()],U.prototype,"connected");const Te=i=>{const e=typeof i=="string"?p[i]:i;if(!it(e))return null;const t=i.toString().toLowerCase().endsWith("loader");class s extends U{constructor(){super(e)}}class r extends U{constructor(){super(...arguments),this.loader=null}createUnderlyingThreeObject(){this.loader=new e(...this.parsedArgs().map(o=>G(o,this)))}async onUnderlyingThreeObjectReady(){const o=this.getAttribute("src");if(!o)throw new Error("Loader requires a source.");if(!this.loader)throw new Error(`Missing loader ${this.tagName}`);this.loader.load(o,a=>{this.instance=a;const h=this.getAttribute("attach");h&&this.executeAttach(h,a),this.refreshAttributes.call(this),this.dispatchEvent(new CustomEvent("loaded",{detail:{instance:a}}))},void 0,a=>{throw new Error(`error loading: ${o}`+a)})}disconnectedCallback(){super.disconnectedCallback(),this.disposeThreeObjects.call(this,[this.loader])}}return t?r:s};var ut=Object.defineProperty,A=(i,e,t,s)=>{for(var r=void 0,n=i.length-1,o;n>=0;n--)(o=i[n])&&(r=o(e,t,r)||r);return r&&ut(e,t,r),r};const pt="orthographic",$e=1/0,re=class re extends C{constructor(){super(),this.scratchV2=new p.Vector2,this.background=null,this.dpr=$e,this.headless=!1,this.manualRender=!1,this.dispatchBeforeRender=!1,this.dispatchAfterRender=!1,this.rendererArgs=[],this.cameraArgs=[],this.raycaster=new p.Raycaster,this.raycastPool=[],this.frame=1/0,this.three=this.createThree(),this.resizeObserver=new ResizeObserver(e=>{e.forEach(({target:t,contentRect:s})=>{var r;if(t===this&&((r=this.three.renderer)==null||r.setSize(s.width*this.dpr,s.height*this.dpr),this.three.camera)){const n=s.width/s.height;if(this.three.camera.type.toLowerCase()==="perspectivecamera")this.three.camera.aspect=n,this.three.camera.updateProjectionMatrix();else if(this.three.camera.type.toLowerCase()==="orthographiccamera"){const o=s.height/s.width,a=10;this.three.camera.top=o*a,this.three.camera.bottom=-o*a,this.three.camera.right=a,this.three.camera.left=-a,this.three.camera.updateProjectionMatrix()}this.manualRender||this.renderThree()}})})}createDefaultScene(){return new p.Scene}createThree(){return{scene:this.createDefaultScene(),camera:null,renderer:null}}connectedCallback(){if(super.connectedCallback(),this.dpr===$e&&(this.dpr=window.devicePixelRatio),this.getAttribute(pt)!==null?this.three.camera=new p.OrthographicCamera(...this.cameraArgs):this.three.camera=new p.PerspectiveCamera(...this.cameraArgs.length?this.cameraArgs:[75]),["scene","camera","renderer"].forEach(e=>{const t=this[e]??this.getAttribute(e)??{},s=typeof t=="string"?JSON.parse(t):t;Object.entries(s).forEach(([r,n])=>{this.three[e]&&K(this.three[e],r.split("-"),G(n,this))})}),this.resizeObserver.observe(this),this.background!==null&&(this.three.scene.background=new p.Color(this.background)),!this.headless){const e=new p.WebGLRenderer(...this.rendererArgs);e.domElement.addEventListener("pointermove",this.onPointerMove.bind(this)),e.domElement.addEventListener("mousemove",this.onPointerMove.bind(this)),e.domElement.addEventListener("click",this.onClick.bind(this)),this.three.renderer=e}this.manualRender||this.updateLoop()}disconnectedCallback(){var e,t,s,r;(e=this.three.renderer)==null||e.domElement.removeEventListener("pointermove",this.onPointerMove.bind(this)),(t=this.three.renderer)==null||t.domElement.removeEventListener("mousemove",this.onPointerMove.bind(this)),(s=this.three.renderer)==null||s.domElement.removeEventListener("click",this.onClick.bind(this)),(r=this.three.renderer)==null||r.dispose(),this.resizeObserver.unobserve(this),cancelAnimationFrame(this.frame)}handleDefaultSlotChange(e){e.target.assignedElements().forEach(t=>{const s=t;s.instance instanceof p.Object3D&&t.getAttributeNames().includes(Z)&&t.getAttributeNames().includes(Z)&&this.raycastPool.push(s.instance)}),this.renderThree()}runRaycast(e){var a,h;if(!this.raycastPool.length||!this.three.camera)return[];const t=((a=this.three.renderer)==null?void 0:a.domElement.width)??0,s=((h=this.three.renderer)==null?void 0:h.domElement.height)??0,r=this.scratchV2.clone().set(e.clientX/(t/this.dpr)*2-1,-(e.clientY/(s/this.dpr))*2+1);return this.raycaster.setFromCamera(r,this.three.camera),this.raycaster.intersectObjects(this.raycastPool).map(c=>({intersect:c,element:this.querySelector(`[${we}="${c.object.uuid}"]`)}))}onPointerMove(e){this.runRaycast.bind(this)(e).forEach(s=>{var r,n,o,a;e.type==="pointermove"?((r=s.element)==null||r.dispatchEvent(new PointerEvent("pointermove")),(n=s.element)==null||n.dispatchEvent(new CustomEvent(Me,{detail:s}))):e.type==="mousemove"&&((o=s.element)==null||o.dispatchEvent(new MouseEvent("mousemove")),(a=s.element)==null||a.dispatchEvent(new CustomEvent(Oe,{detail:s})))})}onClick(e){let t=[];if(e instanceof TouchEvent){const s=e.touches[0];t=this.runRaycast.bind(this)(s),t.forEach(r=>{var n;(n=r.element)==null||n.dispatchEvent(new TouchEvent("touchstart"))})}else t=this.runRaycast.bind(this)(e),t.forEach(s=>{var r;(r=s.element)==null||r.dispatchEvent(new MouseEvent("click"))});t.forEach(s=>{var r;(r=s.element)==null||r.dispatchEvent(new CustomEvent(xe,{detail:s}))})}updateLoop(){this.renderThree(),this.manualRender||(this.frame=requestAnimationFrame(this.updateLoop.bind(this)))}renderThree(e,t){var s;this.dispatchBeforeRender&&this.dispatchEvent(new CustomEvent(Re,{})),this.three.camera&&((s=this.three.renderer)==null||s.render(e??this.three.scene,t??this.three.camera),this.dispatchAfterRender&&this.dispatchEvent(new CustomEvent(Ne,{})))}render(){var e;return se`
|
|
28
28
|
<slot @slotchange=${this.handleDefaultSlotChange}></slot>
|
|
29
29
|
${(e=this.three.renderer)==null?void 0:e.domElement}
|
|
30
30
|
`}};re.styles=De`
|
|
@@ -40,4 +40,4 @@
|
|
|
40
40
|
max-width: 100%;
|
|
41
41
|
max-height: 100%;
|
|
42
42
|
}
|
|
43
|
-
`;let f=re;A([y()],f.prototype,"background");A([y()],f.prototype,"dpr");A([y()],f.prototype,"headless");A([y({attribute:"manual-render",type:Boolean})],f.prototype,"manualRender");A([y({attribute:"dispatch-before-render",type:Boolean})],f.prototype,"dispatchBeforeRender");A([y({attribute:"dispatch-after-render",type:Boolean})],f.prototype,"dispatchAfterRender");A([y({attribute:"renderer-args",type:Object})],f.prototype,"rendererArgs");A([y({attribute:"camera-args",type:Object})],f.prototype,"cameraArgs");class Pe extends C{constructor(){super(...arguments),this.parentLunchbox=null,this.frame=-1,this.scratchV3=new p.Vector3,this.scratchFrustum=new p.Frustum,this.scratchMatrix4=new p.Matrix4}tryAttachUpdate(){var o;const e=(o=this.parentLunchbox)==null?void 0:o.instance;if(!e)return!1;if(!e.isObject3D)throw new Error("html-anchor must be the child of an Object3D");const t=k(this,a=>{var h;return!!((h=a==null?void 0:a.three)!=null&&h.renderer)});if(!t)return console.error("three-lunchbox parent required for html-anchor"),!1;const s=t.three.camera;if(!s)return console.error("camera required for html-anchor"),!1;const r=t.three.renderer;if(!(r!=null&&r.domElement))return console.error("renderer and DOM element required for html-anchor"),!1;const n=()=>{this.frame=requestAnimationFrame(n),s.updateMatrix(),s.updateMatrixWorld(),e.getWorldPosition(this.scratchV3);const a=this.scratchV3.clone();this.scratchV3.project(s),this.scratchV3.multiplyScalar(.5).addScalar(.5),this.scratchV3.y=1-this.scratchV3.y;const h=this.scratchV3.clone().set(r.domElement.width,r.domElement.height,1);h.divideScalar(devicePixelRatio),this.scratchV3.multiply(h);const c=e.position.clone().distanceTo(s.getWorldPosition(this.scratchV3.clone()));this.scratchFrustum.setFromProjectionMatrix(this.scratchMatrix4.clone().multiplyMatrices(s.projectionMatrix,s.matrixWorldInverse));const d=this.scratchFrustum.containsPoint(a);
|
|
43
|
+
`;let f=re;A([y()],f.prototype,"background");A([y()],f.prototype,"dpr");A([y()],f.prototype,"headless");A([y({attribute:"manual-render",type:Boolean})],f.prototype,"manualRender");A([y({attribute:"dispatch-before-render",type:Boolean})],f.prototype,"dispatchBeforeRender");A([y({attribute:"dispatch-after-render",type:Boolean})],f.prototype,"dispatchAfterRender");A([y({attribute:"renderer-args",type:Object})],f.prototype,"rendererArgs");A([y({attribute:"camera-args",type:Object})],f.prototype,"cameraArgs");class Pe extends C{constructor(){super(...arguments),this.parentLunchbox=null,this.frame=-1,this.scratchV3=new p.Vector3,this.scratchFrustum=new p.Frustum,this.scratchMatrix4=new p.Matrix4}tryAttachUpdate(){var o;const e=(o=this.parentLunchbox)==null?void 0:o.instance;if(!e)return!1;if(!e.isObject3D)throw new Error("html-anchor must be the child of an Object3D");const t=k(this,a=>{var h;return!!((h=a==null?void 0:a.three)!=null&&h.renderer)});if(!t)return console.error("three-lunchbox parent required for html-anchor"),!1;const s=t.three.camera;if(!s)return console.error("camera required for html-anchor"),!1;const r=t.three.renderer;if(!(r!=null&&r.domElement))return console.error("renderer and DOM element required for html-anchor"),!1;const n=()=>{this.frame=requestAnimationFrame(n),s.updateMatrix(),s.updateMatrixWorld(),e.getWorldPosition(this.scratchV3);const a=this.scratchV3.clone();this.scratchV3.project(s),this.scratchV3.multiplyScalar(.5).addScalar(.5),this.scratchV3.y=1-this.scratchV3.y;const h=this.scratchV3.clone().set(r.domElement.width,r.domElement.height,1);h.divideScalar(devicePixelRatio),this.scratchV3.multiply(h);const c=e.position.clone().distanceTo(s.getWorldPosition(this.scratchV3.clone()));this.scratchFrustum.setFromProjectionMatrix(this.scratchMatrix4.clone().multiplyMatrices(s.projectionMatrix,s.matrixWorldInverse));const d=this.scratchFrustum.containsPoint(a);this.style.setProperty("--left",`${this.scratchV3.x}px`),this.style.setProperty("--top",`${this.scratchV3.y}px`),this.style.setProperty("--distance-from-camera",`${c}`),this.style.setProperty("--in-frustum",`${d?1:0}`),d?this.classList.add("in-frustum"):this.classList.remove("in-frustum")};return n(),!0}connectedCallback(){super.connectedCallback();const e=this.parentNode;if(!e)throw new Error("html-anchor requires a 3D parent");this.parentLunchbox=e,this.tryAttachUpdate()||this.parentLunchbox.addEventListener("instanceadded",()=>{if(!this.tryAttachUpdate())throw new Error("error attaching html-anchor to Object3D")},{once:!0})}disconnectedCallback(){this.frame!==-1&&cancelAnimationFrame(this.frame)}render(){return se`<slot></slot>`}createRenderRoot(){return this}}const mt=({prependList:i=[]}={})=>{Object.entries({"three-lunchbox":f,"html-anchor":Pe}).forEach(([t,s])=>{customElements.get(t)||customElements.define(t,s)}),[...i,...Y].forEach(t=>{const s=Le(t);if(customElements.get(s))return;const r=Te(t);r&&customElements.define(s,r)})},ft=(i,e,t=window)=>{if(t.customElements.get(i)){console.log(`${i} already registered as a custom element. Try a different name if registering is still required.`);return}const s=Te(e);if(s)t.customElements.define(i,s);else throw new Error(`Could not extend ${i}. The second paramater must be a class.`)},Me="threepointermove",Oe="threemousemove",xe="threeclick",Re="beforerender",Ne="afterrender",Le=i=>{let e=i.split(/\.?(?=[A-Z])/).join("-").toLowerCase().replace(/-g-l-/,"-gl-");return e.includes("-")||(e=`three-${e}`),e},yt=Y.map(Le);exports.AFTER_RENDER_EVENT_NAME=Ne;exports.BEFORE_RENDER_EVENT_NAME=Re;exports.HtmlAnchor=Pe;exports.THREE_CLICK_EVENT_NAME=xe;exports.THREE_MOUSE_MOVE_EVENT_NAME=Oe;exports.THREE_POINTER_MOVE_EVENT_NAME=Me;exports.ThreeBase=U;exports.ThreeLunchbox=f;exports.autoComponents=Y;exports.extend=ft;exports.initLunchbox=mt;exports.webComponentNames=yt;
|