@react-three/drei 9.41.2 → 9.42.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/README.md +22 -6
- package/core/Bounds.cjs.js +1 -1
- package/core/Bounds.js +26 -1
- package/core/Stage.cjs.js +1 -1
- package/core/Stage.d.ts +1 -19
- package/core/Stage.js +2 -1
- package/index.cjs.js +1 -1
- package/package.json +1 -1
- package/web/View.cjs.js +1 -1
- package/web/View.d.ts +10 -2
- package/web/View.js +70 -11
package/package.json
CHANGED
package/web/View.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("three"),r=require("@react-three/fiber");function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("three"),r=require("@react-three/fiber");function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var i=n(e),c=n(t);const o=new c.Color;function s(e){return"top"in e}function a({canvasSize:e,scene:t,index:n,children:c,frames:a,rect:u,track:l}){const h=r.useThree((e=>e.get)),f=r.useThree((e=>e.camera)),g=r.useThree((e=>e.scene)),d=r.useThree((e=>e.setEvents));let p=0;return r.useFrame((r=>{var n,i;(a===1/0||p<=a)&&(u.current=null==(n=l.current)?void 0:n.getBoundingClientRect(),p++);if(u.current){const{position:{left:n,bottom:a,width:l,height:h},isOffscreen:d}=function(e,t){const{right:r,top:n,left:i,bottom:c,width:o,height:a}=t,u=t.bottom<0||n>e.height||r<0||t.left>e.width;if(s(e)){const t=e.top+e.height-c;return{position:{width:o,height:a,left:i-e.left,top:n,bottom:t,right:r},isOffscreen:u}}return{position:{width:o,height:a,top:n,left:i,bottom:e.height-c,right:r},isOffscreen:u}}(e,u.current),p=l/h;if((i=f)&&i.isOrthographicCamera?f.left===l/-2&&f.right===l/2&&f.top===h/2&&f.bottom===h/-2||(Object.assign(f,{left:l/-2,right:l/2,top:h/2,bottom:h/-2}),f.updateProjectionMatrix()):f.aspect!==p&&(f.aspect=p,f.updateProjectionMatrix()),r.gl.setViewport(n,a,l,h),r.gl.setScissor(n,a,l,h),r.gl.setScissorTest(!0),d)return r.gl.getClearColor(o),r.gl.setClearColor(o,r.gl.getClearAlpha()),void r.gl.clear(!0,!0);r.gl.render(c?g:t,f)}}),n),i.useEffect((()=>{const e=h().events.connected;return d({connected:l.current}),()=>d({connected:e})}),[]),i.useEffect((()=>{s(e)||console.warn("Detected @react-three/fiber canvas size does not include position information. <View /> may not work as expected. Upgrade to @react-three/fiber ^8.1.0 for support.\n See https://github.com/pmndrs/drei/issues/944")}),[]),i.createElement(i.Fragment,null,c)}exports.View=({track:e,index:t=1,frames:n=1/0,children:o})=>{const s=i.useRef(null),{size:u,scene:l}=r.useThree(),[h]=i.useState((()=>new c.Scene)),f=i.useCallback(((t,r)=>{if(e.current&&t.target===e.current){const{width:e,height:n,left:i,top:c}=s.current,o=t.clientX-i,a=t.clientY-c;r.pointer.set(o/e*2-1,-a/n*2+1),r.raycaster.setFromCamera(r.pointer,r.camera)}}),[s]),[g,d]=i.useReducer((()=>!0),!1);return i.useEffect((()=>{var t;s.current=null==(t=e.current)?void 0:t.getBoundingClientRect(),d()}),[]),i.createElement(i.Fragment,null,g&&r.createPortal(i.createElement(a,{canvasSize:u,frames:n,scene:l,track:e,rect:s,index:t},o),h,{events:{compute:f,priority:t},size:{width:s.current.width,height:s.current.height}}))};
|
package/web/View.d.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import * as THREE from 'three';
|
|
3
|
-
|
|
3
|
+
declare type LegacyCanvasSize = {
|
|
4
|
+
height: number;
|
|
5
|
+
width: number;
|
|
6
|
+
};
|
|
7
|
+
declare type CanvasSize = LegacyCanvasSize & {
|
|
8
|
+
top: number;
|
|
9
|
+
left: number;
|
|
10
|
+
};
|
|
4
11
|
export declare type ContainerProps = {
|
|
5
12
|
scene: THREE.Scene;
|
|
6
13
|
index: number;
|
|
@@ -8,7 +15,7 @@ export declare type ContainerProps = {
|
|
|
8
15
|
frames: number;
|
|
9
16
|
rect: React.MutableRefObject<DOMRect>;
|
|
10
17
|
track: React.MutableRefObject<HTMLElement>;
|
|
11
|
-
canvasSize:
|
|
18
|
+
canvasSize: LegacyCanvasSize | CanvasSize;
|
|
12
19
|
};
|
|
13
20
|
export declare type ViewProps = {
|
|
14
21
|
track: React.MutableRefObject<HTMLElement>;
|
|
@@ -17,3 +24,4 @@ export declare type ViewProps = {
|
|
|
17
24
|
children?: React.ReactNode;
|
|
18
25
|
};
|
|
19
26
|
export declare const View: ({ track, index, frames, children }: ViewProps) => JSX.Element;
|
|
27
|
+
export {};
|
package/web/View.js
CHANGED
|
@@ -5,6 +5,59 @@ import { useThree, createPortal, useFrame } from '@react-three/fiber';
|
|
|
5
5
|
const isOrthographicCamera = def => def && def.isOrthographicCamera;
|
|
6
6
|
|
|
7
7
|
const col = new THREE.Color();
|
|
8
|
+
/**
|
|
9
|
+
* In `@react-three/fiber` after `v8.0.0` but prior to `v8.1.0`, `state.size` contained only dimension
|
|
10
|
+
* information. After `v8.1.0`, position information (`top`, `left`) was added
|
|
11
|
+
*
|
|
12
|
+
* @todo remove this when drei supports v9 and up
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function isNonLegacyCanvasSize(size) {
|
|
16
|
+
return 'top' in size;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function computeContainerPosition(canvasSize, trackRect) {
|
|
20
|
+
const {
|
|
21
|
+
right,
|
|
22
|
+
top,
|
|
23
|
+
left: trackLeft,
|
|
24
|
+
bottom: trackBottom,
|
|
25
|
+
width,
|
|
26
|
+
height
|
|
27
|
+
} = trackRect;
|
|
28
|
+
const isOffscreen = trackRect.bottom < 0 || top > canvasSize.height || right < 0 || trackRect.left > canvasSize.width;
|
|
29
|
+
|
|
30
|
+
if (isNonLegacyCanvasSize(canvasSize)) {
|
|
31
|
+
const canvasBottom = canvasSize.top + canvasSize.height;
|
|
32
|
+
const bottom = canvasBottom - trackBottom;
|
|
33
|
+
const left = trackLeft - canvasSize.left;
|
|
34
|
+
return {
|
|
35
|
+
position: {
|
|
36
|
+
width,
|
|
37
|
+
height,
|
|
38
|
+
left,
|
|
39
|
+
top,
|
|
40
|
+
bottom,
|
|
41
|
+
right
|
|
42
|
+
},
|
|
43
|
+
isOffscreen
|
|
44
|
+
};
|
|
45
|
+
} // Fall back on old behavior if r3f < 8.1.0
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
const bottom = canvasSize.height - trackBottom;
|
|
49
|
+
return {
|
|
50
|
+
position: {
|
|
51
|
+
width,
|
|
52
|
+
height,
|
|
53
|
+
top,
|
|
54
|
+
left: trackLeft,
|
|
55
|
+
bottom,
|
|
56
|
+
right
|
|
57
|
+
},
|
|
58
|
+
isOffscreen
|
|
59
|
+
};
|
|
60
|
+
}
|
|
8
61
|
|
|
9
62
|
function Container({
|
|
10
63
|
canvasSize,
|
|
@@ -30,15 +83,14 @@ function Container({
|
|
|
30
83
|
|
|
31
84
|
if (rect.current) {
|
|
32
85
|
const {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const positiveYUpBottom = canvasSize.height - bottom;
|
|
86
|
+
position: {
|
|
87
|
+
left,
|
|
88
|
+
bottom,
|
|
89
|
+
width,
|
|
90
|
+
height
|
|
91
|
+
},
|
|
92
|
+
isOffscreen
|
|
93
|
+
} = computeContainerPosition(canvasSize, rect.current);
|
|
42
94
|
const aspect = width / height;
|
|
43
95
|
|
|
44
96
|
if (isOrthographicCamera(camera)) {
|
|
@@ -56,8 +108,8 @@ function Container({
|
|
|
56
108
|
camera.updateProjectionMatrix();
|
|
57
109
|
}
|
|
58
110
|
|
|
59
|
-
state.gl.setViewport(left,
|
|
60
|
-
state.gl.setScissor(left,
|
|
111
|
+
state.gl.setViewport(left, bottom, width, height);
|
|
112
|
+
state.gl.setScissor(left, bottom, width, height);
|
|
61
113
|
state.gl.setScissorTest(true);
|
|
62
114
|
|
|
63
115
|
if (isOffscreen) {
|
|
@@ -81,6 +133,13 @@ function Container({
|
|
|
81
133
|
connected: old
|
|
82
134
|
});
|
|
83
135
|
}, []);
|
|
136
|
+
React.useEffect(() => {
|
|
137
|
+
if (isNonLegacyCanvasSize(canvasSize)) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
console.warn('Detected @react-three/fiber canvas size does not include position information. <View /> may not work as expected. ' + 'Upgrade to @react-three/fiber ^8.1.0 for support.\n See https://github.com/pmndrs/drei/issues/944');
|
|
142
|
+
}, []);
|
|
84
143
|
return /*#__PURE__*/React.createElement(React.Fragment, null, children);
|
|
85
144
|
}
|
|
86
145
|
|