Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AIScientists Dev WorldSeed useMapCamera

From Leeroopedia
Knowledge Sources
Domains Frontend, Interaction
Last Updated 2026-10-26 18:00 GMT

Overview

Concrete React hook implementing Figma-style pan/zoom camera for the world map viewport, provided by the WorldSeed frontend.

Description

The useMapCamera hook manages a camera state (x, y, zoom) via refs and applies it as a CSS transform on the canvas element. It supports mouse wheel zoom (with cursor-position anchoring), trackpad panning, drag-to-pan (with deselect-on-click-without-drag), and multi-touch gesture scaling. `centerView` computes a bounding box from rendered DOM children and fits the canvas within the viewport with optional insets and animation. The hook is idempotent (`isSetup` guard) and cleans up all event listeners on teardown.

Usage

Import this hook in the map view component, passing viewport and canvas refs. Call `setup(onDeselect)` on mount and `cleanup()` on unmount. Call `centerView()` when the layout changes.

Code Reference

Source Location

  • Repository: WorldSeed
  • File: frontend/src/hooks/useMapCamera.ts

Signature

export function useMapCamera(
  viewportRef: React.RefObject<HTMLElement | null>,
  canvasRef: React.RefObject<HTMLElement | null>,
): {
  setup: (onDeselect: () => void) => void;
  cleanup: () => void;
  centerView: (layout?: Record<string, any>, inset?: { top?: number; bottom?: number }, animate?: boolean) => void;
  applyCamera: () => void;
};

Import

import { useMapCamera } from '@/hooks/useMapCamera';

I/O Contract

Inputs

Name Type Required Description
viewportRef RefObject<HTMLElement> Yes Container viewport element ref
canvasRef RefObject<HTMLElement> Yes Canvas element to transform
onDeselect () => void Yes (setup) │ Called when user clicks empty space (no drag)
inset { top?, bottom? } No (centerView) │ Padding for header/footer
animate boolean No (centerView) │ Whether to animate the centering

Outputs

Name Type Description
setup function │ Attaches wheel/mouse/gesture listeners
cleanup function │ Removes all listeners
centerView function │ Fits canvas children in viewport

Usage Examples

import { useRef, useEffect } from 'react';
import { useMapCamera } from '@/hooks/useMapCamera';

function MapView() {
  const viewportRef = useRef<HTMLDivElement>(null);
  const canvasRef = useRef<HTMLDivElement>(null);
  const { setup, cleanup, centerView } = useMapCamera(viewportRef, canvasRef);

  useEffect(() => {
    setup(() => setSelectedId(null));
    return cleanup;
  }, []);

  return (
    <div ref={viewportRef} className="map-viewport">
      <div ref={canvasRef} className="map-canvas">{/* zones */}</div>
    </div>
  );
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment