-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
232 lines (200 loc) · 8.08 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!doctype html>
<html>
<head>
<title>Pkmn Map Gen</title>
<link rel="stylesheet" href="css/base.css" type="text/css">
<script src="js/jquery-1.11.2.min.js"></script>
</head>
<body>
<div id="toolbar" class="toolbar">
<div class="frame">
<span class="input width"><input type="number" id="width"></span>
<span class="input height"><input type="number" id="height"></span>
<span class="divider">|||</span>
<span class="zoom">Zoom: <span id="zoom_percent">100</span>%</span>
<button id="zoomIn">Zoom +</button>
<button id="zoomOut">Zoom -</button>
<a id="button_save" target="_blank"><button>Save Image</button></a>
<button id="button_data">Get Data</button>
<span class="divider">|||</span>
<button id="button_clear">Clear Map</button>
</div>
</div>
<div class="tileset_container">
<div id="selector" class="selector"></div>
<canvas id="tileset" class="" width="128" height="15971"></canvas>
</div>
<div id="map_wrapper" class="map_wrapper">
<canvas id="map" class=""></canvas>
</div>
<script>
// Default Parameters
var grid = 16;
var brush = [0,0];
var mapWidth = 15;
var mapHeight = 15;
var mapSource = "img/map_tile.png";
var mapArray = [];
//
// Draws an image to the canvas
//
// imgData.....an array with the tile's X and Y coordinates. ex. [2, 4]
// posX........X coordinate on the Map
// posy........Y coordinate on the map
//
function drawImage(imgData, posX, posY){
var img = new Image(),
posX = posX*grid,
posY = posY*grid;
img.onload = function(){
m.drawImage(img, imgData[0]*grid, imgData[1]*grid, grid, grid, posX, posY, grid, grid);
};
img.src = mapSource;
}
//
// Generate a new blank map
//
// src.....Map's source image (tileset image)
// w.......Map's Width
// h.......Map's Height
//
var map = document.getElementById('map');
var m = map.getContext('2d');
function newBlankMap(src, w, h){
m.canvas.width = w*grid;
m.canvas.height = h*grid;
// Reset the map and brush
mapArray = []
for(var n=0; n<h; n++){
var newRow = [];
for(var i=0; i<w; i++){
drawImage([0,0], i, n);
newRow.push("(0,0)")
}
mapArray.push(newRow)
}
}
newBlankMap(mapSource, mapWidth, mapHeight);
//
// Build the tileset picker
//
// A simple function to draw the tileset picker on the left
//
var tilesetCanvas = document.getElementById('tileset');
var t = tilesetCanvas.getContext('2d');
function initiateTileset(){
// Draw the tileset to the canvas
function drawTileset(src){
var tilesetImg = new Image();
tilesetImg.onload = function(){
t.drawImage(tilesetImg, 0, 0);
}
tilesetImg.src=src;
}
drawTileset(mapSource);
// Add event listeners
var tilesetMouseDown = 0;
var tilesetTempX = 0;
var tilesetTempY = 0;
tilesetCanvas.addEventListener('mousedown', function(e){
tilesetMouseDown = 1;
selectTile(e);
})
function selectTile(e){
var mouse_x = Math.floor(e.offsetX/grid);
var mouse_y = Math.floor(e.offsetY/grid);
// Re-draw the tileset
t.clearRect(0,0,tilesetCanvas.width,tilesetCanvas.height);
drawTileset("img/map_tile.png");
// Draw the rectangle
var selector = document.getElementById('selector');
selector.style.width = grid+'px';
selector.style.height = grid+'px';
selector.style.left = (mouse_x*grid)+'px';
selector.style.top = (mouse_y*grid)+'px';
brush = [mouse_x, mouse_y];
console.log("Brush: ("+brush[0]+","+brush[1]+")")
}
}
initiateTileset();
//
// [WIP] Allows you to draw on the map.
//
// We need to both draw on the physical map, AND update the mapArray.
//
function initiateDrawing(){
var temp_x;
var temp_y;
var mousedown = 0;
map.addEventListener('mousedown', function(e){
mousedown = 1;
drawAtPoint(e, brush);
})
map.addEventListener('mouseup', function(e){
mousedown = 0;
})
map.addEventListener('mousemove', function(e){
if(mousedown){
drawAtPoint(e, brush);
}
})
function drawAtPoint(mouse, _brush){
var mouse_x = Math.floor(mouse.offsetX/grid);
var mouse_y = Math.floor(mouse.offsetY/grid);
if(mouse_x != temp_x || mouse_y != temp_y){
// Draw the tile
drawImage(_brush, mouse_x, mouse_y);
// Add it to the array
mapArray[mouse_y][mouse_x] = "("+_brush[0]+","+_brush[1]+")";
temp_x = mouse_x;
temp_y = mouse_y;
}
}
}
initiateDrawing();
</script>
<script>
// Work on this nasty shit. It's bad. Really.
$('#button_save').click(function(){
//$('#generated').attr('src', map.toDataURL("image/png"))
var img = map.toDataURL("image/png");
this.href = img;
})
$('#button_data').click(function(){
var output = mapArray;
window.open('data:application/json;' + (window.btoa?'base64,'+btoa(JSON.stringify(output)):JSON.stringify(output)));
/*
var newWindow = window.open("export.html", "Here's your map data", "");
newWindow.arrayData = output;
newWindow.init();
*/
})
var scale = 1
$('#zoomIn').click(function(){
scale += 0.25
$('#zoom_percent').html(scale*100)
// map.style.transform = "scale("+scale+")";
$('.map_wrapper').css({"transform": "translate(-25%, -25%) scale("+scale+")"})
})
$('#zoomOut').click(function(){
scale -= 0.25
$('#zoom_percent').html(scale*100)
$('.map_wrapper').css({"transform": "translate(-25%, -25%) scale("+scale+")"})
})
$('#button_clear').click(function(){
brush = [0,0];
newBlankMap(mapSource, mapWidth, mapHeight);
})
$('.input #width').attr('value', mapWidth);
$('.input #height').attr('value', mapHeight);
$('.input #width').change(function(){
mapWidth = this.value
newBlankMap(mapSource, mapWidth, mapHeight);
})
$('.input #height').change(function(){
mapHeight = this.value
newBlankMap(mapSource, mapWidth, mapHeight);
})
</script>
</body>
</html>