Skip to content

Commit 33626ac

Browse files
author
takuma-hmng8
committed
update useDomSyncer
1 parent 80f96c0 commit 33626ac

File tree

10 files changed

+624
-573
lines changed

10 files changed

+624
-573
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ The second argument contains the dependency array that updates the DOM. For exam
368368
```tsx
369369
const [updateDomSyncer, setDomSyncer, domSyncerObj] = useDomSyncer(
370370
{ size, dpr },
371-
[state]
371+
[state],
372+
state
372373
);
373374

374375
useLayoutEffect(() => {
@@ -378,6 +379,8 @@ useLayoutEffect(() => {
378379
domArr.current = [...document.querySelectorAll(".item2")!];
379380
}
380381
setDomSyncer({
382+
// Since DOM rendering and React updates are asynchronous, the DOM may not be retrieved correctly when the state is updated. In that case, use another logic to get the DOM perfectly before updating updateKey.
383+
updateKey: state,
381384
dom: domArr.current,
382385
boderRadius: [...Array(domArr.current.length)].map((_, i) => i * 50.0),
383386
onIntersect: [...Array(domArr.current.length)].map((_, i) => (entry) => {
@@ -419,15 +422,20 @@ type DomSyncerObject = {
419422
scene: THREE.Scene;
420423
camera: THREE.Camera;
421424
renderTarget: THREE.WebGLRenderTarget;
425+
output: THREE.Texture;
422426
/**
423427
* A function that returns a determination whether the DOM intersects or not.
424428
* The boolean will be updated after executing the onIntersect function.
425429
* @param index - Index of the dom for which you want to return an intersection decision. -1 will return the entire array.
426430
* @param once - If set to true, it will continue to return true once crossed.
427431
*/
428432
isIntersecting: IsIntersecting;
429-
/** Returns the target's DOMRect[] */
433+
/** target's DOMRect[] */
430434
DOMRects: DOMRect[];
435+
/** target's intersetions boolean[] */
436+
intersections: boolean[];
437+
/** You can set callbacks for when at least one DOM is visible and when it is completely hidden. */
438+
useDomView: UseDomView;
431439
};
432440
```
433441

@@ -443,7 +451,13 @@ type DomSyncerParams = {
443451
resolution?: THREE.Vector2[];
444452
/** default:0.0[] */
445453
boderRadius?: number[];
454+
/** the angle you want to rotate */
455+
rotation?: THREE.Euler[];
446456
/** Array of callback functions when crossed */
447457
onIntersect?: ((entry: IntersectionObserverEntry) => void)[];
458+
/** Because DOM rendering and React updates occur asynchronously, there may be a lag between updating dependent arrays and setting DOM arrays. That's what the Key is for. If the dependent array is updated but the Key is not, the loop will skip and return an empty texture. By updating the timing key when DOM acquisition is complete, you can perfectly synchronize DOM and Mesh updates. */
459+
updateKey?: Key;
448460
};
449461
```
462+
463+
`updateKey` : Because DOM rendering and React updates occur asynchronously, there may be a lag between updating dependent arrays and setting DOM arrays. That's what the Key is for. If the dependent array is updated but the Key is not, the loop will skip and return an empty texture. By updating the timing key when DOM acquisition is complete, you can perfectly synchronize DOM and Mesh updates.

app/domSyncer/DomSyncer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export const DomSyncer = ({ state }: { state: number }) => {
4747

4848
const [updateDomSyncer, setDomSyncer, domSyncerObj] = useDomSyncer(
4949
{ size, dpr },
50-
[state]
50+
[state],
51+
state
5152
);
5253

5354
const { setFrameloop } = useThree();
@@ -85,6 +86,7 @@ export const DomSyncer = ({ state }: { state: number }) => {
8586

8687
setDomSyncer({
8788
dom: domArr.current,
89+
updateKey: state,
8890
boderRadius: [...Array(domArr.current.length)].map((_, i) => i * 50.0),
8991
rotation: [...Array(domArr.current.length)].map(
9092
(_, i) => new THREE.Euler(0.0, 0.0, i * 0.1)

0 commit comments

Comments
 (0)