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

@cieloazul310/canvasmap

v0.4.0

Published

Generate plain map for data visualization.

Downloads

16

Readme

Canvas Map for Data Visualization

Canvas マップ -データビジュアライゼーションのための

npm version

Example

Installing

npm install @cieloazul310/canvasmap

How to Use

Node (Vector map)

const { CanvasMap } = require("@cieloazul310/canvasmap");

const width = 1000;
const height = 1000;
const map = new CanvasMap(width, height, {
  center: [140.4602, 36.3703],
  zoom: 13,
});
map
  .renderVectorMap()
  .then((canvas) => {
    canvas.exportPng("./dist/basic.png");
  })
  .catch((err) => console.error(err));

Node (Raster map)

const { CanvasMap } = require("@cieloazul310/canvasmap");

const width = 1000;
const height = 1000;
const map = new CanvasMap(width, height, {
  center: [140.4602, 36.3703],
  zoom: 13,
});
map
  .renderRasterMap({
    tileUrl: "https://cyberjapandata.gsi.go.jp/xyz/seamlessphoto/{z}/{x}/{y}.jpg",
  })
  .then((canvas) => {
    canvas.exportPng("./dist/basic.png");
  })
  .catch((err) => console.error(err));

Browser

import { CanvasMap } from "@cieloazul310/canvasmap/browser";

const width = 1000;
const height = 1000;
const map = new CanvasMap(width, height, {
  center: [140.4602, 36.3703],
  zoom: 13,
});

async function onClick() {
  await map.renderVectorMap();
  const canvas = map.getCanvas();
  const contaienr = document.querySelector("#map");
  container?.appendChild(container);
}

const button = document.querySelector("#rbutton");
button?.addEventListener("click", onClick);

API Reference

CanvasMap (class)

const map = new CanvasMap(width, height, options);

Constructor

  • width (required) number: 生成する地図の横幅
  • height (required) number: 生成する地図の縦幅
  • options (optional): Partial<object>

| name | type | | |------|------|---| | title | string | Map title | | center | [number, number] | The center of map view | | zoom | number | Zoom level of map view | | theme | Partial<Theme> | Map theme including padding, palette and fontSizes | | zoomDelta | number | (if the zoom is 12 and the zoomDelta is 1, the tile zoom level will be 13 ; default to 1). |

zoomDelta: https://observablehq.com/@d3/tile-zoomdelta?collection=@d3/d3-tile

Common Methods

setCenter
  • arguments: [number, number]
  • returns: this
setZoom
  • arguments: number
  • returns: this
setZoomDelta
  • arguments: number
  • returns: this
setProjectionFitExtent
  • arguments: Feature | FeatureCollection
  • returns: this
setBBox
  • arguments: bbox ([minX, minY, maxX, maxY])
  • returns: this
setTitle
  • arguments: string
  • returns: this
addAttribution
  • arguments: string
  • returns: this
setTheme
  • arguments: Partial<{ padding: Partial<Padding>; palette: Partial<Palette> }>
  • returns: this
clearBBox
  • returns: this
getSize
  • returns: object { width: number; height: number }
getProjection
getZoom
  • returns: number
getTiles

CanvasMap (node) Methods

getCanvas
getContext
getPath
async renderVectorMap
  • arguments: Options (optional)
  • returns: Promise<this>

| name | types | | |------|-------|-| | background | string | Background color | | backgroundFeature | Feature \| FeatureCollection | Emphasized feature as background. | | layers | VectorLayerNames[] | VectorLayerNames to render. |

VectorLayerNames: "boundary" | "building" | "contour" | "label" | "railway" | "road" | "symbol" | "waterarea"

async renderRasterMap
  • arguments: Options(optional)
  • returns: Promise<this>

| name | types | | |------|-------|-| | tileUrl | string | Raster tile url (default to https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png ) | | tileSize | number | Tile image size (default to 256) |

async exportPng
  • arguments:
    • fileName: string
    • options: PngOptions (sharp)
  • returns: Promise<this> (CanvasMap class)
async exportJpg
  • arguments:
    • filename: string
    • options: JpegOptions (sharp)
  • returns: Promise<this> (CanvasMap class)
async exportWebp
  • arguments:
    • filename: string
    • options: WebpOptions (sharp)
  • returns: Promise<this> (CanvasMap class)

CanvasMap (browser) Methods

getCanvas
  • returns: HTMLCanvasElement
getContext
  • returns: CanvasRenderingContext2D
getPath
async renderVectorMap
  • arguments: Options (optional)
  • returns: Promise<this>

| name | types | | |------|-------|-| | background | string | Background color or none | | backgroundFeature | Feature \| FeatureCollection | Emphasized feature as background. | | layers | VectorLayerNames[] | VectorLayerNames to render. |

VectorLayerNames: "boundary" | "building" | "contour" | "label" | "railway" | "road" | "symbol" | "waterarea"

async renderRasterMap
  • arguments: Options(optional)
  • returns: Promise<this>

| name | types | | |------|-------|-| | tileUrl | string | Raster tile url (default to https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png ) | | tileSize | number | Tile image size (default to 256) |

Recipes

Basic

const width = 1000;
const height = 1000;
const map = new CanvasMap(width, height);
map.renderVectorMap()
  .then((map) => {
    map.exportPng('map.png');
  });

Use async/await

(async () => {
  const width = 1000;
  const height = 1000;
  const map = new CanvasMap(width, height);
  await map.renderVectorMap();
  map.exportPng('map.png');
})();

Draw GeoJSON

const geojson = JSON.parse(fs.readFileSync('gj.geojson', 'utf8'));
const width = 1000;
const height = 1000;
const map = new CanvasMap(width, height, geojson);
await map.renderVectorMap();
const context = map.getContext();
const path = map.getPath();
// draw features
geojson.features.forEach((feature) => {
  context.beginPath();
  path(feature);
  context.fillStyle = '#aaf';
  context.fill();
});
map.exportPng('map.png');

Browse example codes

References