-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
415 lines (344 loc) · 10.7 KB
/
script.js
File metadata and controls
415 lines (344 loc) · 10.7 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
var max_dim = 100;
var canvas_x;
var canvas_y;
var square_dim = 32
var saved_col = 0
var saved_row = 0
var curr_row = 0
var curr_col = 0
var select_box = false;
var drag_pan = false;
var key_shift = false;
var scale_factor = 1;
var mouse_pix = {x: null, y: null};
var translate_x = 0;
var translate_y = 0;
var canvas_offset_x = 0;
var canvas_offset_y = 0;
var max_scale = 2
var min_scale = 0.5
var saved_floor_paths = new ClipperLib.Paths();
var solution_paths = new ClipperLib.Paths();
var current_path = new ClipperLib.Path();
var tool_selector;
var tool_mode = 'poly';
function ev_tool_change (evt) {
tool_mode = this.value;
}
function constrain_translation()
{
var margin_x = canvas_x * 0.5 / scale_factor
var margin_y = canvas_y * 0.5 / scale_factor
if( translate_x > margin_x ) translate_x = margin_x;
if( translate_y > margin_y) translate_y = margin_y;
if( translate_x < margin_x - max_dim * square_dim)
translate_x = margin_x - max_dim * square_dim;
if( translate_y < margin_y - max_dim * square_dim)
translate_y = margin_y - max_dim * square_dim;
translate_x = Math.floor(translate_x);
translate_y = Math.floor(translate_y);
}
function init() {
var canvas = document.getElementById('mapzone');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext('2d');
canvas_x = canvas.width;
canvas_y = canvas.height;
canvas.addEventListener('mousedown',mousedown,false);
canvas.addEventListener('mouseup',mouseup,false);
canvas.addEventListener('mousemove',mousemove,false);
canvas.addEventListener('contextmenu',contextmenu,false);
canvas.addEventListener('mouseout', mouseout, false);
canvas.addEventListener('DOMMouseScroll', mousewheel, false);
tool_selector = document.getElementById('selector');
tool_selector.addEventListener('change', ev_tool_change, false);
var canvasRectangle = canvas.getBoundingClientRect();
canvas_offset_x = canvasRectangle.left;
canvas_offset_y = canvasRectangle.top;
draw();
}
function draw_paths(ctx, paths)
{
var ii, jj, x, y;
ctx.beginPath();
for(ii = 0; ii < paths.length; ii++) {
for(jj = 0; jj < paths[ii].length; jj++) {
x = paths[ii][jj].X;
y = paths[ii][jj].Y;
if (!jj) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.closePath();
}
ctx.stroke();
ctx.fill();
}
function draw() {
var canvas = document.getElementById('mapzone');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas_x = canvas.width;
canvas_y = canvas.height;
constrain_translation()
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgb(180, 140, 75)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.scale(scale_factor,scale_factor);
ctx.translate(translate_x,translate_y);
// draw the background
var solid_img = new Image();
solid_img.src = 'square_rock.png';
var ptrn_bg = ctx.createPattern(solid_img, 'repeat');
ctx.fillStyle = ptrn_bg;
ctx.fillRect(0, 0, square_dim*max_dim, square_dim*max_dim);
// draw the crosshatch border
var stroke_img = new Image();
stroke_img.src = 'border_crosshatch_alpha.png';
var stroke_pattern = ctx.createPattern(stroke_img, "repeat");
ctx.setLineDash([])
ctx.strokeStyle = stroke_pattern;
ctx.lineWidth = 104;
ctx.lineJoin = 'round';
// draw the walkable floor data
//draw_paths(ctx, saved_floor_paths);
// set up the floor pattern and line border
var walkable = new Image();
walkable.src = 'square_clean.png';
var ptrn = ctx.createPattern(walkable, 'repeat');
var walkable = new Image();
walkable.src = 'square_clean.png';
var ptrn = ctx.createPattern(walkable, 'repeat');
ctx.fillStyle = ptrn;
ctx.setLineDash([])
ctx.strokeStyle = 'rgb(30, 30, 30)';
ctx.lineWidth = 12;
ctx.lineJoin = 'miter';
ctx.miterLimit = 3
// draw the walkable floor data a second time
draw_paths(ctx, saved_floor_paths);
// draw selection graphics while making edits
if( !key_shift ) ctx.strokeStyle = 'rgb(90, 245, 150)';
else ctx.strokeStyle = 'rgb(230, 125, 125)';
ctx.lineWidth = 2 / scale_factor;
if( select_box ) {
if( tool_mode == 'rect' )
{
// draw rectangular selection box
ctx.setLineDash([4, 3]);
ctx.strokeRect( square_dim*Math.min(saved_row, curr_row),
square_dim*Math.min(saved_col, curr_col),
square_dim*(1 + Math.abs(saved_row-curr_row)),
square_dim*(1 + Math.abs(saved_col-curr_col)));
}
if( tool_mode == 'circle' )
{
// draw elliptical selection box
ctx.setLineDash([4, 3]);
ctx.fillStyle = 'rgba(0, 0, 0, 0)';
var paths_to_draw = new ClipperLib.Paths();
var circle_path = get_circle_path( saved_row, saved_col, curr_row, curr_col, square_dim);
paths_to_draw.push(circle_path);
draw_paths(ctx, paths_to_draw);
}
if( tool_mode == 'poly' )
{
ctx.strokeRect( curr_row*square_dim-4, curr_col*square_dim-4, 8, 8 );
// the new line to be added
ctx.setLineDash([4, 3]);
ctx.beginPath();
if( current_path.length > 0 )
ctx.moveTo(current_path[current_path.length-1].X,
current_path[current_path.length-1].Y); // previously added vertex
else
ctx.moveTo(saved_row*square_dim, saved_col*square_dim); // first vertex
ctx.lineTo(curr_row*square_dim, curr_col*square_dim); // currently selected vertex
ctx.stroke();
}
}
// draw any polygon in progress
if( tool_mode == 'poly' && current_path.length > 0 )
{
var paths_to_draw = new ClipperLib.Paths();
paths_to_draw.push(current_path);
ctx.fillStyle = 'rgba(0, 0, 0, 0)';
ctx.setLineDash([]);
draw_paths(ctx, paths_to_draw);
if( current_path.length > 0 )
ctx.strokeRect( current_path[0].X-4, current_path[0].Y-4, 8, 8 );
}
ctx.restore();
}
}
function get_cell_pos(evt)
{
curr_row = Math.floor((evt.clientX-canvas_offset_x)/(square_dim*scale_factor) - translate_x/square_dim);
curr_col = Math.floor((evt.clientY-canvas_offset_y)/(square_dim*scale_factor) - translate_y/square_dim);
if( curr_row < 0 ) curr_row = 0;
if( curr_col < 0 ) curr_col = 0;
if( curr_row >= max_dim ) curr_row = max_dim-1;
if( curr_col >= max_dim ) curr_col = max_dim-1;
}
function get_grid_pos(evt)
{
curr_row = Math.floor((evt.clientX-canvas_offset_x)/(square_dim*scale_factor)+(square_dim/2-translate_x)/square_dim);
curr_col = Math.floor((evt.clientY-canvas_offset_y)/(square_dim*scale_factor)+(square_dim/2-translate_y)/square_dim);
if( curr_row < 0 ) curr_row = 0;
if( curr_col < 0 ) curr_col = 0;
if( curr_row >= max_dim ) curr_row = max_dim-1;
if( curr_col >= max_dim ) curr_col = max_dim-1;
}
function mousedown(evt)
{
mouse_pix.x = evt.clientX;
mouse_pix.y = evt.clientY;
key_shift = evt.shiftKey;
if( evt.which == 1 && !drag_pan ) {
select_box = true;
if( tool_mode == 'rect' || tool_mode == 'circle' )
{
get_cell_pos(evt);
saved_row = curr_row;
saved_col = curr_col;
}
if( tool_mode == 'poly' )
{
get_grid_pos(evt);
saved_row = curr_row;
saved_col = curr_col;
}
draw();
}
if( evt.which == 3 ) {
select_box = false;
drag_pan = true;
}
}
function execute_clipping(clip_paths)
{
var clippingType = key_shift ? ClipperLib.ClipType.ctDifference : ClipperLib.ClipType.ctUnion;
var cpr = new ClipperLib.Clipper();
cpr.AddPaths(saved_floor_paths, ClipperLib.PolyType.ptSubject, true);
cpr.AddPaths(clip_paths, ClipperLib.PolyType.ptClip, true);
solution_paths = new ClipperLib.Paths();
cpr.Execute(clippingType, solution_paths, 1, 1);
console.log(JSON.stringify(solution_paths));
saved_floor_paths = solution_paths;
}
function mouseup(evt)
{
key_shift = evt.shiftKey;
if( evt.which == 1 && select_box == true ) {
select_box = false;
var clip_paths;
if( tool_mode == 'rect' )
{
// Create the rectangle to be clipped against the existing paths
var minrow = Math.min(saved_row, curr_row);
var maxrow = Math.max(saved_row, curr_row)+1;
var mincol = Math.min(saved_col, curr_col);
var maxcol = Math.max(saved_col, curr_col)+1;
clip_paths = [[{X:minrow*square_dim,Y:mincol*square_dim}, {X:maxrow*square_dim,Y:mincol*square_dim},
{X:maxrow*square_dim,Y:maxcol*square_dim}, {X:minrow*square_dim,Y:maxcol*square_dim}]];
execute_clipping(clip_paths);
}
if( tool_mode == 'circle' )
{
// Create the ellipse to be clipped against the existing paths
var circle_path = get_circle_path( saved_row, saved_col, curr_row, curr_col, square_dim);
clip_paths = new ClipperLib.Paths();
clip_paths.push(circle_path);
execute_clipping(clip_paths);
}
if( tool_mode == 'poly' )
{
if( current_path.length > 0 ) {
current_path.push({X:curr_row*square_dim,Y:curr_col*square_dim});
if( current_path[0].X == curr_row*square_dim &&
current_path[0].Y == curr_col*square_dim )
{
clip_paths = new ClipperLib.Paths();
clip_paths.push(current_path);
execute_clipping(clip_paths);
current_path.length = 0;
}
}
else
{
current_path.push({X:saved_row*square_dim,Y:saved_col*square_dim});
current_path.push({X:curr_row*square_dim,Y:curr_col*square_dim});
}
}
}
if( evt.which == 3 && drag_pan == true ) {
drag_pan = false;
}
draw();
}
function mousemove(evt)
{
new_mouse_x = evt.clientX;
new_mouse_y = evt.clientY;
if( select_box && evt.which == 1 ) {
key_shift = evt.shiftKey;
if( tool_mode == 'rect' || tool_mode == 'circle' ) {
get_cell_pos(evt);
}
if( tool_mode == 'poly' ) {
get_grid_pos(evt);
}
}
if( drag_pan ) {
translate_x = Math.round(translate_x + (new_mouse_x - mouse_pix.x)/scale_factor);
translate_y = Math.round(translate_y + (new_mouse_y - mouse_pix.y)/scale_factor);
constrain_translation()
//var viewport_center_x = (canvas.width * 0.5) - translate_x
//if (viewport_center_x < 0) translate_x = canvas.width * 0.5
}
mouse_pix.x = new_mouse_x;
mouse_pix.y = new_mouse_y;
draw();
}
function mousewheel(evt)
{
var delta = Math.max(-1, Math.min(1, (evt.wheelDelta || -evt.detail)));
if( delta > 0)
{
// zoom in, towards the cursor
if(scale_factor < max_scale)
{
translate_x -= mouse_pix.x * 0.5 / scale_factor
translate_y -= mouse_pix.y * 0.5 / scale_factor
scale_factor = scale_factor * 2;
constrain_translation()
}
}
else
{
// zoom out from the center of the canvas
if (scale_factor > min_scale)
{
scale_factor = scale_factor * 0.5;
translate_x += mouse_pix.x * 0.5 / scale_factor
translate_y += mouse_pix.y * 0.5 / scale_factor
constrain_translation()
}
}
if( scale_factor > max_scale) scale_factor = max_scale;
if( scale_factor < min_scale) scale_factor = min_scale;
draw();
}
function mouseout(evt)
{
select_box = false;
drag_pan = false;
draw();
}
// stifle context menu
function contextmenu(evt)
{
evt.preventDefault();
return false;
}