Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

fix: update globe API (#560)

Dillion Verma487c254e35d1
diff --git a/content/docs/components/globe.mdx b/content/docs/components/globe.mdx
index fb9070b6..df855781 100644
--- a/content/docs/components/globe.mdx
+++ b/content/docs/components/globe.mdx
@@ -7,11 +7,6 @@ published: true
video: https://cdn.magicui.design/globe.mp4
---
-## ⚠️ Warning
-
-> The globe has known performance issues causing computer GPU and CPU to spike.
-> See [here](https://github.com/magicuidesign/magicui/issues/42) for more details.
-
<ComponentPreview name="globe-demo" />
## Installation
@@ -54,8 +49,6 @@ npm install cobe
## Props
-### Globe
-
| Prop | Type | Default | Description |
| ----------- | ------------- | ------- | ---------------------------------------------------------------------------------------------- |
| `className` | `string` | `-` | The css classes for the component |
diff --git a/package.json b/package.json
index c1e72ca4..eaa6188f 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,7 @@
"build:cli": "pnpm prefix:cli build",
"release:cli": "pnpm prefix:cli release",
"preinstall": "npx only-allow pnpm",
- "dev": "concurrently \"content-collections watch\" \"next dev\"",
+ "dev": "next dev",
"prebuild": "pnpm build:docs && pnpm build:registry",
"build": "next build",
"build:registry": "npx tsx ./scripts/build-registry.mts",
diff --git a/registry/magicui/globe.tsx b/registry/magicui/globe.tsx
index 4bb7f905..4cd6e032 100644
--- a/registry/magicui/globe.tsx
+++ b/registry/magicui/globe.tsx
@@ -1,10 +1,13 @@
"use client";
import createGlobe, { COBEOptions } from "cobe";
-import { useCallback, useEffect, useRef, useState } from "react";
+import { useMotionValue, useSpring } from "motion/react";
+import { useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
+const MOVEMENT_DAMPING = 1400;
+
const GLOBE_CONFIG: COBEOptions = {
width: 800,
height: 800,
@@ -43,42 +46,38 @@ export function Globe({
let phi = 0;
let width = 0;
const canvasRef = useRef<HTMLCanvasElement>(null);
- const pointerInteracting = useRef(null);
+ const pointerInteracting = useRef<number | null>(null);
const pointerInteractionMovement = useRef(0);
- const [r, setR] = useState(0);
- const updatePointerInteraction = (value: any) => {
+ const r = useMotionValue(0);
+ const rs = useSpring(r, {
+ mass: 1,
+ damping: 30,
+ stiffness: 100,
+ });
+
+ const updatePointerInteraction = (value: number | null) => {
pointerInteracting.current = value;
if (canvasRef.current) {
- canvasRef.current.style.cursor = value ? "grabbing" : "grab";
+ canvasRef.current.style.cursor = value !== null ? "grabbing" : "grab";
}
};
- const updateMovement = (clientX: any) => {
+ const updateMovement = (clientX: number) => {
if (pointerInteracting.current !== null) {
const delta = clientX - pointerInteracting.current;
pointerInteractionMovement.current = delta;
- setR(delta / 200);
- }
- };
-
- const onRender = useCallback(
- (state: Record<string, any>) => {
- if (!pointerInteracting.current) phi += 0.005;
- state.phi = phi + r;
- state.width = width * 2;
- state.height = width * 2;
- },
- [r],
- );
-
- const onResize = () => {
- if (canvasRef.current) {
- width = canvasRef.current.offsetWidth;
+ r.set(r.get() + delta / MOVEMENT_DAMPING);
}
};
useEffect(() => {
+ const onResize = () => {
+ if (canvasRef.current) {
+ width = canvasRef.current.offsetWidth;
+ }
+ };
+
window.addEventListener("resize", onResize);
onResize();
@@ -86,12 +85,20 @@ export function Globe({
...config,
width: width * 2,
height: width * 2,
- onRender,
+ onRender: (state) => {
+ if (!pointerInteracting.current) phi += 0.005;
+ state.phi = phi + rs.get();
+ state.width = width * 2;
+ state.height = width * 2;
+ },
});
- setTimeout(() => (canvasRef.current!.style.opacity = "1"));
- return () => globe.destroy();
- }, []);
+ setTimeout(() => (canvasRef.current!.style.opacity = "1"), 0);
+ return () => {
+ globe.destroy();
+ window.removeEventListener("resize", onResize);
+ };
+ }, [rs, config]);
return (
<div
@@ -105,11 +112,10 @@ export function Globe({
"size-full opacity-0 transition-opacity duration-500 [contain:layout_paint_size]",
)}
ref={canvasRef}
- onPointerDown={(e) =>
- updatePointerInteraction(
- e.clientX - pointerInteractionMovement.current,
- )
- }
+ onPointerDown={(e) => {
+ pointerInteracting.current = e.clientX;
+ updatePointerInteraction(e.clientX);
+ }}
onPointerUp={() => updatePointerInteraction(null)}
onPointerOut={() => updatePointerInteraction(null)}
onMouseMove={(e) => updateMovement(e.clientX)}