@react-three/drei 7.23.1 → 7.23.2

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 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.useState((()=>new n.Box3)),g=a.useMemo((()=>{function e(){const e=b.getSize(new n.Vector3),t=b.getCenter(new n.Vector3),r=Math.max(e.x,e.y,e.z),o=i(p)?4*r:r/(2*Math.atan(Math.PI*p.fov/360)),a=i(p)?4*r:o/p.aspect,c=m*Math.max(o,a);return{box:b,size:e,center:t,distance:c}}return{getSize:e,refresh(e){var t;if((t=e)&&t.isObject3D?b.setFromObject(e):(e=>e&&e.isBox3)(e)?b.copy(e):b.setFromObject(x.current),b.isEmpty()){const e=p.position.length()||10;b.setFromCenterAndSize(new n.Vector3,new n.Vector3(e,e,e))}return this},clip(){const{distance:t}=e();return l&&(l.maxDistance=10*t),p.near=t/100,p.far=100*t,p.updateProjectionMatrix(),l&&l.update(),this},fit(){d.camera.copy(p.position),l&&d.focus.copy(l.target);const{center:r,distance:o}=e(),a=r.clone().sub(p.position).normalize().multiplyScalar(o);if(M.camera.copy(r).sub(a),M.focus.copy(r),i(p)){d.zoom=p.zoom;let e=0,o=0;const a=[new n.Vector3(b.min.x,b.min.y,b.min.z),new n.Vector3(b.min.x,b.max.y,b.min.z),new n.Vector3(b.min.x,b.min.y,b.max.z),new n.Vector3(b.min.x,b.max.y,b.max.z),new n.Vector3(b.max.x,b.max.y,b.max.z),new n.Vector3(b.max.x,b.max.y,b.min.z),new n.Vector3(b.max.x,b.min.y,b.max.z),new n.Vector3(b.max.x,b.min.y,b.min.z)];r.applyMatrix4(p.matrixWorldInverse);for(const t of a)t.applyMatrix4(p.matrixWorldInverse),e=Math.max(e,Math.abs(t.y-r.y)),o=Math.max(o,Math.abs(t.x-r.x));e*=2,o*=2;const i=(p.top-p.bottom)/e,c=(p.right-p.left)/o;M.zoom=Math.min(i,c)/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}}}),[b,p,l,m,t,z,f]);return a.useLayoutEffect((()=>{if(g.refresh(),o&&g.fit(),s&&g.clip(),l){const e=e=>d.animating=!1;return l.addEventListener("start",e),()=>l.removeEventListener("start",e)}}),[g,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:g},e))},exports.useBounds=function(){return a.useContext(c)};
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,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)),h=a.useRef(f);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 d(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)}h.current=f;const[M]=a.useState((()=>({animating:!1,focus:new n.Vector3,camera:new n.Vector3,zoom:1}))),[b]=a.useState((()=>({focus:new n.Vector3,camera:new n.Vector3,zoom:1}))),[g]=a.useState((()=>new n.Box3)),w=a.useMemo((()=>{function e(){const e=g.getSize(new n.Vector3),t=g.getCenter(new n.Vector3),r=Math.max(e.x,e.y,e.z),o=c(p)?4*r:r/(2*Math.atan(Math.PI*p.fov/360)),a=c(p)?4*r:o/p.aspect,i=m*Math.max(o,a);return{box:g,size:e,center:t,distance:i}}return{getSize:e,refresh(e){var t;if((t=e)&&t.isObject3D?g.setFromObject(e):(e=>e&&e.isBox3)(e)?g.copy(e):g.setFromObject(x.current),g.isEmpty()){const e=p.position.length()||10;g.setFromCenterAndSize(new n.Vector3,new n.Vector3(e,e,e))}return this},clip(){const{distance:t}=e();return l&&(l.maxDistance=10*t),p.near=t/100,p.far=100*t,p.updateProjectionMatrix(),l&&l.update(),this},fit(){M.camera.copy(p.position),l&&M.focus.copy(l.target);const{center:r,distance:o}=e(),a=r.clone().sub(p.position).normalize().multiplyScalar(o);if(b.camera.copy(r).sub(a),b.focus.copy(r),c(p)){M.zoom=p.zoom;let e=0,o=0;const a=[new n.Vector3(g.min.x,g.min.y,g.min.z),new n.Vector3(g.min.x,g.max.y,g.min.z),new n.Vector3(g.min.x,g.min.y,g.max.z),new n.Vector3(g.min.x,g.max.y,g.max.z),new n.Vector3(g.max.x,g.max.y,g.max.z),new n.Vector3(g.max.x,g.max.y,g.min.z),new n.Vector3(g.max.x,g.min.y,g.max.z),new n.Vector3(g.max.x,g.min.y,g.min.z)];r.applyMatrix4(p.matrixWorldInverse);for(const t of a)t.applyMatrix4(p.matrixWorldInverse),e=Math.max(e,Math.abs(t.y-r.y)),o=Math.max(o,Math.abs(t.x-r.x));e*=2,o*=2;const c=(p.top-p.bottom)/e,i=(p.right-p.left)/o;b.zoom=Math.min(c,i)/m,t||(p.zoom=b.zoom,p.updateProjectionMatrix())}return t?M.animating=!0:(p.position.copy(b.camera),p.lookAt(b.focus),l&&(l.target.copy(b.focus),l.update()),z()),h.current&&h.current(this.getSize()),this}}}),[g,p,l,m,t,z]);return a.useLayoutEffect((()=>{if(w.refresh(),o&&w.fit(),s&&w.clip(),l){const e=e=>M.animating=!1;return l.addEventListener("start",e),()=>l.removeEventListener("start",e)}}),[s,o,l]),r.useFrame(((e,r)=>{if(M.animating){if(d(M.focus,b.focus,t,r),d(M.camera,b.camera,t,r),M.zoom=n.MathUtils.damp(M.zoom,b.zoom,t,r),p.position.copy(M.camera),c(p)&&(p.zoom=M.zoom,p.updateProjectionMatrix()),l?(l.target.copy(M.focus),l.update()):p.lookAt(M.focus),z(),c(p)&&!(Math.abs(M.zoom-b.zoom)<u))return;if(!c(p)&&!y(M.camera,b.camera))return;if(l&&!y(M.focus,b.focus))return;M.animating=!1}})),a.createElement("group",{ref:x},a.createElement(i.Provider,{value:w},e))},exports.useBounds=function(){return a.useContext(i)};
package/core/Bounds.js CHANGED
@@ -23,6 +23,8 @@ function Bounds({
23
23
 
24
24
  const controls = useThree(state => state.controls);
25
25
  const invalidate = useThree(state => state.invalidate);
26
+ const onFitRef = React.useRef(onFit);
27
+ onFitRef.current = onFit;
26
28
 
27
29
  function equals(a, b) {
28
30
  return Math.abs(a.x - b.x) < eps && Math.abs(a.y - b.y) < eps && Math.abs(a.z - b.z) < eps;
@@ -139,12 +141,12 @@ function Bounds({
139
141
  invalidate();
140
142
  }
141
143
 
142
- if (onFit) onFit(this.getSize());
144
+ if (onFitRef.current) onFitRef.current(this.getSize());
143
145
  return this;
144
146
  }
145
147
 
146
148
  };
147
- }, [box, camera, controls, margin, damping, invalidate, onFit]);
149
+ }, [box, camera, controls, margin, damping, invalidate]);
148
150
  React.useLayoutEffect(() => {
149
151
  api.refresh();
150
152
  if (fit) api.fit();
@@ -156,8 +158,9 @@ function Bounds({
156
158
 
157
159
  controls.addEventListener('start', callback);
158
160
  return () => controls.removeEventListener('start', callback);
159
- }
160
- }, [api, fit, controls]);
161
+ } // eslint-disable-next-line react-hooks/exhaustive-deps
162
+
163
+ }, [clip, fit, controls]);
161
164
  useFrame((state, delta) => {
162
165
  if (current.animating) {
163
166
  damp(current.focus, goal.focus, damping, delta);