-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapcounter.html
96 lines (96 loc) · 2.06 KB
/
mapcounter.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map</title>
<style>
*{
padding:0;
margin: 0;
}
canvas{
position: absolute;
}
#grid{
z-index: 10;
}
#imageD{
z-index: 5;
}
#mock{
z-index: 0;
position: absolute;
background: url(images/worldmock.jpg);
width: 1580px;
height: 780px;
background-position: -2px 0
}
.result{
position: absolute;
top: 850px;
}
</style>
</head>
<body>
<canvas id='imageD' width="1580" height="780"></canvas>
<canvas id='grid' width="1580" height="780"></canvas>
<div id="mock"></div>
<div class="result">
<textarea name="" id="result" cols="30" rows="10"></textarea>
<button id="generate">Generate</button>
</div>
</body>
<script>
(function(){
var img = new Image();
img.src = 'images/world.svg';
//记录map
var Map = new Array(158);
for(var i=0;i<Map.length;i++){
Map[i] = new Array(78);
for(var j=0;j<78;j++){
Map[i][j]=0;
}
}
img.onload = function(){
var canvas = document.getElementById('imageD');
var grid = document.getElementById('grid');
var generate = document.getElementById('generate');
var resultText = document.getElementById('result');
generate.addEventListener("click",function(){
var result = [];
for(var i=0;i<158;i++){
for(j=0;j<78;j++){
if(Map[i][j]){
result.push([i,j]);
}
}
}
resultText.value = JSON.stringify(result);
},false);
grid.strokeStyle="gray";
grid.fillStyle="black";
var gtx = grid.getContext("2d");
for(var i=0;i<=158;i++){
gtx.moveTo(i*10,0);
gtx.lineTo(i*10,780);
}
for(var j=0;j<=78;j++){
gtx.moveTo(0,j*10);
gtx.lineTo(1580,j*10);
}
gtx.stroke();
grid.addEventListener("click",function(e){
var gX = parseInt(e.offsetX/10);
var gY = parseInt(e.offsetY/10);
var gridX = parseInt(e.offsetX/10)*10+1;
var gridY = parseInt(e.offsetY/10)*10+1;
gtx.fillRect(gridX,gridY,10,10);
Map[gX][gY]=1;
},false);
var ctx = canvas.getContext("2d");
ctx.drawImage(img,0,0);
};
})();
</script>
</html>