@tinkatinka/static-map 1.5.3 → 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
@@ -96,6 +96,38 @@ interface StaticMapCircleOptions {
96
96
  }
97
97
  ```
98
98
 
99
+ #### Rectangles
100
+ ```ts
101
+ .addRectangle(bounds; LatLngBounds, options?: Parital<StaticMapRectOptions>);
102
+ ```
103
+
104
+ Options:
105
+
106
+ ```ts
107
+ interface StaticMapRectOptions {
108
+ strokeStyle: string; // css stroke style [default: 'black']
109
+ lineWidth: number; // line width in pixels [default: 1.0]
110
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
111
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
112
+ }
113
+ ```
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
+
99
131
  #### Text
100
132
  ```ts
101
133
  .addText(text: string, anchor: LatLng, options?: Partial<StaticMapTextOptions>);
package/dist/base64img.js CHANGED
@@ -1,8 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const cross_fetch_1 = require("cross-fetch");
1
+ import { fetch } from 'cross-fetch';
4
2
  const base64img = async (url, mimetype = 'image/png', userAgent = 'StaticMap/1.0') => {
5
- const response = await (0, cross_fetch_1.fetch)(url, {
3
+ const response = await fetch(url, {
6
4
  headers: {
7
5
  'User-Agent': userAgent
8
6
  }
@@ -15,4 +13,4 @@ const base64img = async (url, mimetype = 'image/png', userAgent = 'StaticMap/1.0
15
13
  const dataurl = 'data:' + mimetype.trim() + ';base64,' + imgstr;
16
14
  return dataurl;
17
15
  };
18
- exports.default = base64img;
16
+ export default base64img;
@@ -1,11 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compactMap = void 0;
4
1
  const isDefined = (value) => (value !== undefined);
5
- const compactMap = (array, func) => array.map(func).filter(isDefined);
6
- exports.compactMap = compactMap;
2
+ export const compactMap = (array, func) => array.map(func).filter(isDefined);
7
3
  if (!Array.prototype.compactMap) {
8
4
  Array.prototype.compactMap = function (func) {
9
- return (0, exports.compactMap)(this, func);
5
+ return compactMap(this, func);
10
6
  };
11
7
  }
package/dist/index.d.ts CHANGED
@@ -19,24 +19,43 @@ export interface FillOptions {
19
19
  fillStyle: string | CanvasGradient | CanvasPattern;
20
20
  }
21
21
  export type StrokeFillOptions = SetOptional<StrokeOptions, 'strokeStyle'> & SetOptional<FillOptions, 'fillStyle'>;
22
- export type StaticMapOverlay = StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapText | StaticMapScale;
22
+ export type StaticMapOverlay = {
23
+ type: StaticMapOverlayType;
24
+ } & StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapRect | StaticMapPolygon | StaticMapText | StaticMapScale;
25
+ type StaticMapOverlayType = 'image' | 'line' | 'circle' | 'rect' | 'polygon' | 'text' | 'scale';
23
26
  export interface StaticMapImage {
27
+ type: 'image';
24
28
  src: string;
25
29
  bounds: LatLngBounds;
26
30
  }
27
31
  export interface StaticMapLine {
32
+ type: 'line';
28
33
  points: LatLng[];
29
34
  options: StaticMapLineOptions;
30
35
  }
31
36
  export type StaticMapLineOptions = StrokeOptions;
32
37
  export interface StaticMapCircle {
38
+ type: 'circle';
33
39
  center: LatLng;
34
40
  options: StaticMapCircleOptions;
35
41
  }
36
42
  export interface StaticMapCircleOptions extends StrokeFillOptions {
37
43
  radius: number;
38
44
  }
45
+ export type StaticMapRectOptions = StrokeFillOptions;
46
+ export interface StaticMapRect {
47
+ type: 'rect';
48
+ bounds: LatLngBounds;
49
+ options: StaticMapRectOptions;
50
+ }
51
+ export type StaticMapPolygonOptions = StrokeFillOptions;
52
+ export interface StaticMapPolygon {
53
+ type: 'polygon';
54
+ points: LatLng[];
55
+ options: StaticMapPolygonOptions;
56
+ }
39
57
  export interface StaticMapText {
58
+ type: 'text';
40
59
  text: string;
41
60
  anchor: LatLng;
42
61
  options: StaticMapTextOptions;
@@ -49,6 +68,7 @@ export interface StaticMapTextOptions extends CanvasTextDrawingStyles, StrokeFil
49
68
  export type LengthUnits = 'metric' | 'imperial';
50
69
  export type LayoutPosition = 'topleft' | 'topright' | 'bottomleft' | 'bottomright';
51
70
  export interface StaticMapScale {
71
+ type: 'scale';
52
72
  options: StaticMapScaleOptions;
53
73
  }
54
74
  export interface StaticMapScaleOptions {
@@ -87,6 +107,7 @@ export declare class StaticMap {
87
107
  private static defaultOptions;
88
108
  private static defaultLineOptions;
89
109
  private static defaultCircleOptions;
110
+ private static defaultRectOptions;
90
111
  private static defaultTextOptions;
91
112
  private static defaultScaleOptions;
92
113
  readonly options: StaticMapOptions;
@@ -106,6 +127,8 @@ export declare class StaticMap {
106
127
  addLines(points: LatLng[], options?: Partial<StaticMapLineOptions>): StaticMap;
107
128
  addLine(src: LatLng, dst: LatLng, options?: Partial<StaticMapLineOptions>): StaticMap;
108
129
  addCircle(center: LatLng, options?: Partial<StaticMapCircleOptions>): StaticMap;
130
+ addRectangle(bounds: LatLngBounds, options?: Partial<StaticMapRectOptions>): StaticMap;
131
+ addPolygon(points: LatLng[], options?: Partial<StaticMapPolygonOptions>): StaticMap;
109
132
  addText(text: string, anchor: LatLng, options?: Partial<StaticMapTextOptions>): StaticMap;
110
133
  addScale(options?: PartialDeep<StaticMapScaleOptions>): StaticMap;
111
134
  private boundsCenter;
@@ -125,10 +148,10 @@ export declare class StaticMap {
125
148
  private ang;
126
149
  private distance;
127
150
  private scaleDistance;
128
- private overlayType;
129
151
  private overlayExtent;
130
152
  private calculateExtent;
131
153
  private calculateZoom;
132
154
  private loadTile;
133
155
  private createCanvas;
134
156
  }
157
+ export {};
package/dist/index.js CHANGED
@@ -1,21 +1,84 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.StaticMap = void 0;
7
- const canvas_1 = __importDefault(require("canvas"));
8
- const lodash_merge_1 = __importDefault(require("lodash.merge"));
9
- const tilecache_1 = require("./tilecache");
10
- const base64img_1 = __importDefault(require("./base64img"));
11
- const compactmap_1 = require("./compactmap");
1
+ import Canvas from 'canvas';
2
+ import { merge } from 'lodash-es';
3
+ import { TileCache } from './tilecache.js';
4
+ import base64img from './base64img.js';
5
+ import { compactMap } from './compactmap.js';
12
6
  const EARTH_RADIUS_MEAN = 6371008.8;
13
7
  const FEET_PER_METER = 3.2808399;
14
8
  const FEET_PER_MILE = 5280;
15
- class StaticMap {
9
+ export class StaticMap {
10
+ static defaultOptions = {
11
+ width: 512,
12
+ height: 512,
13
+ padding: 0,
14
+ scaling: true,
15
+ tileURL: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
16
+ tileSize: 256,
17
+ tileMaxZoom: 20,
18
+ grayscale: false
19
+ };
20
+ static defaultLineOptions = {
21
+ strokeStyle: 'black',
22
+ lineWidth: 1.0,
23
+ lineCap: 'round',
24
+ lineJoin: 'round'
25
+ };
26
+ static defaultCircleOptions = {
27
+ radius: 5.0,
28
+ strokeStyle: 'rgba(128, 128, 128, 0.5)',
29
+ lineWidth: 1.0,
30
+ lineCap: 'round',
31
+ lineJoin: 'round',
32
+ fillStyle: 'rgba(0, 0, 0, 0.5)'
33
+ };
34
+ static defaultRectOptions = {
35
+ strokeStyle: 'rgba(128, 128, 128, 0.5)',
36
+ lineWidth: 1.0,
37
+ lineCap: 'round',
38
+ lineJoin: 'round',
39
+ fillStyle: 'rgba(0, 0, 0, 0.5)'
40
+ };
41
+ static defaultTextOptions = {
42
+ px: 0,
43
+ py: 0,
44
+ textAlign: 'center',
45
+ textBaseline: 'middle',
46
+ direction: 'inherit',
47
+ font: '12px sans-serif',
48
+ fontKerning: 'auto',
49
+ lineWidth: 1.0,
50
+ lineCap: 'round',
51
+ lineJoin: 'round',
52
+ fillStyle: 'black'
53
+ };
54
+ static defaultScaleOptions = {
55
+ units: 'metric',
56
+ position: 'bottomleft',
57
+ mx: 4,
58
+ my: 4,
59
+ height: 16,
60
+ maxWidth: 20,
61
+ boxStyle: {
62
+ lineWidth: 1.0,
63
+ lineCap: 'square',
64
+ lineJoin: 'miter',
65
+ strokeStyle: 'black',
66
+ fillStyle: 'rgba(255, 255, 255, 0.3)'
67
+ },
68
+ textStyle: {
69
+ font: '12px sans-serif',
70
+ lineWidth: 1.0,
71
+ lineCap: 'round',
72
+ lineJoin: 'round',
73
+ fillStyle: 'black'
74
+ }
75
+ };
76
+ options;
77
+ cache;
78
+ overlays;
16
79
  constructor(options) {
17
- this.options = (0, lodash_merge_1.default)({}, StaticMap.defaultOptions, options);
18
- this.cache = this.options.tileCache ? new tilecache_1.TileCache(this.options.tileCache) : undefined;
80
+ this.options = merge({}, StaticMap.defaultOptions, options);
81
+ this.cache = this.options.tileCache ? new TileCache(this.options.tileCache) : undefined;
19
82
  this.overlays = [];
20
83
  }
21
84
  get extent() {
@@ -49,13 +112,14 @@ class StaticMap {
49
112
  return this;
50
113
  }
51
114
  addImage(src, bounds) {
52
- return this.addOverlay({ src, bounds });
115
+ return this.addOverlay({ type: 'image', src, bounds });
53
116
  }
54
117
  addLines(points, options) {
55
118
  if (points.length >= 2) {
56
119
  return this.addOverlay({
57
- points: points,
58
- options: (0, lodash_merge_1.default)({}, StaticMap.defaultLineOptions, options)
120
+ type: 'line',
121
+ points,
122
+ options: merge({}, StaticMap.defaultLineOptions, options)
59
123
  });
60
124
  }
61
125
  return this;
@@ -65,20 +129,40 @@ class StaticMap {
65
129
  }
66
130
  addCircle(center, options) {
67
131
  return this.addOverlay({
68
- center: center,
69
- options: (0, lodash_merge_1.default)({}, StaticMap.defaultCircleOptions, options)
132
+ type: 'circle',
133
+ center,
134
+ options: merge({}, StaticMap.defaultCircleOptions, options)
70
135
  });
71
136
  }
137
+ addRectangle(bounds, options) {
138
+ return this.addOverlay({
139
+ type: 'rect',
140
+ bounds,
141
+ options: merge({}, StaticMap.defaultRectOptions, options)
142
+ });
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
+ }
72
154
  addText(text, anchor, options) {
73
155
  return this.addOverlay({
74
- text: text,
75
- anchor: anchor,
76
- options: (0, lodash_merge_1.default)({}, StaticMap.defaultTextOptions, options)
156
+ type: 'text',
157
+ text,
158
+ anchor,
159
+ options: merge({}, StaticMap.defaultTextOptions, options)
77
160
  });
78
161
  }
79
162
  addScale(options) {
80
163
  return this.addOverlay({
81
- options: (0, lodash_merge_1.default)({}, StaticMap.defaultScaleOptions, options)
164
+ type: 'scale',
165
+ options: merge({}, StaticMap.defaultScaleOptions, options)
82
166
  });
83
167
  }
84
168
  boundsCenter(bounds) {
@@ -160,25 +244,8 @@ class StaticMap {
160
244
  d = (d >= 10) ? 10 : (d >= 5) ? 5 : (d >= 3) ? 3 : (d >= 2) ? 2 : 1;
161
245
  return p * d;
162
246
  }
163
- overlayType(overlay) {
164
- if (overlay.bounds !== undefined) {
165
- return 'image';
166
- }
167
- else if (overlay.center !== undefined) {
168
- return 'circle';
169
- }
170
- else if (overlay.anchor !== undefined) {
171
- return 'text';
172
- }
173
- else if (overlay.points !== undefined) {
174
- return 'line';
175
- }
176
- else {
177
- return 'scale';
178
- }
179
- }
180
247
  overlayExtent(overlay) {
181
- const type = this.overlayType(overlay);
248
+ const type = overlay.type;
182
249
  switch (type) {
183
250
  case 'image':
184
251
  return overlay.bounds;
@@ -186,7 +253,10 @@ class StaticMap {
186
253
  const c = overlay.center;
187
254
  return { min: c, max: c };
188
255
  }
189
- case 'line': {
256
+ case 'rect':
257
+ return overlay.bounds;
258
+ case 'line':
259
+ case 'polygon': {
190
260
  const min = (arr) => arr.reduce((prev, curr) => ((curr < prev) ? curr : prev));
191
261
  const max = (arr) => arr.reduce((prev, curr) => ((curr > prev) ? curr : prev));
192
262
  const bounds = (points) => ({
@@ -231,7 +301,7 @@ class StaticMap {
231
301
  lng: max(arr.map(b => b.max.lng))
232
302
  }
233
303
  });
234
- return union((0, compactmap_1.compactMap)(this.overlays, this.overlayExtent.bind(this)));
304
+ return union(compactMap(this.overlays, this.overlayExtent.bind(this)));
235
305
  }
236
306
  calculateZoom(extent) {
237
307
  let z = this.options.tileMaxZoom + 1;
@@ -258,7 +328,7 @@ class StaticMap {
258
328
  if (this.cache !== undefined) {
259
329
  const td = { z: zoom, x: tileX, y: tileY };
260
330
  try {
261
- const csrc = await this.cache.pass(td, () => (0, base64img_1.default)(src, 'image/png', this.options.userAgent));
331
+ const csrc = await this.cache.pass(td, () => base64img(src, 'image/png', this.options.userAgent));
262
332
  if (csrc !== undefined) {
263
333
  src = csrc;
264
334
  }
@@ -269,9 +339,9 @@ class StaticMap {
269
339
  }
270
340
  }
271
341
  else {
272
- src = await (0, base64img_1.default)(src, 'image/png', this.options.userAgent);
342
+ src = await base64img(src, 'image/png', this.options.userAgent);
273
343
  }
274
- const image = await canvas_1.default.loadImage(src);
344
+ const image = await Canvas.loadImage(src);
275
345
  return image;
276
346
  }
277
347
  async createCanvas() {
@@ -280,7 +350,7 @@ class StaticMap {
280
350
  throw new Error('Undefined extent');
281
351
  }
282
352
  const { width, height, scaling, backgroundColor, grayscale, tileSize } = this.options;
283
- const canvas = canvas_1.default.createCanvas(width, height);
353
+ const canvas = Canvas.createCanvas(width, height);
284
354
  const ctx = canvas.getContext('2d');
285
355
  if (backgroundColor !== undefined) {
286
356
  ctx.fillStyle = backgroundColor;
@@ -323,13 +393,12 @@ class StaticMap {
323
393
  ctx.putImageData(imgData, 0, 0);
324
394
  }
325
395
  for (const overlay of this.overlays) {
326
- const type = this.overlayType(overlay);
396
+ const type = overlay.type;
327
397
  switch (type) {
328
398
  case 'image': {
329
- const imgdata = overlay;
330
399
  let img;
331
400
  try {
332
- img = new canvas_1.default.Image();
401
+ img = new Canvas.Image();
333
402
  }
334
403
  catch {
335
404
  img = new Image();
@@ -337,80 +406,118 @@ class StaticMap {
337
406
  }
338
407
  const draw = async () => new Promise((resolve, reject) => {
339
408
  img.onload = () => {
340
- const x = this.lngToPx(imgdata.bounds.min.lng, zoom, centerXY.x, scale);
341
- const y = this.latToPy(imgdata.bounds.max.lat, zoom, centerXY.y, scale);
342
- const dx = this.lngToPx(imgdata.bounds.max.lng, zoom, centerXY.x, scale) - x;
343
- const dy = this.latToPy(imgdata.bounds.min.lat, zoom, centerXY.y, scale) - y;
409
+ const x = this.lngToPx(overlay.bounds.min.lng, zoom, centerXY.x, scale);
410
+ const y = this.latToPy(overlay.bounds.max.lat, zoom, centerXY.y, scale);
411
+ const dx = this.lngToPx(overlay.bounds.max.lng, zoom, centerXY.x, scale) - x;
412
+ const dy = this.latToPy(overlay.bounds.min.lat, zoom, centerXY.y, scale) - y;
344
413
  ctx.drawImage(img, x, y, dx, dy);
345
414
  resolve();
346
415
  };
347
416
  img.onerror = reject;
348
- img.src = imgdata.src;
417
+ img.src = overlay.src;
349
418
  });
350
419
  await draw();
351
420
  break;
352
421
  }
353
422
  case 'line': {
354
- const linedata = overlay;
355
- if (linedata.points.length > 1) {
356
- const p0 = this.latlngToPxPy(linedata.points[0], zoom, centerXY, scale);
423
+ if (overlay.points.length > 1) {
424
+ const p0 = this.latlngToPxPy(overlay.points[0], zoom, centerXY, scale);
357
425
  ctx.beginPath();
358
426
  ctx.moveTo(p0.x, p0.y);
359
- for (const p of linedata.points.slice(1)) {
427
+ for (const p of overlay.points.slice(1)) {
360
428
  const pxy = this.latlngToPxPy(p, zoom, centerXY, scale);
361
429
  ctx.lineTo(pxy.x, pxy.y);
362
430
  }
363
- ctx.strokeStyle = linedata.options.strokeStyle;
364
- ctx.lineWidth = linedata.options.lineWidth;
365
- ctx.lineCap = linedata.options.lineCap;
366
- ctx.lineJoin = linedata.options.lineJoin;
431
+ ctx.strokeStyle = overlay.options.strokeStyle;
432
+ ctx.lineWidth = overlay.options.lineWidth;
433
+ ctx.lineCap = overlay.options.lineCap;
434
+ ctx.lineJoin = overlay.options.lineJoin;
367
435
  ctx.stroke();
368
436
  }
369
437
  break;
370
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
+ }
371
463
  case 'circle': {
372
- const circdata = overlay;
373
- const ccenter = this.latlngToPxPy(circdata.center, zoom, centerXY, scale);
464
+ const ccenter = this.latlngToPxPy(overlay.center, zoom, centerXY, scale);
465
+ ctx.beginPath();
466
+ ctx.arc(ccenter.x, ccenter.y, overlay.options.radius, 0, 2 * Math.PI, false);
467
+ if (overlay.options.fillStyle) {
468
+ ctx.fillStyle = overlay.options.fillStyle;
469
+ ctx.fill();
470
+ }
471
+ if (overlay.options.strokeStyle && overlay.options.lineWidth > 0) {
472
+ ctx.strokeStyle = overlay.options.strokeStyle;
473
+ ctx.lineWidth = overlay.options.lineWidth;
474
+ ctx.stroke();
475
+ }
476
+ break;
477
+ }
478
+ case 'rect': {
374
479
  ctx.beginPath();
375
- ctx.arc(ccenter.x, ccenter.y, circdata.options.radius, 0, 2 * Math.PI, false);
376
- if (circdata.options.fillStyle) {
377
- ctx.fillStyle = circdata.options.fillStyle;
480
+ const min = this.latlngToPxPy(overlay.bounds.min, zoom, centerXY, scale);
481
+ const max = this.latlngToPxPy(overlay.bounds.max, zoom, centerXY, scale);
482
+ ctx.rect(min.x, min.y, max.x - min.x, max.y - min.y);
483
+ if (overlay.options.fillStyle) {
484
+ ctx.fillStyle = overlay.options.fillStyle;
378
485
  ctx.fill();
379
486
  }
380
- if (circdata.options.strokeStyle && circdata.options.lineWidth > 0) {
381
- ctx.strokeStyle = circdata.options.strokeStyle;
382
- ctx.lineWidth = circdata.options.lineWidth;
487
+ if (overlay.options.strokeStyle && overlay.options.lineWidth > 0) {
488
+ ctx.strokeStyle = overlay.options.strokeStyle;
489
+ ctx.lineWidth = overlay.options.lineWidth;
490
+ ctx.lineJoin = overlay.options.lineJoin;
491
+ ctx.lineCap = overlay.options.lineCap;
383
492
  ctx.stroke();
384
493
  }
385
494
  break;
386
495
  }
387
496
  case 'text': {
388
- const textdata = overlay;
389
- const anchor = this.latlngToPxPy(textdata.anchor, zoom, centerXY, scale);
390
- const opts = textdata.options;
497
+ const anchor = this.latlngToPxPy(overlay.anchor, zoom, centerXY, scale);
498
+ const opts = overlay.options;
391
499
  ctx.textAlign = opts.textAlign;
392
500
  ctx.textBaseline = opts.textBaseline;
393
501
  ctx.direction = opts.direction;
394
502
  ctx.font = opts.font;
395
503
  if (opts.fillStyle) {
396
504
  ctx.fillStyle = opts.fillStyle;
397
- ctx.fillText(textdata.text, anchor.x + opts.px, anchor.y + opts.py, opts.maxWidth);
505
+ ctx.fillText(overlay.text, anchor.x + opts.px, anchor.y + opts.py, opts.maxWidth);
398
506
  }
399
507
  if (opts.strokeStyle) {
400
508
  ctx.strokeStyle = opts.strokeStyle;
401
- ctx.strokeText(textdata.text, anchor.x + opts.px, anchor.y + opts.py, opts.maxWidth);
509
+ ctx.strokeText(overlay.text, anchor.x + opts.px, anchor.y + opts.py, opts.maxWidth);
402
510
  }
403
511
  break;
404
512
  }
405
513
  case 'scale': {
406
- const scaledata = overlay;
407
514
  const midLeft = { lat: center.lat, lng: extent.min.lng };
408
515
  const midRight = { lat: center.lat, lng: extent.max.lng };
409
516
  const mapWidth = this.distance(midLeft, midRight);
410
- const maxDist = mapWidth * scaledata.options.maxWidth / 100;
517
+ const maxDist = mapWidth * overlay.options.maxWidth / 100;
411
518
  let label;
412
519
  let ratio;
413
- if (scaledata.options.units === 'metric') {
520
+ if (overlay.options.units === 'metric') {
414
521
  const scaleLen = this.scaleDistance(maxDist);
415
522
  label = (scaleLen > 1000) ? `${scaleLen / 1000} km` : `${scaleLen} m`;
416
523
  ratio = scaleLen / maxDist;
@@ -432,17 +539,17 @@ class StaticMap {
432
539
  const midLeftP = this.latlngToPxPy(midLeft, zoom, centerXY, scale);
433
540
  const midRightP = this.latlngToPxPy(midRight, zoom, centerXY, scale);
434
541
  const widthP = Math.abs(midRightP.x - midLeftP.x);
435
- const dx = widthP * ratio * scaledata.options.maxWidth / 100;
542
+ const dx = widthP * ratio * overlay.options.maxWidth / 100;
436
543
  let p0 = { x: 0, y: 0 };
437
- const mpx = Math.max(scaledata.options.mx * width / 100, scaledata.options.my * height / 100);
544
+ const mpx = Math.max(overlay.options.mx * width / 100, overlay.options.my * height / 100);
438
545
  const margin = { x: mpx, y: mpx };
439
- switch (scaledata.options.position) {
546
+ switch (overlay.options.position) {
440
547
  case 'bottomleft': {
441
- p0 = { x: margin.x, y: height - margin.y - scaledata.options.height };
548
+ p0 = { x: margin.x, y: height - margin.y - overlay.options.height };
442
549
  break;
443
550
  }
444
551
  case 'bottomright': {
445
- p0 = { x: width - dx - margin.x, y: height - margin.y - scaledata.options.height };
552
+ p0 = { x: width - dx - margin.x, y: height - margin.y - overlay.options.height };
446
553
  break;
447
554
  }
448
555
  case 'topleft': {
@@ -455,8 +562,8 @@ class StaticMap {
455
562
  }
456
563
  default: break;
457
564
  }
458
- const p1 = { x: p0.x + dx, y: p0.y + scaledata.options.height };
459
- const boxStyle = scaledata.options.boxStyle;
565
+ const p1 = { x: p0.x + dx, y: p0.y + overlay.options.height };
566
+ const boxStyle = overlay.options.boxStyle;
460
567
  if (boxStyle.fillStyle !== undefined) {
461
568
  ctx.fillStyle = boxStyle.fillStyle;
462
569
  ctx.fillRect(p0.x, p0.y, Math.abs(p1.x - p0.x), Math.abs(p1.y - p0.y));
@@ -474,7 +581,7 @@ class StaticMap {
474
581
  ctx.stroke();
475
582
  }
476
583
  const anchor = { x: 0.5 * (p0.x + p1.x), y: p1.y - 3 };
477
- const textStyle = scaledata.options.textStyle;
584
+ const textStyle = overlay.options.textStyle;
478
585
  ctx.textAlign = 'center';
479
586
  ctx.textBaseline = 'bottom';
480
587
  ctx.direction = 'ltr';
@@ -496,63 +603,3 @@ class StaticMap {
496
603
  return canvas;
497
604
  }
498
605
  }
499
- exports.StaticMap = StaticMap;
500
- StaticMap.defaultOptions = {
501
- width: 512,
502
- height: 512,
503
- padding: 0,
504
- scaling: true,
505
- tileURL: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
506
- tileSize: 256,
507
- tileMaxZoom: 20,
508
- grayscale: false
509
- };
510
- StaticMap.defaultLineOptions = {
511
- strokeStyle: 'black',
512
- lineWidth: 1.0,
513
- lineCap: 'round',
514
- lineJoin: 'round'
515
- };
516
- StaticMap.defaultCircleOptions = {
517
- radius: 5.0,
518
- strokeStyle: 'rgba(128, 128, 128, 0.5)',
519
- lineWidth: 1.0,
520
- lineCap: 'round',
521
- lineJoin: 'round',
522
- fillStyle: 'rgba(0, 0, 0, 0.5)'
523
- };
524
- StaticMap.defaultTextOptions = {
525
- px: 0,
526
- py: 0,
527
- textAlign: 'center',
528
- textBaseline: 'middle',
529
- direction: 'inherit',
530
- font: '12px sans-serif',
531
- fontKerning: 'auto',
532
- lineWidth: 1.0,
533
- lineCap: 'round',
534
- lineJoin: 'round',
535
- fillStyle: 'black'
536
- };
537
- StaticMap.defaultScaleOptions = {
538
- units: 'metric',
539
- position: 'bottomleft',
540
- mx: 4,
541
- my: 4,
542
- height: 16,
543
- maxWidth: 20,
544
- boxStyle: {
545
- lineWidth: 1.0,
546
- lineCap: 'square',
547
- lineJoin: 'miter',
548
- strokeStyle: 'black',
549
- fillStyle: 'rgba(255, 255, 255, 0.3)'
550
- },
551
- textStyle: {
552
- font: '12px sans-serif',
553
- lineWidth: 1.0,
554
- lineCap: 'round',
555
- lineJoin: 'round',
556
- fillStyle: 'black'
557
- }
558
- };
package/dist/tilecache.js CHANGED
@@ -1,13 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TileCache = void 0;
4
- const fs_1 = require("fs");
5
- const path_1 = require("path");
6
- const util_1 = require("util");
7
- class TileCache {
1
+ import { existsSync, mkdirSync, readFile, writeFile } from 'fs';
2
+ import { join as pathJoin } from 'path';
3
+ import { promisify } from 'util';
4
+ export class TileCache {
5
+ static fnTemplate = '{z}_{x}_{y}.png';
6
+ path;
8
7
  constructor(path) {
9
8
  this.path = path;
10
- (0, fs_1.mkdirSync)(path, { recursive: true });
9
+ mkdirSync(path, { recursive: true });
11
10
  }
12
11
  async pass(data, generator) {
13
12
  let buf = await this.read(data);
@@ -19,20 +18,18 @@ class TileCache {
19
18
  }
20
19
  async read(data) {
21
20
  const p = this.tilePath(data);
22
- const r = (0, fs_1.existsSync)(p) ? await (0, util_1.promisify)(fs_1.readFile)(p, { encoding: 'utf-8' }) : undefined;
21
+ const r = existsSync(p) ? await promisify(readFile)(p, { encoding: 'utf-8' }) : undefined;
23
22
  return r;
24
23
  }
25
24
  async write(data, src) {
26
25
  const p = this.tilePath(data);
27
- await (0, util_1.promisify)(fs_1.writeFile)(p, src, { encoding: 'utf-8' });
26
+ await promisify(writeFile)(p, src, { encoding: 'utf-8' });
28
27
  }
29
28
  tilePath(data) {
30
29
  const fn = TileCache.fnTemplate
31
30
  .replace('{z}', data.z.toString())
32
31
  .replace('{x}', data.x.toString())
33
32
  .replace('{y}', data.y.toString());
34
- return (0, path_1.join)(this.path, fn);
33
+ return pathJoin(this.path, fn);
35
34
  }
36
35
  }
37
- exports.TileCache = TileCache;
38
- TileCache.fnTemplate = '{z}_{x}_{y}.png';
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@tinkatinka/static-map",
3
- "version": "1.5.3",
3
+ "version": "2.1.0",
4
4
  "description": "Render static map images",
5
5
  "author": "Florian Gmeiner <florian@tinkatinka.com>",
6
6
  "license": "MIT",
7
+ "type": "module",
7
8
  "main": "dist/index.js",
8
9
  "types": "dist/index.d.ts",
9
10
  "files": [
@@ -26,7 +27,7 @@
26
27
  "build": "tsc -p tsconfig.build.json",
27
28
  "clean": "rm -rf dist/*",
28
29
  "lint": "eslint . --ext .ts,.tsx,.js,.jsx",
29
- "test": "jest"
30
+ "test": "tsc -p tsconfig.test.json && node --experimental-vm-modules --no-warnings --test --test-reporter=spec build/tests/"
30
31
  },
31
32
  "browser": {
32
33
  "node-fetch": false
@@ -34,24 +35,18 @@
34
35
  "dependencies": {
35
36
  "canvas": "^2.8.0",
36
37
  "cross-fetch": "^4.0.0",
37
- "lodash.merge": "^4.6.2"
38
+ "lodash-es": "^4.17.21"
38
39
  },
39
40
  "devDependencies": {
40
- "@types/jest": "^29.4.0",
41
- "@types/jest-image-snapshot": "^6.1.0",
42
- "@types/lodash.merge": "^4.6.6",
41
+ "@types/lodash-es": "^4.17.12",
43
42
  "@types/node": "^20.8.9",
44
43
  "@typescript-eslint/eslint-plugin": "^6.9.0",
45
44
  "eslint": "^8.0.1",
46
45
  "eslint-plugin-import": "^2.23.4",
47
- "jest": "^29.5.0",
48
- "jest-image-snapshot": "^6.1.0",
49
- "ts-jest": "^29.0.5",
50
46
  "type-fest": "^4.6.0",
51
47
  "typescript": "^5.2.2"
52
48
  },
53
49
  "peerDependencies": {
54
- "canvas": "^2.8.0",
55
- "lodash.merge": "^4.6.2"
50
+ "canvas": "^2.8.0"
56
51
  }
57
52
  }