-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose globalMatrix through ref.current.globalMatrix fix: card demo
- Loading branch information
1 parent
c1458cb
commit cad0f11
Showing
9 changed files
with
84 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,85 @@ | ||
import { Canvas } from '@react-three/fiber' | ||
import { DefaultProperties, Container, Text, Root } from '@react-three/uikit' | ||
import { DefaultProperties, Container, Text, Root, ComponentInternals } from '@react-three/uikit' | ||
import { OrbitControls } from '@react-three/drei' | ||
import { Perf } from 'r3f-perf' | ||
import { noEvents, PointerEvents } from '@react-three/xr' | ||
import { suspend } from 'suspend-react' | ||
import { useEffect, useMemo, useRef } from 'react' | ||
import { effect, signal } from '@preact/signals-core' | ||
import { Color } from 'three' | ||
|
||
const size = 2.4 * 1920 | ||
const elements = 60000 | ||
|
||
export default function App() { | ||
const getPixel = suspend(loadGetPixel, ['img.png']) | ||
return ( | ||
<Canvas events={noEvents} style={{ height: '100dvh', touchAction: 'none' }} gl={{ localClippingEnabled: true }}> | ||
<color attach="background" args={['black']} /> | ||
<PointerEvents /> | ||
<OrbitControls /> | ||
<Perf /> | ||
<Root width={1920 * 2} positionType="relative"> | ||
<Root width={size} height={size} positionType="relative"> | ||
<DefaultProperties fontSize={4}> | ||
<Container positionType="absolute" gap={1} flexWrap="wrap" inset={0}> | ||
{new Array(20000).fill(null).map((_, i) => { | ||
const borderWidth = Math.random() * 1 | ||
return ( | ||
<Container | ||
key={i} | ||
borderRadius={Math.random() * 5} | ||
backgroundColor={Math.random() * 0xffffff} | ||
borderWidth={borderWidth} | ||
borderColor={Math.random() * 0xffffff} | ||
height={4 + 4 + 2 * borderWidth} | ||
padding={2} | ||
pointerEvents="none" | ||
hover={{ backgroundColor: 'white', borderColor: 'black' }} | ||
> | ||
<Text pointerEvents="auto" hover={{ color: 'red' }}> | ||
Hello World | ||
</Text> | ||
</Container> | ||
) | ||
})} | ||
<Container positionType="absolute" gap={1} justifyContent="space-between" flexWrap="wrap" inset={0}> | ||
{new Array(elements).fill(null).map((_, i) => ( | ||
<Pixel key={i} getPixel={getPixel} /> | ||
))} | ||
</Container> | ||
</DefaultProperties> | ||
</Root> | ||
</Canvas> | ||
) | ||
} | ||
|
||
type GetPixel = (x: number, y: number) => [number, number, number] | ||
|
||
function Pixel({ getPixel }: { getPixel: GetPixel }) { | ||
const borderWidth = Math.random() * 1 | ||
const color = useMemo(() => signal(new Color('black')), []) | ||
const ref = useRef<ComponentInternals>(null) | ||
useEffect(() => { | ||
const internals = ref.current | ||
if (internals == null) { | ||
return | ||
} | ||
return effect(() => { | ||
const center = internals.center.value | ||
if (center == null) { | ||
return | ||
} | ||
const [r, g, b] = getPixel((680 * (center[0] + size / 2)) / size, 680 * ((-center[1] + size / 2) / size)) | ||
color.value = new Color(r / 255, g / 255, b / 255) | ||
}) | ||
}, [color, getPixel]) | ||
return ( | ||
<Container | ||
ref={ref} | ||
borderRadius={Math.random() * 5} | ||
backgroundColor={color} | ||
borderWidth={borderWidth} | ||
borderColor={Math.random() * 0xffffff} | ||
transformTranslateZ={Math.random() * 1} | ||
height={4 + 4 + 2 * borderWidth} | ||
padding={2} | ||
hover={{ backgroundColor: 'white', borderColor: 'black' }} | ||
> | ||
<Text pointerEvents="none">Hello World</Text> | ||
</Container> | ||
) | ||
} | ||
|
||
async function loadGetPixel(url: string) { | ||
return new Promise<GetPixel>((resolve) => { | ||
const img = new Image() | ||
|
||
img.onload = () => { | ||
const canvas = document.createElement('canvas') | ||
canvas.width = 680 | ||
canvas.height = 680 | ||
const ctx = canvas.getContext('2d')! | ||
ctx.drawImage(img, 0, 0, 680, 680) | ||
resolve((x, y) => ctx.getImageData(x, y, 1, 1).data as any) | ||
} | ||
img.src = url | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.