@react-three/drei 7.22.7 → 7.22.8

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("three"),r=require("@react-three/fiber");function o(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a=o(e),n=o(t);const c=e=>e&&e.isOrthographicCamera,i=a.createContext(null);exports.Bounds=function({children:e,damping:t=6,fit:o,clip:s,margin:m=1.2,eps:u=.01}){const f=a.useRef(null),x=r.useThree((e=>e.camera)),p=r.useThree((e=>e.controls)),l=r.useThree((e=>e.invalidate));function y(e,t){return Math.abs(e.x-t.x)<u&&Math.abs(e.y-t.y)<u&&Math.abs(e.z-t.z)<u}function z(e,t,r,o){e.x=n.MathUtils.damp(e.x,t.x,r,o),e.y=n.MathUtils.damp(e.y,t.y,r,o),e.z=n.MathUtils.damp(e.z,t.z,r,o)}const[h]=a.useState((()=>({animating:!1,focus:new n.Vector3,camera:new n.Vector3,zoom:1}))),[d]=a.useState((()=>({focus:new n.Vector3,camera:new n.Vector3,zoom:1}))),M=a.useMemo((()=>{const e=new n.Box3;function r(){const t=e.getSize(new n.Vector3),r=e.getCenter(new n.Vector3),o=Math.max(t.x,t.y,t.z),a=c(x)?4*o:o/(2*Math.atan(Math.PI*x.fov/360)),i=c(x)?4*o:a/x.aspect;return{size:t,center:r,distance:m*Math.max(a,i)}}return{getSize:r,refresh(t){var r;if((r=t)&&r.isObject3D?e.setFromObject(t):(e=>e&&e.isBox3)(t)?e.copy(t):e.setFromObject(f.current),e.isEmpty()){const t=x.position.length()||10;e.setFromCenterAndSize(new n.Vector3,new n.Vector3(t,t,t))}return this},clip(){const{distance:e}=r();return p&&(p.maxDistance=10*e),x.near=e/100,x.far=100*e,x.updateProjectionMatrix(),p&&p.update(),this},fit(){h.camera.copy(x.position),p&&h.focus.copy(p.target);const{center:o,distance:a}=r(),i=o.clone().sub(x.position).normalize().multiplyScalar(a);if(d.camera.copy(o).sub(i),d.focus.copy(o),c(x)){h.zoom=x.zoom;let r=0,a=0;const c=[new n.Vector3(e.min.x,e.min.y,e.min.z),new n.Vector3(e.min.x,e.max.y,e.min.z),new n.Vector3(e.min.x,e.min.y,e.max.z),new n.Vector3(e.min.x,e.max.y,e.max.z),new n.Vector3(e.max.x,e.max.y,e.max.z),new n.Vector3(e.max.x,e.max.y,e.min.z),new n.Vector3(e.max.x,e.min.y,e.max.z),new n.Vector3(e.max.x,e.min.y,e.min.z)];o.applyMatrix4(x.matrixWorldInverse);for(const e of c)e.applyMatrix4(x.matrixWorldInverse),r=Math.max(r,Math.abs(e.y-o.y)),a=Math.max(a,Math.abs(e.x-o.x));r*=2,a*=2;const i=(x.top-x.bottom)/r,s=(x.right-x.left)/a;d.zoom=Math.min(i,s)/m,t||(x.zoom=d.zoom,x.updateProjectionMatrix())}return t?h.animating=!0:(x.position.copy(d.camera),x.lookAt(d.focus),p&&(p.target.copy(d.focus),p.update()),l()),this}}}),[x,p,m,t,l]);return a.useLayoutEffect((()=>{if(M.refresh(),o&&M.fit(),s&&M.clip(),p){const e=e=>h.animating=!1;return p.addEventListener("start",e),()=>p.removeEventListener("start",e)}}),[M,o,p]),r.useFrame(((e,r)=>{if(h.animating){if(z(h.focus,d.focus,t,r),z(h.camera,d.camera,t,r),h.zoom=n.MathUtils.damp(h.zoom,d.zoom,t,r),x.position.copy(h.camera),c(x)&&(x.zoom=h.zoom,x.updateProjectionMatrix()),p?(p.target.copy(h.focus),p.update()):x.lookAt(h.focus),l(),c(x)&&!(Math.abs(h.zoom-d.zoom)<u))return;if(!c(x)&&!y(h.camera,d.camera))return;if(p&&!y(h.focus,d.focus))return;h.animating=!1}})),a.createElement("group",{ref:f},a.createElement(i.Provider,{value:M},e))},exports.useBounds=function(){return a.useContext(i)};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("three"),r=require("@react-three/fiber");function o(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a=o(e),n=o(t);const i=e=>e&&e.isOrthographicCamera,c=a.createContext(null);exports.Bounds=function({children:e,damping:t=6,fit:o,clip:s,margin:m=1.2,eps:u=.01,onFit:f}){const x=a.useRef(null),p=r.useThree((e=>e.camera)),l=r.useThree((e=>e.controls)),z=r.useThree((e=>e.invalidate));function h(e,t){return Math.abs(e.x-t.x)<u&&Math.abs(e.y-t.y)<u&&Math.abs(e.z-t.z)<u}function y(e,t,r,o){e.x=n.MathUtils.damp(e.x,t.x,r,o),e.y=n.MathUtils.damp(e.y,t.y,r,o),e.z=n.MathUtils.damp(e.z,t.z,r,o)}const[d]=a.useState((()=>({animating:!1,focus:new n.Vector3,camera:new n.Vector3,zoom:1}))),[M]=a.useState((()=>({focus:new n.Vector3,camera:new n.Vector3,zoom:1}))),b=a.useMemo((()=>{const e=new n.Box3;function r(){const t=e.getSize(new n.Vector3),r=e.getCenter(new n.Vector3),o=Math.max(t.x,t.y,t.z),a=i(p)?4*o:o/(2*Math.atan(Math.PI*p.fov/360)),c=i(p)?4*o:a/p.aspect,s=m*Math.max(a,c);return{box:e,size:t,center:r,distance:s}}return{getSize:r,refresh(t){var r;if((r=t)&&r.isObject3D?e.setFromObject(t):(e=>e&&e.isBox3)(t)?e.copy(t):e.setFromObject(x.current),e.isEmpty()){const t=p.position.length()||10;e.setFromCenterAndSize(new n.Vector3,new n.Vector3(t,t,t))}return this},clip(){const{distance:e}=r();return l&&(l.maxDistance=10*e),p.near=e/100,p.far=100*e,p.updateProjectionMatrix(),l&&l.update(),this},fit(){d.camera.copy(p.position),l&&d.focus.copy(l.target);const{center:o,distance:a}=r(),c=o.clone().sub(p.position).normalize().multiplyScalar(a);if(M.camera.copy(o).sub(c),M.focus.copy(o),i(p)){d.zoom=p.zoom;let r=0,a=0;const i=[new n.Vector3(e.min.x,e.min.y,e.min.z),new n.Vector3(e.min.x,e.max.y,e.min.z),new n.Vector3(e.min.x,e.min.y,e.max.z),new n.Vector3(e.min.x,e.max.y,e.max.z),new n.Vector3(e.max.x,e.max.y,e.max.z),new n.Vector3(e.max.x,e.max.y,e.min.z),new n.Vector3(e.max.x,e.min.y,e.max.z),new n.Vector3(e.max.x,e.min.y,e.min.z)];o.applyMatrix4(p.matrixWorldInverse);for(const e of i)e.applyMatrix4(p.matrixWorldInverse),r=Math.max(r,Math.abs(e.y-o.y)),a=Math.max(a,Math.abs(e.x-o.x));r*=2,a*=2;const c=(p.top-p.bottom)/r,s=(p.right-p.left)/a;M.zoom=Math.min(c,s)/m,t||(p.zoom=M.zoom,p.updateProjectionMatrix())}return t?d.animating=!0:(p.position.copy(M.camera),p.lookAt(M.focus),l&&(l.target.copy(M.focus),l.update()),z()),f&&f(this.getSize()),this}}}),[p,l,m,t,z]);return a.useLayoutEffect((()=>{if(b.refresh(),o&&b.fit(),s&&b.clip(),l){const e=e=>d.animating=!1;return l.addEventListener("start",e),()=>l.removeEventListener("start",e)}}),[b,o,l]),r.useFrame(((e,r)=>{if(d.animating){if(y(d.focus,M.focus,t,r),y(d.camera,M.camera,t,r),d.zoom=n.MathUtils.damp(d.zoom,M.zoom,t,r),p.position.copy(d.camera),i(p)&&(p.zoom=d.zoom,p.updateProjectionMatrix()),l?(l.target.copy(d.focus),l.update()):p.lookAt(d.focus),z(),i(p)&&!(Math.abs(d.zoom-M.zoom)<u))return;if(!i(p)&&!h(d.camera,M.camera))return;if(l&&!h(d.focus,M.focus))return;d.animating=!1}})),a.createElement("group",{ref:x},a.createElement(c.Provider,{value:b},e))},exports.useBounds=function(){return a.useContext(c)};
package/core/Bounds.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  import * as THREE from 'three';
3
3
  export declare type BoundsApi = {
4
4
  getSize: () => {
5
+ box: THREE.Box3;
5
6
  size: THREE.Vector3;
6
7
  center: THREE.Vector3;
7
8
  distance: number;
@@ -16,6 +17,7 @@ export declare type BoundsProps = JSX.IntrinsicElements['group'] & {
16
17
  clip?: boolean;
17
18
  margin?: number;
18
19
  eps?: number;
20
+ onFit?: (data: BoundsApi) => void;
19
21
  };
20
- export declare function Bounds({ children, damping, fit, clip, margin, eps }: BoundsProps): JSX.Element;
22
+ export declare function Bounds({ children, damping, fit, clip, margin, eps, onFit }: BoundsProps): JSX.Element;
21
23
  export declare function useBounds(): BoundsApi;
package/core/Bounds.js CHANGED
@@ -15,7 +15,8 @@ function Bounds({
15
15
  fit,
16
16
  clip,
17
17
  margin = 1.2,
18
- eps = 0.01
18
+ eps = 0.01,
19
+ onFit
19
20
  }) {
20
21
  const ref = React.useRef(null);
21
22
  const camera = useThree(state => state.camera); // @ts-expect-error new in @react-three/fiber@7.0.5
@@ -55,6 +56,7 @@ function Bounds({
55
56
  const fitWidthDistance = isOrthographic(camera) ? maxSize * 4 : fitHeightDistance / camera.aspect;
56
57
  const distance = margin * Math.max(fitHeightDistance, fitWidthDistance);
57
58
  return {
59
+ box,
58
60
  size,
59
61
  center,
60
62
  distance
@@ -138,6 +140,7 @@ function Bounds({
138
140
  invalidate();
139
141
  }
140
142
 
143
+ if (onFit) onFit(this.getSize());
141
144
  return this;
142
145
  }
143
146