Skip to content

Commit fb3dd07

Browse files
authored
Merge pull request #70 from takuma-hmng8/dev
Bug fix regarding updateKey in useDomSyncer
2 parents b2de102 + 077e797 commit fb3dd07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+12
-4013
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ 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],
372-
state
371+
[state]
373372
);
374373

375374
useLayoutEffect(() => {
@@ -379,8 +378,8 @@ useLayoutEffect(() => {
379378
domArr.current = [...document.querySelectorAll(".item2")!];
380379
}
381380
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,
381+
// 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.updateKey must be a unique value for each update, for example `performance.now()
382+
updateKey: performance.now(),
384383
dom: domArr.current,
385384
boderRadius: [...Array(domArr.current.length)].map((_, i) => i * 50.0),
386385
onIntersect: [...Array(domArr.current.length)].map((_, i) => (entry) => {

app/domSyncer/DomSyncer.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as THREE from "three";
2-
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
2+
import { useEffect, useLayoutEffect, useRef } from "react";
33
import { useFrame, extend, useThree, useLoader } from "@react-three/fiber";
44
import { FxMaterial, FxMaterialProps } from "@/utils/fxMaterial";
55
import {
@@ -47,8 +47,7 @@ export const DomSyncer = ({ state }: { state: number }) => {
4747

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

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

8786
setDomSyncer({
8887
dom: domArr.current,
89-
updateKey: state,
88+
updateKey: performance.now(),
9089
boderRadius: [...Array(domArr.current.length)].map((_, i) => i * 50.0),
9190
rotation: [...Array(domArr.current.length)].map(
9291
(_, i) => new THREE.Euler(0.0, 0.0, i * 0.1)

0 commit comments

Comments
 (0)