-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle02.html
433 lines (394 loc) · 15.7 KB
/
puzzle02.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Pragma" content="no-cache">
<link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="./libs/bootstrap/css/bootstrap-theme.min.css">
<style>
body {
margin: 0px;
padding: 0px;
}
svg {
width: 100%;
height: auto;
padding: 5px;
border-radius: 5px;
border: thin solid black;
}
svg>g>image {
opacity: 1;
}
svg>g>image:hover {
cursor: pointer;
opacity: 0.25;
}
svg>g>text {
font-weight: bold;
text-anchor: middle;
alignment-baseline: middle;
fill: black;
opacity: 0.25;
}
svg>g>text:hover {
cursor: pointer;
fill: red;
opacity: 0.7;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Bootstrap theme</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container theme-showcase" role="main">
<div class="jumbotron">
<h1>Clock</h1>
<p>
It was ten past six by the kitchen clock.<br />
부엌 시계를 보니 6시 10분이었다.
</p>
</div>
<p>
<button id="mixBth" type="button" class="btn btn-xs btn-primary">mix</button>
<button id="mixBth2" type="button" class="btn btn-xs btn-primary">mix2</button>
<button id="solveBth" type="button" class="btn btn-xs btn-primary">solve</button>
<button id="stopBth" type="button" class="btn btn-xs btn-primary">stop</button>
Max Col:
<select class="selectBox" id="maxCol">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5" selected>5</option>
<option value="7">7</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
Max Row:
<select class="selectBox" id="maxRow">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5" selected>5</option>
<option value="7">7</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<span>History Count :</span>
<span id="historyCnt">0</span>
</p>
<div id="puzzleDiv">
<svg id="puzzleSvg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 700 630"></svg>
<div id="progressBar" class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 100%;">
100% Complete (success)
</div>
</div>
</div>
<p>
<img data-src="holder.js/100x100" class="img-thumbnail" alt="100x100" src="./images/images.jpg" data-holder-rendered="true" style="width: 100px; height: 100px;">
<img data-src="holder.js/100x100" class="img-thumbnail" alt="100x100" src="./images/images01.jpg" data-holder-rendered="true" style="width: 100px; height: 100px;">
<img data-src="holder.js/100x100" class="img-thumbnail" alt="100x100" src="./images/images02.jpg" data-holder-rendered="true" style="width: 100px; height: 100px;">
<img data-src="holder.js/100x100" class="img-thumbnail" alt="100x100" src="./images/images03.jpg" data-holder-rendered="true" style="width: 100px; height: 100px;">
<img data-src="holder.js/100x100" class="img-thumbnail" alt="100x100" src="./images/images04.jpg" data-holder-rendered="true" style="width: 100px; height: 100px;">
<img data-src="holder.js/100x100" class="img-thumbnail" alt="100x100" src="./images/images05.jpg" data-holder-rendered="true" style="width: 100px; height: 100px;">
</p>
</div>
<script type="text/javascript" src="libs/jquery/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="libs/d3/d3.js"></script>
<script type="text/javascript" src="libs/bootstrap/js/bootstrap.min.js"></script>
<script>
$(function() {
var imgDataArr = [];
var moveHistoryArr = [];
var nw, nh, dx, dy;
var maxCol = 10, maxRow = 10;
var isShowNumber = true;
var curIdx;
var gab = 1;
var isHistoryStop = false;
var $svg = $("#puzzleSvg");
var $historyCnt = $("#historyCnt");
var $progressBar = $(".progress-bar:first", "#progressBar");
var imageObj = new Image();
var canvas = document.createElement("canvas");
var context = canvas.getContext('2d');
imageObj.onload = function () {
maxCol = parseInt($("#maxCol").val());
maxRow = parseInt($("#maxRow").val());
curIdx = maxCol * maxRow - 1;
imgDataArr = [];
moveHistoryArr = [];
isHistoryStop = false;
nw = this.naturalWidth;
nh = this.naturalHeight;
dx = Math.floor(nw / maxCol);
dy = Math.floor(nh / maxRow);
canvas.width = dx * maxCol;
canvas.height = dy * maxRow;
context.drawImage(imageObj, 0, 0, nw, nh, 0, 0, canvas.width, canvas.height);
context.fillRect((maxCol - 1) * dx, (maxRow - 1) * dy, dx, dy);
for (var row = 0; row < maxRow; row++) {
for (var col = 0; col < maxCol; col++) {
var sx = col * dx;
var sy = row * dy;
imgDataArr.push({image: convertImgDataToBase64URL(context.getImageData(sx, sy, dx, dy), "image/png"), x: col, y: row, idx: imgDataArr.length});
}
}
$historyCnt.text(moveHistoryArr.length);
setProgressBar();
testSvg();
};
imageObj.src = 'images/images03.jpg';
function svgEl(tagName) {
return document.createElementNS("http://www.w3.org/2000/svg", tagName);
}
function convertImgDataToBase64URL(imageData, outputFormat){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = imageData.height;
canvas.width = imageData.width;
ctx.putImageData(imageData, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
return dataURL;
}
function shuffle(array) {
array.sort(function() {
return Math.random() - .5;
});
}
function onClickImage() {
var $group = $(this);
var data = $group.data();
var imgData = $("svg>g:eq(" + curIdx + ")").data();
if (data.y === imgData.y) {
if (data.x < imgData.x) {
for (var i = 0; i < imgData.x - data.x; i ++) moveLeft();
}
else {
for (var i = 0; i < data.x - imgData.x; i ++) moveRight();
}
}
if (data.x === imgData.x) {
if (data.y < imgData.y) {
for (var i = 0; i < imgData.y - data.y; i ++) moveUp();
}
else {
for (var i = 0; i < data.y - imgData.y; i ++) moveDown();
}
}
}
function swapData(srcIdx, tarIdx) {
var $src = $("#G_" + srcIdx);
var $tar = $("#G_" + tarIdx);
var srcData = imgDataArr[srcIdx];
var tarData = imgDataArr[tarIdx];
var tmpData = $.extend({}, srcData);
var $srcClone = $("image:first, text:first", $src).clone(true);
var $tarClone = $("image:first, text:first", $tar).clone(true);
srcData.image = tarData.image;
srcData.idx = tarData.idx;
tarData.image = tmpData.image;
tarData.idx = tmpData.idx;
$src.empty().append($tarClone);
$tar.empty().append($srcClone);
}
function moveUp() {
var y = Math.floor(curIdx / maxCol);
if (y > 0) {
var targetIdx = curIdx - maxCol;
swapData(curIdx, targetIdx);
curIdx = targetIdx;
if (!isHistoryStop) {
moveHistoryArr.push(2);
$historyCnt.text(moveHistoryArr.length);
}
setProgressBar();
}
}
function moveRight() {
var x = curIdx % maxCol;
if (x < maxCol - 1) {
var targetIdx = curIdx + 1;
swapData(curIdx, targetIdx);
curIdx = targetIdx;
if (!isHistoryStop) {
moveHistoryArr.push(3);
$historyCnt.text(moveHistoryArr.length);
}
setProgressBar();
}
}
function moveDown() {
var y = Math.floor(curIdx / maxCol);
if (y < maxRow - 1) {
var targetIdx = curIdx + maxCol;
swapData(curIdx, targetIdx);
curIdx = targetIdx;
if (!isHistoryStop) {
moveHistoryArr.push(0);
$historyCnt.text(moveHistoryArr.length);
}
setProgressBar();
}
}
function moveLeft() {
var x = curIdx % maxCol;
if (x > 0) {
var targetIdx = curIdx - 1;
swapData(curIdx, targetIdx);
curIdx = targetIdx;
if (!isHistoryStop) {
moveHistoryArr.push(1);
$historyCnt.text(moveHistoryArr.length);
}
setProgressBar();
}
}
function testSvg() {
$svg.empty();
$svg.get(0).setAttribute("viewBox", "0 0 " + (maxCol * dx + gab * (maxCol - 1)) + " " + (maxRow * dy + gab * (maxRow - 1)));
//shuffle(imgDataArr);
var fontSize = Math.floor(Math.min(dx, dy) / 3);
imgDataArr.forEach(function(v, i) {
var x = i % maxCol;
var y = Math.floor(i / maxCol);
var tx = x * dx + x * gab;
var ty = y * dy + y * gab;
var $group = $(svgEl("g"));
$group.attr({"id": "G_" + v.idx,"transform": "translate(" + tx + "," + ty + ")"});
var $image = $(svgEl("image"));
$image.attr({"preserveAspectRatio": "xMinYMin meet", "style": "width: " + dx + "px; height: " + dy + "px; margin-left: " + Math.floor(i / maxCol) + ";"});
$image.get(0).setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", v.image);
$group.append($image);
var $text = $(svgEl("text"));
$text.text(v.idx + 1);
$text.attr({"dx": dx / 2, "dy": dy / 2});
$text.css({"font-size": fontSize + "px", "display": ((isShowNumber && v.idx < (maxCol * maxRow - 1))? "" : "none")});
$group.append($text);
$group.data({x: v.x, y: v.y})
$group.click(onClickImage);
$svg.append($group);
});
}
function moveByIdx(m) {
if (m == 0) {
moveUp();
}
else if (m == 1) {
moveRight();
}
else if (m == 2) {
moveDown();
}
else if (m == 3) {
moveLeft();
}
}
function getProgress() {
var totalCnt = imgDataArr.length;
var completCnt = imgDataArr.filter(function(d) {return d.idx == (d.x + d.y * maxCol)}).length;
return Math.floor(completCnt / totalCnt * 100);
}
function setProgressBar() {
var progress = getProgress();
$progressBar.css("width", progress + "%");
$progressBar.empty().append(progress + "% Complete (success)");
}
var timerId;
$("#mixBth").click(function () {
clearInterval(timerId);
isHistoryStop = false;
timerId = setInterval(function() {
moveByIdx(Math.floor(Math.random() * 4));
}, 1);
});
$("#mixBth2").click(function () {
clearInterval(timerId);
isHistoryStop = false;
for (var i = 0; i < 500; i ++) {
moveByIdx(Math.floor(Math.random() * 4));
}
});
$("#solveBth").click(function () {
clearInterval(timerId);
isHistoryStop = true;
timerId = setInterval(function() {
if (moveHistoryArr.length > 0) {
moveByIdx(moveHistoryArr.pop());
$historyCnt.text(moveHistoryArr.length);
}
else {
clearInterval(timerId);
isHistoryStop = false;
}
}, 50);
});
$("#stopBth").click(function () {
clearInterval(timerId);
isHistoryStop = false;
});
$("img.img-thumbnail").click(function() {
imageObj.src = this.src;
});
$("select.selectBox").change(function() {
clearInterval(timerId);
$(imageObj).trigger("onload");
});
$(window).keydown(function(event) {
if (event.keyCode > 36 && event.keyCode < 41) {
event.preventDefault();
event.stopPropagation();
switch (event.keyCode) {
case 37: // Left
moveRight();
break;
case 38: // Up
moveDown();
break;
case 39: // Right
moveLeft();
break;
case 40: // Down
moveUp();
break;
}
}
});
});
</script>
</body>
</html>