-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathimages.js
90 lines (76 loc) · 2.43 KB
/
images.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
/*jslint
browser: true
*/
var BINGO = {
images: [],
getValueFromElement: function (element) {
"use strict";
return parseInt(document.getElementById(element).value, 10);
},
processDrop: function (event) {
"use strict";
var dt = event.dataTransfer;
var files = dt.files;
var count = files.length;
var i;
document.getElementById("imageDrop").textContent = "Received " + count + " images. " + (count + this.images.length) + " images received so far.";
for (i = 0; i < files.length; i += 1) {
this.loadImage(files[i], event.target.id);
}
},
loadImage: function (file) {
"use strict";
var self = this;
var reader = new FileReader();
var img = document.createElement("img");
img.file = file;
self.images.push(img);
reader.onload = (function (aImg) {
return function (e) {
aImg.src = e.target.result;
};
}(img));
reader.readAsDataURL(file);
},
shuffle: function () {
"use strict";
var counter = this.images.length;
var temp;
var index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter -= 1;
// And swap the last element with it
temp = this.images[counter];
this.images[counter] = this.images[index];
this.images[index] = temp;
}
},
makeCard: function () {
"use strict";
var i;
var j;
var card;
var row;
var cell;
var image;
this.shuffle();
document.getElementById("bingo-header").textContent = document.getElementById("title").value;
card = document.getElementsByTagName("tbody")[0];
card.innerHTML = "";
for (i = 0; i < this.getValueFromElement("height"); i += 1) {
row = document.createElement("tr");
for (j = 0; j < this.getValueFromElement("width"); j += 1) {
image = this.images.pop();
cell = document.createElement("td");
cell.appendChild(image);
row.appendChild(cell);
this.images.unshift(image);
}
card.appendChild(row);
}
}
};