Zoom and Pan D3 Geo projections (Ported from vasturiano/d3-geo-zoom)
This library provides smooth zooming and panning functionality for D3 geographic projections using quaternion interpolation for rotation transitions.
npm install @fxi/d3-geo-zoom
import { GeoZoom } from '@fxi/d3-geo-zoom';
import { select } from 'd3-selection';
import { geoOrthographic, geoPath } from 'd3-geo';
// Create SVG element
const svg = select('body')
.append('svg')
.attr('width', 800)
.attr('height', 600);
// Setup projection
const projection = geoOrthographic()
.scale(250)
.translate([400, 300]);
// Create path generator
const path = geoPath()
.projection(projection);
// Initialize zoom behavior
const zoom = new GeoZoom(svg.node());
// Configure zoom behavior
zoom
.setProjection(projection)
.setNorthUp(true)
.onMove(() => {
// Update map on zoom/pan
svg.selectAll('path').attr('d', path);
});
Creates a new GeoZoom instance.
element
: DOM element to attach the zoom behavior to (typically an SVG element)
Sets the D3 geographic projection to use.
projection
: A D3 geographic projection (e.g., geoOrthographic, geoMercator)- Returns: this (for method chaining)
Smoothly rotates to the specified rotation angles.
rotation
: Array of [lambda, phi, gamma] rotation angles in degrees- Returns: this (for method chaining)
Moves the projection in the specified direction.
direction
: One of 'left', 'right', 'up', 'down', or 'north'step
: Step size in degrees (default: 10)- Returns: this (for method chaining)
Resets zoom and rotation to initial state.
- Returns: this (for method chaining)
Enables/disables north-up constraint.
enabled
: Boolean to enable/disable north-up mode- Returns: this (for method chaining)
Sets the allowed scale range.
extent
: Array of [minimum, maximum] scale values- Returns: this (for method chaining)
Sets the duration for smooth transitions.
duration
: Duration in milliseconds- Returns: this (for method chaining)
Sets a callback function to be called when the projection moves.
callback
: Function that receives { scale, rotation } as parameter- Returns: this (for method chaining)
Gets the underlying D3 zoom behavior.
- Returns: D3 zoom behavior instance
For more examples and detailed API documentation, visit our documentation site.
- Clone the repository
- Install dependencies:
npm install
npm run dev
: Start development server for demonpm run dev:docs
: Start development server for documentation sitenpm run build
: Build library for productionnpm run build:docs
: Build documentation sitenpm run preview
: Preview production build
npm test
: Run tests in watch mode (for development)npm run test:ci
: Run tests once (used in CI and git hooks)npm run test:ui
: Open test UInpm run coverage
: Generate test coverage report
The documentation site is built using Vite and hosted on GitHub Pages. To work on the documentation:
- Run
npm run dev:docs
to start the documentation development server - Edit files in the
docs/src
directory:index.html
: Main documentation pagestyle.css
: Documentation stylesmain.js
: Interactive examples and demos
The documentation site is automatically deployed to GitHub Pages when changes are pushed to the main branch.
This project uses Commitizen for standardized commit messages. Instead of using git commit
, use:
npm run commit
This will:
- Run tests in non-watch mode (via pre-commit hook)
- Start an interactive prompt to create a properly formatted commit message
- Validate the commit message format
The prompt will help you create a commit message with:
- Type of change (feat, fix, docs, etc.)
- Scope (optional)
- Description
- Breaking changes (if any)
- Issues being closed (if any)
This project uses GitHub Actions for:
- Runs on every push and pull request
- Tests against Node.js 18.x and 20.x
- Automatically publishes to npm on release
- Automatically builds and deploys documentation to GitHub Pages
- Triggered on push to main branch
- Available at https://fxi.github.io/d3-geo-zoom
This project uses semantic-release for automated versioning:
fix:
commits trigger a patch release (1.0.0 -> 1.0.1)feat:
commits trigger a minor release (1.0.0 -> 1.1.0)BREAKING CHANGE:
in commit body triggers a major release (1.0.0 -> 2.0.0)
This is a port of the original d3-geo-zoom by Vasco Asturiano.
See the LICENSE file for details.