versatiles-svg-renderer
    Preparing search index...

    Class PNGMapRenderer

    Renders many views of one MapLibre style, as PNG or SVG.

    An SVGMapRenderer that can also render PNG, sharing everything it keeps between the two formats: the parsed style, the sprite and the tiles. For PNG it also keeps the decoded tile and sprite images. Like renderToPNG, PNG rendering works in Node.js only.

    import { PNGMapRenderer } from '@versatiles/png-renderer';

    const map = new PNGMapRenderer({ style });

    const berlin = await map.renderPNG({ lon: 13.4, lat: 52.52, zoom: 12 });
    const potsdam = await map.renderPNG({ lon: 13.06, lat: 52.4, zoom: 12 });
    const svg = await map.renderSVG({ lon: 13.4, lat: 52.52, zoom: 12 });

    Hierarchy (View Summary)

    Index
    • Forgets the fetched tiles and sprite, and the decoded images.

      Returns void

    • Draws view onto renderer, which must already have the view's size.

      Type Parameters

      • R extends Renderer

      Parameters

      Returns Promise<R>

    • Where a coordinate lands in the image of view: its position in the units of width and height, the same in the SVG and on the canvas of renderCanvas. Use it to place your own drawing on the map.

      On the globe, a coordinate on its far side is hidden; then this returns undefined. A position outside the image is returned as is, so it can lie beyond width and height, or be negative. The map ends at about ±85.05° latitude, as in MapLibre: a latitude beyond that, up to the poles, gives the position of the map's edge.

      Parameters

      • view: ViewOptions

        The view the image was rendered with.

      • lonLat: [number, number]

        Longitude and latitude, in degrees.

      Returns [number, number] | undefined

      [x, y], or undefined for a point hidden on the globe, and for a longitude or latitude that is not a finite number.

      const view = { lon: 13.4, lat: 52.52, zoom: 12 };
      const canvas = await map.renderCanvas(view);
      const [x, y] = map.project(view, [13.3777, 52.5163])!; // Brandenburg Gate
      const ctx = canvas.getContext('2d');
      ctx.beginPath();
      ctx.arc(x, y, 6, 0, 2 * Math.PI);
      ctx.fill();

      If width or height is not positive.

    • Renders one view of the map onto a canvas, to draw on top of it or to encode it in another format.

      The canvas is width × scale by height × scale pixels. Its 2D context is in its default state, except that it is scaled by scale: draw in the same units as width and height. Encode it with canvas.encode('webp' | 'jpeg' | 'avif' | 'png') or canvas.toBuffer(…).

      The canvas records what is drawn and only paints the pixels when they are needed, usually when it is encoded, so drawing more on it costs little. Reading pixels (getImageData) paints everything recorded so far, every time it is called.

      Parameters

      • view: PNGViewOptions = {}

        Size, centre, zoom and pixel density. All optional.

      Returns Promise<Canvas>

      A Canvas of @napi-rs/canvas.

      const canvas = await map.renderCanvas({ lon: 13.4, lat: 52.52, zoom: 12 });
      await writeFile('berlin.webp', await canvas.encode('webp'));

      To draw on the map, see the example on SVGMapRenderer.project.

    • Renders one view of the map as PNG.

      Parameters

      • view: PNGViewOptions = {}

        Size, centre, zoom and pixel density. All optional.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The encoded PNG file, as described on renderToPNG.

      If the @napi-rs/canvas binary cannot be loaded, if width, height or scale is not positive, or if a font in fonts cannot be loaded.

    • Renders one view of the map as SVG. The result is a single self-contained SVG document, as described on renderToSVG.

      Parameters

      • view: ViewOptions = {}

        Size, centre and zoom. All optional.

      Returns Promise<string>

      The SVG document, as a string beginning with <svg.

      If width or height is not positive.

    • The opposite of SVGMapRenderer.project: the coordinate shown at a position in the image of view, e.g. where a user clicked on it.

      Where the image shows no map — next to the globe, or beyond the poles of the mercator map (about ±85°) — this returns undefined. The longitude is between -180 and 180.

      Parameters

      • view: ViewOptions

        The view the image was rendered with.

      • xy: [number, number]

        A position in the units of width and height.

      Returns [number, number] | undefined

      [lon, lat] in degrees, or undefined where there is no map, and for a position that is not a finite number.

      If width or height is not positive.