cobe.js — Lightweight WebGL Globe Skill
When to use
- A “spinning globe” / location markers in hero or about pages
- You want a small, focused globe lib (not full three.js)
- Decorative + interactive (markers, rotation) with minimal setup
Key APIs/patterns
- Core:
import createGlobe from "cobe"
const globe = createGlobe(canvas, { ...options, onRender(state) { ... } })
- Important options (common):
devicePixelRatio, width, height
phi, theta (rotation angles)
scale, dark, diffuse
baseColor, markerColor, glowColor
markers: [{ location: [lat, lon], size, color? }]
- Lifecycle:
globe.toggle() pauses RAF
globe.destroy() removes instance
Common pitfalls
- Canvas sizing mismatch
- Set CSS size AND set canvas
width/height scaled for DPR.
- Not updating on resize
- Recompute width/height and recreate or update params.
- Too high DPR on mobile
Quick recipe: responsive globe with markers
import createGlobe from "cobe";
const canvas = document.getElementById("cobe");
let phi = 0;
function setup() {
const rect = canvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio, 2);
canvas.width = Math.round(rect.width * dpr);
canvas.height = Math.round(rect.height * dpr);
const globe = createGlobe(canvas, {
devicePixelRatio: dpr,
width: canvas.width,
height: canvas.height,
phi: 0,
theta: 0.2,
dark: 0,
diffuse: 1.2,
scale: 1,
mapSamples: 16000,
mapBrightness: 6,
baseColor: [0.2, 0.2, 0.25],
glowColor: [1, 1, 1],
markerColor: [0.8, 0.5, 1],
markers: [{ location: [1.3521, 103.8198], size: 0.08 }],
onRender: (state) => {
state.phi = phi;
phi += 0.01;
},
});
return globe;
}
let globe = setup();
window.addEventListener("resize", () => {
globe.destroy();
globe = setup();
});
What to ask the user
- Globe size and placement (hero, section, card)?
- Marker locations + colors (brand-aligned)?
- Interaction needs (drag to rotate vs. ambient spin)?
1---2name: cobejs3description: Use when adding a lightweight interactive globe with cobe (canvas setup, markers, interaction, performance, integration with React/Next.js).4---5
6# cobe.js — Lightweight WebGL Globe Skill
7
8## When to use
9- A “spinning globe” / location markers in hero or about pages
10- You want a small, focused globe lib (not full three.js)
11- Decorative + interactive (markers, rotation) with minimal setup
12
13## Key APIs/patterns
14- Core:
15 - `import createGlobe from "cobe"`
16 - `const globe = createGlobe(canvas, { ...options, onRender(state) { ... } })`
17- Important options (common):
18 - `devicePixelRatio`, `width`, `height`
19 - `phi`, `theta` (rotation angles)
20 - `scale`, `dark`, `diffuse`
21 - `baseColor`, `markerColor`, `glowColor`
22 - `markers: [{ location: [lat, lon], size, color? }]`
23- Lifecycle:
24 - `globe.toggle()` pauses RAF
25 - `globe.destroy()` removes instance
26
27## Common pitfalls
28- Canvas sizing mismatch
29 - Set CSS size AND set canvas `width/height` scaled for DPR.
30- Not updating on resize
31 - Recompute width/height and recreate or update params.
32- Too high DPR on mobile
33 - Clamp DPR to 1–2.
34
35## Quick recipe: responsive globe with markers
36```js
37import createGlobe from "cobe";
38
39const canvas = document.getElementById("cobe");
40let phi = 0;
41
42function setup() {
43 const rect = canvas.getBoundingClientRect();
44 const dpr = Math.min(window.devicePixelRatio, 2);
45 canvas.width = Math.round(rect.width * dpr);
46 canvas.height = Math.round(rect.height * dpr);
47
48 const globe = createGlobe(canvas, {
49 devicePixelRatio: dpr,
50 width: canvas.width,
51 height: canvas.height,
52 phi: 0,
53 theta: 0.2,
54 dark: 0,
55 diffuse: 1.2,
56 scale: 1,
57 mapSamples: 16000,
58 mapBrightness: 6,
59 baseColor: [0.2, 0.2, 0.25],
60 glowColor: [1, 1, 1],
61 markerColor: [0.8, 0.5, 1],
62 markers: [{ location: [1.3521, 103.8198], size: 0.08 }],
63 onRender: (state) => {
64 state.phi = phi;
65 phi += 0.01;
66 },
67 });
68
69 return globe;
70}
71
72let globe = setup();
73window.addEventListener("resize", () => {
74 globe.destroy();
75 globe = setup();
76});
77```
78
79## What to ask the user
80- Globe size and placement (hero, section, card)?
81- Marker locations + colors (brand-aligned)?
82- Interaction needs (drag to rotate vs. ambient spin)?