@react-three/drei 7.22.8 → 7.22.9

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.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)};
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)};
package/core/Bounds.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  /// <reference types="react" />
2
2
  import * as THREE from 'three';
3
+ export declare type SizeProps = {
4
+ box: THREE.Box3;
5
+ size: THREE.Vector3;
6
+ center: THREE.Vector3;
7
+ distance: number;
8
+ };
3
9
  export declare type BoundsApi = {
4
- getSize: () => {
5
- box: THREE.Box3;
6
- size: THREE.Vector3;
7
- center: THREE.Vector3;
8
- distance: number;
9
- };
10
+ getSize: () => SizeProps;
10
11
  refresh(object?: THREE.Object3D | THREE.Box3): any;
11
12
  clip(): any;
12
13
  fit(): any;
@@ -17,7 +18,7 @@ export declare type BoundsProps = JSX.IntrinsicElements['group'] & {
17
18
  clip?: boolean;
18
19
  margin?: number;
19
20
  eps?: number;
20
- onFit?: (data: BoundsApi) => void;
21
+ onFit?: (data: SizeProps) => void;
21
22
  };
22
23
  export declare function Bounds({ children, damping, fit, clip, margin, eps, onFit }: BoundsProps): JSX.Element;
23
24
  export declare function useBounds(): BoundsApi;
package/core/Bounds.js CHANGED
@@ -45,9 +45,8 @@ function Bounds({
45
45
  camera: new THREE.Vector3(),
46
46
  zoom: 1
47
47
  }));
48
+ const [box] = React.useState(() => new THREE.Box3());
48
49
  const api = React.useMemo(() => {
49
- const box = new THREE.Box3();
50
-
51
50
  function getSize() {
52
51
  const size = box.getSize(new THREE.Vector3());
53
52
  const center = box.getCenter(new THREE.Vector3());
@@ -145,7 +144,7 @@ function Bounds({
145
144
  }
146
145
 
147
146
  };
148
- }, [camera, controls, margin, damping, invalidate]);
147
+ }, [box, camera, controls, margin, damping, invalidate, onFit]);
149
148
  React.useLayoutEffect(() => {
150
149
  api.refresh();
151
150
  if (fit) api.fit();