-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslyd.js
executable file
·714 lines (632 loc) · 18.9 KB
/
slyd.js
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
* classList.js: Cross-browser full element.classList implementation.
* 2012-11-15
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
/*global self, document, DOMException */
/*! @source http://purl.eligrey.com/github/classList.js/blob/master/classList.js*/
if (typeof document !== "undefined" && !("classList" in document.createElement("a"))) {
(function (view) {
"use strict";
if (!('HTMLElement' in view) && !('Element' in view)) return;
var
classListProp = "classList"
, protoProp = "prototype"
, elemCtrProto = (view.HTMLElement || view.Element)[protoProp]
, objCtr = Object
, strTrim = String[protoProp].trim || function () {
return this.replace(/^\s+|\s+$/g, "");
}
, arrIndexOf = Array[protoProp].indexOf || function (item) {
var
i = 0
, len = this.length
;
for (; i < len; i++) {
if (i in this && this[i] === item) {
return i;
}
}
return -1;
}
// Vendors: please allow content code to instantiate DOMExceptions
, DOMEx = function (type, message) {
this.name = type;
this.code = DOMException[type];
this.message = message;
}
, checkTokenAndGetIndex = function (classList, token) {
if (token === "") {
throw new DOMEx(
"SYNTAX_ERR"
, "An invalid or illegal string was specified"
);
}
if (/\s/.test(token)) {
throw new DOMEx(
"INVALID_CHARACTER_ERR"
, "String contains an invalid character"
);
}
return arrIndexOf.call(classList, token);
}
, ClassList = function (elem) {
var
trimmedClasses = strTrim.call(elem.className)
, classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
, i = 0
, len = classes.length
;
for (; i < len; i++) {
this.push(classes[i]);
}
this._updateClassName = function () {
elem.className = this.toString();
};
}
, classListProto = ClassList[protoProp] = []
, classListGetter = function () {
return new ClassList(this);
}
;
// Most DOMException implementations don't allow calling DOMException's toString()
// on non-DOMExceptions. Error's toString() is sufficient here.
DOMEx[protoProp] = Error[protoProp];
classListProto.item = function (i) {
return this[i] || null;
};
classListProto.contains = function (token) {
token += "";
return checkTokenAndGetIndex(this, token) !== -1;
};
classListProto.add = function () {
var
tokens = arguments
, i = 0
, l = tokens.length
, token
, updated = false
;
do {
token = tokens[i] + "";
if (checkTokenAndGetIndex(this, token) === -1) {
this.push(token);
updated = true;
}
}
while (++i < l);
if (updated) {
this._updateClassName();
}
};
classListProto.remove = function () {
var
tokens = arguments
, i = 0
, l = tokens.length
, token
, updated = false
;
do {
token = tokens[i] + "";
var index = checkTokenAndGetIndex(this, token);
if (index !== -1) {
this.splice(index, 1);
updated = true;
}
}
while (++i < l);
if (updated) {
this._updateClassName();
}
};
classListProto.toggle = function (token, forse) {
token += "";
var
result = this.contains(token)
, method = result ?
forse !== true && "remove"
:
forse !== false && "add"
;
if (method) {
this[method](token);
}
return !result;
};
classListProto.toString = function () {
return this.join(" ");
};
if (objCtr.defineProperty) {
var classListPropDesc = {
get: classListGetter
, enumerable: true
, configurable: true
};
try {
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
} catch (ex) { // IE 8 doesn't support enumerable:true
if (ex.number === -0x7FF5EC54) {
classListPropDesc.enumerable = false;
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
}
}
} else if (objCtr[protoProp].__defineGetter__) {
elemCtrProto.__defineGetter__(classListProp, classListGetter);
}
}(self));
}
/*
* SLYD: a sliding puzzle game by Ned Kahvo
*/
(function(slyd) {
slyd.util = (function(){
this.convertDatasetKey = function(key) {
var keyParts = key.split(/(?=[A-Z])/);
return ("data-" + keyParts.join("-")).toLowerCase();
};
this.getDataset = function(element, key) {
if (!("dataset" in element)) {
return element.getAttribute(slyd.util.convertDatasetKey(key));
}
return element.dataset[key];
};
this.setDataset = function(element, key, value) {
if (!("dataset" in element)) {
element.setAttribute(slyd.util.convertDatasetKey(key), value);
} else {
element.dataset[key] = value;
}
};
this.getScale = function(viewport, object) {
// will fill the viewport with the object, preserving the aspect ratio
var scale = viewport.width / object.width; // stretch or compress the object to fit the viewport width
if (scale * object.height < viewport.height) {
scale = viewport.height / object.height; // stretch or compress the object to fit the viewport height
}
return scale;
};
this.getImageData = function(path, width, height, handleImageData) {
var img = new Image();
var onImageLoad = function() {
var imageWidth = this.width,
imageHeight = this.height;
var viewport = {
width: width,
height: height
};
var object = {
width: imageWidth,
height: imageHeight
};
// stretch or compress the image and center within the viewport
var scale = slyd.util.getScale(viewport, object),
sW = Math.ceil( imageWidth * scale ),
sH = Math.ceil( imageHeight * scale ),
sX = Math.floor( (width - sW)/2 ),
sY = Math.floor( (height - sH)/2 );
var cvs = document.createElement("canvas");
cvs.width = width;
cvs.height = height;
var ctx = cvs.getContext("2d");
ctx.drawImage(img, sX, sY, sW, sH);
var imageData = ctx.getImageData(0, 0, width, height);
handleImageData(imageData, cvs.toDataURL());
};
img.onload = onImageLoad;
img.src = path;
};
return this;
})();
slyd.Timer = function(element) {
var _startTime, _endTime, _intervalId,
_displayInterval = 1000,
_timerElement = element;
var sec = 1000,
min = 60 * sec;
var getTimestamp = function() {
return new Date().getTime();
};
var updateTime = function(timestamp) {
_timerElement && (_timerElement.innerHTML = this.formatTime(timestamp - _startTime));
};
var zeroPad = function(number) {
return number < 10 ? "0"+number : number;
};
this.formatTime = function(timeDiff) {
var minutes = Math.floor(timeDiff / min);
timeDiff = timeDiff % min;
var seconds = Math.floor(timeDiff / sec);
return zeroPad(minutes) + ":" +zeroPad(seconds);
};
this.start = function() {
_startTime = getTimestamp();
var _this = this;
updateTime.apply(_this, [getTimestamp()]);
_intervalId = window.setInterval(function() {
updateTime.apply(_this, [getTimestamp()]);
}, _displayInterval);
};
this.stop = function() {
var _this = this;
window.clearInterval(_intervalId);
_endTime = getTimestamp();
updateTime.apply(_this, [_endTime]);
return _endTime - _startTime;
};
return this;
};
slyd.Model = function(cols, rows, sortedTiles, swapCheckFunction) {
var _tilesX = cols,
_tilesY = rows,
_sortedTiles = sortedTiles,
_canSwap = swapCheckFunction,
_shuffledTiles = [],
_lastMovedElements = [];
var swapTiles = function(tiles, indexA, indexB) {
if (!_canSwap(tiles[indexA], tiles[indexB])) {
// not a valid position, can't swap tiles
return false;
}
var temp = tiles[indexA];
tiles[indexA] = tiles[indexB];
tiles[indexB] = temp;
_lastMovedElements = [tiles[indexA], tiles[indexB]];
return true;
};
var getTileIndex = function(tile) {
var index = _shuffledTiles.indexOf(tile);
return index;
};
this.shuffleTiles = function() {
_shuffledTiles = _sortedTiles.slice(0);
var tile = _shuffledTiles[_shuffledTiles.length-1];
var directions = [[1,0],[-1,0],[0,1],[0,-1]];
var direction;
var count = _tilesX*_tilesY*directions.length*10;
while (--count) {
do {
direction = directions[Math.floor(Math.random() * directions.length)];
} while( !this.moveTile(tile, direction) );
}
return _shuffledTiles;
};
this.isGameOver = function() {
for (var i=0, len=_shuffledTiles.length; i<len; i++) {
if ( getTileIndex(_shuffledTiles[i]) != getTileIndex(_sortedTiles[i]) ) {
return false;
}
}
return true;
};
this.getLastMovedElements = function() {
return _lastMovedElements;
};
this.moveTile = function(tile, direction) {
var index = getTileIndex(tile);
if (direction[0] > 0) {
// move right
if (index % _tilesX != _tilesX - 1) {
return swapTiles(_shuffledTiles, index, index + 1);
}
return false;
}
if (direction[0] < 0) {
// move left
if (index % _tilesX != 0) {
return swapTiles(_shuffledTiles, index, index - 1);
}
return false;
}
if (direction[1] > 0) {
// move up
if (index - _tilesX >= 0) {
return swapTiles(_shuffledTiles, index, index - _tilesX);
}
return false;
}
if (direction[1] < 0) {
// move down
if (index + _tilesX < _tilesX * _tilesY) {
return swapTiles(_shuffledTiles, index, index + _tilesX);
}
return false;
}
return false;
};
return this;
};
slyd.View = function(slydController, containerElement, cols, rows) {
var _container = containerElement,
_cols = cols,
_rows = rows;
var _board = document.createElement("div");
_board.style.width = "100%";
_board.style.height = "100%";
_container.appendChild(_board);
// TODO: scale the game to fit different sized screens
var _tileElements = [];
var _selected = {};
var _hintImageDataURL = "";
var handleStart = function(event) {
event.preventDefault();
_selected.element = event.target;
_selected.tileIndex = slyd.util.getDataset(_selected.element, "number");
_selected.touchStart = {
x: event.touches[0].clientX,
y: event.touches[0].clientY
};
_selected.moveThreshold = {
x: Math.floor( parseInt(_selected.element.style.width, 10) * 0.5 ),
y: Math.floor( parseInt(_selected.element.style.height, 10) * 0.5 )
};
_selected.ready = true;
};
var checkWin = function() {
if (slydController.isGameOver()) {
_board.getElementsByClassName("hide")[0].classList.remove("hide");
}
};
var endMove = function() {
_selected = {};
};
var handleMove = function(event) {
if (!_selected.ready) return;
event.preventDefault();
_selected.touchDelta = {
x: event.touches[0].clientX - _selected.touchStart.x,
y: event.touches[0].clientY - _selected.touchStart.y
};
if (_selected.touchDelta.x < 0 && -1*_selected.touchDelta.x > _selected.moveThreshold.x) {
// move left
slydController.moveTile(_selected.element, [-1, 0]) && swapTiles(slydController.getLastMovedElements());
endMove();
return;
}
if (_selected.touchDelta.x > 0 && _selected.touchDelta.x > _selected.moveThreshold.x) {
// move right
slydController.moveTile(_selected.element, [1, 0]) && swapTiles(slydController.getLastMovedElements());
endMove();
return;
}
if (_selected.touchDelta.y < 0 && -1*_selected.touchDelta.y > _selected.moveThreshold.y) {
// move up
slydController.moveTile(_selected.element, [0, 1]) && swapTiles(slydController.getLastMovedElements());
endMove();
return;
}
if (_selected.touchDelta.y > 0 && _selected.touchDelta.y > _selected.moveThreshold.y) {
// move down
slydController.moveTile(_selected.element, [0, -1]) && swapTiles(slydController.getLastMovedElements());
endMove();
return;
}
};
var handleEnd = function(event) {
event.preventDefault();
endMove();
};
var handleTransitionEnd = function(event) {
event.stopPropagation();
event.preventDefault();
checkWin();
};
var swapTiles = function(elements) {
var temp = {
top: elements[0].style.top,
left: elements[0].style.left
};
elements[0].style.top = elements[1].style.top;
elements[0].style.left = elements[1].style.left;
elements[1].style.top = temp.top;
elements[1].style.left = temp.left;
};
this.getWidth = function() {
return _container.clientWidth;
};
this.getHeight = function() {
return _container.clientHeight;
};
this.getTileWidth = function() {
return Math.floor(this.getWidth() / _cols);
};
this.getTileHeight = function() {
return Math.floor(this.getHeight() / _rows);
};
this.generateTileElements = function(tileImages, tileWidth, tileHeight) {
var tile;
for (var i=0, len=tileImages.length; i<len; i++) {
tile = document.createElement("div");
tile.style.backgroundImage = "url(" + tileImages[i] + ")";
tile.style.width = tileWidth + "px";
tile.style.height = tileHeight + "px";
tile.classList.add("tile");
slyd.util.setDataset(tile, "number", i);
_tileElements.push(tile);
}
_tileElements[_tileElements.length-1].classList.add("hide");
return _tileElements;
};
this.drawTiles = function(tileElements, cols, rows) {
var tile, tileHeight, tileWidth, col, row,
parentElementRect = _board.getBoundingClientRect();
for (var i=0, len=tileElements.length; i<len; i++) {
tile = tileElements[i];
tileHeight = parseInt(tile.style.height, 10);
tileWidth = parseInt(tile.style.width, 10);
row = Math.floor(i / cols);
col = i % cols;
tile.style.position = "absolute";
tile.style.top = row * tileHeight + parentElementRect.top + "px";
tile.style.left = col * tileWidth + parentElementRect.left + "px";
_board.appendChild(tileElements[i]);
}
};
this.setHintImage = function(hintImageDataURL) {
_hintImageDataURL = hintImageDataURL;
};
this.toggleHint = function() {
if (!_hintImageDataURL) { return; }
_container.classList.toggle("showHint");
_container.style.backgroundImage = _container.classList.contains("showHint") ? "url("+_hintImageDataURL+")" : "";
};
this.attachEvents = function() {
for(var i=0, len=_tileElements.length; i<len; i++) {
var tile = _tileElements[i];
tile.addEventListener("touchstart", handleStart, false);
tile.addEventListener("touchmove", handleMove, false);
tile.addEventListener("touchend", handleEnd, false);
tile.addEventListener("touchleave", handleEnd, false);
if (tile.classList.contains("hide")) {
tile.addEventListener("webkitTransitionEnd", handleTransitionEnd, false);
}
}
};
// resize container to handle size differences due to rounding
_container.style.width = this.getTileWidth() * _cols + "px";
_container.style.height = this.getTileHeight() * _rows + "px";
return this;
};
slyd.Controller = function(parameters) {
var _slydModel;
var _slydView;
var _slydController = {};
var _slydTimer = slyd.Timer(parameters.timer);
_slydController.moveTile = function(element, direction) {
return _slydModel.moveTile(element, direction);
};
_slydController.getLastMovedElements = function() {
return _slydModel.getLastMovedElements();
};
_slydController.isGameOver = function() {
if (_slydModel.isGameOver()) {
var time = _slydTimer.stop();
window.setTimeout(function(){
parameters.onWin(_slydTimer.formatTime(time));
}, 10);
return true;
}
return false;
};
var makeTilesFromImageData = function(imageData, tileWidth, tileHeight, cols, rows) {
var cvs = document.createElement("canvas");
cvs.width = tileWidth;
cvs.height = tileHeight;
var ctx = cvs.getContext("2d");
var tiles = [];
for (var y=0; y<rows; y++) {
for (var x=0; x<cols; x++) {
var drawX = -1*x*tileWidth;
var drawY = -1*y*tileHeight;
ctx.putImageData(imageData, drawX, drawY);
tiles.push( cvs.toDataURL() );
}
}
return tiles;
};
this.play = function(image, difficulty) {
var difficulties = [
{cols: 3, rows: 3},
{cols: 4, rows: 4},
{cols: 5, rows: 5}
];
var cols = difficulties[difficulty].cols,
rows = difficulties[difficulty].rows;
_slydView = slyd.View(_slydController, parameters.container, cols, rows);
var width = _slydView.getWidth(),
height = _slydView.getHeight(),
tileWidth = _slydView.getTileWidth(),
tileHeight = _slydView.getTileHeight();
slyd.util.getImageData(image, width, height, function(imageData, imageDataURL) {
_slydView.setHintImage(imageDataURL);
var tiles = makeTilesFromImageData(imageData, tileWidth, tileHeight, cols, rows);
var sortedTileElements = _slydView.generateTileElements(tiles, tileWidth, tileHeight);
_slydModel = slyd.Model(cols, rows, sortedTileElements, function(tileA, tileB){
return ( tileA.classList.contains("hide") || tileB.classList.contains("hide") );
});
var randomTileElements = _slydModel.shuffleTiles();
_slydView.drawTiles(randomTileElements, cols, rows);
_slydView.attachEvents(randomTileElements, _slydController);
_slydTimer.start();
});
};
this.toggleHint = function() {
_slydView && _slydView.toggleHint();
};
return this;
};
slyd.setup = function(parameters) {
return slyd.Controller(parameters);
};
})(window.slyd = window.slyd || {});
/*
* Leaderboard by Ned Kahvo
* uses localStorage to store your top game scores
*/
(function(leaderboard) {
leaderboard.create = function(game, sortFunction) {
var _game = game,
_sortFunction = sortFunction || function(){return 0;},
_leaders = [],
_public = {};
try {
var storedLeaders = JSON.parse( localStorage.getItem(_game) );
if (storedLeaders) {
_leaders = storedLeaders;
}
} catch(exception) { console.log(exception); }
_public.list = function(number) {
var count = _leaders.length;
if (number && number > 0 && number < count) {
count = number;
}
return _leaders.slice(0, count);
};
_public.add = function(score, player) {
var record = {
"score": score,
"player": player
};
_leaders.push(record);
_leaders.sort(_sortFunction);
localStorage.setItem(_game, JSON.stringify(_leaders));
return _leaders.indexOf(record) + 1;
};
return _public;
};
})(window.leaderboard = window.leaderboard || {});
var showLeaders = function(leaders) {
var leaderBoard = document.getElementById("leaders");
leaderBoard.innerHTML = "";
var leader;
for (var i=0, len=leaders.length; i<len; i++) {
leader = document.createElement("li");
leader.innerHTML = [leaders[i].score, leaders[i].player].join(" ");
leaderBoard.appendChild(leader);
}
};
var slydLeaders = leaderboard.create("slyd", function(leaderA, leaderB) {
return leaderA.score < leaderB.score ? -1 : 1;
});
var leaders = slydLeaders.list(5);
if (leaders.length > 0) {
showLeaders(leaders);
}
var slydGame = slyd.setup({
container: document.getElementById("container"),
timer: document.getElementById("timer"),
onWin: function(time) {
var rank = slydLeaders.add(time, "");
alert("You win!!\nYou rock!\n\nTime: "+time+"\nRanking: "+rank);
showLeaders(slydLeaders.list(5));
}
});
var levels = ["images/darth-vader.jpg", "images/eiffel-tower.jpg"];
slydGame.play(levels[0], 0);
document.getElementById("toggle_hint").addEventListener("click", function(event) {
event.preventDefault();
slydGame.toggleHint();
}, false);