3.0.2 • Published 6 years ago
karto v3.0.2
karto
Define your maps once and render to:
Usage
npm install kartoDefine the map
import karto from 'karto'
import polygonGeometry from './polygon.json'
import lineGeometry from './line.json'
const map = karto`
<map>
<polygon geometry=${polygonGeometry} fill="blue" stroke="lightblue" />
<line geometry=${lineGeometry} stroke="red" strokeWidth=${3} strokeDasharray=${[10, 2]} />
</map>
`karto is a tagged template. Use an xml-like syntax to describe your map.
Elements
<map>
<map> must be the parent element.
Attributes
projectionis ad3-geoGeoProjection. Defaults to ageoMercatorprojection that fits all geometrieswidthis anumber. Defaults to1000heightis anumber. Defaults to500backgroundColoris an HTML color. Defaults toundefined
<polygon>
Attributes
geometryis a GeoJSON geometry of typePolygonorMultiPolygon. REQUIRED- all stroke attributes
- all fill attributes
<line>
Attributes
geometryis a GeoJSON geometry of typeLineStringorMultiLineString. REQUIRED- all stroke attributes
<marker>
geometryis a GeoJSON geometry of typePointorMultiPoint. REQUIRED- all stroke attributes
- all fill attributes
<circle>
geometryis a GeoJSON geometry of typePointorMultiPoint. REQUIREDris anumber. It is the radius of the circle. REQUIRED- all stroke attributes
- all fill attributes
<label>
geometryis a GeoJSON geometry of typePointorMultiPoint. REQUIREDtextis astring. The content of the label. REQUIREDtranslateis an array of twonumbers.- all stroke attributes
- all fill attributes
- all text attributes
<tiles>
urlis astring, the url to the tile server. REQUIREDattributionis astring, the copyright for the tiles. REQUIREDextis astring, the file extension if applicablesubdomainsis astring, the server subdomains if applicable
Look at Leaflet providers for an idea of how this works.
Style attributes
All elements have an opacity attribute. It is a number between 0 and 1.
Fill attributes
fillis an HTML color. Defaults toblackfillOpacityis anumberbetween 0 and 1
Stroke attributes
strokeis an HTML color. Defaults toundefinedstrokeWidthis anumber. Defaults to1strokeLinejoinis astring. Options:arcs|bevel|miter|miter-clip|roundstrokeDasharrayis an array ofnumbersstrokeDashoffsetis anumberstrokeLinecapis astring. Options:butt|round|squarestrokeOpacityis anumberbetween 0 and 1
Text attributes
fontFamilyis astringfontSizeis anumbertextAnchoris astring. Options:start|middle|end
Render the map
renderSvgString
Takes one argument, the map definition.
import karto, { renderSvgString } from 'karto'
const map = karto`
// your map definition
`
renderSvgString(map)
.then(console.log)renderToCanvas
Takes two arguments:
- The
idof an existing<div>to which the canvas will be added - The map definition
import karto from 'karto'
import renderToCanvas from 'karto/dist/renderers/canvas'
const map = karto`
// your map definition
`
renderToCanvas('divId', map)renderToLeaflet
Takes two arguments:
- The
idof an existing<div>to which the map will be added - The map definition
import karto from 'karto'
import renderToLeaflet from 'karto/dist/renderers/leaflet'
const map = karto`
// your map definition
`
renderToLeaflet('divId', map)