forked from ozanyerli/turkeyvisited
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turkeyvisited.js
88 lines (72 loc) · 2.69 KB
/
turkeyvisited.js
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
85
86
87
88
console.log("Hello");
const HOVER_COLOR = "#EFAE88"
const MAP_COLOR = "#fff2e3"
d3.json('tr-cities.json').then(function (data) {
let width = 1200; height = 800;
let projection = d3.geoEqualEarth();
projection.fitSize([width, height], data);
let path = d3.geoPath().projection(projection);
let svg = d3.select("#map_container").append('svg').attr("width", width).attr("height", height);
let g = svg.append('g').selectAll('path').data(data.features).join('path').attr('d', path).attr('fill', MAP_COLOR).attr('stroke', '#000')
.on("mouseover", function (d, i) {
d3.select(this).attr("fill", HOVER_COLOR)
})
.on("mouseout", function (d, i) {
if (!d.noFill)
d3.select(this).attr("fill", MAP_COLOR)
})
.on("click", function (d, i) {
d.noFill = d.noFill || false;
if (!d.noFill) {
d3.select(this).attr("fill", HOVER_COLOR);
} else {
d3.select(this).attr("fill", MAP_COLOR);
}
d.noFill = !d.noFill;
});
console.log(data.features.map((f) => f.properties.name))
g = svg.append('g')
g
.selectAll("text")
.data(data.features)
.enter()
.append("text")
.text(function (d) {
return d.properties.name;
})
.attr("x", function (d) {
return path.centroid(d)[0];
})
.attr("y", function (d) {
return path.centroid(d)[1];
})
.attr('text-anchor', 'middle')
.attr('font-size', '10pt')
.attr('style', 'color: black;')
.attr('style', 'pointer-events: none;');
});
function downloadMap() {
let div = document.getElementById('map_container')
html2canvas(div).then(
function (canvas) {
var destCanvas = document.createElement('canvas');
destCanvas.width = canvas.width;
destCanvas.height = canvas.height;
var destCtx = destCanvas.getContext('2d')
destCtx.drawImage(canvas, 0, 0)
const ctx = destCanvas.getContext('2d')
ctx.textBaseline = "top"
ctx.font = "2em Calibri";
ctx.fillStyle = "black";
ctx.textAlign = "start";
var textWidth = ctx.measureText("ozanyerli.github.io/turkeyvisited")
ctx.fillText("ozanyerli.github.io/turkeyvisited", 10, canvas.height - 25);
/*ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(canvas.width, canvas.height);
ctx.stroke(); */
destCanvas.toBlob(function (blob) {
saveAs(blob, "turkeyvisited.png")
})
})
}