-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
288 lines (238 loc) · 9.35 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CustoCat</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-inverse" role="navigation" style="padding-left:130px;">
<a class="navbar-brand" href="/">CustoCat.com</a>
<ul class="nav navbar-nav">
<li>
<a href="https://library.custocat.com">Custocat Libaray</a>
</li>
<li class="active">
<a href="/">Create your own
<span class="sr-only">(current)</span>
</a>
</li>
<li>
<a href="https://www.twitter.com/custocat" target="_blank">Twitter</a>
</li>
<li>
<a href="https://github.com/aaronosher/custocat" target="_blank">GitHub</a>
</li>
<li>
<a href="https://devpost.com/software/custocat" target="_blank">DevPost</a>
</li>
</ul>
</nav>
<div style="display:none">
<img src="images/head/image1.png" class="imgHead" id="headImage" width="300px" height="300px">
<img src="images/legs/image1.png" class="imgLegs" id="legsImage" width="300px" height="300px">
<img src="images/arms/image1.png" class="imgArm" id="armsImage" width="300px" height="300px">
</div>
<br>
<div>
<button onclick="nextImage('headImage', 0)" class="btn btn-primary nextCat">
<span class="glyphicon glyphicon-forward"> Next Cat
</button>
<button onclick="nextImage('legsImage', 2)" class="btn btn-primary nextOct">
<span class="glyphicon glyphicon-forward"> Next Octopus
</button>
<button onclick="nextImage('armsImage', 1)" class="btn btn-primary nextArm">
<span class="glyphicon glyphicon-forward"> Next Arm
</button>
<button onclick="previousImage('headImage', 0)" class="btn btn-info prevCat">
Previous Cat <span class="glyphicon glyphicon-backward"></span>
</button>
<button onclick="previousImage('legsImage', 2)" class="btn btn-info prevOct">
Previous Octopus <span class="glyphicon glyphicon-backward"></span>
</button>
<button onclick="previousImage('armsImage', 1)" class="btn btn-info prevArm">
Previous Arm <span class="glyphicon glyphicon-backward"></span>
</button>
</div>
<button onclick="randomiseAll()" class="btn btn-danger random">
Randomise <span class="glyphicon glyphicon-question-sign"></span>
</button>
<button onclick="saveImage()" class="btn btn-success save">
Upload Octocat <span class="glyphicon glyphicon-open"></span>
</button>
<canvas id="canvas" width="600" height="600">
</canvas>
<div class="footer container small center">
The Octocat is a registered trademark of GitHub. We do not claim the rights to any images on our site.
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getFiles() {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", '/files', false); // false for synchronous request
xmlHttp.send(null);
return JSON.parse(xmlHttp.responseText);
}
var mainArray = getFiles()
// Counter array that holds the current image for each image array
var counterArr = [0,0,0]
// Element Array - Holds tags matched with array numbers
var elementArray = [
['armsImage', 1],
['legsImage', 2],
['headImage', 0]
]
var ctx = initialiseCanvas()
function nextImage(element, arrayNum) {
// Grab image variable from the document
var img = document.getElementById(element)
// If the counter is at the end of the array, loop around to the start of the array
if (counterArr[arrayNum] == mainArray[arrayNum].length - 1) {
counterArr[arrayNum] = 0
// Update image source to new value
img.src = mainArray[arrayNum][0]
img.onload = function () {
drawImages()
}
}
// Else increase the counter and update image source
else {
counterArr[arrayNum] += 1
img.src = mainArray[arrayNum][counterArr[arrayNum]]
img.onload = function () {
drawImages()
}
}
}
function previousImage(element, arrayNum) {
// Grab image variable from the document
var img = document.getElementById(element)
// If the counter is at 0, loop around to the end of the array
if (counterArr[arrayNum] == 0) {
counterArr[arrayNum] = mainArray[arrayNum].length - 1
// Update image source to new value
img.src = mainArray[arrayNum][counterArr[arrayNum]]
img.onload = function () {
drawImages()
}
}
// Else decrease the counter and update image source
else {
counterArr[arrayNum] -= 1
img.src = mainArray[arrayNum][counterArr[arrayNum]]
img.onload = function () {
drawImages()
}
}
}
function randomiseAll() {
// Loop through all elements in the all elements array
for (index in elementArray) {
// Grab the arrayNum of the element type
let arrayNum = elementArray[index][1]
// Generate random num for an image that isn't the current image
randomNum = getRandomInt(0, mainArray[arrayNum].length)
while (randomNum == counterArr[arrayNum]) {
randomNum = getRandomInt(0, mainArray[arrayNum].length)
}
// Set the new image and update the current image in the counterArr
document.getElementById(elementArray[index][0]).src = mainArray[arrayNum][randomNum]
counterArr[arrayNum] = randomNum
drawImages()
}
}
// Get a random int value between a min and a max
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function drawImages() {
let armsImg = document.getElementById('armsImage')
let legsImg = document.getElementById('legsImage')
let headImg = document.getElementById('headImage')
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(armsImg, 40, 150, 300, 300)
ctx.drawImage(legsImg, 300, 300, 300, 300)
ctx.drawImage(headImg, 300, 10, 300, 300)
// Draw the text
ctx.font="30px verdana";
ctx.globalAlpha=.75;
ctx.fillStyle='white'
ctx.fillText("Custocat.com", 350, 340);
ctx.fillStyle='black'
ctx.fillText("Custocat.com", 352, 342);
ctx.globalAlpha=1;
// If you remove the watermark on this image and use our site to submit an octocat to the GitHubEducation
// challenge and do not acknowledge it was made using the site, know one thing...
/// You are a scumbag, don't do that!
}
function initialiseCanvas() {
// Initialise the canvas element
let canvas = document.getElementById('canvas')
let ctx = canvas.getContext('2d')
// Drawing functions
let headImg = document.getElementById('headImage')
let legsImg = document.getElementById('legsImage')
let armsImg = document.getElementById('armsImage')
ctx.drawImage(armsImg, 40, 170, 300, 300)
ctx.drawImage(legsImg, 300, 300, 300, 300)
ctx.drawImage(headImg, 300, 10, 300, 300)
return ctx
}
function saveImage() {
var fileName = prompt("Please enter your Octocat name", "")
if (fileName != "" && fileName) {
var twitterHandle = prompt("Please enter your Twitter Handle", "@")
}
if (twitterHandle === "" || twitterHandle === "@") {
alert("You must enter a name and Twitter handle")
}
else if (fileName && twitterHandle) {
var imgData = ctx.getImageData(0, 0, 600, 600)
var dataURL = canvas.toDataURL("image/png")
var blobBin = atob(dataURL.split(',')[1]);
var array = [];
for(var i = 0; i < blobBin.length; i++) {
array.push(blobBin.charCodeAt(i));
}
var blob=new Blob([new Uint8Array(array)], {type: 'image/png'});
var ajaxReturnVal = uploadOctocat(blob, fileName, twitterHandle)
if (ajaxReturnVal == 200) {
alert("Octocat uploaded successfully!")
}
else if (ajaxReturnVal == 400) {
alert("Oh no! Octocat failed to upload, try again")
}
else {
alert("Uh oh. We don't know what's happened to your Octocat!")
}
}
}
function uploadOctocat(image, name, author) {
// Creat an instance of FormData
var data = new FormData()
// Success trakcer
var success = false
console.log(image)
// Add keys
data.append("octocat", image)
data.append("name", name)
data.append("submitter", author)
console.log(data)
$.ajax({
url: "https://api.custocat.com/image",
method: "POST",
dataType: "json",
data: data,
processData: false,
contentType: false,
success: function(res) { success = true; console.log(res); },
error: function(err) { success = false; console.error(err); }
})
return success
}
var canvas = document.getElementById('canvas')
var ctx = initialiseCanvas()
</script>
</body>
</html>