npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

emptymap.js

v1.0.1

Published

A module that helps in navigation of map like pan, zoom and rotation but does nothing itself.

Downloads

198

Readme

emptymap.js

A module that helps with navigation of a map, like pan, zoom and rotation, but does nothing itself.

emptymap.js takes advantage of CSS/SVG transform property. If you render your geojson data (spatial data) on a HTML page in form of SVG then this module can easily help with panning, zooming and rotating the SVG map. On SVG drag, pinch and rotate this module calculates the transformation matrix needed for the events to take effect. The module also calculates the transformation matrix for a <div> that contains map tiles as img tag.

installation

npm install emptymap.js

usage & API

To initiate the instance we need to pass the map SVG/DIV size and projection extent. In case of spherical (web) mercator projection there no need to pass the extent as this is the default projection.

var EmptyMap = require('emptymap.js'),
  size = { width: 714, height: 400 },
  view = {
    center: [-9655821.34064295, 4238014.557249293],
    zoom: 4,
    rotation: -15
  }, 
  em = new EmptyMap(size);

function updateMap (err, state) {
  // state has the transformation matrices
  if(err) {
    console.log(err);
    return;
  }

  //get SVG map layer (where id='svgmap')
  var svgLayer = document.getElementById('svgmap'),
    matrix = state.matrix.join(', ');
  svgLayer.setAttribute('transform', 'matrix(' + matrix + ')');

  //get map tile layer (where id='tilemap')
  var tileLayer = document.getElementById('tilemap'),
    tileMatrix = state.tileMatrix.join(',');
  tileLayer.style.transform = 'matrix(' + tileMatrix + ')';
  
  // TODO: of course you need to load the tiles for this view
}

//set the initial view
em.setView(view, updateMap);

// now lets pinch and rotate the map
em.scaleRotate(
  {
    center: [300, 200],
    rotation: 30, // in degree clock wise positive 
    factor: 2, // scale (pinch) factor
  },
  updateMap
);

API

constructor(viewportSize [,options])

Creates emptymap.js instance with map div's viewportSize and other options

viewportSize is the object with map div's width and height in pixel

{ 
  width: Number
  height: Number
}

options: different options are as follows:

  • projExtent: projection extent
  • tileSize: map tile size
  • view: initial map view i.e. center, zoom/resolution and rotation
  • callback: callback function that handle the matrix values after view is set

projExtent projection extent, default spherical mercator extent

  projExt: { 
    left: Number,
    right: Number,
    bottom: Number,
    top: Number
  }

tileSize i.e. whole projection extent should fit into one square tile, default is 256

view object with center, zoom/resolution and rotation

view: {
  center: [x, y], // default [0, 0]
  zoom: Integer, // default is 0
  resolution: Number, // zoom takes precedence over resolution if both are present
  rotation: Number // in degree default is 0
}

callback function that should accept error and current map state while state has following values:

{
  matrix: array of 6 transformation coefficients for svg map
  tileMatrix: array of 6 transformation coefficients for tile map
  map: reference of the map itself
}

callback usage:

function (err, state) {
  // state has the transformation matrices
  if(err) {
    console.log(err);
    return;
  }

  //get SVG map layer (where id='svgmap')
  var svgLayer = document.getElementById('svgmap'),
    matrix = state.matrix.join(', ');
  svgLayer.setAttribute('transform', 'matrix(' + matrix + ')');

  //get map tile layer (where id='tilemap')
  var tileLayer = document.getElementById('tilemap'),
    tileMatrix = state.tileMatrix.join(',');
  tileLayer.style.transform = 'matrix(' + tileMatrix + ')';
  
  // load tiles for new zoom level
}

.setView(view [, callback, scope])

Sets a view to the map with parameters:

  • view: As explained above.
  • callback: Callback function as explained in constructor.
  • scope: this for callback function.

.move(delta [, callback, scope])

Pans the map by given viewport's delta pixel values for x and y direction. Parameters are:

  • delta:
    • {deltaX: Number, deltaY: Number}
    • deltaX: Viewport pixels in x direction.
    • deltaY: Viewport pixels in y direction.
  • callback: Callback function as explained in constructor.
  • scope: this for callback function

.scaleRotate(params [, callback, scope])

Scale and rotate the map. Parameters are:

  • center: [x, y] center position for scale/rotation on viewport in pixels. Default value is center of viewport.
  • factor: Float scale factor to zoom in/out the map. Default is 1.
  • rotation: Float in degrees, clockwise positive. This is the delta rotation to be applied to the map. Default value is 0 degrees.
  • callback: Callback function as explained in constructor.
  • scope: this for callback function.

.resetTileMatrix([callback, scope])

Reset tile map matrix so that only the rotation transformation is applied, as scale and pan would be null for tile map. This function can be called after every transition/event pan, pinch (scale) and rotatation. During transition/event the tile matrix can be applied to the tile layer. Generally the callback of this function should also check if tiles are needed to be loaded for current map state.

  • callback: Callback function as explained in constructor.
  • scope: this for callback function.

.getCenter

Returns viewport center coordinates as an array of x and y in projected coordinate system.

.getResolution

Returns current resolution of the map.

.getRotation

Returns current rotation of the map in degree (clockwise positive).

.getZoom

Returns current zoom level of the map.

.getNearestZoom

When map state is at fractional zoom it returns the closest non-fractional zoom value.

.getView

Return current map state object as:

{
  center: Array of [x, y],
  resolution: Float,
  zoom: Float,
  rotation: Float in degrees (clockwise positive)
}

.getExtent

Returns viewport corner coordinates as an object:

{
  ul: upper left projected coordinates,
  ll: lower left projected coordinates,
  lr: lower right projected coordinates,
  ur: upper right projected coordinates
}

.getVewportBBox

Retruns viewport's BBox/MBR in projected coordinate system. Its different from map extent. Once map is rotated the viewport is not aligned to coordinate axis. Viewport's BBox should be aligned to coordinate axis.

{
  left: projeceted x,
  right: projected x,
  bottom: projected y,
  top: projected y
}

.toLongLat([x, y])

Converts viewport pixel coordinates to maps projected coordinates ([x, y]).

.toViewport([x, y])

Converts maps projected coordinates to viewport pixel coordinates ([x, y]).

Note: To set center, resolution/zoom and rotation use setView as followed:

var view = em.getView();
view.rotation = 25;

em.setView(view, function (err, state) {
  if (err) {
    console.log(err);
    return;
  }
  // set matrices to  required layers
});
```

## developing
Once you run
 
```npm install```

then for running test 

```npm run test```

and to create build

```npm run build```



## license
This project is licensed under the terms of the MIT license.