@tinkatinka/static-map 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -112,6 +112,22 @@ interface StaticMapRectOptions {
112
112
  }
113
113
  ```
114
114
 
115
+ #### Polygons
116
+ ```ts
117
+ addPolygon(points: LatLng[], options?: Partial<StaticMapPolygonOptions>);
118
+ ```
119
+
120
+ Options:
121
+
122
+ ```ts
123
+ interface StaticMapPolygonOptions {
124
+ strokeStyle: string; // css stroke style [default: 'black']
125
+ lineWidth: number; // line width in pixels [default: 1.0]
126
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
127
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
128
+ }
129
+ ```
130
+
115
131
  #### Text
116
132
  ```ts
117
133
  .addText(text: string, anchor: LatLng, options?: Partial<StaticMapTextOptions>);
package/dist/index.d.ts CHANGED
@@ -21,8 +21,8 @@ export interface FillOptions {
21
21
  export type StrokeFillOptions = SetOptional<StrokeOptions, 'strokeStyle'> & SetOptional<FillOptions, 'fillStyle'>;
22
22
  export type StaticMapOverlay = {
23
23
  type: StaticMapOverlayType;
24
- } & StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapRect | StaticMapText | StaticMapScale;
25
- type StaticMapOverlayType = 'image' | 'line' | 'circle' | 'rect' | 'text' | 'scale';
24
+ } & StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapRect | StaticMapPolygon | StaticMapText | StaticMapScale;
25
+ type StaticMapOverlayType = 'image' | 'line' | 'circle' | 'rect' | 'polygon' | 'text' | 'scale';
26
26
  export interface StaticMapImage {
27
27
  type: 'image';
28
28
  src: string;
@@ -48,6 +48,12 @@ export interface StaticMapRect {
48
48
  bounds: LatLngBounds;
49
49
  options: StaticMapRectOptions;
50
50
  }
51
+ export type StaticMapPolygonOptions = StrokeFillOptions;
52
+ export interface StaticMapPolygon {
53
+ type: 'polygon';
54
+ points: LatLng[];
55
+ options: StaticMapPolygonOptions;
56
+ }
51
57
  export interface StaticMapText {
52
58
  type: 'text';
53
59
  text: string;
@@ -122,6 +128,7 @@ export declare class StaticMap {
122
128
  addLine(src: LatLng, dst: LatLng, options?: Partial<StaticMapLineOptions>): StaticMap;
123
129
  addCircle(center: LatLng, options?: Partial<StaticMapCircleOptions>): StaticMap;
124
130
  addRectangle(bounds: LatLngBounds, options?: Partial<StaticMapRectOptions>): StaticMap;
131
+ addPolygon(points: LatLng[], options?: Partial<StaticMapPolygonOptions>): StaticMap;
125
132
  addText(text: string, anchor: LatLng, options?: Partial<StaticMapTextOptions>): StaticMap;
126
133
  addScale(options?: PartialDeep<StaticMapScaleOptions>): StaticMap;
127
134
  private boundsCenter;
package/dist/index.js CHANGED
@@ -141,6 +141,16 @@ export class StaticMap {
141
141
  options: merge({}, StaticMap.defaultRectOptions, options)
142
142
  });
143
143
  }
144
+ addPolygon(points, options) {
145
+ if (points.length >= 2) {
146
+ return this.addOverlay({
147
+ type: 'polygon',
148
+ points,
149
+ options: merge({}, StaticMap.defaultLineOptions, options)
150
+ });
151
+ }
152
+ return this;
153
+ }
144
154
  addText(text, anchor, options) {
145
155
  return this.addOverlay({
146
156
  type: 'text',
@@ -245,7 +255,8 @@ export class StaticMap {
245
255
  }
246
256
  case 'rect':
247
257
  return overlay.bounds;
248
- case 'line': {
258
+ case 'line':
259
+ case 'polygon': {
249
260
  const min = (arr) => arr.reduce((prev, curr) => ((curr < prev) ? curr : prev));
250
261
  const max = (arr) => arr.reduce((prev, curr) => ((curr > prev) ? curr : prev));
251
262
  const bounds = (points) => ({
@@ -425,6 +436,30 @@ export class StaticMap {
425
436
  }
426
437
  break;
427
438
  }
439
+ case 'polygon': {
440
+ if (overlay.points.length > 1) {
441
+ const p0 = this.latlngToPxPy(overlay.points[0], zoom, centerXY, scale);
442
+ ctx.beginPath();
443
+ ctx.moveTo(p0.x, p0.y);
444
+ for (const p of overlay.points.slice(1)) {
445
+ const pxy = this.latlngToPxPy(p, zoom, centerXY, scale);
446
+ ctx.lineTo(pxy.x, pxy.y);
447
+ }
448
+ ctx.closePath();
449
+ if (overlay.options.fillStyle) {
450
+ ctx.fillStyle = overlay.options.fillStyle;
451
+ ctx.fill();
452
+ }
453
+ if (overlay.options.strokeStyle && overlay.options.lineWidth > 0) {
454
+ ctx.strokeStyle = overlay.options.strokeStyle;
455
+ ctx.lineWidth = overlay.options.lineWidth;
456
+ ctx.lineCap = overlay.options.lineCap;
457
+ ctx.lineJoin = overlay.options.lineJoin;
458
+ ctx.stroke();
459
+ }
460
+ }
461
+ break;
462
+ }
428
463
  case 'circle': {
429
464
  const ccenter = this.latlngToPxPy(overlay.center, zoom, centerXY, scale);
430
465
  ctx.beginPath();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkatinka/static-map",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Render static map images",
5
5
  "author": "Florian Gmeiner <florian@tinkatinka.com>",
6
6
  "license": "MIT",