cobe

repository·main·Indexed 26 days ago

https://github.com/shuding/cobe

A high-performance, zero-dependency globe visualization library (~5KB) using WebGL to render interactive globes with markers and arcs. It supports CSS Anchor Positioning to bind DOM elements to markers and arcs via custom CSS properties, and provides a `createGlobe` function to initialize and manage the globe's lifecycle and visual configuration.

Tokens
2K
Snippets
3
Records
8
Agent score
91%

What's inside cobe

  1. Quick Start with Cobe

    main

    To use Cobe, create a <canvas> element and pass it to the createGlobe function along with a configuration object. The onRender callback allows you to update parameters (like phi or theta) on every animation frame to create rotation or other animations.

    import createGlobe from 'cobe'
    
    let phi = 0
    let canvas = document.getElementById("cobe")
    
    const globe = createGlobe(canvas, {
      devicePixelRatio: 2,
      width: 1000,
      height: 1000,
      phi: 0,
      theta: 0,
      dark: 0,
      diffuse: 1.2,
      scale: 1,
      mapSamples: 16000,
      mapBrightness: 6,
      baseColor: [0.3, 0.3, 0.3],
      markerColor: [1, 0.5, 1],
      glowColor: [1, 1, 1],
      offset: [0, 0],
      markers: [
        { location: [37.7595, -122.4367], size: 0.03 },
        { location: [40.7128, -74.006], size: 0.1, color: [1, 0, 0] },
      ],
      arcs: [
        {
          from: [37.7595, -122.4367],
          to: [40.7128, -74.006],
          color: [1, 0.5, 0.5],
        },
      ],
      arcColor: [1, 0.5, 1],
      arcWidth: 0.5,
      arcHeight: 0.3,
      markerElevation: 0.02,
      onRender: (state) => {
        state.phi = phi
        phi += 0.01
      },
    })
    
    // To destroy the instance and bindings:
    // globe.destroy()
  2. Use Bindable Markers and Arcs with CSS Anchor Positioning

    main

    You can assign an id to markers and arcs to enable CSS Anchor Positioning. Cobe exposes specific CSS custom properties to help you position and animate elements relative to these markers/arcs.

    CSS Variables Exposed:

    • --cobe-{id}: CSS anchor name for positioning markers.
    • --cobe-arc-{id}: CSS anchor name for positioning arcs.
    • --cobe-visible-{id}: Visibility variable (0 when behind the globe, 1 when visible).
    • --cobe-visible-arc-{id}: Visibility variable for arcs (0 when behind the globe, 1 when visible).

    Use these variables to drive CSS properties like opacity, filter (blur), or scale for smooth transitions as markers move behind the globe.

    // Adding IDs to markers and arcs
    markers: [
      { location: [37.7595, -122.4367], size: 0.03, id: 'sf' },
    ],
    arcs: [
      { from: [37.7595, -122.4367], to: [35.6762, 139.6503], id: 'sf-tokyo' },
    ]
    /* Example usage in CSS */
    .marker-label {
      position: absolute;
      position-anchor: --cobe-sf;
      bottom: anchor(top);
      left: anchor(center);
      opacity: var(--cobe-visible-sf, 0);
      filter: blur(calc((1 - var(--cobe-visible-sf, 0)) * 8px));
      transition: opacity 0.3s, filter 0.3s;
    }
    
    .arc-label {
      position: absolute;
      position-anchor: --cobe-arc-sf-tokyo;
      bottom: anchor(top);
      left: anchor(center);
      opacity: var(--cobe-visible-arc-sf-tokyo, 0);
    }
  3. Initialize a COBE globe

    main

    To create a COBE globe, call the default export function with an HTMLCanvasElement and an options object. The function returns an object containing update and destroy methods to manage the globe's state and lifecycle.

    Options (opts)

    KeyTypeDefaultDescription
    widthnumberrequiredCanvas width
    heightnumberrequiredCanvas height
    devicePixelRationumber1Scaling factor for high-DPI displays
    phinumber0Rotation around the Y-axis
    thetanumber0Rotation around the X-axis
    markersArray<Object>[]Array of marker objects
    arcsArray<Object>[]Array of arc objects
    mapSamplesnumber10000Number of samples for the land texture
    mapBrightnessnumber1Brightness of the map
    mapBaseBrightnessnumber0Base brightness of the map
    baseColornumber[][1, 1, 1]RGB color for the globe base
    markerColornumber[][1, 0.5, 0]RGB color for markers (if no color is provided per marker)
    glowColornumber[][1, 1, 1]RGB color for the globe glow
    arcColornumber[][0.3, 0.6, 1]RGB color for arcs (if no color is provided per arc)
    arcWidthnumber1Width of the arcs
    arcHeightnumber0.2Height/elevation of the arcs
    diffusenumber1Diffuse lighting factor
    darknumber0Darkness factor
    opacitynumber1Overall opacity
    offsetnumber[][0, 0][x, y] offset for the globe
    scalenumber1Scale of the globe
    markerElevationnumber0.05Elevation of markers and arcs above the surface
    contextObject{}WebGL context configuration (e.g., alpha, antialias)
  4. Configure Arcs

    main

    Arcs connect two geographic locations on the globe. You can specify a custom color for each arc; otherwise, it will fall back to the global arcColor setting.

    // Example arc configuration
    arcs: [
      {
        from: [37.7595, -122.4367],
        to: [35.6762, 139.6503],
        color: [1, 0.5, 0.5], // optional, uses arcColor if not set
      },
    ]
  5. Configure COBEOptions

    main

    The COBEOptions object defines the visual properties of the globe.

    Required properties:

    • width: number
    • height: number
    • phi: number (latitude/rotation)
    • theta: number (longitude/rotation)
    • mapSamples: number
    • mapBrightness: number
    • baseColor: [number, number, number] (RGB)
    • markerColor: [number, number, number] (RGB)
    • glowColor: [number, number, number] (RGB)
    • diffuse: number
    • devicePixelRatio: number
    • dark: number

    Optional properties:

    • mapBaseBrightness: number
    • markers: Marker[]
    • opacity: number
    • offset: [number, number]
    • scale: number
    • context: WebGLContextAttributes
    • arcs: Arc[] (v2+)
    • arcColor: [number, number, number] (v2+)
    • arcWidth: number (v2+)
    • arcHeight: number (v2+)
    • markerElevation: number (v2+)
  6. Initialize a globe with createGlobe()

    main
    To create a new globe instance, call createGlobe passing an HTMLCanvasElement and a COBEOptions object. The function returns a Globe object which provides methods to update the globe's state or destroy it.
  7. Define Markers and Arcs

    main

    You can add interactive or visual elements to the globe using markers and arcs arrays within COBEOptions.

    Marker Interface:

    • location: [number, number] (latitude, longitude)
    • size: number
    • color?: [number, number, number]
    • id?: string

    Arc Interface:

    • from: [number, number] (start latitude, longitude)
    • to: [number, number] (end latitude, longitude)
    • color?: [number, number, number]
    • id?: string
  8. Update or destroy the globe instance

    main

    The Globe object returned by createGlobe provides two methods:

    • update(state: Partial<COBEOptions>): Updates the globe with new configuration values. Only the properties provided in the partial object will be changed.
    • destroy(): Cleans up the globe instance.