-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
187 lines (142 loc) · 4.55 KB
/
index.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<head>
<style>
body {
margin: 0;
}
svg {
background: #eee;
}
.sphere {
fill: #fff;
}
.land {
fill: #000;
}
.boundary {
fill: none;
stroke: #fff;
stroke-linejoin: round;
stroke-linecap: round;
vector-effect: non-scaling-stroke;
}
#toCanvasButton {
position: absolute;
top: 20px;
left: 20px;
}
</style>
<script src="//d3js.org/d3.v5.min.js"></script>
<script src="//unpkg.com/topojson@3"></script>
<script src="pathfinding-browser.min.js"></script>
</head>
<body>
<button id="toCanvasButton">To canvas</button>
<script>
const width = window.innerWidth;
const height = window.innerHeight;
const toCanvasButton = document.querySelector("#toCanvasButton");
let canvas;
toCanvasButton.addEventListener("click", () => {
svgToCanvas();
})
const projection = d3.geoMercator()
.translate([width / 2, height / 2])
.scale((width - 1) / 2 / Math.PI);
const path = d3.geoPath()
.projection(projection);
const zoom = d3.zoom()
.scaleExtent([1, 20])
.on('zoom', zoomed);
const svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height);
const g = svg.append('g');
svg.call(zoom);
d3.json('//unpkg.com/world-atlas@1/world/110m.json')
.then(world => {
g.append('path')
.datum(topojson.merge(world, world.objects.countries.geometries))
.attr('class', 'land')
.attr('d', path);
});
function zoomed() {
g
.selectAll('path') // To prevent stroke width from scaling
.attr('transform', d3.event.transform);
}
const rectSize = 10;
const svgSize = document.querySelector("svg").getBoundingClientRect()
const widthNumber = Math.ceil(svgSize.width / rectSize);
const heightNumber = Math.ceil(svgSize.height / rectSize);
let matrix = [];
let colorMatrix = [];
for (let indexW = 0; indexW < widthNumber; indexW++) {
matrix[indexW] = [];
colorMatrix[indexW] = [];
for (let indexH = 0; indexH < heightNumber; indexH++) {
svg.append('rect')
.attr("x", indexW * rectSize)
.attr("y", indexH * rectSize)
.attr("width", rectSize)
.attr("height", rectSize)
.style("stroke", "white")
.style("fill", "none")
.style("stroke-width", 1);
matrix[indexW][indexH] = [indexW * rectSize + rectSize / 2, indexH * rectSize + rectSize / 2];
colorMatrix[indexW][indexH] = {}
}
}
// Create the export function - this will just export
// the first svg element it finds
function svgToCanvas() {
var svg = d3.select("svg"),
img = new Image(),
serializer = new XMLSerializer();
var w = svg.get
// prepend style to svg
svg.insert('defs', ":first-child")
// generate IMG in new tab
var svgStr = serializer.serializeToString(svg.node());
img.onload = function () {
h = this.height;
w = this.width;
canvas = document.createElement("canvas");
document.body.appendChild(canvas)
canvas.width = w;
canvas.height = h;
canvas.getContext("2d").drawImage(this, 0, 0, w, h);
var pixel = canvas.getContext("2d").getImageData(10, 10, 1, 1).data
console.log('', canvas);
for (let index = 0; index < matrix.length; index++) {
for (let i = 0; i < matrix[index].length; i++) {
var pixel = canvas.getContext("2d").getImageData(matrix[index][i][0], matrix[index][i][1], 1, 1).data.toString()
//console.log('',pixel);
colorMatrix[index][i] = {
coord: matrix[index][i],
color: pixel
}
}
}
buildNewMap (colorMatrix)
}
img.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgStr)));
};
function buildNewMap (colorMatrix){
const matrix = colorMatrix;
svg.selectAll("*").remove();
for (let index = 0; index < matrix.length; index++) {
for (let i = 0; i < matrix[index].length; i++) {
const {coord,color} = matrix[index][i];
svg.append('rect')
.attr("x", coord[0])
.attr("y", coord[1])
.attr("width", rectSize)
.attr("height", rectSize)
.style("stroke", "#ccc")
.style("fill", 'rgba('+color+')')
.style("stroke-width", 0.5);
}
}
}
</script>