I am investigating Issue of pointer become (0, 0) of useFrame Hook’s in @react-three/drei’s View Component
Recently, the ‘View’ has been renewed, and I’ve been experimenting with various things, and when I was making a movement that moves the position of the PointerLight depending on the position of the mouse, I encountered that the Pointer does not move.
I’ve created the smallest project I can reproduce to do this research.
If anyone knows, I would appreciate your guidance.
I still have a shallow understanding of r3f and drei, and I don’t understand them in depth,
but, I reproduced it as below.
※: all code: foasho/r3f-view-search (github.com)
Here is the code I referenced.: drcmda/views (github.com)
- Creating a ‘Scene’ component
import React from "react";
import { Canvas, CanvasProps, addEffect } from "@react-three/fiber";
import { Preload, View } from "@react-three/drei";
import Lenis from "@studio-freight/lenis";
const lenis = new Lenis({ syncTouch: true });
addEffect((t) => lenis.raf(t));
export const Scene = ({ style, ...props }: SceneProps) => {
return (
<Canvas
shadows
style={{
position: "fixed",
top: 0,
left: 0,
width: "100vw",
height: "100vh",
pointerEvents: "none",
...style,
}}
eventSource={document.body}
eventPrefix="client"
{...props}
>
<Preload all />
<View.Port />
</Canvas>
);
};
- Creating
SampleView
export const SampleView = () => {
return (
<View className="h-full w-full">
<PointerLighting />
<MyText />
<BackGroundPlane />
</View>
)
};
- Creating
PointerLighting
import { useRef } from "react";
import { useFrame } from "@react-three/fiber";
import type { PointLight } from "three";
/**
* A light that follows the pointer.
*/
export const PointerLighting = () => {
const ref = useRef<PointLight>(null);
useFrame(({ viewport, pointer }) => {
if (ref.current) {
// not working pointer.x and pointer.y
// always (0, 0)
console.log(pointer.x, pointer.y);
ref.current.position.set(
(pointer.x * viewport.width) / 2,
(pointer.y * viewport.height) / 2,
5.5
);
}
});
return <pointLight ref={ref} intensity={20} />;
};
- Call from
App
function App() {
return (
<div className="h-screen w-screen">
<SampleView />
<Scene children={undefined} />
</div>
)
}
- Result (gif)
not working pointer tracking
