-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGlobeClickable.tsx
70 lines (56 loc) · 2.02 KB
/
GlobeClickable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { useEffect, useRef } from "react";
import * as THREE from "three";
import { Sprite, SpriteMaterial, TextureLoader } from "three";
import { useFrame } from "@react-three/fiber";
interface Landmass {
lat: number;
lng: number;
imageURL: string;
onClickFunction: () => void;
}
interface ClickableImagesProps {
landmasses: Landmass[];
}
function ClickableImages({ landmasses }: ClickableImagesProps) {
const groupRef = useRef<THREE.Group | null>(null);
useEffect(() => {
if (groupRef.current) {
landmasses.forEach((landmass) => {
const { lat, lng, imageURL, onClickFunction } = landmass;
// Load texture
const textureLoader = new TextureLoader();
const texture = textureLoader.load(imageURL);
// Create a sprite with the texture
const spriteMaterial = new SpriteMaterial({ map: texture });
const sprite = new Sprite(spriteMaterial);
// Set the size of the sprite
const size = 8; // Adjust size as needed (e.g. 8 for Tailwind scale)
sprite.scale.set(size, size, 1);
// Convert lat/lng to 3D coordinates
const radius = 100; // Adjust radius as needed
const phi = (90 - lat) * (Math.PI / 180);
const theta = lng * (Math.PI / 180);
sprite.position.set(
radius * Math.sin(phi) * Math.cos(theta),
radius * Math.cos(phi),
radius * Math.sin(phi) * Math.sin(theta)
);
// Create a click handler for the sprite
const handleClick = (event: any) => {
event.stopPropagation();
onClickFunction();
};
// Add event listener to the sprite for the 'click' event
sprite.addEventListener('click', handleClick);
// Add the sprite to the group
groupRef.current.add(sprite);
});
}
}, [landmasses]);
// Use useFrame hook to continuously update the scene if needed
useFrame(() => {
// Add any animation logic here if required
});
return <group ref={groupRef} />;
}
export default ClickableImages;