-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdraw.js
More file actions
84 lines (78 loc) · 2.54 KB
/
draw.js
File metadata and controls
84 lines (78 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const geometry = require("./geometry");
const { createCanvas } = require("canvas");
const tinycolor = require("tinycolor2");
function render(geojson, meta, options = {}) {
const bounds = options.bounds;
const width = parseInt(options.width) || 512;
const height =
parseInt(options.height) ||
geometry.calculateHeightToMaintainAspect(width, bounds);
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d");
const scaling = {
x: { offset: -bounds.left, scale: width / (bounds.right - bounds.left) },
y: { offset: bounds.top, scale: height / (bounds.top - bounds.bottom) }
};
debugger;
ctx.lineWidth = options.strokeWidth;
ctx.antialias = options.antialias || "default";
ctx.antialias = "none";
ctx.globalCompositeOperation = "multiply";
ctx.quality = "nearest";
// ctx.globalCompositeOperation = "multiply";
const area = geometry.calculateArea(geojson.features);
const ssArea = area * scaling.x.scale * scaling.y.scale;
const coverageFraction = ssArea / height / width;
ctx.lineWidth *= 0.1 * Math.pow(2.3, Math.log10(1 / coverageFraction));
ctx.lineWidth = 0.5;
geojson.features.forEach(feature => {
ctx.fillStyle = lookupColor(
meta,
feature.properties,
options.colorProperty
);
ctx.strokeStyle =
options.strokeColor ||
tinycolor(ctx.fillStyle)
.darken(10)
.toString();
drawGeometries(ctx, feature, scaling);
});
return { buffer: canvas.toBuffer(), width, height };
}
function drawGeometries(ctx, feature, scaling) {
feature.geometry &&
feature.geometry.coordinates.forEach(coordinates =>
drawGeometry(ctx, coordinates, scaling)
);
}
function drawGeometry(ctx, coordinates, scaling) {
if (Array.isArray(coordinates[0])) {
coordinates.forEach(coord => {
drawGeometry(ctx, coord, scaling);
return;
});
}
ctx.beginPath();
coordinates.forEach(coordIn => {
const x = (coordIn[0] + scaling.x.offset) * scaling.x.scale;
const y = (scaling.y.offset - coordIn[1]) * scaling.y.scale;
ctx.lineTo(x, y);
});
ctx.closePath();
ctx.stroke();
ctx.fill();
}
function lookupColor(meta, properties, colorProperty) {
if (!properties) return meta.farge;
if (!meta.barn) return meta.farge;
let key = properties[colorProperty] || properties.code; //HACK
if (!key) return meta.farge;
key = key.replace("LA-", "-");
key = key.replace("NA-", "-");
for (var barn of meta.barn) {
if (barn[colorProperty].indexOf(key) >= 0) return barn.farge;
}
return meta.farge;
}
module.exports = render;