-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
222 lines (218 loc) · 9.19 KB
/
game.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
var requestAnimationFrame;
var canvas;
var token;//Token to identify
var context;
var pixelSize; // Real size of each pixel
var rects; // All loaded color pixels
var color = []; // Color to rect match;
var mouselocation; // Which element is the mouse over
var offsetX;
function create2dArray(length) {
var array2d = [];
for (var i = 0; i < length; i++) {
array2d[i] = [];
for (var j = 0; j < length; j++) {
array2d[i][j] = 0;
}
}
return array2d;
}
function getElement(x, callback)
{
var request = new XMLHttpRequest();
request.open("POST","getElement/",true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("which="+encodeURIComponent(x)+"&token="+encodeURIComponent(token));
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
console.log(request.responseText);
res = request.responseText;
if(x >= 0) res = JSON.parse(request.responseText);
if(callback != undefined) callback(res);
return res;
}
}
}
function divideRect(x)
{
getElement(x, function(res)
{
if(res == undefined || rects[x] == undefined) return;
var sx = rects[x].x;
var sy = rects[x].y;
var sw = rects[x].w;
var sh = rects[x].h;
color[rects[x].c.slice(1,7)] = undefined;
if(res.tl == undefined) return;
rects[4*x+1] = {x: sx, y: sy, w: sw/2, h: sh/2, c: '#'+res.tl.substring(0,6), al: res.tl.substring(6,8)};
rects[4*x+2] = {x: sx+sw/2, y: sy, w: sw/2, h: sh/2, c: '#'+res.tr.substring(0,6), al: res.tr.substring(6,8)};
rects[4*x+3] = {x: sx, y: sy+sh/2, w: sw/2, h: sh/2, c: '#'+res.bl.substring(0,6), al: res.tr.substring(6,8)};
rects[4*x+4] = {x: sx+sw/2, y: sy+sh/2, w: sw/2, h: sh/2, c: '#'+res.br.substring(0,6), al: res.tr.substring(6,8)};
/*if(color[res.tl.substring(0,6)] == undefined) color[res.tl.substring(0,6)] = [4*x+1];
else color[res.tl.substring(0,6)][color[res.tl.substring(0,6)].length] = 4*x+1;
if(color[res.tr.substring(0,6)] == undefined) color[res.tr.substring(0,6)] = [4*x+2];
else color[res.tr.substring(0,6)][color[res.tr.substring(0,6)].length] = 4*x+2;
if(color[res.bl.substring(0,6)] == undefined) color[res.bl.substring(0,6)] = [4*x+3];
else color[res.bl.substring(0,6)][color[res.bl.substring(0,6)].length] = 4*x+3;
if(color[res.br.substring(0,6)] == undefined) color[res.br.substring(0,6)] = [4*x+4];
else color[res.br.substring(0,6)][color[res.br.substring(0,6)].length] = 4*x+4;*///COLORS BASED ON CANVAS
//console.log(color);
});
}
function sendKeyword(word)
{
var request = new XMLHttpRequest();
request.open("POST","tryWord/",true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("word="+encodeURIComponent(word)+"&token="+encodeURIComponent(token));
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
console.log(request.responseText);
if(request.responseText.length == 0) return;
var x = "<div id='preword' ";
if(JSON.parse(request.responseText).points < 0) x+="style='background-color:#ffb3b3;'";
document.getElementById("previouswords").innerHTML = x+'>'+word+"</div><div id='prepoints'><b>"+JSON.parse(request.responseText).points+"</b></div>"+document.getElementById("previouswords").innerHTML;
document.getElementById('word').value = "";
}
}
return 0;
}
// CREATE HOVER ANIMATION + HOLD&DRAG + OPTIMISE
var mouseon = false; // is the mouse held
// END
function rgbToHex(rgb)
{
return ('0'+parseInt(rgb[0]).toString(16)).slice(1)+
('0'+parseInt(rgb[1]).toString(16)).slice(1)+
('0'+parseInt(rgb[2]).toString(16)).slice(1);
}
function init()
{
window.addEventListener("mouseup", function(args) {
var x = (args.x-offsetX)/pixelSize;
var y = (args.y)/pixelSize;
mouseon = false;
for(var i = rects.length-1; i >= 0; i --)
{
if(rects[i] != undefined && x > rects[i].x && x < rects[i].x+rects[i].w && y > rects[i].y && y < rects[i].y+rects[i].h)
{
divideRect(i);
i = -1;
}
}
}, false);
window.addEventListener("mousedown", function(args)
{
mouseon = true;
}, false);
window.addEventListener("mousemove", function(args)
{
//console.log(color[rgbToHex(context.getImageData(args.x-8, args.y-8, 1, 1).data)]);
//console.log(args.x,args.y);
var where = rgbToHex(context.getImageData(args.x-offsetX, args.y, 1, 1).data);
if(where == undefined) return;
if(!mouseon)
{
document.getElementById('colorbox').style.backgroundColor = '#'+where;
}
else
{
var x = (args.x-offsetX)/pixelSize;
var y = (args.y)/pixelSize;
for(var i = rects.length-1; i >= 0; i --)
{
if(rects[i] != undefined && x > rects[i].x && x < rects[i].x+rects[i].w && y > rects[i].y && y < rects[i].y+rects[i].h)
{
divideRect(i);
i = -1;
}
}
}
}, false);
$("#word-form").submit(function(e) {
e.preventDefault();
});
requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
canvas = document.getElementById("canvas-id");
canvas.width = 700;
canvas.height = 700;
context = canvas.getContext("2d");
pixelSize = canvas.width/128;
token = localStorage.getItem('token')//retrieve token
if(localStorage.getItem('rects') != undefined && localStorage.getItem('rects') != "undefined" && localStorage.getItem('rects') != "[]") rects = JSON.parse(localStorage.getItem('rects'));
else getElement(-1, function(res) {rects = [{x: 0, y :0, w: 128, h: 128, c: '#'+res}]; color[res] = [0]}); // Get first rectangle
document.body.style.paddingLeft=(window.innerWidth-document.getElementById('left-col').clientWidth-canvas.width-document.getElementById('right-col').clientWidth)/2;
//console.log(window.outerWidth,document.getElementById('left-col').clientWidth+canvas.width+document.getElementById('right-col').clientWidth);
document.getElementById('canvas-container').style.left = document.getElementById('canvas-container').getBoundingClientRect().left+(window.innerWidth-document.getElementById('left-col').clientWidth-canvas.width-document.getElementById('right-col').clientWidth)/2;
document.getElementById('right-col').style.left = document.getElementById('right-col').getBoundingClientRect().left+(window.innerWidth-document.getElementById('left-col').clientWidth-canvas.width-document.getElementById('right-col').clientWidth)/2;
//if(localStorage.getItem('color') != undefined && localStorage.getItem('color') != "undefined" && localStorage.getItem('color') != "[]") color = JSON.parse(localStorage.getItem('color'));
}
function nextImage()
{
var request = new XMLHttpRequest();
request.open("POST","nextImage/",true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("token="+encodeURIComponent(token));
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
//rects = [];
//context.clearRect(0,0,canvas.width, canvas.height);
getElement(-1, function(res) {rects = [{x: 0, y :0, w: 128, h: 128, c: '#'+res}]; color[res] = [0]});
document.getElementById('previouswords').innerHTML = "";
}
}
}
function getPoints(callback)
{
var request = new XMLHttpRequest();
request.open("POST","getPoints/",true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("token="+encodeURIComponent(token));
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
//console.log(request.responseText);
callback(JSON.parse(request.responseText).points);
}
}
}
function update()
{
localStorage.setItem('rects',JSON.stringify(rects));
offsetX = canvas.getBoundingClientRect().left;
document.getElementById('colorbox').style.height=document.getElementById('heightSample').clientHeight;
getPoints(function(res)
{
document.getElementById('points').innerHTML=res;
});
//localStorage.setItem('color',JSON.stringify(color));
setTimeout(update, 1000);
}
function draw() {
context.clearRect(0,0,canvas.width, canvas.height);
context.globalAlpha = 1;
if(rects != undefined && rects.length != undefined)
{
for(var i = 0; i < rects.length; i ++)
{
if(rects[i] != undefined)
{
context.globalAlpha = parseInt(rects[i].al,16);
context.fillStyle = rects[i].c;
context.fillRect(rects[i].x*pixelSize, rects[i].y*pixelSize, rects[i].w*pixelSize, rects[i].h*pixelSize);
}
}
}
requestAnimationFrame(draw);
}
init();
update();
draw();