commit 7966cb1934f427e450e068f56f89e039326e1af5
parent b1116e753bf5128d67a2c7fb1baa99417f37d902
Author: Sergej Orlov <wladimirych@gmail.com>
Date: Sat, 18 Mar 2017 12:38:19 +0300
[print] overlay grid
Diffstat:
3 files changed, 85 insertions(+), 1 deletion(-)
diff --git a/src/lib/leaflet.control.printPages/control.js b/src/lib/leaflet.control.printPages/control.js
@@ -16,6 +16,7 @@ import 'lib/leaflet.control.commons';
import logging from 'lib/logging';
import {MagneticMeridians} from './decoration.magnetic-meridians';
import {OverlayScale} from './decoration.scale';
+import {Grid} from './decoration.grid';
ko.extenders.checkNumberRange = function(target, range) {
return ko.pureComputed({
@@ -208,6 +209,9 @@ L.Control.PrintPages = L.Control.extend({
);
const resolution = this.resolution();
const decorationLayers = [];
+ if (this.gridOn()) {
+ decorationLayers.push(new Grid());
+ }
if (this.magneticMerisiansdOn()) {
decorationLayers.push(new MagneticMeridians());
}
@@ -241,6 +245,9 @@ L.Control.PrintPages = L.Control.extend({
label: page.getLabel()
}];
const decorationLayers = [];
+ if (this.gridOn()) {
+ decorationLayers.push(new Grid());
+ }
if (this.magneticMerisiansdOn()) {
decorationLayers.push(new MagneticMeridians());
}
diff --git a/src/lib/leaflet.control.printPages/decoration.grid.js b/src/lib/leaflet.control.printPages/decoration.grid.js
@@ -0,0 +1,77 @@
+import {PrintStaticLayer} from './decorations';
+
+class Grid extends PrintStaticLayer {
+ lineThicknessMm = 0.2;
+ minGridIntervalMm = 15;
+
+ fontSizeMm = 3;
+ font = 'verdana';
+ paddingMm = 1;
+
+ intervals = [1, 1.5, 2, 3.3, 5, 7.5,
+ 10, 15, 20, 33, 50, 75,
+ 100, 150, 200, 333, 500, 750,
+ 1000, 1500, 2000, 3333, 5000, 7500,
+ 10000, 15000, 20000, 33333, 50000, 75000,
+ 100000, 150000, 200000, 333333, 500000, 750000,
+ 1000000, 1500000, 2000000, 3333333, 5000000, 7500000];
+
+ getGridInterval(printOptions) {
+ const minGridIntervalM = this.minGridIntervalMm / 10 * printOptions.scale;
+ let intervalM;
+ for (intervalM of this.intervals) {
+ if (intervalM > minGridIntervalM) {
+ break
+ }
+ }
+ return {intervalM, intervalMm: intervalM / printOptions.scale * 10};
+ }
+
+ formatDistance(x) {
+ let unit;
+ if (x < 1000) {
+ unit = 'm';
+ } else {
+ x /= 1000;
+ unit = 'km';
+ }
+ if (x % 1) {
+ x = x.toFixed(1);
+ }
+ return `${x} ${unit}`;
+ }
+
+ _drawRaster(canvas, printOptions) {
+ const ctx = canvas.getContext('2d');
+ ctx.beginPath();
+ const lineThickness = this.lineThicknessMm / 25.4 * printOptions.resolution;
+ const {intervalMm, intervalM} = this.getGridInterval(printOptions);
+ const intervalPx = intervalMm / 25.4 * printOptions.resolution;
+ ctx.lineWidth = lineThickness;
+ ctx.strokeStyle = '#ccc';
+ const width = printOptions.destPixelSize.x;
+ const height = printOptions.destPixelSize.y;
+ for (let x = 0; x <= width; x += intervalPx) {
+ ctx.moveTo(x, 0);
+ ctx.lineTo(x, height);
+ }
+ for (let y = height; y >= 0; y -= intervalPx) {
+ ctx.moveTo(0, y);
+ ctx.lineTo(width, y);
+ }
+ ctx.stroke();
+
+ const caption = 'Grid ' + this.formatDistance(intervalM);
+ const fontSize = this.fontSizeMm / 25.4 * printOptions.resolution;
+ const padding = this.paddingMm / 25.4 * printOptions.resolution;
+ ctx.font = `${fontSize}px ${this.font}`;
+ const textWidth = ctx.measureText(caption).width;
+ ctx.textBaseline = 'bottom';
+ ctx.fillStyle = '#ffffff';
+ ctx.fillRect(0, height - fontSize - 2 * padding, textWidth + 2 * padding, fontSize + 2 * padding);
+ ctx.fillStyle = '#000000';
+ ctx.fillText(caption, padding, height - padding);
+ }
+}
+
+export {Grid};
+\ No newline at end of file
diff --git a/src/lib/leaflet.control.printPages/decoration.scale.js b/src/lib/leaflet.control.printPages/decoration.scale.js
@@ -21,7 +21,6 @@ class OverlayScale extends PrintStaticLayer {
ctx.fillRect(0, 0, textWidth + 2 * padding, fontSize + 2 * padding);
ctx.fillStyle = '#000000';
ctx.fillText(caption, padding, fontSize + padding);
-
}
}