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).4metadata:5 source: https://skillmd.com/skills/mengto/cobejs6---78# cobe.js — Lightweight WebGL Globe Skill910## When to use11- A “spinning globe” / location markers in hero or about pages12- You want a small, focused globe lib (not full three.js)13- Decorative + interactive (markers, rotation) with minimal setup1415## Key APIs/patterns16- Core:17 - `import createGlobe from "cobe"`18 - `const globe = createGlobe(canvas, { ...options, onRender(state) { ... } })`19- Important options (common):20 - `devicePixelRatio`, `width`, `height`21 - `phi`, `theta` (rotation angles)22 - `scale`, `dark`, `diffuse`23 - `baseColor`, `markerColor`, `glowColor`24 - `markers: [{ location: [lat, lon], size, color? }]`25- Lifecycle:26 - `globe.toggle()` pauses RAF27 - `globe.destroy()` removes instance2829## Common pitfalls30- Canvas sizing mismatch31 - Set CSS size AND set canvas `width/height` scaled for DPR.32- Not updating on resize33 - Recompute width/height and recreate or update params.34- Too high DPR on mobile35 - Clamp DPR to 1–2.3637## Quick recipe: responsive globe with markers38```js39import createGlobe from "cobe";4041const canvas = document.getElementById("cobe");42let phi = 0;4344function setup() {45 const rect = canvas.getBoundingClientRect();46 const dpr = Math.min(window.devicePixelRatio, 2);47 canvas.width = Math.round(rect.width * dpr);48 canvas.height = Math.round(rect.height * dpr);4950 const globe = createGlobe(canvas, {51 devicePixelRatio: dpr,52 width: canvas.width,53 height: canvas.height,54 phi: 0,55 theta: 0.2,56 dark: 0,57 diffuse: 1.2,58 scale: 1,59 mapSamples: 16000,60 mapBrightness: 6,61 baseColor: [0.2, 0.2, 0.25],62 glowColor: [1, 1, 1],63 markerColor: [0.8, 0.5, 1],64 markers: [{ location: [1.3521, 103.8198], size: 0.08 }],65 onRender: (state) => {66 state.phi = phi;67 phi += 0.01;68 },69 });7071 return globe;72}7374let globe = setup();75window.addEventListener("resize", () => {76 globe.destroy();77 globe = setup();78});79```8081## What to ask the user82- Globe size and placement (hero, section, card)?83- Marker locations + colors (brand-aligned)?84- Interaction needs (drag to rotate vs. ambient spin)?