nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

decoration.scale.js (2386B)


      1 import {PrintStaticLayer} from './decorations';
      2 
      3 function pageScaleRange(printOptions) {
      4     const pageSize = printOptions.destPixelSize.divideBy(printOptions.resolution / 25.4);
      5     const bounds = printOptions.latLngBounds;
      6     const southLen = bounds.getSouthEast().distanceTo(bounds.getSouthWest());
      7     const northLen = bounds.getNorthEast().distanceTo(bounds.getNorthWest());
      8     const nScale = Math.round(northLen / pageSize.x * 10);
      9     const sScale = Math.round(southLen / pageSize.x * 10);
     10     return {
     11         min: Math.min(nScale, sScale, printOptions.scale),
     12         max: Math.max(nScale, sScale, printOptions.scale)
     13     };
     14 }
     15 
     16 function formatScale(nominalScale, scaleRange) {
     17     const threshold = 0.05;
     18     if (
     19         Math.abs(nominalScale - scaleRange.min) / nominalScale > threshold ||
     20         Math.abs(nominalScale - scaleRange.max) / nominalScale > threshold
     21     ) {
     22         let unit;
     23         scaleRange = {...scaleRange};
     24         if (scaleRange.min >= 1000) {
     25             scaleRange.min /= 1000;
     26             scaleRange.max /= 1000;
     27             unit = 'km';
     28         } else {
     29             unit = 'm';
     30         }
     31         return `${scaleRange.min} – ${scaleRange.max} ${unit} in 1 cm`;
     32     }
     33     let unit;
     34     if (nominalScale >= 1000) {
     35         nominalScale /= 1000;
     36         unit = 'km';
     37     } else {
     38         unit = 'm';
     39     }
     40     return `${nominalScale} ${unit} in 1 cm`;
     41 }
     42 
     43 class OverlayScale extends PrintStaticLayer {
     44     fontSizeMm = 3;
     45     font = 'verdana';
     46     paddingMm = 1;
     47     overlaySolid = true;
     48 
     49     _drawRaster(canvas, printOptions) {
     50         const ctx = canvas.getContext('2d');
     51         let caption = formatScale(printOptions.scale, pageScaleRange(printOptions));
     52         if (printOptions.pagesCount > 1) {
     53             caption += ` | Page ${printOptions.pageLabel} / ${printOptions.pagesCount}`;
     54         }
     55         const fontSize = this.fontSizeMm / 25.4 * printOptions.resolution;
     56         const padding = this.paddingMm / 25.4 * printOptions.resolution;
     57         ctx.font = `${fontSize}px ${this.font}`;
     58         const textWidth = ctx.measureText(caption).width;
     59         ctx.textBaseline = 'bottom';
     60         ctx.fillStyle = 'rgba(255, 255, 255, 0.7)';
     61         ctx.fillRect(0, 0, textWidth + 2 * padding, fontSize + 2 * padding);
     62         ctx.fillStyle = '#000000';
     63         ctx.fillText(caption, padding, fontSize + padding);
     64     }
     65 }
     66 
     67 export {OverlayScale};